From c4014b8ba958c733156ca069b5866adc8a3d50fa Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Tue, 22 Oct 2013 14:43:23 +0000 Subject: [PATCH] SMACK-441 ServiceDiscoveryManager identities should be non-static and kept in a Set to allow multiple identities as per XEP-0030 git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@13783 b35dd754-fafc-0310-a699-88a17e54d16e --- .../smackx/ServiceDiscoveryManager.java | 65 ++++++++++--------- .../smackx/entitycaps/EntityCapsManager.java | 3 +- .../smackx/packet/DiscoverInfo.java | 11 +++- .../smackx/ServiceDiscoveryManagerTest.java | 4 +- 4 files changed, 49 insertions(+), 34 deletions(-) diff --git a/source/org/jivesoftware/smackx/ServiceDiscoveryManager.java b/source/org/jivesoftware/smackx/ServiceDiscoveryManager.java index 9e31f6777..566294837 100644 --- a/source/org/jivesoftware/smackx/ServiceDiscoveryManager.java +++ b/source/org/jivesoftware/smackx/ServiceDiscoveryManager.java @@ -54,7 +54,8 @@ public class ServiceDiscoveryManager { private static final String DEFAULT_IDENTITY_CATEGORY = "client"; private static final String DEFAULT_IDENTITY_TYPE = "pc"; - private static List identities = new LinkedList(); + private Set identities = new HashSet(); + private DiscoverInfo.Identity identity = new Identity(DEFAULT_IDENTITY_CATEGORY, DEFAULT_IDENTITY_NAME, DEFAULT_IDENTITY_TYPE); private EntityCapsManager capsManager; @@ -74,7 +75,6 @@ public class ServiceDiscoveryManager { new ServiceDiscoveryManager(connection); } }); - identities.add(new Identity(DEFAULT_IDENTITY_CATEGORY, DEFAULT_IDENTITY_NAME, DEFAULT_IDENTITY_TYPE)); } /** @@ -86,6 +86,7 @@ public class ServiceDiscoveryManager { */ public ServiceDiscoveryManager(Connection connection) { this.connection = connection; + identities.add(identity); init(); } @@ -107,13 +108,8 @@ public class ServiceDiscoveryManager { * @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; - } + public String getIdentityName() { + return identity.getName(); } /** @@ -123,10 +119,8 @@ public class ServiceDiscoveryManager { * @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); + public void setIdentityName(String name) { + identity.setName(name); } /** @@ -137,13 +131,8 @@ public class ServiceDiscoveryManager { * @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; - } + public String getIdentityType() { + return identity.getType(); } /** @@ -154,14 +143,30 @@ public class ServiceDiscoveryManager { * @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); - } + public void setIdentityType(String type) { + identity.setType(type); + } + + /** + * Add an identity to the client. + * + * @param identity + */ + public void addIdentity(DiscoverInfo.Identity identity) { + identities.add(identity); + } + + /** + * 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); + return true; } /** @@ -169,8 +174,8 @@ public class ServiceDiscoveryManager { * * @return */ - public static List getIdentities() { - return Collections.unmodifiableList(identities); + public Set getIdentities() { + return Collections.unmodifiableSet(identities); } /** diff --git a/source/org/jivesoftware/smackx/entitycaps/EntityCapsManager.java b/source/org/jivesoftware/smackx/entitycaps/EntityCapsManager.java index 1da222efd..c15f11c03 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; @@ -457,9 +458,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/packet/DiscoverInfo.java b/source/org/jivesoftware/smackx/packet/DiscoverInfo.java index ba873a96d..756fd7191 100644 --- a/source/org/jivesoftware/smackx/packet/DiscoverInfo.java +++ b/source/org/jivesoftware/smackx/packet/DiscoverInfo.java @@ -312,7 +312,16 @@ public class DiscoverInfo extends IQ { public String getName() { 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/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()); }