2 * Sone - WebOfTrustUpdater.java - Copyright © 2012 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 java.util.concurrent.BlockingQueue;
21 import java.util.concurrent.LinkedBlockingQueue;
22 import java.util.logging.Level;
23 import java.util.logging.Logger;
25 import net.pterodactylus.sone.freenet.plugin.PluginException;
26 import net.pterodactylus.sone.freenet.wot.DefaultIdentity;
27 import net.pterodactylus.sone.freenet.wot.Identity;
28 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
29 import net.pterodactylus.sone.freenet.wot.Trust;
30 import net.pterodactylus.sone.freenet.wot.WebOfTrustConnector;
31 import net.pterodactylus.sone.freenet.wot.WebOfTrustException;
32 import net.pterodactylus.util.logging.Logging;
33 import net.pterodactylus.util.service.AbstractService;
34 import net.pterodactylus.util.validation.Validation;
36 import com.google.inject.Inject;
39 * Updates WebOfTrust identity data in a background thread because communicating
40 * with the WebOfTrust plugin can potentially last quite long.
42 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
44 public class WebOfTrustUpdater extends AbstractService {
47 private static final Logger logger = Logging.getLogger(WebOfTrustUpdater.class);
50 @SuppressWarnings("synthetic-access")
51 private final WebOfTrustUpdateJob stopJob = new WebOfTrustUpdateJob();
53 /** The web of trust connector. */
54 private final WebOfTrustConnector webOfTrustConnector;
56 /** The queue for jobs. */
57 private final BlockingQueue<WebOfTrustUpdateJob> updateJobs = new LinkedBlockingQueue<WebOfTrustUpdateJob>();
60 * Creates a new trust updater.
62 * @param webOfTrustConnector
63 * The web of trust connector
66 public WebOfTrustUpdater(WebOfTrustConnector webOfTrustConnector) {
67 super("Trust Updater");
68 this.webOfTrustConnector = webOfTrustConnector;
76 * Updates the trust relation between the truster and the trustee. This
77 * method will return immediately and perform a trust update in the
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}
86 * to remove 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
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
135 * The own identity to add the context to
139 * {@code true} to wait for the end of the operation,
140 * {@code false} to return immediately
141 * @return {@code true} if the context was added successfully, {@code false}
142 * if the context was not added successfully, or if the job should
143 * not 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);
230 protected void serviceRun() {
231 while (!shouldStop()) {
233 WebOfTrustUpdateJob updateJob = updateJobs.take();
234 if (shouldStop() || (updateJob == stopJob)) {
237 logger.log(Level.FINE, "Running Trust Update Job: " + updateJob);
238 long startTime = System.currentTimeMillis();
240 long endTime = System.currentTimeMillis();
241 logger.log(Level.FINE, "Trust Update Job finished, took " + (endTime - startTime) + " ms.");
242 } catch (InterruptedException ie1) {
243 /* happens, ignore, loop. */
252 protected void serviceStop() {
254 updateJobs.put(stopJob);
255 } catch (InterruptedException ie1) {
256 /* the queue is unbounded so it should never block. */
261 * Base class for WebOfTrust update jobs.
263 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
265 private class WebOfTrustUpdateJob {
267 /** Object for synchronization. */
268 @SuppressWarnings("hiding")
269 private final Object syncObject = new Object();
271 /** Whether the job has finished. */
272 private boolean finished;
274 /** Whether the job was successful. */
275 private boolean success;
282 * Performs the actual update operation.
284 * The implementation of this class does nothing.
291 * Waits for completion of this job or stopping of the WebOfTrust
294 * @return {@code true} if this job finished successfully, {@code false}
297 * @see WebOfTrustUpdater#stop()
299 @SuppressWarnings("synthetic-access")
300 public boolean waitForCompletion() {
301 synchronized (syncObject) {
302 while (!finished && !shouldStop()) {
305 } catch (InterruptedException ie1) {
306 /* we’re looping, ignore. */
318 * Signals that this job has finished.
321 * {@code true} if this job finished successfully,
322 * {@code false} otherwise
324 protected void finish(boolean success) {
325 synchronized (syncObject) {
327 this.success = success;
328 syncObject.notifyAll();
335 * Base class for WebOfTrust trust update jobs.
337 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
339 private class WebOfTrustTrustUpdateJob extends WebOfTrustUpdateJob {
341 /** The identity giving the trust. */
342 protected final OwnIdentity truster;
344 /** The identity receiving the trust. */
345 protected final Identity trustee;
348 * Creates a new trust update job.
351 * The identity giving the trust
353 * The identity receiving the trust
355 @SuppressWarnings("synthetic-access")
356 public WebOfTrustTrustUpdateJob(OwnIdentity truster, Identity trustee) {
357 this.truster = truster;
358 this.trustee = trustee;
369 public boolean equals(Object object) {
370 if ((object == null) || !object.getClass().equals(getClass())) {
373 WebOfTrustTrustUpdateJob updateJob = (WebOfTrustTrustUpdateJob) object;
374 return ((truster == null) ? (updateJob.truster == null) : updateJob.truster.equals(truster)) && ((trustee == null) ? (updateJob.trustee == null) : updateJob.trustee.equals(trustee));
381 public int hashCode() {
382 return getClass().hashCode() ^ ((truster == null) ? 0 : truster.hashCode()) ^ ((trustee == null) ? 0 : trustee.hashCode());
389 public String toString() {
390 return String.format("%s[truster=%s,trustee=%s]", getClass().getSimpleName(), (truster == null) ? null : truster.getId(), (trustee == null) ? null : trustee.getId());
396 * Update job that sets the trust relation between two identities.
398 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
400 private class SetTrustJob extends WebOfTrustTrustUpdateJob {
402 /** The score of the relation. */
403 private final Integer score;
405 /** The comment of the relation. */
406 private final String comment;
409 * Creates a new set trust job.
412 * The identity giving the trust
414 * The identity receiving the trust
416 * The score of the trust (from -100 to 100, may be
417 * {@code null} to remote the trust relation completely)
419 * The comment of the trust relation
421 public SetTrustJob(OwnIdentity truster, Identity trustee, Integer score, String comment) {
422 super(truster, trustee);
424 this.comment = comment;
431 @SuppressWarnings("synthetic-access")
435 if (trustee instanceof DefaultIdentity) {
436 ((DefaultIdentity) trustee).setTrust(truster, new Trust(score, null, 0));
438 webOfTrustConnector.setTrust(truster, trustee, score, comment);
440 if (trustee instanceof DefaultIdentity) {
441 ((DefaultIdentity) trustee).setTrust(truster, null);
443 webOfTrustConnector.removeTrust(truster, trustee);
446 } catch (WebOfTrustException wote1) {
447 logger.log(Level.WARNING, "Could not set Trust value for " + truster + " -> " + trustee + " to " + score + " (" + comment + ")!", wote1);
455 * Base class for context updates of an {@link OwnIdentity}.
457 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
459 private class WebOfTrustContextUpdateJob extends WebOfTrustUpdateJob {
461 /** The own identity whose contexts to manage. */
462 protected final OwnIdentity ownIdentity;
464 /** The context to update. */
465 protected final String context;
468 * Creates a new context update job.
471 * The own identity to update
473 * The context to update
475 @SuppressWarnings("synthetic-access")
476 public WebOfTrustContextUpdateJob(OwnIdentity ownIdentity, String context) {
477 Validation.begin().isNotNull("OwnIdentity", ownIdentity).isNotNull("Context", context).check();
478 this.ownIdentity = ownIdentity;
479 this.context = context;
490 public boolean equals(Object object) {
491 if ((object == null) || !object.getClass().equals(getClass())) {
494 WebOfTrustContextUpdateJob updateJob = (WebOfTrustContextUpdateJob) object;
495 return updateJob.ownIdentity.equals(ownIdentity) && updateJob.context.equals(context);
502 public int hashCode() {
503 return getClass().hashCode() ^ ownIdentity.hashCode() ^ context.hashCode();
510 public String toString() {
511 return String.format("%s[ownIdentity=%s,context=%s]", getClass().getSimpleName(), ownIdentity, context);
517 * Job that adds a context to an {@link OwnIdentity}.
519 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
521 private class AddContextJob extends WebOfTrustContextUpdateJob {
524 * Creates a new add-context job.
527 * The own identity whose contexts to manage
531 public AddContextJob(OwnIdentity ownIdentity, String context) {
532 super(ownIdentity, context);
539 @SuppressWarnings("synthetic-access")
542 webOfTrustConnector.addContext(ownIdentity, context);
543 ownIdentity.addContext(context);
545 } catch (PluginException pe1) {
546 logger.log(Level.WARNING, String.format("Could not add Context “%2$s” to Own Identity %1$s!", ownIdentity, context), pe1);
554 * Job that removes a context from an {@link OwnIdentity}.
556 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
558 private class RemoveContextJob extends WebOfTrustContextUpdateJob {
561 * Creates a new remove-context job.
564 * The own identity whose contexts to manage
566 * The context to remove
568 public RemoveContextJob(OwnIdentity ownIdentity, String context) {
569 super(ownIdentity, context);
576 @SuppressWarnings("synthetic-access")
579 webOfTrustConnector.removeContext(ownIdentity, context);
580 ownIdentity.removeContext(context);
582 } catch (PluginException pe1) {
583 logger.log(Level.WARNING, String.format("Could not remove Context “%2$s” to Own Identity %1$s!", ownIdentity, context), pe1);
591 * Base class for update jobs that deal with properties.
593 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
595 private class WebOfTrustPropertyUpdateJob extends WebOfTrustUpdateJob {
597 /** The own identity to update properties on. */
598 protected final OwnIdentity ownIdentity;
600 /** The name of the property to update. */
601 protected final String propertyName;
604 * Creates a new property update job.
607 * The own identity to update the property on
608 * @param propertyName
609 * The name of the property to update
611 @SuppressWarnings("synthetic-access")
612 public WebOfTrustPropertyUpdateJob(OwnIdentity ownIdentity, String propertyName) {
613 this.ownIdentity = ownIdentity;
614 this.propertyName = propertyName;
625 public boolean equals(Object object) {
626 if ((object == null) || !object.getClass().equals(getClass())) {
629 WebOfTrustPropertyUpdateJob updateJob = (WebOfTrustPropertyUpdateJob) object;
630 return updateJob.ownIdentity.equals(ownIdentity) && updateJob.propertyName.equals(propertyName);
637 public int hashCode() {
638 return getClass().hashCode() ^ ownIdentity.hashCode() ^ propertyName.hashCode();
645 public String toString() {
646 return String.format("%s[ownIdentity=%s,propertyName=%s]", getClass().getSimpleName(), ownIdentity, propertyName);
652 * WebOfTrust update job that sets a property on an {@link OwnIdentity}.
654 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
656 private class SetPropertyJob extends WebOfTrustPropertyUpdateJob {
658 /** The value of the property to set. */
659 private final String propertyValue;
662 * Creates a new set-property job.
665 * The own identity to set the property on
666 * @param propertyName
667 * The name of the property to set
668 * @param propertyValue
669 * The value of the property to set
671 public SetPropertyJob(OwnIdentity ownIdentity, String propertyName, String propertyValue) {
672 super(ownIdentity, propertyName);
673 this.propertyValue = propertyValue;
680 @SuppressWarnings("synthetic-access")
683 if (propertyValue == null) {
684 webOfTrustConnector.removeProperty(ownIdentity, propertyName);
685 ownIdentity.removeProperty(propertyName);
687 webOfTrustConnector.setProperty(ownIdentity, propertyName, propertyValue);
688 ownIdentity.setProperty(propertyName, propertyValue);
691 } catch (PluginException pe1) {
692 logger.log(Level.WARNING, String.format("Could not set Property “%2$s” to “%3$s” on Own Identity %1$s!", ownIdentity, propertyName, propertyValue), pe1);