1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-11-02 06:45:59 +01:00

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
This commit is contained in:
Florian Schmaus 2013-10-22 14:43:23 +00:00 committed by flow
parent 5f793d4f44
commit c4014b8ba9
4 changed files with 49 additions and 34 deletions

View file

@ -54,7 +54,8 @@ public class ServiceDiscoveryManager {
private static final String DEFAULT_IDENTITY_CATEGORY = "client"; private static final String DEFAULT_IDENTITY_CATEGORY = "client";
private static final String DEFAULT_IDENTITY_TYPE = "pc"; private static final String DEFAULT_IDENTITY_TYPE = "pc";
private static List<DiscoverInfo.Identity> identities = new LinkedList<DiscoverInfo.Identity>(); private Set<DiscoverInfo.Identity> identities = new HashSet<DiscoverInfo.Identity>();
private DiscoverInfo.Identity identity = new Identity(DEFAULT_IDENTITY_CATEGORY, DEFAULT_IDENTITY_NAME, DEFAULT_IDENTITY_TYPE);
private EntityCapsManager capsManager; private EntityCapsManager capsManager;
@ -74,7 +75,6 @@ public class ServiceDiscoveryManager {
new ServiceDiscoveryManager(connection); 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) { public ServiceDiscoveryManager(Connection connection) {
this.connection = connection; this.connection = connection;
identities.add(identity);
init(); init();
} }
@ -107,13 +108,8 @@ public class ServiceDiscoveryManager {
* @return the name of the client that will be returned when asked for the client identity * @return the name of the client that will be returned when asked for the client identity
* in a disco request. * in a disco request.
*/ */
public static String getIdentityName() { public String getIdentityName() {
DiscoverInfo.Identity identity = identities.get(0); return identity.getName();
if (identity != null) {
return identity.getName();
} else {
return null;
}
} }
/** /**
@ -123,10 +119,8 @@ public class ServiceDiscoveryManager {
* @param name the name of the client that will be returned when asked for the client identity * @param name the name of the client that will be returned when asked for the client identity
* in a disco request. * in a disco request.
*/ */
public static void setIdentityName(String name) { public void setIdentityName(String name) {
DiscoverInfo.Identity identity = identities.remove(0); identity.setName(name);
identity = new DiscoverInfo.Identity(DEFAULT_IDENTITY_CATEGORY, name, DEFAULT_IDENTITY_TYPE);
identities.add(identity);
} }
/** /**
@ -137,13 +131,8 @@ public class ServiceDiscoveryManager {
* @return the type of client that will be returned when asked for the client identity in a * @return the type of client that will be returned when asked for the client identity in a
* disco request. * disco request.
*/ */
public static String getIdentityType() { public String getIdentityType() {
DiscoverInfo.Identity identity = identities.get(0); return identity.getType();
if (identity != null) {
return identity.getType();
} else {
return null;
}
} }
/** /**
@ -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 * @param type the type of client that will be returned when asked for the client identity in a
* disco request. * disco request.
*/ */
public static void setIdentityType(String type) { public void setIdentityType(String type) {
DiscoverInfo.Identity identity = identities.get(0); identity.setType(type);
if (identity != null) { }
identity.setType(type);
} else { /**
identity = new DiscoverInfo.Identity(DEFAULT_IDENTITY_CATEGORY, DEFAULT_IDENTITY_NAME, type); * Add an identity to the client.
identities.add(identity); *
} * @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 * @return
*/ */
public static List<DiscoverInfo.Identity> getIdentities() { public Set<DiscoverInfo.Identity> getIdentities() {
return Collections.unmodifiableList(identities); return Collections.unmodifiableSet(identities);
} }
/** /**

View file

@ -56,6 +56,7 @@ import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Queue; import java.util.Queue;
import java.util.Set;
import java.util.SortedSet; import java.util.SortedSet;
import java.util.TreeSet; import java.util.TreeSet;
import java.util.WeakHashMap; import java.util.WeakHashMap;
@ -457,9 +458,9 @@ public class EntityCapsManager {
if (connection != null) if (connection != null)
jidCaps.put(connection.getUser(), new NodeVerHash(ENTITY_NODE, currentCapsVersion, "sha-1")); jidCaps.put(connection.getUser(), new NodeVerHash(ENTITY_NODE, currentCapsVersion, "sha-1"));
final List<Identity> identities = new LinkedList<Identity>(ServiceDiscoveryManager.getInstanceFor(connection).getIdentities());
sdm.setNodeInformationProvider(ENTITY_NODE + '#' + currentCapsVersion, new NodeInformationProvider() { sdm.setNodeInformationProvider(ENTITY_NODE + '#' + currentCapsVersion, new NodeInformationProvider() {
List<String> features = sdm.getFeaturesList(); List<String> features = sdm.getFeaturesList();
List<Identity> identities = new LinkedList<Identity>(ServiceDiscoveryManager.getIdentities());
List<PacketExtension> packetExtensions = sdm.getExtendedInfoAsList(); List<PacketExtension> packetExtensions = sdm.getExtendedInfoAsList();
@Override @Override

View file

@ -313,6 +313,15 @@ public class DiscoverInfo extends IQ {
return name; 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 * Returns the entity's type. To get the official registry of values for the
* 'type' attribute refer to <a href="http://www.jabber.org/registrar/disco-categories.html">Jabber::Registrar</a> * 'type' attribute refer to <a href="http://www.jabber.org/registrar/disco-categories.html">Jabber::Registrar</a>

View file

@ -53,10 +53,10 @@ public class ServiceDiscoveryManagerTest extends SmackTestCase {
Iterator<Identity> identities = info.getIdentities(); Iterator<Identity> identities = info.getIdentities();
assertTrue("No identities were found", identities.hasNext()); assertTrue("No identities were found", identities.hasNext());
Identity identity = identities.next(); Identity identity = identities.next();
assertEquals("Name in identity is wrong", ServiceDiscoveryManager.getIdentityName(), assertEquals("Name in identity is wrong", discoManager.getIdentityName(),
identity.getName()); identity.getName());
assertEquals("Category in identity is wrong", "client", identity.getCategory()); 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()); identity.getType());
assertFalse("More identities were found", identities.hasNext()); assertFalse("More identities were found", identities.hasNext());
} }