2 * Sone - WebOfTrustUpdater.java - Copyright © 2013 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.sone.core;
20 import static com.google.common.base.Preconditions.checkNotNull;
22 import java.util.concurrent.BlockingQueue;
23 import java.util.concurrent.LinkedBlockingQueue;
24 import java.util.logging.Level;
25 import java.util.logging.Logger;
27 import net.pterodactylus.sone.freenet.plugin.PluginException;
28 import net.pterodactylus.sone.freenet.wot.DefaultIdentity;
29 import net.pterodactylus.sone.freenet.wot.Identity;
30 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
31 import net.pterodactylus.sone.freenet.wot.Trust;
32 import net.pterodactylus.sone.freenet.wot.WebOfTrustConnector;
33 import net.pterodactylus.sone.freenet.wot.WebOfTrustException;
34 import net.pterodactylus.util.logging.Logging;
35 import net.pterodactylus.util.service.AbstractService;
37 import com.google.inject.Inject;
40 * Updates WebOfTrust identity data in a background thread because communicating
41 * with the WebOfTrust plugin can potentially last quite long.
43 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
45 public class WebOfTrustUpdater extends AbstractService {
48 private static final Logger logger = Logging.getLogger(WebOfTrustUpdater.class);
51 @SuppressWarnings("synthetic-access")
52 private final WebOfTrustUpdateJob stopJob = new WebOfTrustUpdateJob();
54 /** The web of trust connector. */
55 private final WebOfTrustConnector webOfTrustConnector;
57 /** The queue for jobs. */
58 private final BlockingQueue<WebOfTrustUpdateJob> updateJobs = new LinkedBlockingQueue<WebOfTrustUpdateJob>();
61 * Creates a new trust updater.
63 * @param webOfTrustConnector
64 * The web of trust connector
67 public WebOfTrustUpdater(WebOfTrustConnector webOfTrustConnector) {
68 super("Trust Updater");
69 this.webOfTrustConnector = webOfTrustConnector;
77 * Updates the trust relation between the truster and the trustee. This method
78 * will return immediately and perform a trust update in the background.
81 * The identity giving the trust
83 * The identity receiving the trust
85 * The new level of trust (from -100 to 100, may be {@code null} to remove
86 * the trust completely)
88 * The comment of the trust relation
90 public void setTrust(OwnIdentity truster, Identity trustee, Integer score, String comment) {
91 SetTrustJob setTrustJob = new SetTrustJob(truster, trustee, score, comment);
92 if (updateJobs.contains(setTrustJob)) {
93 updateJobs.remove(setTrustJob);
95 logger.log(Level.FINER, "Adding Trust Update Job: " + setTrustJob);
97 updateJobs.put(setTrustJob);
98 } catch (InterruptedException e) {
99 /* the queue is unbounded so it should never block. */
104 * Adds the given context to the given own identity.
107 * The own identity to add the context to
111 public void addContext(OwnIdentity ownIdentity, String context) {
112 addContextWait(ownIdentity, context, false);
116 * Adds the given context to the given own identity, waiting for completion of
120 * The own identity to add the context to
123 * @return {@code true} if the context was added successfully, {@code false}
126 public boolean addContextWait(OwnIdentity ownIdentity, String context) {
127 return addContextWait(ownIdentity, context, true);
131 * Adds the given context to the given own identity, waiting for completion of
135 * The own identity to add the context to
139 * {@code true} to wait for the end of the operation, {@code false} to return
141 * @return {@code true} if the context was added successfully, {@code false} if
142 * the context was not added successfully, or if the job should not
143 * wait for completion
145 private boolean addContextWait(OwnIdentity ownIdentity, String context, boolean wait) {
146 AddContextJob addContextJob = new AddContextJob(ownIdentity, context);
147 if (!updateJobs.contains(addContextJob)) {
148 logger.log(Level.FINER, "Adding Context Job: " + addContextJob);
150 updateJobs.put(addContextJob);
151 } catch (InterruptedException ie1) {
152 /* the queue is unbounded so it should never block. */
155 return addContextJob.waitForCompletion();
158 for (WebOfTrustUpdateJob updateJob : updateJobs) {
159 if (updateJob.equals(addContextJob)) {
160 return updateJob.waitForCompletion();
168 * Removes the given context from the given own identity.
171 * The own identity to remove the context from
173 * The context to remove
175 public void removeContext(OwnIdentity ownIdentity, String context) {
176 RemoveContextJob removeContextJob = new RemoveContextJob(ownIdentity, context);
177 if (!updateJobs.contains(removeContextJob)) {
178 logger.log(Level.FINER, "Adding Context Job: " + removeContextJob);
180 updateJobs.put(removeContextJob);
181 } catch (InterruptedException ie1) {
182 /* the queue is unbounded so it should never block. */
188 * Sets a property on the given own identity.
191 * The own identity to set the property on
192 * @param propertyName
193 * The name of the property to set
194 * @param propertyValue
195 * The value of the property to set
197 public void setProperty(OwnIdentity ownIdentity, String propertyName, String propertyValue) {
198 SetPropertyJob setPropertyJob = new SetPropertyJob(ownIdentity, propertyName, propertyValue);
199 if (updateJobs.contains(setPropertyJob)) {
200 updateJobs.remove(setPropertyJob);
202 logger.log(Level.FINER, "Adding Property Job: " + setPropertyJob);
204 updateJobs.put(setPropertyJob);
205 } catch (InterruptedException e) {
206 /* the queue is unbounded so it should never block. */
211 * Removes a property from the given own identity.
214 * The own identity to remove the property from
215 * @param propertyName
216 * The name of the property to remove
218 public void removeProperty(OwnIdentity ownIdentity, String propertyName) {
219 setProperty(ownIdentity, propertyName, null);
227 protected void serviceRun() {
228 while (!shouldStop()) {
230 WebOfTrustUpdateJob updateJob = updateJobs.take();
231 if (shouldStop() || (updateJob == stopJob)) {
234 logger.log(Level.FINE, "Running Trust Update Job: " + updateJob);
235 long startTime = System.currentTimeMillis();
237 long endTime = System.currentTimeMillis();
238 logger.log(Level.FINE, "Trust Update Job finished, took " + (endTime - startTime) + " ms.");
239 } catch (InterruptedException ie1) {
240 /* happens, ignore, loop. */
246 protected void serviceStop() {
248 updateJobs.put(stopJob);
249 } catch (InterruptedException ie1) {
250 /* the queue is unbounded so it should never block. */
255 * Base class for WebOfTrust update jobs.
257 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
259 private class WebOfTrustUpdateJob {
261 /** Object for synchronization. */
262 @SuppressWarnings("hiding")
263 private final Object syncObject = new Object();
265 /** Whether the job has finished. */
266 private boolean finished;
268 /** Whether the job was successful. */
269 private boolean success;
276 * Performs the actual update operation.
278 * The implementation of this class does nothing.
285 * Waits for completion of this job or stopping of the WebOfTrust updater.
287 * @return {@code true} if this job finished successfully, {@code false}
289 * @see WebOfTrustUpdater#stop()
291 @SuppressWarnings("synthetic-access")
292 public boolean waitForCompletion() {
293 synchronized (syncObject) {
294 while (!finished && !shouldStop()) {
297 } catch (InterruptedException ie1) {
298 /* we’re looping, ignore. */
310 * Signals that this job has finished.
313 * {@code true} if this job finished successfully, {@code false} otherwise
315 protected void finish(boolean success) {
316 synchronized (syncObject) {
318 this.success = success;
319 syncObject.notifyAll();
326 * Update job that sets the trust relation between two identities.
328 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
330 private class SetTrustJob extends WebOfTrustUpdateJob {
332 /** The identity giving the trust. */
333 private final OwnIdentity truster;
335 /** The identity receiving the trust. */
336 private final Identity trustee;
338 /** The score of the relation. */
339 private final Integer score;
341 /** The comment of the relation. */
342 private final String comment;
345 * Creates a new set trust job.
348 * The identity giving the trust
350 * The identity receiving the trust
352 * The score of the trust (from -100 to 100, may be {@code null} to remote
353 * the trust relation completely)
355 * The comment of the trust relation
357 public SetTrustJob(OwnIdentity truster, Identity trustee, Integer score, String comment) {
358 this.truster = truster;
359 this.trustee = trustee;
361 this.comment = comment;
365 @SuppressWarnings("synthetic-access")
369 if (trustee instanceof DefaultIdentity) {
370 ((DefaultIdentity) trustee).setTrust(truster, new Trust(score, null, 0));
372 webOfTrustConnector.setTrust(truster, trustee, score, comment);
374 if (trustee instanceof DefaultIdentity) {
375 ((DefaultIdentity) trustee).setTrust(truster, null);
377 webOfTrustConnector.removeTrust(truster, trustee);
380 } catch (WebOfTrustException wote1) {
381 logger.log(Level.WARNING, "Could not set Trust value for " + truster + " -> " + trustee + " to " + score + " (" + comment + ")!", wote1);
391 public boolean equals(Object object) {
392 if ((object == null) || !object.getClass().equals(getClass())) {
395 SetTrustJob updateJob = (SetTrustJob) object;
396 return ((truster == null) ? (updateJob.truster == null) : updateJob.truster.equals(truster)) && ((trustee == null) ? (updateJob.trustee == null) : updateJob.trustee.equals(trustee));
400 public int hashCode() {
401 return getClass().hashCode() ^ ((truster == null) ? 0 : truster.hashCode()) ^ ((trustee == null) ? 0 : trustee.hashCode());
405 public String toString() {
406 return String.format("%s[truster=%s,trustee=%s]", getClass().getSimpleName(), (truster == null) ? null : truster.getId(), (trustee == null) ? null : trustee.getId());
412 * Base class for context updates of an {@link OwnIdentity}.
414 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
416 private class WebOfTrustContextUpdateJob extends WebOfTrustUpdateJob {
418 /** The own identity whose contexts to manage. */
419 protected final OwnIdentity ownIdentity;
421 /** The context to update. */
422 protected final String context;
425 * Creates a new context update job.
428 * The own identity to update
430 * The context to update
432 @SuppressWarnings("synthetic-access")
433 public WebOfTrustContextUpdateJob(OwnIdentity ownIdentity, String context) {
434 this.ownIdentity = checkNotNull(ownIdentity, "ownIdentity must not be null");
435 this.context = checkNotNull(context, "context must not be null");
443 public boolean equals(Object object) {
444 if ((object == null) || !object.getClass().equals(getClass())) {
447 WebOfTrustContextUpdateJob updateJob = (WebOfTrustContextUpdateJob) object;
448 return updateJob.ownIdentity.equals(ownIdentity) && updateJob.context.equals(context);
452 public int hashCode() {
453 return getClass().hashCode() ^ ownIdentity.hashCode() ^ context.hashCode();
457 public String toString() {
458 return String.format("%s[ownIdentity=%s,context=%s]", getClass().getSimpleName(), ownIdentity, context);
464 * Job that adds a context to an {@link OwnIdentity}.
466 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
468 private class AddContextJob extends WebOfTrustContextUpdateJob {
471 * Creates a new add-context job.
474 * The own identity whose contexts to manage
478 public AddContextJob(OwnIdentity ownIdentity, String context) {
479 super(ownIdentity, context);
483 @SuppressWarnings("synthetic-access")
486 webOfTrustConnector.addContext(ownIdentity, context);
487 ownIdentity.addContext(context);
489 } catch (PluginException pe1) {
490 logger.log(Level.WARNING, String.format("Could not add Context “%2$s” to Own Identity %1$s!", ownIdentity, context), pe1);
498 * Job that removes a context from an {@link OwnIdentity}.
500 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
502 private class RemoveContextJob extends WebOfTrustContextUpdateJob {
505 * Creates a new remove-context job.
508 * The own identity whose contexts to manage
510 * The context to remove
512 public RemoveContextJob(OwnIdentity ownIdentity, String context) {
513 super(ownIdentity, context);
517 @SuppressWarnings("synthetic-access")
520 webOfTrustConnector.removeContext(ownIdentity, context);
521 ownIdentity.removeContext(context);
523 } catch (PluginException pe1) {
524 logger.log(Level.WARNING, String.format("Could not remove Context “%2$s” to Own Identity %1$s!", ownIdentity, context), pe1);
532 * WebOfTrust update job that sets a property on an {@link OwnIdentity}.
534 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
536 private class SetPropertyJob extends WebOfTrustUpdateJob {
538 /** The own identity to update properties on. */
539 private final OwnIdentity ownIdentity;
541 /** The name of the property to update. */
542 private final String propertyName;
544 /** The value of the property to set. */
545 private final String propertyValue;
548 * Creates a new set-property job.
551 * The own identity to set the property on
552 * @param propertyName
553 * The name of the property to set
554 * @param propertyValue
555 * The value of the property to set
557 public SetPropertyJob(OwnIdentity ownIdentity, String propertyName, String propertyValue) {
558 this.ownIdentity = ownIdentity;
559 this.propertyName = propertyName;
560 this.propertyValue = propertyValue;
564 @SuppressWarnings("synthetic-access")
567 if (propertyValue == null) {
568 webOfTrustConnector.removeProperty(ownIdentity, propertyName);
569 ownIdentity.removeProperty(propertyName);
571 webOfTrustConnector.setProperty(ownIdentity, propertyName, propertyValue);
572 ownIdentity.setProperty(propertyName, propertyValue);
575 } catch (PluginException pe1) {
576 logger.log(Level.WARNING, String.format("Could not set Property “%2$s” to “%3$s” on Own Identity %1$s!", ownIdentity, propertyName, propertyValue), pe1);
586 public boolean equals(Object object) {
587 if ((object == null) || !object.getClass().equals(getClass())) {
590 SetPropertyJob updateJob = (SetPropertyJob) object;
591 return updateJob.ownIdentity.equals(ownIdentity) && updateJob.propertyName.equals(propertyName);
595 public int hashCode() {
596 return getClass().hashCode() ^ ownIdentity.hashCode() ^ propertyName.hashCode();
600 public String toString() {
601 return String.format("%s[ownIdentity=%s,propertyName=%s]", getClass().getSimpleName(), ownIdentity, propertyName);