Merge remote-tracking branch 'beak/next' into next
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 12 Sep 2012 07:14:08 +0000 (09:14 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 12 Sep 2012 07:14:08 +0000 (09:14 +0200)
src/main/java/net/pterodactylus/sone/core/Core.java
src/main/java/net/pterodactylus/sone/core/WebOfTrustUpdater.java
src/main/java/net/pterodactylus/sone/freenet/wot/IdentityManager.java
src/main/java/net/pterodactylus/sone/freenet/wot/WebOfTrustConnector.java
src/main/java/net/pterodactylus/sone/template/ProfileAccessor.java
src/main/java/net/pterodactylus/sone/template/SoneAccessor.java

index e3fdd19..9e1e6c6 100644 (file)
@@ -53,7 +53,6 @@ import net.pterodactylus.sone.freenet.wot.Identity;
 import net.pterodactylus.sone.freenet.wot.IdentityListener;
 import net.pterodactylus.sone.freenet.wot.IdentityManager;
 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
-import net.pterodactylus.sone.freenet.wot.Trust;
 import net.pterodactylus.sone.main.SonePlugin;
 import net.pterodactylus.util.config.Configuration;
 import net.pterodactylus.util.config.ConfigurationException;
@@ -1042,26 +1041,6 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
        }
 
        /**
-        * Retrieves the trust relationship from the origin to the target. If the
-        * trust relationship can not be retrieved, {@code null} is returned.
-        *
-        * @see Identity#getTrust(OwnIdentity)
-        * @param origin
-        *            The origin of the trust tree
-        * @param target
-        *            The target of the trust
-        * @return The trust relationship
-        */
-       public Trust getTrust(Sone origin, Sone target) {
-               if (!isLocalSone(origin)) {
-                       logger.log(Level.WARNING, String.format("Tried to get trust from remote Sone: %s", origin));
-                       return null;
-               }
-               webOfTrustUpdater.getTrust((OwnIdentity) origin.getIdentity(), target.getIdentity());
-               return target.getIdentity().getTrust((OwnIdentity) origin.getIdentity());
-       }
-
-       /**
         * Sets the trust value of the given origin Sone for the target Sone.
         *
         * @param origin
@@ -1987,11 +1966,13 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
        @Override
        public void serviceStop() {
                synchronized (localSones) {
-                       for (SoneInserter soneInserter : soneInserters.values()) {
-                               soneInserter.removeSoneInsertListener(this);
-                               soneInserter.stop();
+                       for (Entry<Sone, SoneInserter> soneInserter : soneInserters.entrySet()) {
+                               soneInserter.getValue().removeSoneInsertListener(this);
+                               soneInserter.getValue().stop();
+                               saveSone(soneInserter.getKey());
                        }
                }
+               saveConfiguration();
                webOfTrustUpdater.stop();
                updateChecker.stop();
                updateChecker.removeUpdateListener(this);
@@ -2369,7 +2350,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
         *            The URI to derive the Sone URI from
         * @return The derived URI
         */
