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;
21 import static java.util.logging.Logger.getLogger;
23 import java.util.concurrent.BlockingQueue;
24 import java.util.concurrent.LinkedBlockingQueue;
25 import java.util.logging.Level;
26 import java.util.logging.Logger;
28 import net.pterodactylus.sone.freenet.plugin.PluginException;
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.service.AbstractService;
36 import com.google.common.annotations.VisibleForTesting;
37 import com.google.inject.Inject;
38 import com.google.inject.Singleton;
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>
47 public class WebOfTrustUpdaterImpl extends AbstractService implements WebOfTrustUpdater {
50 private static final Logger logger = getLogger(WebOfTrustUpdaterImpl.class.getName());
53 @SuppressWarnings("synthetic-access")
54 private final WebOfTrustUpdateJob stopJob = new WebOfTrustUpdateJob();
56 /** The web of trust connector. */
57 private final WebOfTrustConnector webOfTrustConnector;
59 /** The queue for jobs. */
60 private final BlockingQueue<WebOfTrustUpdateJob> updateJobs = new LinkedBlockingQueue<WebOfTrustUpdateJob>();
63 * Creates a new trust updater.
65 * @param webOfTrustConnector
66 * The web of trust connector
69 public WebOfTrustUpdaterImpl(WebOfTrustConnector webOfTrustConnector) {
70 super("Trust Updater");
71 this.webOfTrustConnector = webOfTrustConnector;
79 * Updates the trust relation between the truster and the trustee. This method
80 * will return immediately and perform a trust update in the background.
83 * The identity giving the trust
85 * The identity receiving the trust
87 * The new level of trust (from -100 to 100, may be {@code null} to remove
88 * the trust completely)
90 * The comment of the trust relation
93 public void setTrust(OwnIdentity truster, Identity trustee, Integer score, String comment) {
94 SetTrustJob setTrustJob = new SetTrustJob(truster, trustee, score, comment);
95 if (updateJobs.contains(setTrustJob)) {
96 updateJobs.remove(setTrustJob);
98 logger.log(Level.FINER, "Adding Trust Update Job: " + setTrustJob);
100 updateJobs.put(setTrustJob);
101 } catch (InterruptedException e) {
102 /* the queue is unbounded so it should never block. */
107 * Adds the given context to the given own identity, waiting for completion of
111 * The own identity to add the context to
114 * @return {@code true} if the context was added successfully, {@code false}
118 public boolean addContextWait(OwnIdentity ownIdentity, String context) {
119 AddContextJob addContextJob = new AddContextJob(ownIdentity, context);
120 if (!updateJobs.contains(addContextJob)) {
121 logger.log(Level.FINER, "Adding Context Job: " + addContextJob);
123 updateJobs.put(addContextJob);
124 } catch (InterruptedException ie1) {
125 /* the queue is unbounded so it should never block. */
127 return addContextJob.waitForCompletion();
129 for (WebOfTrustUpdateJob updateJob : updateJobs) {
130 if (updateJob.equals(addContextJob)) {
131 return updateJob.waitForCompletion();
139 * Removes the given context from the given own identity.
142 * The own identity to remove the context from
144 * The context to remove
147 public void removeContext(OwnIdentity ownIdentity, String context) {
148 RemoveContextJob removeContextJob = new RemoveContextJob(ownIdentity, context);
149 if (!updateJobs.contains(removeContextJob)) {
150 logger.log(Level.FINER, "Adding Context Job: " + removeContextJob);
152 updateJobs.put(removeContextJob);
153 } catch (InterruptedException ie1) {
154 /* the queue is unbounded so it should never block. */
160 * Sets a property on the given own identity.
163 * The own identity to set the property on
164 * @param propertyName
165 * The name of the property to set
166 * @param propertyValue
167 * The value of the property to set
170 public void setProperty(OwnIdentity ownIdentity, String propertyName, String propertyValue) {
171 SetPropertyJob setPropertyJob = new SetPropertyJob(ownIdentity, propertyName, propertyValue);
172 if (updateJobs.contains(setPropertyJob)) {
173 updateJobs.remove(setPropertyJob);
175 logger.log(Level.FINER, "Adding Property Job: " + setPropertyJob);
177 updateJobs.put(setPropertyJob);
178 } catch (InterruptedException e) {
179 /* the queue is unbounded so it should never block. */
184 * Removes a property from the given own identity.
187 * The own identity to remove the property from
188 * @param propertyName
189 * The name of the property to remove
192 public void removeProperty(OwnIdentity ownIdentity, String propertyName) {
193 setProperty(ownIdentity, propertyName, null);
202 protected void serviceRun() {
203 while (!shouldStop()) {
205 WebOfTrustUpdateJob updateJob = updateJobs.take();
209 logger.log(Level.FINE, "Running Trust Update Job: " + updateJob);
210 long startTime = System.currentTimeMillis();
212 long endTime = System.currentTimeMillis();
213 logger.log(Level.FINE, "Trust Update Job finished, took " + (endTime - startTime) + " ms.");
214 } catch (InterruptedException ie1) {
215 /* happens, ignore, loop. */
222 protected void serviceStop() {
224 updateJobs.put(stopJob);
225 } catch (InterruptedException ie1) {
226 /* the queue is unbounded so it should never block. */
231 * Base class for WebOfTrust update jobs.
233 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
236 class WebOfTrustUpdateJob implements Runnable {
238 /** Object for synchronization. */
239 @SuppressWarnings("hiding")
240 private final Object syncObject = new Object();
242 /** Whether the job has finished. */
243 private boolean finished;
245 /** Whether the job was successful. */
246 private boolean success;
253 * Performs the actual update operation.
255 * The implementation of this class does nothing.
263 * Waits for completion of this job or stopping of the WebOfTrust updater.
265 * @return {@code true} if this job finished successfully, {@code false}
267 * @see WebOfTrustUpdaterImpl#stop()
269 @SuppressWarnings("synthetic-access")
270 public boolean waitForCompletion() {
271 synchronized (syncObject) {
272 while (!finished && !shouldStop()) {
275 } catch (InterruptedException ie1) {
276 /* we’re looping, ignore. */
288 * Signals that this job has finished.
291 * {@code true} if this job finished successfully, {@code false} otherwise
293 protected void finish(boolean success) {
294 synchronized (syncObject) {
296 this.success = success;
297 syncObject.notifyAll();
304 * Update job that sets the trust relation between two identities.
306 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
309 class SetTrustJob extends WebOfTrustUpdateJob {
311 /** The identity giving the trust. */
312 private final OwnIdentity truster;
314 /** The identity receiving the trust. */
315 private final Identity trustee;
317 /** The score of the relation. */
318 private final Integer score;
320 /** The comment of the relation. */
321 private final String comment;
324 * Creates a new set trust job.
327 * The identity giving the trust
329 * The identity receiving the trust
331 * The score of the trust (from -100 to 100, may be {@code null} to remote
332 * the trust relation completely)
334 * The comment of the trust relation
336 public SetTrustJob(OwnIdentity truster, Identity trustee, Integer score, String comment) {
337 this.truster = checkNotNull(truster, "truster must not be null");
338 this.trustee = checkNotNull(trustee, "trustee must not be null");
340 this.comment = comment;
345 @SuppressWarnings("synthetic-access")
349 webOfTrustConnector.setTrust(truster, trustee, score, comment);
350 trustee.setTrust(truster, new Trust(score, null, 0));
352 webOfTrustConnector.removeTrust(truster, trustee);
353 trustee.removeTrust(truster);
356 } catch (WebOfTrustException wote1) {
357 logger.log(Level.WARNING, "Could not set Trust value for " + truster + " -> " + trustee + " to " + score + " (" + comment + ")!", wote1);
368 public boolean equals(Object object) {
369 if ((object == null) || !object.getClass().equals(getClass())) {
372 SetTrustJob updateJob = (SetTrustJob) object;
373 return updateJob.truster.equals(truster) && updateJob.trustee.equals(trustee);
378 public int hashCode() {
379 return getClass().hashCode() ^ truster.hashCode() ^ trustee.hashCode();
384 public String toString() {
385 return String.format("%s[truster=%s,trustee=%s]", getClass().getSimpleName(), truster.getId(), trustee.getId());
391 * Base class for context updates of an {@link OwnIdentity}.
393 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
396 class WebOfTrustContextUpdateJob extends WebOfTrustUpdateJob {
398 /** The own identity whose contexts to manage. */
399 protected final OwnIdentity ownIdentity;
401 /** The context to update. */
402 protected final String context;
405 * Creates a new context update job.
408 * The own identity to update
410 * The context to update
412 @SuppressWarnings("synthetic-access")
413 public WebOfTrustContextUpdateJob(OwnIdentity ownIdentity, String context) {
414 this.ownIdentity = checkNotNull(ownIdentity, "ownIdentity must not be null");
415 this.context = checkNotNull(context, "context must not be null");
424 public boolean equals(Object object) {
425 if ((object == null) || !object.getClass().equals(getClass())) {
428 WebOfTrustContextUpdateJob updateJob = (WebOfTrustContextUpdateJob) object;
429 return updateJob.ownIdentity.equals(ownIdentity) && updateJob.context.equals(context);
434 public int hashCode() {
435 return getClass().hashCode() ^ ownIdentity.hashCode() ^ context.hashCode();
440 public String toString() {
441 return String.format("%s[ownIdentity=%s,context=%s]", getClass().getSimpleName(), ownIdentity, context);
447 * Job that adds a context to an {@link OwnIdentity}.
449 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
452 class AddContextJob extends WebOfTrustContextUpdateJob {
455 * Creates a new add-context job.
458 * The own identity whose contexts to manage
462 public AddContextJob(OwnIdentity ownIdentity, String context) {
463 super(ownIdentity, context);
468 @SuppressWarnings("synthetic-access")
471 webOfTrustConnector.addContext(ownIdentity, context);
472 ownIdentity.addContext(context);
474 } catch (PluginException pe1) {
475 logger.log(Level.WARNING, String.format("Could not add Context “%2$s” to Own Identity %1$s!", ownIdentity, context), pe1);
483 * Job that removes a context from an {@link OwnIdentity}.
485 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
488 class RemoveContextJob extends WebOfTrustContextUpdateJob {
491 * Creates a new remove-context job.
494 * The own identity whose contexts to manage
496 * The context to remove
498 public RemoveContextJob(OwnIdentity ownIdentity, String context) {
499 super(ownIdentity, context);
504 @SuppressWarnings("synthetic-access")
507 webOfTrustConnector.removeContext(ownIdentity, context);
508 ownIdentity.removeContext(context);
510 } catch (PluginException pe1) {
511 logger.log(Level.WARNING, String.format("Could not remove Context “%2$s” to Own Identity %1$s!", ownIdentity, context), pe1);
519 * WebOfTrust update job that sets a property on an {@link OwnIdentity}.
521 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
524 class SetPropertyJob extends WebOfTrustUpdateJob {
526 /** The own identity to update properties on. */
527 private final OwnIdentity ownIdentity;
529 /** The name of the property to update. */
530 private final String propertyName;
532 /** The value of the property to set. */
533 private final String propertyValue;
536 * Creates a new set-property job.
539 * The own identity to set the property on
540 * @param propertyName
541 * The name of the property to set
542 * @param propertyValue
543 * The value of the property to set
545 public SetPropertyJob(OwnIdentity ownIdentity, String propertyName, String propertyValue) {
546 this.ownIdentity = ownIdentity;
547 this.propertyName = propertyName;
548 this.propertyValue = propertyValue;
553 @SuppressWarnings("synthetic-access")
556 if (propertyValue == null) {
557 webOfTrustConnector.removeProperty(ownIdentity, propertyName);
558 ownIdentity.removeProperty(propertyName);
560 webOfTrustConnector.setProperty(ownIdentity, propertyName, propertyValue);
561 ownIdentity.setProperty(propertyName, propertyValue);
564 } catch (PluginException pe1) {
565 logger.log(Level.WARNING, String.format("Could not set Property “%2$s” to “%3$s” on Own Identity %1$s!", ownIdentity, propertyName, propertyValue), pe1);
576 public boolean equals(Object object) {
577 if ((object == null) || !object.getClass().equals(getClass())) {
580 SetPropertyJob updateJob = (SetPropertyJob) object;
581 return updateJob.ownIdentity.equals(ownIdentity) && updateJob.propertyName.equals(propertyName);
586 public int hashCode() {
587 return getClass().hashCode() ^ ownIdentity.hashCode() ^ propertyName.hashCode();
592 public String toString() {
593 return String.format("%s[ownIdentity=%s,propertyName=%s]", getClass().getSimpleName(), ownIdentity, propertyName);