diff --git a/documentation/extensions/caps.html b/documentation/extensions/caps.html index f22fa2ce3..b729d6d30 100644 --- a/documentation/extensions/caps.html +++ b/documentation/extensions/caps.html @@ -33,7 +33,7 @@ Enable Entity Capabilities
       // Get an instance of entity caps manager for the specified connection
-      EntityCapsManager mgr = EntityCapsManager.getInstanceFor(connection);
+      EntityCapsManager mgr = EntityCapsManager.getInstanceFor(connection);
 
       // Enable entity capabilities
       mgr.enableEntityCaps();
@@ -45,7 +45,7 @@ Configure a persistent cache for Entity Capabilities
 
       // Get an instance of entity caps manager for the specified connection
-      EntityCapsManager mgr = EntityCapsManager.getInstanceFor(connection);
+      EntityCapsManager mgr = EntityCapsManager.getInstanceFor(connection);
 
       // Create an cache, see smackx.entitycaps.cache for pre-defined cache implementations
       EntityCapsPersistentCache cache = new SimpleDirectoryPersistentCache(new File("/foo/cachedir"));
diff --git a/source/org/jivesoftware/smack/SmackConfiguration.java b/source/org/jivesoftware/smack/SmackConfiguration.java
index 02f1d2115..5395a1906 100644
--- a/source/org/jivesoftware/smack/SmackConfiguration.java
+++ b/source/org/jivesoftware/smack/SmackConfiguration.java
@@ -28,8 +28,8 @@ import java.util.Enumeration;
 import java.util.List;
 import java.util.Vector;
 
-import org.jivesoftware.smack.parsing.ParsingExceptionCallback;
 import org.jivesoftware.smack.parsing.ExceptionThrowingCallback;
+import org.jivesoftware.smack.parsing.ParsingExceptionCallback;
 import org.xmlpull.mxp1.MXParser;
 import org.xmlpull.v1.XmlPullParser;
 
@@ -123,6 +123,9 @@ public final class SmackConfiguration {
                                 else if (parser.getName().equals("autoEnableEntityCaps")) {
                                     autoEnableEntityCaps = Boolean.parseBoolean(parser.nextText());
                                 }
+                                else if (parser.getName().equals("autoEnableEntityCaps")) {
+                                    autoEnableEntityCaps = Boolean.parseBoolean(parser.nextText());
+                                }
                             }
                             eventType = parser.next();
                         }
@@ -319,14 +322,6 @@ public final class SmackConfiguration {
         SmackConfiguration.localSocks5ProxyPort = localSocks5ProxyPort;
     }
 