-       private FreenetURI getSoneUri(String uriString) {
+       private static FreenetURI getSoneUri(String uriString) {
                try {
                        FreenetURI uri = new FreenetURI(uriString).setDocName("Sone").setMetaString(new String[0]);
                        return uri;
index cb73297..bdfc1ae 100644 (file)
@@ -70,28 +70,6 @@ public class WebOfTrustUpdater extends AbstractService {
        //
 
        /**
-        * Retrieves the trust relation between the truster and the trustee. This
-        * method will return immediately and perform a trust update in the
-        * background.
-        *
-        * @param truster
-        *            The identity giving the trust
-        * @param trustee
-        *            The identity receiving the trust
-        */
-       public void getTrust(OwnIdentity truster, Identity trustee) {
-               GetTrustJob getTrustJob = new GetTrustJob(truster, trustee);
-               if (!updateJobs.contains(getTrustJob)) {
-                       logger.log(Level.FINER, "Adding Trust Update Job: " + getTrustJob);
-                       try {
-                               updateJobs.put(getTrustJob);
-                       } catch (InterruptedException ie1) {
-                               /* the queue is unbounded so it should never block. */
-                       }
-               }
-       }
-
-       /**
         * Updates the trust relation between the truster and the trustee. This
         * method will return immediately and perform a trust update in the
         * background.
@@ -471,45 +449,6 @@ public class WebOfTrustUpdater extends AbstractService {
        }
 
        /**
-        * Update job that retrieves the trust relation between two identities.
-        *
-        * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
-        */
-       private class GetTrustJob extends WebOfTrustTrustUpdateJob {
-
-               /**
-                * Creates a new trust update job.
-                *
-                * @param truster
-                *            The identity giving the trust
-                * @param trustee
-                *            The identity receiving the trust
-                */
-               public GetTrustJob(OwnIdentity truster, Identity trustee) {
-                       super(truster, trustee);
-               }
-
-               /**
-                * {@inheritDoc}
-                */
-               @Override
-               @SuppressWarnings("synthetic-access")
-               public void run() {
-                       try {
-                               Trust trust = webOfTrustConnector.getTrust(truster, trustee);
-                               if (trustee instanceof DefaultIdentity) {
-                                       ((DefaultIdentity) trustee).setTrust(truster, trust);
-                               }
-                               finish(true);
-                       } catch (PluginException pe1) {
-                               logger.log(Level.WARNING, "Could not get Trust value for " + truster + " -> " + trustee + "!", pe1);
-                               finish(false);
-                       }
-               }
-
-       }
-
-       /**
         * Base class for context updates of an {@link OwnIdentity}.
         *
         * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
@@ -684,8 +623,8 @@ public class WebOfTrustUpdater extends AbstractService {
                        if ((object == null) || !object.getClass().equals(getClass())) {
                                return false;
                        }
-                       WebOfTrustContextUpdateJob updateJob = (WebOfTrustContextUpdateJob) object;
-                       return updateJob.ownIdentity.equals(ownIdentity) && updateJob.context.equals(propertyName);
+                       WebOfTrustPropertyUpdateJob updateJob = (WebOfTrustPropertyUpdateJob) object;
+                       return updateJob.ownIdentity.equals(ownIdentity) && updateJob.propertyName.equals(propertyName);
                }
 
                /**
index 8e0b124..3c3c9f9 100644 (file)
@@ -19,6 +19,7 @@ package net.pterodactylus.sone.freenet.wot;
 
 import java.util.Collections;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Set;
@@ -26,8 +27,6 @@ import java.util.logging.Level;
 import java.util.logging.Logger;
 
 import net.pterodactylus.sone.freenet.plugin.PluginException;
-import net.pterodactylus.util.collection.mapper.Mapper;
-import net.pterodactylus.util.collection.mapper.Mappers;
 import net.pterodactylus.util.logging.Logging;
 import net.pterodactylus.util.service.AbstractService;
 
@@ -155,27 +154,7 @@ public class IdentityManager extends AbstractService {
         * @return All own identities
         */
        public Set<OwnIdentity> getAllOwnIdentities() {
-               try {
-                       Set<OwnIdentity> ownIdentities = webOfTrustConnector.loadAllOwnIdentities();
-                       Map<String, OwnIdentity> newOwnIdentities = new HashMap<String, OwnIdentity>();
-                       for (OwnIdentity ownIdentity : ownIdentities) {
-                               newOwnIdentities.put(ownIdentity.getId(), ownIdentity);
-                       }
-                       checkOwnIdentities(newOwnIdentities);
-                       return Mappers.mappedSet(ownIdentities, new Mapper<OwnIdentity, OwnIdentity>() {
-
-                               /**
-                                * {@inheritDoc}
-                                */
-                               @Override
-                               public OwnIdentity map(OwnIdentity input) {
-                                       return new DefaultOwnIdentity(input);
-                               }
-                       });
-               } catch (WebOfTrustException wote1) {
-                       logger.log(Level.WARNING, "Could not load all own identities!", wote1);
-                       return Collections.emptySet();
-               }
+               return new HashSet<OwnIdentity>(currentOwnIdentities.values());
        }
 
        //
index 435f078..531275d 100644 (file)
@@ -21,6 +21,7 @@ import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
+import java.util.concurrent.atomic.AtomicLong;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
@@ -28,6 +29,7 @@ import net.pterodactylus.sone.freenet.plugin.ConnectorListener;
 import net.pterodactylus.sone.freenet.plugin.PluginConnector;
 import net.pterodactylus.sone.freenet.plugin.PluginException;
 import net.pterodactylus.util.logging.Logging;
+import net.pterodactylus.util.number.Numbers;
 import freenet.support.SimpleFieldSet;
 import freenet.support.api.Bucket;
 
@@ -36,7 +38,7 @@ import freenet.support.api.Bucket;
  *
  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
  */
-public class WebOfTrustConnector implements ConnectorListener {
+public class WebOfTrustConnector {
 
        /** The logger. */
        private static final Logger logger = Logging.getLogger(WebOfTrustConnector.class);
@@ -44,11 +46,8 @@ public class WebOfTrustConnector implements ConnectorListener {
        /** The name of the WoT plugin. */
        private static final String WOT_PLUGIN_NAME = "plugins.WebOfTrust.WebOfTrust";
 
-       /** A random connection identifier. */
-       private static final String PLUGIN_CONNECTION_IDENTIFIER = "Sone-WoT-Connector-" + Math.abs(Math.random());
-
-       /** The current reply. */
-       private Reply reply;
+       /** Counter for connection identifiers. */
+       private final AtomicLong counter = new AtomicLong();
 
        /** The plugin connector. */
        private final PluginConnector pluginConnector;
@@ -62,7 +61,6 @@ public class WebOfTrustConnector implements ConnectorListener {
         */
        public WebOfTrustConnector(PluginConnector pluginConnector) {
                this.pluginConnector = pluginConnector;
-               pluginConnector.addConnectorListener(WOT_PLUGIN_NAME, PLUGIN_CONNECTION_IDENTIFIER, this);
        }
 
        //
@@ -73,10 +71,7 @@ public class WebOfTrustConnector implements ConnectorListener {
         * Stops the web of trust connector.
         */
        public void stop() {
-               pluginConnector.removeConnectorListener(WOT_PLUGIN_NAME, PLUGIN_CONNECTION_IDENTIFIER, this);
-               synchronized (reply) {
-                       reply.notifyAll();
-               }
+               /* does nothing. */
        }
 
        /**
@@ -134,7 +129,7 @@ public class WebOfTrustConnector implements ConnectorListener {
         *             if an error occured talking to the Web of Trust plugin
         */
        public Set<Identity> loadTrustedIdentities(OwnIdentity ownIdentity, String context) throws PluginException {
-               Reply reply = performRequest(SimpleFieldSetConstructor.create().put("Message", "GetIdentitiesByScore").put("Truster", ownIdentity.getId()).put("Selection", "+").put("Context", (context == null) ? "" : context).get());
+               Reply reply = performRequest(SimpleFieldSetConstructor.create().put("Message", "GetIdentitiesByScore").put("Truster", ownIdentity.getId()).put("Selection", "+").put("Context", (context == null) ? "" : context).put("WantTrustValues", "true").get());
                SimpleFieldSet fields = reply.getFields();
                Set<Identity> identities = new HashSet<Identity>();
                int identityCounter = -1;
@@ -148,6 +143,10 @@ public class WebOfTrustConnector implements ConnectorListener {
                        DefaultIdentity identity = new DefaultIdentity(id, nickname, requestUri);
                        identity.setContexts(parseContexts("Contexts" + identityCounter + ".", fields));
                        identity.setProperties(parseProperties("Properties" + identityCounter + ".", fields));
+                       Integer trust = Numbers.safeParseInteger(fields.get("Trust" + identityCounter), null);
+                       int score = Numbers.safeParseInteger(fields.get("Score" + identityCounter));
+                       int rank = Numbers.safeParseInteger(fields.get("Rank" + identityCounter));
+                       identity.setTrust(ownIdentity, new Trust(trust, score, rank));
                        identities.add(identity);
                }
                return identities;
@@ -318,7 +317,7 @@ public class WebOfTrustConnector implements ConnectorListener {
         *            The fields to parse the contexts from
         * @return The parsed contexts
         */
-       private Set<String> parseContexts(String prefix, SimpleFieldSet fields) {
+       private static Set<String> parseContexts(String prefix, SimpleFieldSet fields) {
                Set<String> contexts = new HashSet<String>();
                int contextCounter = -1;
                while (true) {
@@ -340,7 +339,7 @@ public class WebOfTrustConnector implements ConnectorListener {
         *            The fields to parse the properties from
         * @return The parsed properties
         */
-       private Map<String, String> parseProperties(String prefix, SimpleFieldSet fields) {
+       private static Map<String, String> parseProperties(String prefix, SimpleFieldSet fields) {
                Map<String, String> properties = new HashMap<String, String>();
                int propertiesCounter = -1;
                while (true) {
@@ -380,17 +379,34 @@ public class WebOfTrustConnector implements ConnectorListener {
         * @throws PluginException
         *             if the request could not be sent
         */
-       private synchronized Reply performRequest(SimpleFieldSet fields, Bucket data) throws PluginException {
-               reply = new Reply();
+       private Reply performRequest(SimpleFieldSet fields, Bucket data) throws PluginException {
+               final String identifier = "FCP-Command-" + System.currentTimeMillis() + "-" + counter.getAndIncrement();
+               final Reply reply = new Reply();
                logger.log(Level.FINE, String.format("Sending FCP Request: %s", fields.get("Message")));
+               ConnectorListener connectorListener = new ConnectorListener() {
+
+                       @Override
+                       @SuppressWarnings("synthetic-access")
+                       public void receivedReply(PluginConnector pluginConnector, SimpleFieldSet fields, Bucket data) {
+                               String messageName = fields.get("Message");
+                               logger.log(Level.FINEST, String.format("Received Reply from Plugin: %s", messageName));
+                               synchronized (reply) {
+                                       reply.setFields(fields);
+                                       reply.setData(data);
+                                       reply.notify();
+                               }
+                       }
+               };
+               pluginConnector.addConnectorListener(WOT_PLUGIN_NAME, identifier, connectorListener);
                synchronized (reply) {
-                       pluginConnector.sendRequest(WOT_PLUGIN_NAME, PLUGIN_CONNECTION_IDENTIFIER, fields, data);
+                       pluginConnector.sendRequest(WOT_PLUGIN_NAME, identifier, fields, data);
                        try {
                                reply.wait();
                        } catch (InterruptedException ie1) {
                                logger.log(Level.WARNING, String.format("Got interrupted while waiting for reply on %s.", fields.get("Message")), ie1);
                        }
                }
+               pluginConnector.removeConnectorListener(WOT_PLUGIN_NAME, identifier, connectorListener);
                logger.log(Level.FINEST, String.format("Received FCP Response for %s: %s", fields.get("Message"), (reply.getFields() != null) ? reply.getFields().get("Message") : null));
                if ((reply.getFields() == null) || "Error".equals(reply.getFields().get("Message"))) {
                        throw new PluginException("Could not perform request for " + fields.get("Message"));
@@ -398,24 +414,6 @@ public class WebOfTrustConnector implements ConnectorListener {
                return reply;
        }
 
-       //
-       // INTERFACE ConnectorListener
-       //
-
-       /**
-        * {@inheritDoc}
-        */
-       @Override
-       public void receivedReply(PluginConnector pluginConnector, SimpleFieldSet fields, Bucket data) {
-               String messageName = fields.get("Message");
-               logger.log(Level.FINEST, String.format("Received Reply from Plugin: %s", messageName));
-               synchronized (reply) {
-                       reply.setFields(fields);
-                       reply.setData(data);
-                       reply.notify();
-               }
-       }
-
        /**
         * Container for the data of the reply from a plugin.
         *
index 1f29b65..f004967 100644 (file)
@@ -21,6 +21,7 @@ import net.pterodactylus.sone.core.Core;
 import net.pterodactylus.sone.data.Profile;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.data.Sone.ShowCustomAvatars;
+import net.pterodactylus.sone.freenet.wot.OwnIdentity;
 import net.pterodactylus.sone.freenet.wot.Trust;
 import net.pterodactylus.util.template.Accessor;
 import net.pterodactylus.util.template.ReflectionAccessor;
@@ -83,7 +84,7 @@ public class ProfileAccessor extends ReflectionAccessor {
                        if (showCustomAvatars == ShowCustomAvatars.FOLLOWED) {
                                return currentSone.hasFriend(remoteSone.getId()) ? avatarId : null;
                        }
-                       Trust trust = core.getTrust(currentSone, remoteSone);
+                       Trust trust = remoteSone.getIdentity().getTrust((OwnIdentity) currentSone.getIdentity());
                        if (trust == null) {
                                return null;
                        }
index eac8a93..eca8654 100644 (file)
@@ -24,6 +24,7 @@ import net.pterodactylus.sone.core.Core;
 import net.pterodactylus.sone.data.Profile;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.data.Sone.SoneStatus;
+import net.pterodactylus.sone.freenet.wot.OwnIdentity;
 import net.pterodactylus.sone.freenet.wot.Trust;
 import net.pterodactylus.sone.web.WebInterface;
 import net.pterodactylus.sone.web.ajax.GetTimesAjaxPage;
@@ -106,7 +107,7 @@ public class SoneAccessor extends ReflectionAccessor {
                        if (currentSone == null) {
                                return null;
                        }
-                       Trust trust = core.getTrust(currentSone, sone);
+                       Trust trust = sone.getIdentity().getTrust((OwnIdentity) currentSone.getIdentity());
                        logger.log(Level.FINEST, String.format("Trust for %s by %s: %s", sone, currentSone, trust));
                        if (trust == null) {
                                return new Trust(null, null, null);