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.common.annotations.VisibleForTesting;
38 import com.google.inject.Inject;
41 * Updates WebOfTrust identity data in a background thread because communicating
42 * with the WebOfTrust plugin can potentially last quite long.
44 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
46 public class WebOfTrustUpdater extends AbstractService {
49 private static final Logger logger = Logging.getLogger(WebOfTrustUpdater.class);
52 @SuppressWarnings("synthetic-access")
53 private final WebOfTrustUpdateJob stopJob = new WebOfTrustUpdateJob();
55 /** The web of trust connector. */
56 private final WebOfTrustConnector webOfTrustConnector;
58 /** The queue for jobs. */
59 private final BlockingQueue<WebOfTrustUpdateJob> updateJobs = new LinkedBlockingQueue<WebOfTrustUpdateJob>();
62 * Creates a new trust updater.
64 * @param webOfTrustConnector
65 * The web of trust connector
68 public WebOfTrustUpdater(WebOfTrustConnector webOfTrustConnector) {
69 super("Trust Updater");
70 this.webOfTrustConnector = webOfTrustConnector;
78 * Updates the trust relation between the truster and the trustee. This method
79 * will return immediately and perform a trust update in the background.
82 * The identity giving the trust
84 * The identity receiving the trust
86 * The new level of trust (from -100 to 100, may be {@code null} to remove
87 * the trust completely)
89 * The comment of the trust relation
91 public void setTrust(OwnIdentity truster, Identity trustee, Integer score, String comment) {
92 SetTrustJob setTrustJob = new SetTrustJob(truster, trustee, score, comment);
93 if (updateJobs.contains(setTrustJob)) {
94 updateJobs.remove(setTrustJob);
96 logger.log(Level.FINER, "Adding Trust Update Job: " + setTrustJob);
98 updateJobs.put(setTrustJob);
99 } catch (InterruptedException e) {
100 /* the queue is unbounded so it should never block. */
105 * Adds the given context to the given own identity.
108 * The own identity to add the context to
112 public void addContext(OwnIdentity ownIdentity, String context) {
113 addContextWait(ownIdentity, context, false);
117 * Adds the given context to the given own identity, waiting for completion of
121 * The own identity to add the context to
124 * @return {@code true} if the context was added successfully, {@code false}
127 public boolean addContextWait(OwnIdentity ownIdentity, String context) {
128 return addContextWait(ownIdentity, context, true);
132 * Adds the given context to the given own identity, waiting for completion of
136 * The own identity to add the context to
140 * {@code true} to wait for the end of the operation, {@code false} to return
142 * @return {@code true} if the context was added successfully, {@code false} if
143 * the context was not added successfully, or if the job should not
144 * wait for completion
146 private boolean addContextWait(OwnIdentity ownIdentity, String context, boolean wait) {
147 AddContextJob addContextJob = new AddContextJob(ownIdentity, context);
148 if (!updateJobs.contains(addContextJob)) {
149 logger.log(Level.FINER, "Adding Context Job: " + addContextJob);
151 updateJobs.put(addContextJob);
152 } catch (InterruptedException ie1) {
153 /* the queue is unbounded so it should never block. */
156 return addContextJob.waitForCompletion();
159 for (WebOfTrustUpdateJob updateJob : updateJobs) {
160 if (updateJob.equals(addContextJob)) {
161 return updateJob.waitForCompletion();
169 * Removes the given context from the given own identity.
172 * The own identity to remove the context from
174 * The context to remove
176 public void removeContext(OwnIdentity ownIdentity, String context) {
177 RemoveContextJob removeContextJob = new RemoveContextJob(ownIdentity, context);
178 if (!updateJobs.contains(removeContextJob)) {
179 logger.log(Level.FINER, "Adding Context Job: " + removeContextJob);
181 updateJobs.put(removeContextJob);
182 } catch (InterruptedException ie1) {
183 /* the queue is unbounded so it should never block. */
189 * Sets a property on the given own identity.
192 * The own identity to set the property on
193 * @param propertyName
194 * The name of the property to set
195 * @param propertyValue
196 * The value of the property to set
198 public void setProperty(OwnIdentity ownIdentity, String propertyName, String propertyValue) {
199 SetPropertyJob setPropertyJob = new SetPropertyJob(ownIdentity, propertyName, propertyValue);
200 if (updateJobs.contains(setPropertyJob)) {
201 updateJobs.remove(setPropertyJob);
203 logger.log(Level.FINER, "Adding Property Job: " + setPropertyJob);
205 updateJobs.put(setPropertyJob);
206 } catch (InterruptedException e) {
207 /* the queue is unbounded so it should never block. */
212 * Removes a property from the given own identity.
215 * The own identity to remove the property from
216 * @param propertyName
217 * The name of the property to remove
219 public void removeProperty(OwnIdentity ownIdentity, String propertyName) {
220 setProperty(ownIdentity, propertyName, null);
229 protected void serviceRun() {
230 while (!shouldStop()) {
232 WebOfTrustUpdateJob updateJob = updateJobs.take();
233 if (shouldStop() || (updateJob == stopJob)) {
236 logger.log(Level.FINE, "Running Trust Update Job: " + updateJob);
237 long startTime = System.currentTimeMillis();
239 long endTime = System.currentTimeMillis();
240 logger.log(Level.FINE, "Trust Update Job finished, took " + (endTime - startTime) + " ms.");
241 } catch (InterruptedException ie1) {
242 /* 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>
263 class WebOfTrustUpdateJob implements Runnable {
265 /** Object for synchronization. */
266 @SuppressWarnings("hiding")
267 private final Object syncObject = new Object();
269 /** Whether the job has finished. */
270 private boolean finished;
272 /** Whether the job was successful. */
273 private boolean success;
280 * Performs the actual update operation.
282 * The implementation of this class does nothing.
290 * Waits for completion of this job or stopping of the WebOfTrust updater.
292 * @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, {@code false} otherwise
320 protected void finish(boolean success) {
321 synchronized (syncObject) {
323 this.success = success;
324 syncObject.notifyAll();
331 * Update job that sets the trust relation between two identities.
333 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
335 private class SetTrustJob extends WebOfTrustUpdateJob {
337 /** The identity giving the trust. */
338 private final OwnIdentity truster;
340 /** The identity receiving the trust. */
341 private final Identity trustee;
343 /** The score of the relation. */
344 private final Integer score;
346 /** The comment of the relation. */
347 private final String comment;
350 * Creates a new set trust job.
353 * The identity giving the trust
355 * The identity receiving the trust
357 * The score of the trust (from -100 to 100, may be {@code null} to remote
358 * the trust relation completely)
360 * The comment of the trust relation
362 public SetTrustJob(OwnIdentity truster, Identity trustee, Integer score, String comment) {
363 this.truster = truster;
364 this.trustee = trustee;
366 this.comment = comment;
371 @SuppressWarnings("synthetic-access")
375 if (trustee instanceof DefaultIdentity) {
376 ((DefaultIdentity) trustee).setTrust(truster, new Trust(score, null, 0));
378 webOfTrustConnector.setTrust(truster, trustee, score, comment);
380 if (trustee instanceof DefaultIdentity) {
381 ((DefaultIdentity) trustee).setTrust(truster, null);
383 webOfTrustConnector.removeTrust(truster, trustee);
386 } catch (WebOfTrustException wote1) {
387 logger.log(Level.WARNING, "Could not set Trust value for " + truster + " -> " + trustee + " to " + score + " (" + comment + ")!", wote1);
398 public boolean equals(Object object) {
399 if ((object == null) || !object.getClass().equals(getClass())) {
402 SetTrustJob updateJob = (SetTrustJob) object;
403 return ((truster == null) ? (updateJob.truster == null) : updateJob.truster.equals(truster)) && ((trustee == null) ? (updateJob.trustee == null) : updateJob.trustee.equals(trustee));
408 public int hashCode() {
409 return getClass().hashCode() ^ ((truster == null) ? 0 : truster.hashCode()) ^ ((trustee == null) ? 0 : trustee.hashCode());
414 public String toString() {
415 return String.format("%s[truster=%s,trustee=%s]", getClass().getSimpleName(), (truster == null) ? null : truster.getId(), (trustee == null) ? null : trustee.getId());
421 * Base class for context updates of an {@link OwnIdentity}.
423 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
426 class WebOfTrustContextUpdateJob extends WebOfTrustUpdateJob {
428 /** The own identity whose contexts to manage. */
429 protected final OwnIdentity ownIdentity;
431 /** The context to update. */
432 protected final String context;
435 * Creates a new context update job.
438 * The own identity to update
440 * The context to update
442 @SuppressWarnings("synthetic-access")
443 public WebOfTrustContextUpdateJob(OwnIdentity ownIdentity, String context) {
444 this.ownIdentity = checkNotNull(ownIdentity, "ownIdentity must not be null");
445 this.context = checkNotNull(context, "context must not be null");
454 public boolean equals(Object object) {
455 if ((object == null) || !object.getClass().equals(getClass())) {
458 WebOfTrustContextUpdateJob updateJob = (WebOfTrustContextUpdateJob) object;
459 return updateJob.ownIdentity.equals(ownIdentity) && updateJob.context.equals(context);
464 public int hashCode() {
465 return getClass().hashCode() ^ ownIdentity.hashCode() ^ context.hashCode();
470 public String toString() {
471 return String.format("%s[ownIdentity=%s,context=%s]", getClass().getSimpleName(), ownIdentity, context);
477 * Job that adds a context to an {@link OwnIdentity}.
479 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
482 class AddContextJob extends WebOfTrustContextUpdateJob {
485 * Creates a new add-context job.
488 * The own identity whose contexts to manage
492 public AddContextJob(OwnIdentity ownIdentity, String context) {
493 super(ownIdentity, context);
498 @SuppressWarnings("synthetic-access")
501 webOfTrustConnector.addContext(ownIdentity, context);
502 ownIdentity.addContext(context);
504 } catch (PluginException pe1) {
505 logger.log(Level.WARNING, String.format("Could not add Context “%2$s” to Own Identity %1$s!", ownIdentity, context), pe1);
513 * Job that removes a context from an {@link OwnIdentity}.
515 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
518 class RemoveContextJob extends WebOfTrustContextUpdateJob {
521 * Creates a new remove-context job.
524 * The own identity whose contexts to manage
526 * The context to remove
528 public RemoveContextJob(OwnIdentity ownIdentity, String context) {
529 super(ownIdentity, context);
534 @SuppressWarnings("synthetic-access")
537 webOfTrustConnector.removeContext(ownIdentity, context);
538 ownIdentity.removeContext(context);
540 } catch (PluginException pe1) {
541 logger.log(Level.WARNING, String.format("Could not remove Context “%2$s” to Own Identity %1$s!", ownIdentity, context), pe1);
549 * WebOfTrust update job that sets a property on an {@link OwnIdentity}.
551 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
554 class SetPropertyJob extends WebOfTrustUpdateJob {
556 /** The own identity to update properties on. */
557 private final OwnIdentity ownIdentity;
559 /** The name of the property to update. */
560 private final String propertyName;
562 /** The value of the property to set. */
563 private final String propertyValue;
566 * Creates a new set-property job.
569 * The own identity to set the property on
570 * @param propertyName
571 * The name of the property to set
572 * @param propertyValue
573 * The value of the property to set
575 public SetPropertyJob(OwnIdentity ownIdentity, String propertyName, String propertyValue) {
576 this.ownIdentity = ownIdentity;
577 this.propertyName = propertyName;
578 this.propertyValue = propertyValue;
583 @SuppressWarnings("synthetic-access")
586 if (propertyValue == null) {
587 webOfTrustConnector.removeProperty(ownIdentity, propertyName);
588 ownIdentity.removeProperty(propertyName);
590 webOfTrustConnector.setProperty(ownIdentity, propertyName, propertyValue);
591 ownIdentity.setProperty(propertyName, propertyValue);
594 } catch (PluginException pe1) {
595 logger.log(Level.WARNING, String.format("Could not set Property “%2$s” to “%3$s” on Own Identity %1$s!", ownIdentity, propertyName, propertyValue), pe1);
606 public boolean equals(Object object) {
607 if ((object == null) || !object.getClass().equals(getClass())) {
610 SetPropertyJob updateJob = (SetPropertyJob) object;
611 return updateJob.ownIdentity.equals(ownIdentity) && updateJob.propertyName.equals(propertyName);
616 public int hashCode() {
617 return getClass().hashCode() ^ ownIdentity.hashCode() ^ propertyName.hashCode();
622 public String toString() {
623 return String.format("%s[ownIdentity=%s,propertyName=%s]", getClass().getSimpleName(), ownIdentity, propertyName);