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);
228 protected void serviceRun() {
229 while (!shouldStop()) {
231 WebOfTrustUpdateJob updateJob = updateJobs.take();
232 if (shouldStop() || (updateJob == stopJob)) {
235 logger.log(Level.FINE, "Running Trust Update Job: " + updateJob);
236 long startTime = System.currentTimeMillis();
238 long endTime = System.currentTimeMillis();
239 logger.log(Level.FINE, "Trust Update Job finished, took " + (endTime - startTime) + " ms.");
240 } catch (InterruptedException ie1) {
241 /* happens, ignore, loop. */
248 protected void serviceStop() {
250 updateJobs.put(stopJob);
251 } catch (InterruptedException ie1) {
252 /* the queue is unbounded so it should never block. */
257 * Base class for WebOfTrust update jobs.
259 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
261 private class WebOfTrustUpdateJob {
263 /** Object for synchronization. */
264 @SuppressWarnings("hiding")
265 private final Object syncObject = new Object();
267 /** Whether the job has finished. */
268 private boolean finished;
270 /** Whether the job was successful. */
271 private boolean success;
278 * Performs the actual update operation.
280 * The implementation of this class does nothing.
287 * Waits for completion of this job or stopping of the WebOfTrust updater.
289 * @return {@code true} if this job finished successfully, {@code false}
291 * @see WebOfTrustUpdater#stop()
293 @SuppressWarnings("synthetic-access")
294 public boolean waitForCompletion() {
295 synchronized (syncObject) {
296 while (!finished && !shouldStop()) {
299 } catch (InterruptedException ie1) {
300 /* we’re looping, ignore. */
312 * Signals that this job has finished.
315 * {@code true} if this job finished successfully, {@code false} otherwise
317 protected void finish(boolean success) {
318 synchronized (syncObject) {
320 this.success = success;
321 syncObject.notifyAll();
328 * Update job that sets the trust relation between two identities.
330 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
332 private class SetTrustJob extends WebOfTrustUpdateJob {
334 /** The identity giving the trust. */
335 private final OwnIdentity truster;
337 /** The identity receiving the trust. */
338 private final Identity trustee;
340 /** The score of the relation. */
341 private final Integer score;
343 /** The comment of the relation. */
344 private final String comment;
347 * Creates a new set trust job.
350 * The identity giving the trust
352 * The identity receiving the trust
354 * The score of the trust (from -100 to 100, may be {@code null} to remote
355 * the trust relation completely)
357 * The comment of the trust relation
359 public SetTrustJob(OwnIdentity truster, Identity trustee, Integer score, String comment) {
360 this.truster = truster;
361 this.trustee = trustee;
363 this.comment = comment;
368 @SuppressWarnings("synthetic-access")
372 if (trustee instanceof DefaultIdentity) {
373 ((DefaultIdentity) trustee).setTrust(truster, new Trust(score, null, 0));
375 webOfTrustConnector.setTrust(truster, trustee, score, comment);
377 if (trustee instanceof DefaultIdentity) {
378 ((DefaultIdentity) trustee).setTrust(truster, null);
380 webOfTrustConnector.removeTrust(truster, trustee);
383 } catch (WebOfTrustException wote1) {
384 logger.log(Level.WARNING, "Could not set Trust value for " + truster + " -> " + trustee + " to " + score + " (" + comment + ")!", wote1);
395 public boolean equals(Object object) {
396 if ((object == null) || !object.getClass().equals(getClass())) {
399 SetTrustJob updateJob = (SetTrustJob) object;
400 return ((truster == null) ? (updateJob.truster == null) : updateJob.truster.equals(truster)) && ((trustee == null) ? (updateJob.trustee == null) : updateJob.trustee.equals(trustee));
405 public int hashCode() {
406 return getClass().hashCode() ^ ((truster == null) ? 0 : truster.hashCode()) ^ ((trustee == null) ? 0 : trustee.hashCode());
411 public String toString() {
412 return String.format("%s[truster=%s,trustee=%s]", getClass().getSimpleName(), (truster == null) ? null : truster.getId(), (trustee == null) ? null : trustee.getId());
418 * Base class for context updates of an {@link OwnIdentity}.
420 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
422 private class WebOfTrustContextUpdateJob extends WebOfTrustUpdateJob {
424 /** The own identity whose contexts to manage. */
425 protected final OwnIdentity ownIdentity;
427 /** The context to update. */
428 protected final String context;
431 * Creates a new context update job.
434 * The own identity to update
436 * The context to update
438 @SuppressWarnings("synthetic-access")
439 public WebOfTrustContextUpdateJob(OwnIdentity ownIdentity, String context) {
440 this.ownIdentity = checkNotNull(ownIdentity, "ownIdentity must not be null");
441 this.context = checkNotNull(context, "context must not be null");
450 public boolean equals(Object object) {
451 if ((object == null) || !object.getClass().equals(getClass())) {
454 WebOfTrustContextUpdateJob updateJob = (WebOfTrustContextUpdateJob) object;
455 return updateJob.ownIdentity.equals(ownIdentity) && updateJob.context.equals(context);
460 public int hashCode() {
461 return getClass().hashCode() ^ ownIdentity.hashCode() ^ context.hashCode();
466 public String toString() {
467 return String.format("%s[ownIdentity=%s,context=%s]", getClass().getSimpleName(), ownIdentity, context);
473 * Job that adds a context to an {@link OwnIdentity}.
475 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
477 private class AddContextJob extends WebOfTrustContextUpdateJob {
480 * Creates a new add-context job.
483 * The own identity whose contexts to manage
487 public AddContextJob(OwnIdentity ownIdentity, String context) {
488 super(ownIdentity, context);
493 @SuppressWarnings("synthetic-access")
496 webOfTrustConnector.addContext(ownIdentity, context);
497 ownIdentity.addContext(context);
499 } catch (PluginException pe1) {
500 logger.log(Level.WARNING, String.format("Could not add Context “%2$s” to Own Identity %1$s!", ownIdentity, context), pe1);
508 * Job that removes a context from an {@link OwnIdentity}.
510 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
512 private class RemoveContextJob extends WebOfTrustContextUpdateJob {
515 * Creates a new remove-context job.
518 * The own identity whose contexts to manage
520 * The context to remove
522 public RemoveContextJob(OwnIdentity ownIdentity, String context) {
523 super(ownIdentity, context);
528 @SuppressWarnings("synthetic-access")
531 webOfTrustConnector.removeContext(ownIdentity, context);
532 ownIdentity.removeContext(context);
534 } catch (PluginException pe1) {
535 logger.log(Level.WARNING, String.format("Could not remove Context “%2$s” to Own Identity %1$s!", ownIdentity, context), pe1);
543 * WebOfTrust update job that sets a property on an {@link OwnIdentity}.
545 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
547 private class SetPropertyJob extends WebOfTrustUpdateJob {
549 /** The own identity to update properties on. */
550 private final OwnIdentity ownIdentity;
552 /** The name of the property to update. */
553 private final String propertyName;
555 /** The value of the property to set. */
556 private final String propertyValue;
559 * Creates a new set-property job.
562 * The own identity to set the property on
563 * @param propertyName
564 * The name of the property to set
565 * @param propertyValue
566 * The value of the property to set
568 public SetPropertyJob(OwnIdentity ownIdentity, String propertyName, String propertyValue) {
569 this.ownIdentity = ownIdentity;
570 this.propertyName = propertyName;
571 this.propertyValue = propertyValue;
576 @SuppressWarnings("synthetic-access")
579 if (propertyValue == null) {
580 webOfTrustConnector.removeProperty(ownIdentity, propertyName);
581 ownIdentity.removeProperty(propertyName);
583 webOfTrustConnector.setProperty(ownIdentity, propertyName, propertyValue);
584 ownIdentity.setProperty(propertyName, propertyValue);
587 } catch (PluginException pe1) {
588 logger.log(Level.WARNING, String.format("Could not set Property “%2$s” to “%3$s” on Own Identity %1$s!", ownIdentity, propertyName, propertyValue), pe1);
599 public boolean equals(Object object) {
600 if ((object == null) || !object.getClass().equals(getClass())) {
603 SetPropertyJob updateJob = (SetPropertyJob) object;
604 return updateJob.ownIdentity.equals(ownIdentity) && updateJob.propertyName.equals(propertyName);
609 public int hashCode() {
610 return getClass().hashCode() ^ ownIdentity.hashCode() ^ propertyName.hashCode();
615 public String toString() {
616 return String.format("%s[ownIdentity=%s,propertyName=%s]", getClass().getSimpleName(), ownIdentity, propertyName);