Extract Identity interfaces, move functionality to identity classes.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 13 Dec 2010 20:47:13 +0000 (21:47 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 13 Dec 2010 20:47:13 +0000 (21:47 +0100)
This commit also makes all non-local identity events depend on an own
identity so that a real view on a web of trust is achieved.

src/main/java/net/pterodactylus/sone/freenet/wot/DefaultIdentity.java [new file with mode: 0644]
src/main/java/net/pterodactylus/sone/freenet/wot/DefaultOwnIdentity.java [new file with mode: 0644]
src/main/java/net/pterodactylus/sone/freenet/wot/Identity.java
src/main/java/net/pterodactylus/sone/freenet/wot/IdentityListener.java
src/main/java/net/pterodactylus/sone/freenet/wot/IdentityListenerManager.java
src/main/java/net/pterodactylus/sone/freenet/wot/IdentityManager.java
src/main/java/net/pterodactylus/sone/freenet/wot/OwnIdentity.java
src/main/java/net/pterodactylus/sone/freenet/wot/WebOfTrustConnector.java

diff --git a/src/main/java/net/pterodactylus/sone/freenet/wot/DefaultIdentity.java b/src/main/java/net/pterodactylus/sone/freenet/wot/DefaultIdentity.java
new file mode 100644 (file)
index 0000000..288a99e
--- /dev/null
@@ -0,0 +1,242 @@
+/*
+ * Sone - DefaultIdentity.java - Copyright © 2010 David Roden
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+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.Set;
+
+/**
+ * A Web of Trust identity.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class DefaultIdentity implements Identity {
+
+       /** The ID of the identity. */
+       private final String id;
+
+       /** The nickname of the identity. */
+       private final String nickname;
+
+       /** The request URI of the identity. */
+       private final String requestUri;
+
+       /** The contexts of the identity. */
+       private final Set<String> contexts = Collections.synchronizedSet(new HashSet<String>());
+
+       /** The properties of the identity. */
+       private final Map<String, String> properties = Collections.synchronizedMap(new HashMap<String, String>());
+
+       /**
+        * Creates a new identity.
+        *
+        * @param id
+        *            The ID of the identity
+        * @param nickname
+        *            The nickname of the identity
+        * @param requestUri
+        *            The request URI of the identity
+        */
+       public DefaultIdentity(String id, String nickname, String requestUri) {
+               this.id = id;
+               this.nickname = nickname;
+               this.requestUri = requestUri;
+       }
+
+       //
+       // ACCESSORS
+       //
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public String getId() {
+               return id;
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public String getNickname() {
+               return nickname;
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public String getRequestUri() {
+               return requestUri;
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public Set<String> getContexts() {
+               return Collections.unmodifiableSet(contexts);
+       }
+
+       /**
+        * Sets the contexts of this identity.
+        * <p>
+        * This method is only called by the {@link IdentityManager}.
+        *
+        * @param contexts
+        *            The contexts to set
+        */
+       void setContextsPrivate(Set<String> contexts) {
+               this.contexts.clear();
+               this.contexts.addAll(contexts);
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public boolean hasContext(String context) {
+               return contexts.contains(context);
+       }
+
+       /**
+        * Adds the given context to this identity.
+        * <p>
+        * This method is only called by the {@link IdentityManager}.
+        *
+        * @param context
+        *            The context to add
+        */
+       void addContextPrivate(String context) {
+               contexts.add(context);
+       }
+
+       /**
+        * Removes the given context from this identity.
+        * <p>
+        * This method is only called by the {@link IdentityManager}.
+        *
+        * @param context
+        *            The context to remove
+        */
+       public void removeContextPrivate(String context) {
+               contexts.remove(context);
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public Map<String, String> getProperties() {
+               synchronized (properties) {
+                       return Collections.unmodifiableMap(properties);
+               }
+       }
+
+       /**
+        * Sets all properties of this identity.
+        * <p>
+        * This method is only called by the {@link IdentityManager}.
+        *
+        * @param properties
+        *            The new properties of this identity
+        */
+       void setPropertiesPrivate(Map<String, String> properties) {
+               synchronized (this.properties) {
+                       this.properties.clear();
+                       this.properties.putAll(properties);
+               }
+       }
+
+       /**
+        * Sets the property with the given name to the given value.
+        * <p>
+        * This method is only called by the {@link IdentityManager}.
+        *
+        * @param name
+        *            The name of the property
+        * @param value
+        *            The value of the property
+        */
+       void setPropertyPrivate(String name, String value) {
+               synchronized (properties) {
+                       properties.put(name, value);
+               }
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public String getProperty(String name) {
+               synchronized (properties) {
+                       return properties.get(name);
+               }
+       }
+
+       /**
+        * Removes the property with the given name.
+        * <p>
+        * This method is only called by the {@link IdentityManager}.
+        *
+        * @param name
+        *            The name of the property to remove
+        */
+       void removePropertyPrivate(String name) {
+               synchronized (properties) {
+                       properties.remove(name);
+               }
+       }
+
+       //
+       // OBJECT METHODS
+       //
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public int hashCode() {
+               return id.hashCode();
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public boolean equals(Object object) {
+               if (!(object instanceof DefaultIdentity)) {
+                       return false;
+               }
+               DefaultIdentity identity = (DefaultIdentity) object;
+               return identity.id.equals(id);
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public String toString() {
+               return getClass().getSimpleName() + "[id=" + id + ",nickname=" + nickname + ",contexts=" + contexts + ",properties=" + properties + "]";
+       }
+
+}
diff --git a/src/main/java/net/pterodactylus/sone/freenet/wot/DefaultOwnIdentity.java b/src/main/java/net/pterodactylus/sone/freenet/wot/DefaultOwnIdentity.java
new file mode 100644 (file)
index 0000000..01ddc12
--- /dev/null
@@ -0,0 +1,138 @@
+/*
+ * Sone - DefaultOwnIdentity.java - Copyright © 2010 David Roden
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.sone.freenet.wot;
+
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+
+/**
+ * An own identity is an identity that the owner of the node has full control
+ * over.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class DefaultOwnIdentity extends DefaultIdentity implements OwnIdentity {
+
+       /** The identity manager. */
+       private final WebOfTrustConnector webOfTrustConnector;
+
+       /** The insert URI of the identity. */
+       private final String insertUri;
+
+       /**
+        * Creates a new own identity.
+        *
+        * @param webOfTrustConnector
+        *            The identity manager
+        * @param id
+        *            The ID of the identity
+        * @param nickname
+        *            The nickname of the identity
+        * @param requestUri
+        *            The request URI of the identity
+        * @param insertUri
+        *            The insert URI of the identity
+        */
+       public DefaultOwnIdentity(WebOfTrustConnector webOfTrustConnector, String id, String nickname, String requestUri, String insertUri) {
+               super(id, nickname, requestUri);
+               this.webOfTrustConnector = webOfTrustConnector;
+               this.insertUri = insertUri;
+       }
+
+       //
+       // ACCESSORS
+       //
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public String getInsertUri() {
+               return insertUri;
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public void addContext(String context) throws WebOfTrustException {
+               webOfTrustConnector.addContext(this, context);
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public void removeContext(String context) throws WebOfTrustException {
+               webOfTrustConnector.removeContext(this, context);
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public void setContexts(Set<String> contexts) throws WebOfTrustException {
+               for (String context : getContexts()) {
+                       if (!contexts.contains(context)) {
+                               webOfTrustConnector.removeContext(this, context);
+                       }
+               }
+               for (String context : contexts) {
+                       if (!getContexts().contains(context)) {
+                               webOfTrustConnector.addContext(this, context);
+                       }
+               }
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public void setProperty(String name, String value) throws WebOfTrustException {
+               webOfTrustConnector.setProperty(this, name, value);
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public void removeProperty(String name) throws WebOfTrustException {
+               webOfTrustConnector.removeProperty(this, name);
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public void setProperties(Map<String, String> properties) throws WebOfTrustException {
+               for (Entry<String, String> oldProperty : getProperties().entrySet()) {
+                       if (!properties.containsKey(oldProperty.getKey())) {
+                               webOfTrustConnector.removeProperty(this, oldProperty.getKey());
+                       } else {
+                               webOfTrustConnector.setProperty(this, oldProperty.getKey(), properties.get(oldProperty.getKey()));
+                       }
+               }
+               for (Entry<String, String> newProperty : properties.entrySet()) {
+                       if (!getProperties().containsKey(newProperty.getKey())) {
+                               webOfTrustConnector.setProperty(this, newProperty.getKey(), newProperty.getValue());
+                       }
+               }
+       }
+
+}
index 5816b47..6df6447 100644 (file)
 
 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.Set;
 
 /**
- * A Web of Trust identity.
+ * Interface for web of trust identities, defining all functions that can be
+ * performed on an identity. The identity is the main entry point for identity
+ * management.
  *
  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
  */
-public class Identity {
-
-       /** The ID of the identity. */
-       private final String id;
-
-       /** The nickname of the identity. */
-       private final String nickname;
-
-       /** The request URI of the identity. */
-       private final String requestUri;
-
-       /** The contexts of the identity. */
-       private final Set<String> contexts = Collections.synchronizedSet(new HashSet<String>());
-
-       /** The properties of the identity. */
-       private final Map<String, String> properties = Collections.synchronizedMap(new HashMap<String, String>());
-
-       /**
-        * Creates a new identity.
-        *
-        * @param id
-        *            The ID of the identity
-        * @param nickname
-        *            The nickname of the identity
-        * @param requestUri
-        *            The request URI of the identity
-        */
-       public Identity(String id, String nickname, String requestUri) {
-               this.id = id;
-               this.nickname = nickname;
-               this.requestUri = requestUri;
-       }
-
-       //
-       // ACCESSORS
-       //
+public interface Identity {
 
        /**
         * Returns the ID of the identity.
         *
         * @return The ID of the identity
         */
-       public String getId() {
-               return id;
-       }
+       public String getId();
 
        /**
         * Returns the nickname of the identity.
         *
         * @return The nickname of the identity
         */
-       public String getNickname() {
-               return nickname;
-       }
+       public String getNickname();
 
        /**
         * Returns the request URI of the identity.
         *
         * @return The request URI of the identity
         */
-       public String getRequestUri() {
-               return requestUri;
-       }
+       public String getRequestUri();
 
        /**
         * Returns all contexts of this identity.
         *
         * @return All contexts of this identity
         */
-       public Set<String> getContexts() {
-               return Collections.unmodifiableSet(contexts);
-       }
-
-       /**
-        * Sets all contexts of this identity.
-        * <p>
-        * This method is only called by the {@link IdentityManager}.
-        *
-        * @param contexts
-        *            All contexts of the identity
-        */
-       void setContexts(Set<String> contexts) {
-               this.contexts.clear();
-               this.contexts.addAll(contexts);
-       }
+       public Set<String> getContexts();
 
        /**
         * Returns whether this identity has the given context.
@@ -122,75 +65,14 @@ public class Identity {
         * @return {@code true} if this identity has the given context,
         *         {@code false} otherwise
         */
-       public boolean hasContext(String context) {
-               return contexts.contains(context);
-       }
-
-       /**
-        * Adds the given context to this identity.
-        * <p>
-        * This method is only called by the {@link IdentityManager}.
-        *
-        * @param context
-        *            The context to add
-        */
-       void addContext(String context) {
-               contexts.add(context);
-       }
-
-       /**
-        * Removes the given context from this identity.
-        * <p>
-        * This method is only called by the {@link IdentityManager}.
-        *
-        * @param context
-        *            The context to remove
-        */
-       void removeContext(String context) {
-               contexts.remove(context);
-       }
+       public boolean hasContext(String context);
 
        /**
         * Returns all properties of this identity.
         *
         * @return All properties of this identity
         */
-       public Map<String, String> getProperties() {
-               synchronized (properties) {
-                       return Collections.unmodifiableMap(properties);
-               }
-       }
-
-       /**
-        * Sets all properties of this identity.
-        * <p>
-        * This method is only called by the {@link IdentityManager}.
-        *
-        * @param properties
-        *            The new properties of this identity
-        */
-       void setProperties(Map<String, String> properties) {
-               synchronized (this.properties) {
-                       this.properties.clear();
-                       this.properties.putAll(properties);
-               }
-       }
-
-       /**
-        * Sets the property with the given name to the given value.
-        * <p>
-        * This method is only called by the {@link IdentityManager}.
-        *
-        * @param name
-        *            The name of the property
-        * @param value
-        *            The value of the property
-        */
-       void setProperty(String name, String value) {
-               synchronized (properties) {
-                       properties.put(name, value);
-               }
-       }
+       public Map<String, String> getProperties();
 
        /**
         * Returns the value of the property with the given name.
@@ -199,56 +81,6 @@ public class Identity {
         *            The name of the property
         * @return The value of the property
         */
-       public String getProperty(String name) {
-               synchronized (properties) {
-                       return properties.get(name);
-               }
-       }
-
-       /**
-        * Removes the property with the given name.
-        * <p>
-        * This method is only called by the {@link IdentityManager}.
-        *
-        * @param name
-        *            The name of the property to remove
-        */
-       void removeProperty(String name) {
-               synchronized (properties) {
-                       properties.remove(name);
-               }
-       }
-
-       //
-       // OBJECT METHODS
-       //
-
-       /**
-        * {@inheritDoc}
-        */
-       @Override
-       public int hashCode() {
-               return id.hashCode();
-       }
-
-       /**
-        * {@inheritDoc}
-        */
-       @Override
-       public boolean equals(Object object) {
-               if (!(object instanceof Identity)) {
-                       return false;
-               }
-               Identity identity = (Identity) object;
-               return identity.id.equals(id);
-       }
-
-       /**
-        * {@inheritDoc}
-        */
-       @Override
-       public String toString() {
-               return getClass().getSimpleName() + "[id=" + id + ",nickname=" + nickname + ",contexts=" + contexts + ",properties=" + properties + "]";
-       }
+       public String getProperty(String name);
 
 }
index 64ea61f..3721f49 100644 (file)
@@ -47,25 +47,31 @@ public interface IdentityListener extends EventListener {
        /**
         * Notifies a listener that a new identity was discovered.
         *
+        * @param ownIdentity
+        *            The own identity at the root of the trust tree
         * @param identity
         *            The new identity
         */
-       public void identityAdded(Identity identity);
+       public void identityAdded(OwnIdentity ownIdentity, Identity identity);
 
        /**
         * Notifies a listener that some properties of the identity have changed.
         *
+        * @param ownIdentity
+        *            The own identity at the root of the trust tree
         * @param identity
         *            The updated identity
         */
-       public void identityUpdated(Identity identity);
+       public void identityUpdated(OwnIdentity ownIdentity, Identity identity);
 
        /**
         * Notifies a listener that an identity has gone away.
         *
+        * @param ownIdentity
+        *            The own identity at the root of the trust tree
         * @param identity
         *            The disappeared identity
         */
-       public void identityRemoved(Identity identity);
+       public void identityRemoved(OwnIdentity ownIdentity, Identity identity);
 
 }
index c08fd16..c6ea783 100644 (file)
@@ -68,39 +68,45 @@ public class IdentityListenerManager extends AbstractListenerManager<IdentityMan
        /**
         * Notifies all listeners that a new identity was discovered.
         *
-        * @see IdentityListener#identityAdded(Identity)
+        * @see IdentityListener#identityAdded(OwnIdentity, Identity)
+        * @param ownIdentity
+        *            The own identity at the root of the trust tree
         * @param identity
         *            The new identity
         */
-       public void fireIdentityAdded(Identity identity) {
+       public void fireIdentityAdded(OwnIdentity ownIdentity, Identity identity) {
                for (IdentityListener identityListener : getListeners()) {
-                       identityListener.identityAdded(identity);
+                       identityListener.identityAdded(ownIdentity, identity);
                }
        }
 
        /**
         * Notifies all listeners that some properties of the identity have changed.
         *
-        * @see IdentityListener#identityUpdated(Identity)
+        * @see IdentityListener#identityUpdated(OwnIdentity, Identity)
+        * @param ownIdentity
+        *            The own identity at the root of the trust tree
         * @param identity
         *            The updated identity
         */
-       public void fireIdentityUpdated(Identity identity) {
+       public void fireIdentityUpdated(OwnIdentity ownIdentity, Identity identity) {
                for (IdentityListener identityListener : getListeners()) {
-                       identityListener.identityUpdated(identity);
+                       identityListener.identityUpdated(ownIdentity, identity);
                }
        }
 
        /**
         * Notifies all listeners that an identity has gone away.
         *
-        * @see IdentityListener#identityRemoved(Identity)
+        * @see IdentityListener#identityRemoved(OwnIdentity, Identity)
+        * @param ownIdentity
+        *            The own identity at the root of the trust tree
         * @param identity
         *            The disappeared identity
         */
-       public void fireIdentityRemoved(Identity identity) {
+       public void fireIdentityRemoved(OwnIdentity ownIdentity, Identity identity) {
                for (IdentityListener identityListener : getListeners()) {
-                       identityListener.identityRemoved(identity);
+                       identityListener.identityRemoved(ownIdentity, identity);
                }
        }
 
index fc9d651..dbf4206 100644 (file)
@@ -160,93 +160,13 @@ public class IdentityManager extends AbstractService {
                        }
                        checkOwnIdentities(newOwnIdentities);
                        return ownIdentities;
-               } catch (PluginException pe1) {
-                       logger.log(Level.WARNING, "Could not load all own identities!", pe1);
+               } catch (WebOfTrustException wote1) {
+                       logger.log(Level.WARNING, "Could not load all own identities!", wote1);
                        return Collections.emptySet();
                }
        }
 
        //
-       // ACTIONS
-       //
-
-       /**
-        * Adds a context to the given own identity.
-        *
-        * @param ownIdentity
-        *            The own identity
-        * @param context
-        *            The context to add
-        */
-       public void addContext(OwnIdentity ownIdentity, String context) {
-               if (ownIdentity.hasContext(context)) {
-                       return;
-               }
-               try {
-                       webOfTrustConnector.addContext(ownIdentity, context);
-                       ownIdentity.addContext(context);
-               } catch (PluginException pe1) {
-                       logger.log(Level.WARNING, "Could not add context " + context + " to OwnIdentity " + ownIdentity + ".", pe1);
-               }
-       }
-
-       /**
-        * Removes a context from the given own identity.
-        *
-        * @param ownIdentity
-        *            The own identity
-        * @param context
-        *            The context to remove
-        */
-       public void removeContext(OwnIdentity ownIdentity, String context) {
-               if (!ownIdentity.hasContext(context)) {
-                       return;
-               }
-               try {
-                       webOfTrustConnector.removeContext(ownIdentity, context);
-                       ownIdentity.removeContext(context);
-               } catch (PluginException pe1) {
-                       logger.log(Level.WARNING, "Could not remove context " + context + " from OwnIdentity " + ownIdentity + ".", pe1);
-               }
-       }
-
-       /**
-        * Sets the property with the given name to the given value.
-        *
-        * @param ownIdentity
-        *            The own identity
-        * @param name
-        *            The name of the property
-        * @param value
-        *            The value of the property
-        */
-       public void setProperty(OwnIdentity ownIdentity, String name, String value) {
-               try {
-                       webOfTrustConnector.setProperty(ownIdentity, name, value);
-                       ownIdentity.setProperty(name, value);
-               } catch (PluginException pe1) {
-                       logger.log(Level.WARNING, "Could not set property “" + name + "” to “" + value + "” for OwnIdentity: " + ownIdentity, pe1);
-               }
-       }
-
-       /**
-        * Removes the property with the given name.
-        *
-        * @param ownIdentity
-        *            The own identity
-        * @param name
-        *            The name of the property to remove
-        */
-       public void removeProperty(OwnIdentity ownIdentity, String name) {
-               try {
-                       webOfTrustConnector.removeProperty(ownIdentity, name);
-                       ownIdentity.removeProperty(name);
-               } catch (PluginException pe1) {
-                       logger.log(Level.WARNING, "Could not remove property “" + name + "” from OwnIdentity: " + ownIdentity, pe1);
-               }
-       }
-
-       //
        // SERVICE METHODS
        //
 
@@ -255,15 +175,14 @@ public class IdentityManager extends AbstractService {
         */
        @Override
        protected void serviceRun() {
-               Map<String, Identity> oldIdentities = Collections.emptyMap();
+               Map<OwnIdentity, Map<String, Identity>> oldIdentities = Collections.emptyMap();
                while (!shouldStop()) {
-                       Map<String, Identity> currentIdentities = new HashMap<String, Identity>();
+                       Map<OwnIdentity, Map<String, Identity>> currentIdentities = new HashMap<OwnIdentity, Map<String, Identity>>();
                        Map<String, OwnIdentity> currentOwnIdentities = new HashMap<String, OwnIdentity>();
 
-                       /* get all identities with the wanted context from WoT. */
-                       Set<OwnIdentity> ownIdentities;
                        try {
-                               ownIdentities = webOfTrustConnector.loadAllOwnIdentities();
+                               /* get all identities with the wanted context from WoT. */
+                               Set<OwnIdentity> ownIdentities = webOfTrustConnector.loadAllOwnIdentities();
 
                                /* check for changes. */
                                for (OwnIdentity ownIdentity : ownIdentities) {
@@ -272,76 +191,80 @@ public class IdentityManager extends AbstractService {
                                checkOwnIdentities(currentOwnIdentities);
 
                                /* now filter for context and get all identities. */
-                               currentOwnIdentities.clear();
                                for (OwnIdentity ownIdentity : ownIdentities) {
                                        if ((context != null) && !ownIdentity.hasContext(context)) {
                                                continue;
                                        }
-                                       currentOwnIdentities.put(ownIdentity.getId(), ownIdentity);
-                                       for (Identity identity : webOfTrustConnector.loadTrustedIdentities(ownIdentity, context)) {
-                                               currentIdentities.put(identity.getId(), identity);
-                                       }
-                               }
 
-                               /* find removed identities. */
-                               for (Identity oldIdentity : oldIdentities.values()) {
-                                       if (!currentIdentities.containsKey(oldIdentity.getId())) {
-                                               identityListenerManager.fireIdentityRemoved(oldIdentity);
+                                       Set<Identity> trustedIdentities = webOfTrustConnector.loadTrustedIdentities(ownIdentity, context);
+                                       Map<String, Identity> identities = new HashMap<String, Identity>();
+                                       currentIdentities.put(ownIdentity, identities);
+                                       for (Identity identity : trustedIdentities) {
+                                               identities.put(identity.getId(), identity);
                                        }
-                               }
 
-                               /* find new identities. */
-                               for (Identity currentIdentity : currentIdentities.values()) {
-                                       if (!oldIdentities.containsKey(currentIdentity.getId())) {
-                                               identityListenerManager.fireIdentityAdded(currentIdentity);
+                                       /* find new identities. */
+                                       for (Identity currentIdentity : currentIdentities.get(ownIdentities).values()) {
+                                               if (!oldIdentities.containsKey(currentIdentity.getId())) {
+                                                       identityListenerManager.fireIdentityAdded(ownIdentity, currentIdentity);
+                                               }
                                        }
-                               }
 
-                               /* check for changes in the contexts. */
-                               for (Identity oldIdentity : oldIdentities.values()) {
-                                       if (!currentIdentities.containsKey(oldIdentity.getId())) {
-                                               continue;
-                                       }
-                                       Identity newIdentity = currentIdentities.get(oldIdentity.getId());
-                                       Set<String> oldContexts = oldIdentity.getContexts();
-                                       Set<String> newContexts = newIdentity.getContexts();
-                                       if (oldContexts.size() != newContexts.size()) {
-                                               identityListenerManager.fireIdentityUpdated(newIdentity);
-                                               continue;
-                                       }
-                                       for (String oldContext : oldContexts) {
-                                               if (!newContexts.contains(oldContext)) {
-                                                       identityListenerManager.fireIdentityUpdated(newIdentity);
-                                                       break;
+                                       /* find removed identities. */
+                                       if (oldIdentities.containsKey(ownIdentity)) {
+                                               for (Identity oldIdentity : oldIdentities.get(ownIdentities).values()) {
+                                                       if (!currentIdentities.containsKey(oldIdentity.getId())) {
+                                                               identityListenerManager.fireIdentityRemoved(ownIdentity, oldIdentity);
+                                                       }
                                                }
-                                       }
-                               }
 
-                               /* check for changes in the properties. */
-                               for (Identity oldIdentity : oldIdentities.values()) {
-                                       if (!currentIdentities.containsKey(oldIdentity.getId())) {
-                                               continue;
-                                       }
-                                       Identity newIdentity = currentIdentities.get(oldIdentity.getId());
-                                       Map<String, String> oldProperties = oldIdentity.getProperties();
-                                       Map<String, String> newProperties = newIdentity.getProperties();
-                                       if (oldProperties.size() != newProperties.size()) {
-                                               identityListenerManager.fireIdentityUpdated(newIdentity);
-                                               continue;
-                                       }
-                                       for (Entry<String, String> oldProperty : oldProperties.entrySet()) {
-                                               if (!newProperties.containsKey(oldProperty.getKey()) || !newProperties.get(oldProperty.getKey()).equals(oldProperty.getValue())) {
-                                                       identityListenerManager.fireIdentityUpdated(newIdentity);
-                                                       break;
+                                               /* 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;
+                                       /* remember the current set of identities. */
+                                       oldIdentities = currentIdentities;
+                               }
 
-                       } catch (PluginException pe1) {
-                               logger.log(Level.WARNING, "WoT has disappeared!", pe1);
+                       } catch (WebOfTrustException wote1) {
+                               logger.log(Level.WARNING, "WoT has disappeared!", wote1);
                        }
 
                        /* wait a minute before checking again. */
index d9ec160..deaf25c 100644 (file)
 
 package net.pterodactylus.sone.freenet.wot;
 
+import java.util.Map;
+import java.util.Set;
+
 /**
- * An own identity is an identity that the owner of the node has full control
- * over.
+ * Defines a local identity, an own identity.
  *
  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
  */
-public class OwnIdentity extends Identity {
+public interface OwnIdentity extends Identity {
 
-       /** The insert URI of the identity. */
-       private final String insertUri;
+       /**
+        * Returns the insert URI of the identity.
+        *
+        * @return The insert URI of the identity
+        */
+       public String getInsertUri();
 
        /**
-        * Creates a new own identity.
+        * Adds the given context to this identity.
+        * <p>
+        * This method is only called by the {@link IdentityManager}.
         *
-        * @param id
-        *            The ID of the identity
-        * @param nickname
-        *            The nickname of the identity
-        * @param requestUri
-        *            The request URI of the identity
-        * @param insertUri
-        *            The insert URI of the identity
+        * @param context
+        *            The context to add
+        * @throws WebOfTrustException
+        *             if an error occurs
         */
-       public OwnIdentity(String id, String nickname, String requestUri, String insertUri) {
-               super(id, nickname, requestUri);
-               this.insertUri = insertUri;
-       }
+       public void addContext(String context) throws WebOfTrustException;
 
-       //
-       // ACCESSORS
-       //
+       /**
+        * Sets all contexts of this identity.
+        * <p>
+        * This method is only called by the {@link IdentityManager}.
+        *
+        * @param contexts
+        *            All contexts of the identity
+        * @throws WebOfTrustException
+        *             if an error occurs
+        */
+       public void setContexts(Set<String> contexts) throws WebOfTrustException;
 
        /**
-        * Returns the insert URI of the identity.
+        * Removes the given context from this identity.
+        * <p>
+        * This method is only called by the {@link IdentityManager}.
         *
-        * @return The insert URI of the identity
+        * @param context
+        *            The context to remove
+        * @throws WebOfTrustException
+        *             if an error occurs
+        */
+       public void removeContext(String context) throws WebOfTrustException;
+
+       /**
+        * Sets the property with the given name to the given value.
+        * <p>
+        * This method is only called by the {@link IdentityManager}.
+        *
+        * @param name
+        *            The name of the property
+        * @param value
+        *            The value of the property
+        * @throws WebOfTrustException
+        *             if an error occurs
+        */
+       public void setProperty(String name, String value) throws WebOfTrustException;
+
+       /**
+        * Sets all properties of this identity.
+        * <p>
+        * This method is only called by the {@link IdentityManager}.
+        *
+        * @param properties
+        *            The new properties of this identity
+        * @throws WebOfTrustException
+        *             if an error occurs
+        */
+       public void setProperties(Map<String, String> properties) throws WebOfTrustException;
+
+       /**
+        * Removes the property with the given name.
+        * <p>
+        * This method is only called by the {@link IdentityManager}.
+        *
+        * @param name
+        *            The name of the property to remove
+        * @throws WebOfTrustException
+        *             if an error occurs
         */
-       public String getInsertUri() {
-               return insertUri;
-       }
+       public void removeProperty(String name) throws WebOfTrustException;
 
 }
index 61068f8..08f512c 100644 (file)
@@ -90,9 +90,9 @@ public class WebOfTrustConnector implements ConnectorListener {
                        String requestUri = fields.get("RequestURI" + ownIdentityCounter);
                        String insertUri = fields.get("InsertURI" + ownIdentityCounter);
                        String nickname = fields.get("Nickname" + ownIdentityCounter);
-                       OwnIdentity ownIdentity = new OwnIdentity(id, nickname, requestUri, insertUri);
-                       ownIdentity.setContexts(parseContexts("Contexts" + ownIdentityCounter + ".", fields));
-                       ownIdentity.setProperties(parseProperties("Properties" + ownIdentityCounter + ".", fields));
+                       DefaultOwnIdentity ownIdentity = new DefaultOwnIdentity(this, id, nickname, requestUri, insertUri);
+                       ownIdentity.setContextsPrivate(parseContexts("Contexts" + ownIdentityCounter + ".", fields));
+                       ownIdentity.setPropertiesPrivate(parseProperties("Properties" + ownIdentityCounter + ".", fields));
                        ownIdentities.add(ownIdentity);
                }
                return ownIdentities;
@@ -136,9 +136,9 @@ public class WebOfTrustConnector implements ConnectorListener {
                        }
                        String nickname = fields.get("Nickname" + identityCounter);
                        String requestUri = fields.get("RequestURI" + identityCounter);
-                       Identity identity = new Identity(id, nickname, requestUri);
-                       identity.setContexts(parseContexts("Contexts" + identityCounter + ".", fields));
-                       identity.setProperties(parseProperties("Properties" + identityCounter + ".", fields));
+                       DefaultIdentity identity = new DefaultIdentity(id, nickname, requestUri);
+                       identity.setContextsPrivate(parseContexts("Contexts" + identityCounter + ".", fields));
+                       identity.setPropertiesPrivate(parseProperties("Properties" + identityCounter + ".", fields));
                        identities.add(identity);
                }
                return identities;