-    /**
-     * Check if Entity Caps are enabled as default for every new connection
-     * @return
-     */
-    public static boolean autoEnableEntityCaps() {
-        return autoEnableEntityCaps;
-    }
-
     /**
      * Set if Entity Caps are enabled or disabled for every new connection
      * 
@@ -336,6 +331,14 @@ public final class SmackConfiguration {
         autoEnableEntityCaps = b;
     }
 
+    /**
+     * Check if Entity Caps are enabled as default for every new connection
+     * @return
+     */
+    public static boolean autoEnableEntityCaps() {
+        return autoEnableEntityCaps;
+    }
+
     /**
      * Set the default parsing exception callback for all newly created connections
      *
diff --git a/source/org/jivesoftware/smackx/ServiceDiscoveryManager.java b/source/org/jivesoftware/smackx/ServiceDiscoveryManager.java
index 21047b2c0..e236514a2 100644
--- a/source/org/jivesoftware/smackx/ServiceDiscoveryManager.java
+++ b/source/org/jivesoftware/smackx/ServiceDiscoveryManager.java
@@ -54,8 +54,11 @@ public class ServiceDiscoveryManager {
     private static final String DEFAULT_IDENTITY_NAME = "Smack";
     private static final String DEFAULT_IDENTITY_CATEGORY = "client";
     private static final String DEFAULT_IDENTITY_TYPE = "pc";
+    private static DiscoverInfo.Identity defaultIdentity = new Identity(DEFAULT_IDENTITY_CATEGORY,
+            DEFAULT_IDENTITY_NAME, DEFAULT_IDENTITY_TYPE);
 
-    private static List identities = new LinkedList();
+    private Set identities = new HashSet();
+    private DiscoverInfo.Identity identity = defaultIdentity;
 
     private EntityCapsManager capsManager;
 
@@ -75,7 +78,16 @@ public class ServiceDiscoveryManager {
                 getInstanceFor(connection);
             }
         });
-        identities.add(new Identity(DEFAULT_IDENTITY_CATEGORY, DEFAULT_IDENTITY_NAME, DEFAULT_IDENTITY_TYPE));
+    }
+
+    /**
+     * Set the default identity all new connections will have. If unchanged the default identity is an
+     * identity where category is set to 'client', type is set to 'pc' and name is set to 'Smack'.
+     * 
+     * @param identity
+     */
+    public static void setDefaultIdentity(DiscoverInfo.Identity identity) {
+        defaultIdentity = identity;
     }
 
     /**
@@ -89,6 +101,7 @@ public class ServiceDiscoveryManager {
     @Deprecated
     public ServiceDiscoveryManager(Connection connection) {
         this.connection = new WeakReference(connection);
+
         // Register the new instance and associate it with the connection 
         instances.put(connection, this);
 
@@ -179,6 +192,90 @@ public class ServiceDiscoveryManager {
         connection.addPacketListener(packetListener, packetFilter);
     }
 
+    /**
+     * Returns the name of the client that will be returned when asked for the client identity
+     * in a disco request. The name could be any value you need to identity this client.
+     * 
+     * @return the name of the client that will be returned when asked for the client identity
+     *          in a disco request.
+     */
+    public String getIdentityName() {
+        return identity.getName();
+    }
+
+    /**
+     * Sets the name of the client that will be returned when asked for the client identity
+     * in a disco request. The name could be any value you need to identity this client.
+     * 
+     * @param name the name of the client that will be returned when asked for the client identity
+     *          in a disco request.
+     */
+    public void setIdentityName(String name) {
+        identity.setName(name);
+        renewEntityCapsVersion();
+    }
+
+    /**
+     * Returns the type of client that will be returned when asked for the client identity in a 
+     * disco request. The valid types are defined by the category client. Follow this link to learn 
+     * the possible types: Jabber::Registrar.
+     * 
+     * @return the type of client that will be returned when asked for the client identity in a 
+     *          disco request.
+     */
+    public String getIdentityType() {
+        return identity.getType();
+    }
+
+    /**
+     * Sets the type of client that will be returned when asked for the client identity in a 
+     * disco request. The valid types are defined by the category client. Follow this link to learn 
+     * the possible types: Jabber::Registrar.
+     * 
+     * @param type the type of client that will be returned when asked for the client identity in a 
+     *          disco request.
+     */
+    public void setIdentityType(String type) {
+        identity.setType(type);
+        renewEntityCapsVersion();
+    }
+
+    /**
+     * Add an identity to the client.
+     * 
+     * @param identity
+     */
+    public void addIdentity(DiscoverInfo.Identity identity) {
+        identities.add(identity);
+        renewEntityCapsVersion();
+    }
+
+    /**
+     * Remove an identity from the client. Note that the client needs at least one identity, the default identity, which
+     * can not be removed.
+     * 
+     * @param identity
+     * @return true, if successful. Otherwise the default identity was given.
+     */
+    public boolean removeIdentity(DiscoverInfo.Identity identity) {
+        if (identity.equals(this.identity)) return false;
+        identities.remove(identity);
+        renewEntityCapsVersion();
+        return true;
+    }
+
+    /**
+     * Returns all identities of this client as unmodifiable Collection
+     * 
+     * @return
+     */
+    public Set getIdentities() {
+        Set res = new HashSet(identities);
+        // Add the default identity that must exist
+        res.add(defaultIdentity);
+        return Collections.unmodifiableSet(res);
+    }
+
     /**
      * Returns the ServiceDiscoveryManager instance associated with a given Connection.
      * 
@@ -193,79 +290,6 @@ public class ServiceDiscoveryManager {
         return sdm;
     }
 
-    /**
-     * Returns the name of the client that will be returned when asked for the client identity
-     * in a disco request. The name could be any value you need to identity this client.
-     * 
-     * @return the name of the client that will be returned when asked for the client identity
-     *          in a disco request.
-     */
-    public static String getIdentityName() {
-        DiscoverInfo.Identity identity = identities.get(0);
-        if (identity != null) {
-            return identity.getName();
-        } else {
-            return null;
-        }
-    }
-
-    /**
-     * Sets the name of the client that will be returned when asked for the client identity
-     * in a disco request. The name could be any value you need to identity this client.
-     * 
-     * @param name the name of the client that will be returned when asked for the client identity
-     *          in a disco request.
-     */
-    public static void setIdentityName(String name) {
-        DiscoverInfo.Identity identity = identities.remove(0);
-        identity = new DiscoverInfo.Identity(DEFAULT_IDENTITY_CATEGORY, name, DEFAULT_IDENTITY_TYPE);
-        identities.add(identity);
-    }
-
-    /**
-     * Returns the type of client that will be returned when asked for the client identity in a 
-     * disco request. The valid types are defined by the category client. Follow this link to learn 
-     * the possible types: Jabber::Registrar.
-     * 
-     * @return the type of client that will be returned when asked for the client identity in a 
-     *          disco request.
-     */
-    public static String getIdentityType() {
-        DiscoverInfo.Identity identity = identities.get(0);
-        if (identity != null) {
-            return identity.getType();
-        } else {
-            return null;
-        }
-    }
-
-    /**
-     * Sets the type of client that will be returned when asked for the client identity in a 
-     * disco request. The valid types are defined by the category client. Follow this link to learn 
-     * the possible types: Jabber::Registrar.
-     * 
-     * @param type the type of client that will be returned when asked for the client identity in a 
-     *          disco request.
-     */
-    public static void setIdentityType(String type) {
-        DiscoverInfo.Identity identity = identities.get(0);
-        if (identity != null) {
-            identity.setType(type);
-        } else {
-            identity = new DiscoverInfo.Identity(DEFAULT_IDENTITY_CATEGORY, DEFAULT_IDENTITY_NAME, type);
-            identities.add(identity);
-        }
-    }
-
-    /**
-     * Returns all identities of this client as unmodifiable Collection
-     * 
-     * @return
-     */
-    public static List getIdentities() {
-        return Collections.unmodifiableList(identities);
-    }
-
     /**
      * Add discover info response data.
      * 
@@ -275,7 +299,7 @@ public class ServiceDiscoveryManager {
      */
     public void addDiscoverInfoTo(DiscoverInfo response) {
         // First add the identities of the connection
-        response.addIdentities(identities);
+        response.addIdentities(getIdentities());
 
         // Add the registered features to the response
         synchronized (features) {
diff --git a/source/org/jivesoftware/smackx/entitycaps/EntityCapsManager.java b/source/org/jivesoftware/smackx/entitycaps/EntityCapsManager.java
index afce78bbe..563b606a2 100644
--- a/source/org/jivesoftware/smackx/entitycaps/EntityCapsManager.java
+++ b/source/org/jivesoftware/smackx/entitycaps/EntityCapsManager.java
@@ -56,6 +56,7 @@ import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.Queue;
+import java.util.Set;
 import java.util.SortedSet;
 import java.util.TreeSet;
 import java.util.WeakHashMap;
@@ -444,9 +445,9 @@ public class EntityCapsManager {
         if (connection != null)
             jidCaps.put(connection.getUser(), new NodeVerHash(ENTITY_NODE, currentCapsVersion, "sha-1"));
 
+        final List identities = new LinkedList(ServiceDiscoveryManager.getInstanceFor(connection).getIdentities());
         sdm.setNodeInformationProvider(ENTITY_NODE + '#' + currentCapsVersion, new NodeInformationProvider() {
             List features = sdm.getFeaturesList();
-            List identities = new LinkedList(ServiceDiscoveryManager.getIdentities());
             List packetExtensions = sdm.getExtendedInfoAsList();
 
             @Override
diff --git a/source/org/jivesoftware/smackx/entitycaps/cache/SimpleDirectoryPersistentCache.java b/source/org/jivesoftware/smackx/entitycaps/cache/SimpleDirectoryPersistentCache.java
index ae0e11633..5d2365463 100644
--- a/source/org/jivesoftware/smackx/entitycaps/cache/SimpleDirectoryPersistentCache.java
+++ b/source/org/jivesoftware/smackx/entitycaps/cache/SimpleDirectoryPersistentCache.java
@@ -28,7 +28,6 @@ import java.io.StringReader;
 import org.jivesoftware.smack.packet.IQ;
 import org.jivesoftware.smack.provider.IQProvider;
 import org.jivesoftware.smack.util.Base32Encoder;
-import org.jivesoftware.smack.util.Base64Encoder;
 import org.jivesoftware.smack.util.StringEncoder;
 import org.jivesoftware.smackx.entitycaps.EntityCapsManager;
 import org.jivesoftware.smackx.packet.DiscoverInfo;
diff --git a/source/org/jivesoftware/smackx/packet/DiscoverInfo.java b/source/org/jivesoftware/smackx/packet/DiscoverInfo.java
index 46b408af7..884c1f529 100644
--- a/source/org/jivesoftware/smackx/packet/DiscoverInfo.java
+++ b/source/org/jivesoftware/smackx/packet/DiscoverInfo.java
@@ -313,6 +313,15 @@ public class DiscoverInfo extends IQ {
             return name;
         }
 
+        /**
+         * Set the identity's name.
+         * 
+         * @param name
+         */
+        public void setName(String name) {
+            this.name = name;
+        }
+
         /**
          * Returns the entity's type. To get the official registry of values for the 
          * 'type' attribute refer to Jabber::Registrar 
diff --git a/test-unit/org/jivesoftware/smackx/entitycaps/EntityCapsManagerTest.java b/test-unit/org/jivesoftware/smackx/entitycaps/EntityCapsManagerTest.java
index 158fe466b..91057f7d1 100644
--- a/test-unit/org/jivesoftware/smackx/entitycaps/EntityCapsManagerTest.java
+++ b/test-unit/org/jivesoftware/smackx/entitycaps/EntityCapsManagerTest.java
@@ -1,6 +1,8 @@
 package org.jivesoftware.smackx.entitycaps;
 
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
 
 import java.io.File;
 import java.io.IOException;
@@ -9,11 +11,9 @@ import java.util.LinkedList;
 
 import org.jivesoftware.smack.packet.IQ;
 import org.jivesoftware.smack.util.Base32Encoder;
-import org.jivesoftware.smack.util.Base64Encoder;
 import org.jivesoftware.smack.util.Base64FileUrlEncoder;
 import org.jivesoftware.smack.util.StringEncoder;
 import org.jivesoftware.smackx.FormField;
-import org.jivesoftware.smackx.entitycaps.EntityCapsManager;
 import org.jivesoftware.smackx.entitycaps.cache.EntityCapsPersistentCache;
 import org.jivesoftware.smackx.entitycaps.cache.SimpleDirectoryPersistentCache;
 import org.jivesoftware.smackx.packet.DataForm;
diff --git a/test/org/jivesoftware/smackx/ServiceDiscoveryManagerTest.java b/test/org/jivesoftware/smackx/ServiceDiscoveryManagerTest.java
index 9ba422e3e..12369f27e 100644
--- a/test/org/jivesoftware/smackx/ServiceDiscoveryManagerTest.java
+++ b/test/org/jivesoftware/smackx/ServiceDiscoveryManagerTest.java
@@ -53,10 +53,10 @@ public class ServiceDiscoveryManagerTest extends SmackTestCase {
             Iterator identities = info.getIdentities();
             assertTrue("No identities were found", identities.hasNext());
             Identity identity = identities.next();
-            assertEquals("Name in identity is wrong", ServiceDiscoveryManager.getIdentityName(),
+            assertEquals("Name in identity is wrong", discoManager.getIdentityName(),
                     identity.getName());
             assertEquals("Category in identity is wrong", "client", identity.getCategory());
-            assertEquals("Type in identity is wrong", ServiceDiscoveryManager.getIdentityType(),
+            assertEquals("Type in identity is wrong", discoManager.getIdentityType(),
                     identity.getType());
             assertFalse("More identities were found", identities.hasNext());
         }