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;
37 * Updates WebOfTrust identity data in a background thread because communicating
38 * with the WebOfTrust plugin can potentially last quite long.
40 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
42 public class WebOfTrustUpdater extends AbstractService {
45 private static final Logger logger = Logging.getLogger(WebOfTrustUpdater.class);
48 @SuppressWarnings("synthetic-access")
49 private final WebOfTrustUpdateJob stopJob = new WebOfTrustUpdateJob();
51 /** The web of trust connector. */
52 private final WebOfTrustConnector webOfTrustConnector;
54 /** The queue for jobs. */
55 private final BlockingQueue<WebOfTrustUpdateJob> updateJobs = new LinkedBlockingQueue<WebOfTrustUpdateJob>();
58 * Creates a new trust updater.
60 * @param webOfTrustConnector
61 * The web of trust connector
63 public WebOfTrustUpdater(WebOfTrustConnector webOfTrustConnector) {
64 super("Trust Updater");
65 this.webOfTrustConnector = webOfTrustConnector;
73 * Updates the trust relation between the truster and the trustee. This
74 * method will return immediately and perform a trust update in the
78 * The identity giving the trust
80 * The identity receiving the trust
82 * The new level of trust (from -100 to 100, may be {@code null}
83 * to remove the trust completely)
85 * The comment of the trust relation
87 public void setTrust(OwnIdentity truster, Identity trustee, Integer score, String comment) {
88 SetTrustJob setTrustJob = new SetTrustJob(truster, trustee, score, comment);
89 if (updateJobs.contains(setTrustJob)) {
90 updateJobs.remove(setTrustJob);
92 logger.log(Level.FINER, "Adding Trust Update Job: " + setTrustJob);
94 updateJobs.put(setTrustJob);
95 } catch (InterruptedException e) {
96 /* the queue is unbounded so it should never block. */
101 * Adds the given context to the given own identity.
104 * The own identity to add the context to
108 public void addContext(OwnIdentity ownIdentity, String context) {
109 addContextWait(ownIdentity, context, false);
113 * Adds the given context to the given own identity, waiting for completion
117 * The own identity to add the context to
120 * @return {@code true} if the context was added successfully, {@code false}
123 public boolean addContextWait(OwnIdentity ownIdentity, String context) {
124 return addContextWait(ownIdentity, context, true);
128 * Adds the given context to the given own identity, waiting for completion
132 * The own identity to add the context to
136 * {@code true} to wait for the end of the operation,
137 * {@code false} to return immediately
138 * @return {@code true} if the context was added successfully, {@code false}
139 * if the context was not added successfully, or if the job should
140 * not wait for completion
142 private boolean addContextWait(OwnIdentity ownIdentity, String context, boolean wait) {
143 AddContextJob addContextJob = new AddContextJob(ownIdentity, context);
144 if (!updateJobs.contains(addContextJob)) {
145 logger.log(Level.FINER, "Adding Context Job: " + addContextJob);
147 updateJobs.put(addContextJob);
148 } catch (InterruptedException ie1) {
149 /* the queue is unbounded so it should never block. */
152 return addContextJob.waitForCompletion();
155 for (WebOfTrustUpdateJob updateJob : updateJobs) {
156 if (updateJob.equals(addContextJob)) {
157 return updateJob.waitForCompletion();
165 * Removes the given context from the given own identity.
168 * The own identity to remove the context from
170 * The context to remove
172 public void removeContext(OwnIdentity ownIdentity, String context) {
173 RemoveContextJob removeContextJob = new RemoveContextJob(ownIdentity, context);
174 if (!updateJobs.contains(removeContextJob)) {
175 logger.log(Level.FINER, "Adding Context Job: " + removeContextJob);
177 updateJobs.put(removeContextJob);
178 } catch (InterruptedException ie1) {
179 /* the queue is unbounded so it should never block. */
185 * Sets a property on the given own identity.
188 * The own identity to set the property on
189 * @param propertyName
190 * The name of the property to set
191 * @param propertyValue
192 * The value of the property to set
194 public void setProperty(OwnIdentity ownIdentity, String propertyName, String propertyValue) {
195 SetPropertyJob setPropertyJob = new SetPropertyJob(ownIdentity, propertyName, propertyValue);
196 if (updateJobs.contains(setPropertyJob)) {
197 updateJobs.remove(setPropertyJob);
199 logger.log(Level.FINER, "Adding Property Job: " + setPropertyJob);
201 updateJobs.put(setPropertyJob);
202 } catch (InterruptedException e) {
203 /* the queue is unbounded so it should never block. */
208 * Removes a property from the given own identity.
211 * The own identity to remove the property from
212 * @param propertyName
213 * The name of the property to remove
215 public void removeProperty(OwnIdentity ownIdentity, String propertyName) {
216 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. */
249 protected void serviceStop() {
251 updateJobs.put(stopJob);
252 } catch (InterruptedException ie1) {
253 /* the queue is unbounded so it should never block. */
258 * Base class for WebOfTrust update jobs.
260 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
262 private class WebOfTrustUpdateJob {
264 /** Object for synchronization. */
265 @SuppressWarnings("hiding")
266 private final Object syncObject = new Object();
268 /** Whether the job has finished. */
269 private boolean finished;
271 /** Whether the job was successful. */
272 private boolean success;
279 * Performs the actual update operation.
281 * The implementation of this class does nothing.
288 * Waits for completion of this job or stopping of the WebOfTrust
291 * @return {@code true} if this job finished successfully, {@code false}
294 * @see WebOfTrustUpdater#stop()
296 @SuppressWarnings("synthetic-access")
297 public boolean waitForCompletion() {
298 synchronized (syncObject) {
299 while (!finished && !shouldStop()) {
302 } catch (InterruptedException ie1) {
303 /* we’re looping, ignore. */
315 * Signals that this job has finished.
318 * {@code true} if this job finished successfully,
319 * {@code false} otherwise
321 protected void finish(boolean success) {
322 synchronized (syncObject) {
324 this.success = success;
325 syncObject.notifyAll();
332 * Base class for WebOfTrust trust update jobs.
334 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
336 private class WebOfTrustTrustUpdateJob extends WebOfTrustUpdateJob {
338 /** The identity giving the trust. */
339 protected final OwnIdentity truster;
341 /** The identity receiving the trust. */
342 protected final Identity trustee;
345 * Creates a new trust update job.
348 * The identity giving the trust
350 * The identity receiving the trust
352 @SuppressWarnings("synthetic-access")
353 public WebOfTrustTrustUpdateJob(OwnIdentity truster, Identity trustee) {
354 this.truster = truster;
355 this.trustee = trustee;
366 public boolean equals(Object object) {
367 if ((object == null) || !object.getClass().equals(getClass())) {
370 WebOfTrustTrustUpdateJob updateJob = (WebOfTrustTrustUpdateJob) object;
371 return ((truster == null) ? (updateJob.truster == null) : updateJob.truster.equals(truster)) && ((trustee == null) ? (updateJob.trustee == null) : updateJob.trustee.equals(trustee));
378 public int hashCode() {
379 return getClass().hashCode() ^ ((truster == null) ? 0 : truster.hashCode()) ^ ((trustee == null) ? 0 : trustee.hashCode());
386 public String toString() {
387 return String.format("%s[truster=%s,trustee=%s]", getClass().getSimpleName(), (truster == null) ? null : truster.getId(), (trustee == null) ? null : trustee.getId());
393 * Update job that sets the trust relation between two identities.
395 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
397 private class SetTrustJob extends WebOfTrustTrustUpdateJob {
399 /** The score of the relation. */
400 private final Integer score;
402 /** The comment of the relation. */
403 private final String comment;
406 * Creates a new set trust job.
409 * The identity giving the trust
411 * The identity receiving the trust
413 * The score of the trust (from -100 to 100, may be
414 * {@code null} to remote the trust relation completely)
416 * The comment of the trust relation
418 public SetTrustJob(OwnIdentity truster, Identity trustee, Integer score, String comment) {
419 super(truster, trustee);
421 this.comment = comment;
428 @SuppressWarnings("synthetic-access")
432 if (trustee instanceof DefaultIdentity) {
433 ((DefaultIdentity) trustee).setTrust(truster, new Trust(score, null, 0));
435 webOfTrustConnector.setTrust(truster, trustee, score, comment);
437 if (trustee instanceof DefaultIdentity) {
438 ((DefaultIdentity) trustee).setTrust(truster, null);
440 webOfTrustConnector.removeTrust(truster, trustee);
443 } catch (WebOfTrustException wote1) {
444 logger.log(Level.WARNING, "Could not set Trust value for " + truster + " -> " + trustee + " to " + score + " (" + comment + ")!", wote1);
452 * Base class for context updates of an {@link OwnIdentity}.
454 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
456 private class WebOfTrustContextUpdateJob extends WebOfTrustUpdateJob {
458 /** The own identity whose contexts to manage. */
459 protected final OwnIdentity ownIdentity;
461 /** The context to update. */
462 protected final String context;
465 * Creates a new context update job.
468 * The own identity to update
470 * The context to update
472 @SuppressWarnings("synthetic-access")
473 public WebOfTrustContextUpdateJob(OwnIdentity ownIdentity, String context) {
474 Validation.begin().isNotNull("OwnIdentity", ownIdentity).isNotNull("Context", context).check();
475 this.ownIdentity = ownIdentity;
476 this.context = context;
487 public boolean equals(Object object) {
488 if ((object == null) || !object.getClass().equals(getClass())) {
491 WebOfTrustContextUpdateJob updateJob = (WebOfTrustContextUpdateJob) object;
492 return updateJob.ownIdentity.equals(ownIdentity) && updateJob.context.equals(context);
499 public int hashCode() {
500 return getClass().hashCode() ^ ownIdentity.hashCode() ^ context.hashCode();
507 public String toString() {
508 return String.format("%s[ownIdentity=%s,context=%s]", getClass().getSimpleName(), ownIdentity, context);
514 * Job that adds a context to an {@link OwnIdentity}.
516 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
518 private class AddContextJob extends WebOfTrustContextUpdateJob {
521 * Creates a new add-context job.
524 * The own identity whose contexts to manage
528 public AddContextJob(OwnIdentity ownIdentity, String context) {
529 super(ownIdentity, context);
536 @SuppressWarnings("synthetic-access")
539 webOfTrustConnector.addContext(ownIdentity, context);
540 ownIdentity.addContext(context);
542 } catch (PluginException pe1) {
543 logger.log(Level.WARNING, String.format("Could not add Context “%2$s” to Own Identity %1$s!", ownIdentity, context), pe1);
551 * Job that removes a context from an {@link OwnIdentity}.
553 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
555 private class RemoveContextJob extends WebOfTrustContextUpdateJob {
558 * Creates a new remove-context job.
561 * The own identity whose contexts to manage
563 * The context to remove
565 public RemoveContextJob(OwnIdentity ownIdentity, String context) {
566 super(ownIdentity, context);
573 @SuppressWarnings("synthetic-access")
576 webOfTrustConnector.removeContext(ownIdentity, context);
577 ownIdentity.removeContext(context);
579 } catch (PluginException pe1) {
580 logger.log(Level.WARNING, String.format("Could not remove Context “%2$s” to Own Identity %1$s!", ownIdentity, context), pe1);
588 * Base class for update jobs that deal with properties.
590 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
592 private class WebOfTrustPropertyUpdateJob extends WebOfTrustUpdateJob {
594 /** The own identity to update properties on. */
595 protected final OwnIdentity ownIdentity;
597 /** The name of the property to update. */
598 protected final String propertyName;
601 * Creates a new property update job.
604 * The own identity to update the property on
605 * @param propertyName
606 * The name of the property to update
608 @SuppressWarnings("synthetic-access")
609 public WebOfTrustPropertyUpdateJob(OwnIdentity ownIdentity, String propertyName) {
610 this.ownIdentity = ownIdentity;
611 this.propertyName = propertyName;
622 public boolean equals(Object object) {
623 if ((object == null) || !object.getClass().equals(getClass())) {
626 WebOfTrustPropertyUpdateJob updateJob = (WebOfTrustPropertyUpdateJob) object;
627 return updateJob.ownIdentity.equals(ownIdentity) && updateJob.propertyName.equals(propertyName);
634 public int hashCode() {
635 return getClass().hashCode() ^ ownIdentity.hashCode() ^ propertyName.hashCode();
642 public String toString() {
643 return String.format("%s[ownIdentity=%s,propertyName=%s]", getClass().getSimpleName(), ownIdentity, propertyName);
649 * WebOfTrust update job that sets a property on an {@link OwnIdentity}.
651 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
653 private class SetPropertyJob extends WebOfTrustPropertyUpdateJob {
655 /** The value of the property to set. */
656 private final String propertyValue;
659 * Creates a new set-property job.
662 * The own identity to set the property on
663 * @param propertyName
664 * The name of the property to set
665 * @param propertyValue
666 * The value of the property to set
668 public SetPropertyJob(OwnIdentity ownIdentity, String propertyName, String propertyValue) {
669 super(ownIdentity, propertyName);
670 this.propertyValue = propertyValue;
677 @SuppressWarnings("synthetic-access")
680 if (propertyValue == null) {
681 webOfTrustConnector.removeProperty(ownIdentity, propertyName);
682 ownIdentity.removeProperty(propertyName);
684 webOfTrustConnector.setProperty(ownIdentity, propertyName, propertyValue);
685 ownIdentity.setProperty(propertyName, propertyValue);
688 } catch (PluginException pe1) {
689 logger.log(Level.WARNING, String.format("Could not set Property “%2$s” to “%3$s” on Own Identity %1$s!", ownIdentity, propertyName, propertyValue), pe1);