1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-11-26 16:22:06 +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_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;
@ -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<DiscoverInfo.Identity> getIdentities() {
return Collections.unmodifiableList(identities);
public Set<DiscoverInfo.Identity> getIdentities() {
return Collections.unmodifiableSet(identities);
}
/**

View file

@ -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<Identity> identities = new LinkedList<Identity>(ServiceDiscoveryManager.getInstanceFor(connection).getIdentities());
sdm.setNodeInformationProvider(ENTITY_NODE + '#' + currentCapsVersion, new NodeInformationProvider() {
List<String> features = sdm.getFeaturesList();
List<Identity> identities = new LinkedList<Identity>(ServiceDiscoveryManager.getIdentities());
List<PacketExtension> packetExtensions = sdm.getExtendedInfoAsList();
@Override

View file

@ -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 <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();
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());
}