1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-12-23 13:07:59 +01:00

ProviderManager is no longer static. SMACK-182

git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@6128 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
Gaston Dombiak 2006-11-16 19:31:30 +00:00 committed by gato
parent aa92ee9942
commit 69b36d5861
4 changed files with 62 additions and 26 deletions

View file

@ -603,7 +603,7 @@ class PacketReader {
// Otherwise, see if there is a registered provider for // Otherwise, see if there is a registered provider for
// this element name and namespace. // this element name and namespace.
else { else {
Object provider = ProviderManager.getIQProvider(elementName, namespace); Object provider = ProviderManager.getInstance().getIQProvider(elementName, namespace);
if (provider != null) { if (provider != null) {
if (provider instanceof IQProvider) { if (provider instanceof IQProvider) {
iqPacket = ((IQProvider)provider).parseIQ(parser); iqPacket = ((IQProvider)provider).parseIQ(parser);

View file

@ -105,16 +105,53 @@ import java.util.concurrent.ConcurrentHashMap;
* set the properties of the class using the values in the packet extension sub-element. When an * set the properties of the class using the values in the packet extension sub-element. When an
* extension provider is not registered for an element name and namespace combination, Smack will * extension provider is not registered for an element name and namespace combination, Smack will
* store all top-level elements of the sub-packet in DefaultPacketExtension object and then * store all top-level elements of the sub-packet in DefaultPacketExtension object and then
* attach it to the packet. * attach it to the packet.<p>
*
* It is possible to provide a custom provider manager instead of the default implementation
* provided by Smack. If you want to provide your own provider manager then you need to do it
* before creating any {@link org.jivesoftware.smack.XMPPConnection} by sending the static
* {@link #setInstance(ProviderManager)} message. Trying to change the provider manager after
* an XMPPConnection was created will result in an {@link IllegalStateException} error.
* *
* @author Matt Tucker * @author Matt Tucker
*/ */
public class ProviderManager { public class ProviderManager {
private static Map<String, Object> extensionProviders = new ConcurrentHashMap<String, Object>(); private static ProviderManager instance;
private static Map<String, Object> iqProviders = new ConcurrentHashMap<String, Object>();
static { private Map<String, Object> extensionProviders = new ConcurrentHashMap<String, Object>();
private Map<String, Object> iqProviders = new ConcurrentHashMap<String, Object>();
/**
* Returns the only ProviderManager valid instance. Use {@link #setInstance(ProviderManager)}
* to configure your own provider manager. If non was provided then an instance of this
* class will be used.
*
* @return the only ProviderManager valid instance.
*/
public static synchronized ProviderManager getInstance() {
if (instance == null) {
instance = new ProviderManager();
}
return instance;
}
/**
* Sets the only ProviderManager valid instance to be used by all XMPPConnections. If you
* want to provide your own provider manager then you need to do it before creating
* any XMPPConnection. Otherwise an IllegalStateException will be thrown.
*
* @param providerManager the only ProviderManager valid instance to be used.
* @throws IllegalStateException if a provider manager was already configued.
*/
public static synchronized void setInstance(ProviderManager providerManager) {
if (instance != null) {
throw new IllegalStateException("ProviderManager singleton already set");
}
instance = providerManager;
}
protected void initialize() {
// Load IQ processing providers. // Load IQ processing providers.
try { try {
// Get an array of class loaders to try loading the providers files from. // Get an array of class loaders to try loading the providers files from.
@ -155,7 +192,7 @@ public class ProviderManager {
// Add the provider to the map. // Add the provider to the map.
Class provider = Class.forName(className); Class provider = Class.forName(className);
if (IQProvider.class.isAssignableFrom(provider)) { if (IQProvider.class.isAssignableFrom(provider)) {
iqProviders.put(key, (IQProvider)provider.newInstance()); iqProviders.put(key, provider.newInstance());
} }
else if (IQ.class.isAssignableFrom(provider)) { else if (IQ.class.isAssignableFrom(provider)) {
iqProviders.put(key, provider); iqProviders.put(key, provider);
@ -243,7 +280,7 @@ public class ProviderManager {
* @param namespace the XML namespace. * @param namespace the XML namespace.
* @return the IQ provider. * @return the IQ provider.
*/ */
public static Object getIQProvider(String elementName, String namespace) { public Object getIQProvider(String elementName, String namespace) {
String key = getProviderKey(elementName, namespace); String key = getProviderKey(elementName, namespace);
return iqProviders.get(key); return iqProviders.get(key);
} }
@ -255,7 +292,7 @@ public class ProviderManager {
* *
* @return all IQProvider instances. * @return all IQProvider instances.
*/ */
public static Collection<Object> getIQProviders() { public Collection<Object> getIQProviders() {
return Collections.unmodifiableCollection(iqProviders.values()); return Collections.unmodifiableCollection(iqProviders.values());
} }
@ -268,7 +305,7 @@ public class ProviderManager {
* @param namespace the XML namespace. * @param namespace the XML namespace.
* @param provider the IQ provider. * @param provider the IQ provider.
*/ */
public static void addIQProvider(String elementName, String namespace, public void addIQProvider(String elementName, String namespace,
Object provider) Object provider)
{ {
if (!(provider instanceof IQProvider || (provider instanceof Class && if (!(provider instanceof IQProvider || (provider instanceof Class &&
@ -289,7 +326,7 @@ public class ProviderManager {
* @param elementName the XML element name. * @param elementName the XML element name.
* @param namespace the XML namespace. * @param namespace the XML namespace.
*/ */
public static void removeIQProvider(String elementName, String namespace) { public void removeIQProvider(String elementName, String namespace) {
String key = getProviderKey(elementName, namespace); String key = getProviderKey(elementName, namespace);
iqProviders.remove(key); iqProviders.remove(key);
} }
@ -309,11 +346,11 @@ public class ProviderManager {
* *
* <p>Note: this method is generally only called by the internal Smack classes. * <p>Note: this method is generally only called by the internal Smack classes.
* *
* @param elementName * @param elementName element name associated with extension provider.
* @param namespace * @param namespace namespace associated with extension provider.
* @return the extenion provider. * @return the extenion provider.
*/ */
public static Object getExtensionProvider(String elementName, String namespace) { public Object getExtensionProvider(String elementName, String namespace) {
String key = getProviderKey(elementName, namespace); String key = getProviderKey(elementName, namespace);
return extensionProviders.get(key); return extensionProviders.get(key);
} }
@ -327,7 +364,7 @@ public class ProviderManager {
* @param namespace the XML namespace. * @param namespace the XML namespace.
* @param provider the extension provider. * @param provider the extension provider.
*/ */
public static void addExtensionProvider(String elementName, String namespace, public void addExtensionProvider(String elementName, String namespace,
Object provider) Object provider)
{ {
if (!(provider instanceof PacketExtensionProvider || provider instanceof Class)) { if (!(provider instanceof PacketExtensionProvider || provider instanceof Class)) {
@ -346,7 +383,7 @@ public class ProviderManager {
* @param elementName the XML element name. * @param elementName the XML element name.
* @param namespace the XML namespace. * @param namespace the XML namespace.
*/ */
public static void removeExtensionProvider(String elementName, String namespace) { public void removeExtensionProvider(String elementName, String namespace) {
String key = getProviderKey(elementName, namespace); String key = getProviderKey(elementName, namespace);
extensionProviders.remove(key); extensionProviders.remove(key);
} }
@ -358,7 +395,7 @@ public class ProviderManager {
* *
* @return all PacketExtensionProvider instances. * @return all PacketExtensionProvider instances.
*/ */
public static Collection<Object> getExtensionProviders() { public Collection<Object> getExtensionProviders() {
return Collections.unmodifiableCollection(extensionProviders.values()); return Collections.unmodifiableCollection(extensionProviders.values());
} }
@ -369,7 +406,7 @@ public class ProviderManager {
* @param namespace the namespace. * @param namespace the namespace.
* @return a unique key for the element name and namespace pair. * @return a unique key for the element name and namespace pair.
*/ */
private static String getProviderKey(String elementName, String namespace) { private String getProviderKey(String elementName, String namespace) {
StringBuilder buf = new StringBuilder(); StringBuilder buf = new StringBuilder();
buf.append("<").append(elementName).append("/><").append(namespace).append("/>"); buf.append("<").append(elementName).append("/><").append(namespace).append("/>");
return buf.toString(); return buf.toString();
@ -380,7 +417,7 @@ public class ProviderManager {
* *
* @return an array of ClassLoader instances. * @return an array of ClassLoader instances.
*/ */
private static ClassLoader[] getClassLoaders() { private ClassLoader[] getClassLoaders() {
ClassLoader[] classLoaders = new ClassLoader[2]; ClassLoader[] classLoaders = new ClassLoader[2];
classLoaders[0] = ProviderManager.class.getClassLoader(); classLoaders[0] = ProviderManager.class.getClassLoader();
classLoaders[1] = Thread.currentThread().getContextClassLoader(); classLoaders[1] = Thread.currentThread().getContextClassLoader();
@ -395,6 +432,7 @@ public class ProviderManager {
} }
private ProviderManager() { private ProviderManager() {
super();
initialize();
} }
} }

View file

@ -364,7 +364,7 @@ public class PacketParserUtils {
throws Exception throws Exception
{ {
// See if a provider is registered to handle the extension. // See if a provider is registered to handle the extension.
Object provider = ProviderManager.getExtensionProvider(elementName, namespace); Object provider = ProviderManager.getInstance().getExtensionProvider(elementName, namespace);
if (provider != null) { if (provider != null) {
if (provider instanceof PacketExtensionProvider) { if (provider instanceof PacketExtensionProvider) {
return ((PacketExtensionProvider)provider).parseExtension(parser); return ((PacketExtensionProvider)provider).parseExtension(parser);
@ -438,6 +438,7 @@ public class PacketParserUtils {
* @param type the type of the property. * @param type the type of the property.
* @param value the encode String value to decode. * @param value the encode String value to decode.
* @return the String value decoded into the specified type. * @return the String value decoded into the specified type.
* @throws Exception If decoding failed due to an error.
*/ */
private static Object decode(Class type, String value) throws Exception { private static Object decode(Class type, String value) throws Exception {
if (type.getName().equals("java.lang.String")) { if (type.getName().equals("java.lang.String")) {

View file

@ -224,7 +224,7 @@ public class EnhancedDebuggerWindow {
iqProvidersPanel.setLayout(new GridLayout(1, 1)); iqProvidersPanel.setLayout(new GridLayout(1, 1));
iqProvidersPanel.setBorder(BorderFactory.createTitledBorder("Installed IQ Providers")); iqProvidersPanel.setBorder(BorderFactory.createTitledBorder("Installed IQ Providers"));
Vector<String> providers = new Vector<String>(); Vector<String> providers = new Vector<String>();
for (Object provider : ProviderManager.getIQProviders()) { for (Object provider : ProviderManager.getInstance().getIQProviders()) {
if (provider.getClass() == Class.class) { if (provider.getClass() == Class.class) {
providers.add(((Class) provider).getName()); providers.add(((Class) provider).getName());
} }
@ -243,7 +243,7 @@ public class EnhancedDebuggerWindow {
extensionProvidersPanel.setLayout(new GridLayout(1, 1)); extensionProvidersPanel.setLayout(new GridLayout(1, 1));
extensionProvidersPanel.setBorder(BorderFactory.createTitledBorder("Installed Extension Providers")); extensionProvidersPanel.setBorder(BorderFactory.createTitledBorder("Installed Extension Providers"));
providers = new Vector<String>(); providers = new Vector<String>();
for (Object provider : ProviderManager.getExtensionProviders()) { for (Object provider : ProviderManager.getInstance().getExtensionProviders()) {
if (provider.getClass() == Class.class) { if (provider.getClass() == Class.class) {
providers.add(((Class) provider).getName()); providers.add(((Class) provider).getName());
} }
@ -370,9 +370,6 @@ public class EnhancedDebuggerWindow {
} }
public boolean isVisible() { public boolean isVisible() {
if (frame != null) { return frame != null && frame.isVisible();
return frame.isVisible();
}
return false;
} }
} }