Merge branch 'release-0.0.3' 0.0.3
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 16 Sep 2011 09:13:47 +0000 (11:13 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 16 Sep 2011 09:13:47 +0000 (11:13 +0200)
pom.xml
src/main/java/net/pterodactylus/wotns/freenet/wot/IdentityManager.java
src/main/java/net/pterodactylus/wotns/freenet/wot/WebOfTrustConnector.java
src/main/java/net/pterodactylus/wotns/main/IdentityComparator.java
src/main/java/net/pterodactylus/wotns/main/WoTNSPlugin.java
src/main/resources/templates/index.html

diff --git a/pom.xml b/pom.xml
index 24d87ae..37e947e 100644 (file)
--- a/pom.xml
+++ b/pom.xml
@@ -2,7 +2,7 @@
        <modelVersion>4.0.0</modelVersion>
        <groupId>net.pterodactylus</groupId>
        <artifactId>WoTNS</artifactId>
-       <version>0.0.2</version>
+       <version>0.0.3</version>
        <dependencies>
                <dependency>
                        <groupId>net.pterodactylus</groupId>
index 1411ab3..ea90dac 100644 (file)
@@ -17,6 +17,7 @@
 
 package net.pterodactylus.wotns.freenet.wot;
 
+import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -26,9 +27,7 @@ import java.util.Map.Entry;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
-import net.pterodactylus.util.collection.SetBuilder;
 import net.pterodactylus.util.logging.Logging;
-import net.pterodactylus.util.object.Default;
 import net.pterodactylus.util.service.AbstractService;
 import net.pterodactylus.wotns.freenet.plugin.PluginException;
 
@@ -67,7 +66,7 @@ public class IdentityManager extends AbstractService {
        private Map<String, OwnIdentity> currentOwnIdentities = new HashMap<String, OwnIdentity>();
 
        /** The currently trusted identities. */
-       private Map<OwnIdentity, Set<Identity>> currentTrustedIdentities = new HashMap<OwnIdentity, Set<Identity>>();
+       private Map<OwnIdentity, Collection<Identity>> currentTrustedIdentities = new HashMap<OwnIdentity, Collection<Identity>>();
 
        /**
         * Creates a new identity manager.
@@ -172,15 +171,31 @@ public class IdentityManager extends AbstractService {
                }
        }
 
+       /**
+        * Returns all identities that are trusted by the given own identity. In
+        * addition to all non-own identities the given own identity is also
+        * returned.
+        *
+        * @param ownIdentity
+        *            The own identity to get the trusted identities for
+        * @return The identities trusted by the given own identity
+        */
        public Set<Identity> getTrustedIdentities(OwnIdentity ownIdentity) {
-               SetBuilder<Identity> identities = new SetBuilder<Identity>();
+               Set<Identity> identities = new HashSet<Identity>();
                if ((context == null) || ownIdentity.getContexts().contains(context)) {
                        identities.add(ownIdentity);
                }
-               synchronized (syncObject) {
-                       identities.addAll(Default.forNull(currentTrustedIdentities.get(ownIdentity), Collections.<Identity> emptySet()));
+               try {
+                       Set<Identity> trustedIdentities = webOfTrustConnector.loadTrustedIdentities(ownIdentity, context);
+                       Map<String, Identity> newTrustedIdentities = new HashMap<String, Identity>();
+                       for (Identity trustedIdentity : trustedIdentities) {
+                               newTrustedIdentities.put(trustedIdentity.getId(), trustedIdentity);
+                       }
+                       checkTrustedIdentities(ownIdentity, newTrustedIdentities);
+               } catch (WebOfTrustException wote1) {
+                       logger.log(Level.WARNING, String.format("Could not load all trusted identities for %s.", ownIdentity), wote1);
                }
-               return identities.get();
+               return identities;
        }
 
        //
@@ -192,7 +207,6 @@ public class IdentityManager extends AbstractService {
         */
        @Override
        protected void serviceRun() {
-               Map<OwnIdentity, Map<String, Identity>> oldIdentities = Collections.emptyMap();
                while (!shouldStop()) {
                        Map<OwnIdentity, Map<String, Identity>> currentIdentities = new HashMap<OwnIdentity, Map<String, Identity>>();
                        @SuppressWarnings("hiding")
@@ -218,7 +232,10 @@ public class IdentityManager extends AbstractService {
                                                identities.put(identity.getId(), identity);
                                        }
 
-                                       /* add own identities, too, as long as the WoT doesn’t do that. */
+                                       /*
+                                        * add own identities, too, as long as the WoT doesn’t do
+                                        * that.
+                                        */
                                        for (OwnIdentity additionalOwnIdentity : ownIdentities) {
                                                if (additionalOwnIdentity == ownIdentity) {
                                                        continue;
@@ -241,75 +258,7 @@ public class IdentityManager extends AbstractService {
 
                                /* now check for changes in remote identities. */
                                for (OwnIdentity ownIdentity : currentOwnIdentities.values()) {
-
-                                       /* find new identities. */
-                                       for (Identity currentIdentity : currentIdentities.get(ownIdentity).values()) {
-                                               if (!oldIdentities.containsKey(ownIdentity) || !oldIdentities.get(ownIdentity).containsKey(currentIdentity.getId())) {
-                                                       identityListenerManager.fireIdentityAdded(ownIdentity, currentIdentity);
-                                               }
-                                       }
-
-                                       /* find removed identities. */
-                                       if (oldIdentities.containsKey(ownIdentity)) {
-                                               for (Identity oldIdentity : oldIdentities.get(ownIdentity).values()) {
-                                                       if (!currentIdentities.get(ownIdentity).containsKey(oldIdentity.getId())) {
-                                                               identityListenerManager.fireIdentityRemoved(ownIdentity, oldIdentity);
-                                                       }
-                                               }
-
-                                               /* check for changes in the contexts. */
-                                               for (Identity oldIdentity : oldIdentities.get(ownIdentity).values()) {
-                                                       if (!currentIdentities.get(ownIdentity).containsKey(oldIdentity.getId())) {
-                                                               continue;
-                                                       }
-                                                       Identity newIdentity = currentIdentities.get(ownIdentity).get(oldIdentity.getId());
-                                                       Set<String> oldContexts = oldIdentity.getContexts();
-                                                       Set<String> newContexts = newIdentity.getContexts();
-                                                       if (oldContexts.size() != newContexts.size()) {
-                                                               identityListenerManager.fireIdentityUpdated(ownIdentity, newIdentity);
-                                                               continue;
-                                                       }
-                                                       for (String oldContext : oldContexts) {
-                                                               if (!newContexts.contains(oldContext)) {
-                                                                       identityListenerManager.fireIdentityUpdated(ownIdentity, newIdentity);
-                                                                       break;
-                                                               }
-                                                       }
-                                               }
-
-                                               /* check for changes in the properties. */
-                                               for (Identity oldIdentity : oldIdentities.get(ownIdentity).values()) {
-                                                       if (!currentIdentities.get(ownIdentity).containsKey(oldIdentity.getId())) {
-                                                               continue;
-                                                       }
-                                                       Identity newIdentity = currentIdentities.get(ownIdentity).get(oldIdentity.getId());
-                                                       Map<String, String> oldProperties = oldIdentity.getProperties();
-                                                       Map<String, String> newProperties = newIdentity.getProperties();
-                                                       if (oldProperties.size() != newProperties.size()) {
-                                                               identityListenerManager.fireIdentityUpdated(ownIdentity, newIdentity);
-                                                               continue;
-                                                       }
-                                                       for (Entry<String, String> oldProperty : oldProperties.entrySet()) {
-                                                               if (!newProperties.containsKey(oldProperty.getKey()) || !newProperties.get(oldProperty.getKey()).equals(oldProperty.getValue())) {
-                                                                       identityListenerManager.fireIdentityUpdated(ownIdentity, newIdentity);
-                                                                       break;
-                                                               }
-                                                       }
-                                               }
-                                       }
-                               }
-
-                               /* remember the current set of identities. */
-                               oldIdentities = currentIdentities;
-                               synchronized (syncObject) {
-                                       currentTrustedIdentities.clear();
-                                       for (Entry<OwnIdentity, Map<String, Identity>> entry : currentIdentities.entrySet()) {
-                                               Set<Identity> identities = new HashSet<Identity>();
-                                               currentTrustedIdentities.put(entry.getKey(), identities);
-                                               for (Identity identity : entry.getValue().values()) {
-                                                       identities.add(identity);
-                                               }
-                                       }
+                                       checkTrustedIdentities(ownIdentity, currentIdentities.get(ownIdentity));
                                }
                        }
 
@@ -336,6 +285,7 @@ public class IdentityManager extends AbstractService {
                        for (OwnIdentity oldOwnIdentity : currentOwnIdentities.values()) {
                                if (!newOwnIdentities.containsKey(oldOwnIdentity.getId())) {
                                        identityListenerManager.fireOwnIdentityRemoved(oldOwnIdentity);
+                                       currentTrustedIdentities.remove(oldOwnIdentity);
                                }
                        }
 
@@ -351,4 +301,81 @@ public class IdentityManager extends AbstractService {
                }
        }
 
+       /**
+        * Checks the given identities for changes since the last check.
+        *
+        * @param ownIdentity
+        *            The own identity trusting the given identities
+        * @param trustedIdentities
+        *            The trusted identities
+        */
+       private void checkTrustedIdentities(OwnIdentity ownIdentity, Map<String, Identity> trustedIdentities) {
+
+               @SuppressWarnings("hiding")
+               Map<String, Identity> currentTrustedIdentities = new HashMap<String, Identity>();
+               synchronized (syncObject) {
+                       if (this.currentTrustedIdentities.containsKey(ownIdentity)) {
+                               for (Identity identity : this.currentTrustedIdentities.get(ownIdentity)) {
+                                       currentTrustedIdentities.put(identity.getId(), identity);
+                               }
+                       }
+               }
+
+               /* find new identities. */
+               for (Identity currentIdentity : trustedIdentities.values()) {
+                       if (!currentTrustedIdentities.containsKey(currentIdentity.getId())) {
+                               identityListenerManager.fireIdentityAdded(ownIdentity, currentIdentity);
+                       }
+               }
+
+               /* find removed identities. */
+               for (Identity oldIdentity : currentTrustedIdentities.values()) {
+                       if (!trustedIdentities.containsKey(oldIdentity.getId())) {
+                               identityListenerManager.fireIdentityRemoved(ownIdentity, oldIdentity);
+                       }
+               }
+
+               /* check for changes in the contexts. */
+               for (Identity oldIdentity : currentTrustedIdentities.values()) {
+                       if (!trustedIdentities.containsKey(oldIdentity.getId())) {
+                               continue;
+                       }
+                       Identity newIdentity = trustedIdentities.get(oldIdentity.getId());
+                       Set<String> oldContexts = oldIdentity.getContexts();
+                       Set<String> newContexts = newIdentity.getContexts();
+                       if (oldContexts.size() != newContexts.size()) {
+                               identityListenerManager.fireIdentityUpdated(ownIdentity, newIdentity);
+                               continue;
+                       }
+                       boolean changed = false;
+                       for (String oldContext : oldContexts) {
+                               if (!newContexts.contains(oldContext)) {
+                                       identityListenerManager.fireIdentityUpdated(ownIdentity, newIdentity);
+                                       changed = true;
+                                       break;
+                               }
+                       }
+                       if (changed) {
+                               continue;
+                       }
+                       Map<String, String> oldProperties = oldIdentity.getProperties();
+                       Map<String, String> newProperties = newIdentity.getProperties();
+                       if (oldProperties.size() != newProperties.size()) {
+                               identityListenerManager.fireIdentityUpdated(ownIdentity, newIdentity);
+                               continue;
+                       }
+                       for (Entry<String, String> oldProperty : oldProperties.entrySet()) {
+                               if (!newProperties.containsKey(oldProperty.getKey()) || !newProperties.get(oldProperty.getKey()).equals(oldProperty.getValue())) {
+                                       identityListenerManager.fireIdentityUpdated(ownIdentity, newIdentity);
+                                       break;
+                               }
+                       }
+               }
+
+               synchronized (syncObject) {
+                       this.currentTrustedIdentities.put(ownIdentity, trustedIdentities.values());
+               }
+
+       }
+
 }
index 8a16efc..f56f739 100644 (file)
@@ -70,6 +70,10 @@ public class WebOfTrustConnector implements ConnectorListener {
        // ACTIONS
        //
 
+       /**
+        * Stops the web of trust connector and disconnects from the plugin
+        * connector.
+        */
        public void stop() {
                pluginConnector.removeConnectorListener(WOT_PLUGIN_NAME, PLUGIN_CONNECTION_IDENTIFIER, this);
        }
@@ -110,10 +114,10 @@ public class WebOfTrustConnector implements ConnectorListener {
         * @param ownIdentity
         *            The own identity
         * @return All trusted identities
-        * @throws PluginException
+        * @throws WebOfTrustException
         *             if an error occured talking to the Web of Trust plugin
         */
-       public Set<Identity> loadTrustedIdentities(OwnIdentity ownIdentity) throws PluginException {
+       public Set<Identity> loadTrustedIdentities(OwnIdentity ownIdentity) throws WebOfTrustException {
                return loadTrustedIdentities(ownIdentity, null);
        }
 
@@ -126,10 +130,10 @@ public class WebOfTrustConnector implements ConnectorListener {
         * @param context
         *            The context to filter, or {@code null}
         * @return All trusted identities
-        * @throws PluginException
+        * @throws WebOfTrustException
         *             if an error occured talking to the Web of Trust plugin
         */
-       public Set<Identity> loadTrustedIdentities(OwnIdentity ownIdentity, String context) throws PluginException {
+       public Set<Identity> loadTrustedIdentities(OwnIdentity ownIdentity, String context) throws WebOfTrustException {
                @SuppressWarnings("hiding")
                Reply reply = performRequest(SimpleFieldSetConstructor.create().put("Message", "GetIdentitiesByScore").put("TreeOwner", ownIdentity.getId()).put("Selection", "+").put("Context", (context == null) ? "" : context).get());
                SimpleFieldSet fields = reply.getFields();
index ca540a7..c903a23 100644 (file)
@@ -37,7 +37,7 @@ public class IdentityComparator {
                 */
                @Override
                public int compare(Identity leftIdentity, Identity rightIdentity) {
-                       return leftIdentity.getNickname().compareTo(rightIdentity.getNickname());
+                       return leftIdentity.getNickname().compareToIgnoreCase(rightIdentity.getNickname());
                }
 
        }
index f08239e..f24afc7 100644 (file)
@@ -46,7 +46,7 @@ public class WoTNSPlugin implements FredPlugin, FredPluginL10n, FredPluginBaseL1
                Logging.setupConsoleLogging();
        }
 
-       private static final Version VERSION = new Version(0, 0, 2);
+       private static final Version VERSION = new Version(0, 0, 3);
 
        private PluginRespirator pluginRespirator;
 
index 6ed3fee..94b0623 100644 (file)
@@ -1,6 +1,6 @@
 <%include include/head.html>
 <h1>WoTNS</h1>
-<p>The Web of Trust Service uses properties of Web of Trust identities to implement a DNS-like scheme on top of Freenet.</p>
+<p>The Web of Trust Name Service uses properties of Web of Trust identities to implement a DNS-like scheme on top of Freenet.</p>
 <p>The general syntax is: <code>http://<%request.host>/tns/<i>identity</i>/<i>target</i></code>.</p>
 <p><i>identity</i> is the nickname of a Web of Trust identity, such as “WoTNS”. It is also possible to include the beginning of the identity’s
 key into <i>identity</i>, in the form of <i>nickname@start-of-key</i>, such as “WoTNS@DAx”.</p>
@@ -9,7 +9,7 @@ has to fulfill the Web of Trust’s requirement for a valid property name.</p>
 <p>To be able to add targets for one of your identities, you first have to create an identity using the <a href="/WebOfTrust/">Web of Trust</a>,
 and you have to enable the identity here.</p>
 
-<%foreach enabledIdentities identity>
+<%foreach enabledIdentities identity|sort>
        <%first>
                <h1>Enabled Identities</h1>
        <%/first>
@@ -21,7 +21,7 @@ and you have to enable the identity here.</p>
        </form>
 <%/foreach>
 
-<%foreach disabledIdentities identity>
+<%foreach disabledIdentities identity|sort>
        <%first>
                <h1>Disabled Identities</h1>
        <%/first>