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

Use multiple class loaders to try to load smack.providers file.

git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@2154 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
Matt Tucker 2003-10-23 15:38:16 +00:00 committed by mtucker
parent 8688966ca6
commit d4d351aae2

View file

@ -146,95 +146,103 @@ public class ProviderManager {
static { static {
// Load IQ processing providers. // Load IQ processing providers.
try { try {
Enumeration enum = ProviderManager.class.getClassLoader().getResources( // Get an array of class loaders to try loading the providers files from.
"META-INF/smack.providers"); ClassLoader[] classLoaders = getClassLoaders();
while (enum.hasMoreElements()) { for (int i=0; i<classLoaders.length; i++) {
URL url = (URL)enum.nextElement(); Enumeration enum = classLoaders[i].getResources(
java.io.InputStream providerStream = null; "META-INF/smack.providers");
try { while (enum.hasMoreElements()) {
providerStream = url.openStream(); URL url = (URL)enum.nextElement();
XmlPullParserFactory factory = XmlPullParserFactory.newInstance( java.io.InputStream providerStream = null;
"org.xmlpull.mxp1.MXParserFactory", null); try {
factory.setNamespaceAware(true); providerStream = url.openStream();
XmlPullParser parser = factory.newPullParser(); XmlPullParserFactory factory = XmlPullParserFactory.newInstance(
parser.setInput(providerStream, "UTF-8"); "org.xmlpull.mxp1.MXParserFactory", null);
int eventType = parser.getEventType(); factory.setNamespaceAware(true);
do { XmlPullParser parser = factory.newPullParser();
if (eventType == XmlPullParser.START_TAG) { parser.setInput(providerStream, "UTF-8");
if (parser.getName().equals("iqProvider")) { int eventType = parser.getEventType();
parser.next(); do {
parser.next(); if (eventType == XmlPullParser.START_TAG) {
String elementName = parser.nextText(); if (parser.getName().equals("iqProvider")) {
parser.next(); parser.next();
parser.next(); parser.next();
String namespace = parser.nextText(); String elementName = parser.nextText();
parser.next(); parser.next();
parser.next(); parser.next();
String className = parser.nextText(); String namespace = parser.nextText();
// Only add the provider for the namespace if one isn't parser.next();
// already registered. parser.next();
String key = getProviderKey(elementName, namespace); String className = parser.nextText();
if (!iqProviders.containsKey(key)) { // Only add the provider for the namespace if one isn't
// Attempt to load the provider class and then create // already registered.
// a new instance if it's an IQProvider. Otherwise, if it's String key = getProviderKey(elementName, namespace);
// an IQ class, add the class object itself, then we'll use if (!iqProviders.containsKey(key)) {
// reflection later to create instances of the class. // Attempt to load the provider class and then create
try { // a new instance if it's an IQProvider. Otherwise, if it's
// Add the provider to the map. // an IQ class, add the class object itself, then we'll use
Class provider = Class.forName(className); // reflection later to create instances of the class.
if (IQProvider.class.isAssignableFrom(provider)) { try {
iqProviders.put(key, provider.newInstance()); // Add the provider to the map.
Class provider = Class.forName(className);
if (IQProvider.class.isAssignableFrom(provider)) {
iqProviders.put(key, provider.newInstance());
}
else if (IQ.class.isAssignableFrom(provider)) {
iqProviders.put(key, provider);
}
} }
else if (IQ.class.isAssignableFrom(provider)) { catch (ClassNotFoundException cnfe) {
iqProviders.put(key, provider); cnfe.printStackTrace();
} }
} }
catch (ClassNotFoundException cnfe) { }
cnfe.printStackTrace(); else if (parser.getName().equals("extensionProvider")) {
parser.next();
parser.next();
String elementName = parser.nextText();
parser.next();
parser.next();
String namespace = parser.nextText();
parser.next();
parser.next();
String className = parser.nextText();
// Only add the provider for the namespace if one isn't
// already registered.
String key = getProviderKey(elementName, namespace);
if (!extensionProviders.containsKey(key)) {
// Attempt to load the provider class and then create
// a new instance if it's a Provider. Otherwise, if it's
// a PacketExtension, add the class object itself and
// then we'll use reflection later to create instances
// of the class.
try {
// Add the provider to the map.
Class provider = Class.forName(className);
if (PacketExtensionProvider.class.isAssignableFrom(
provider))
{
extensionProviders.put(key, provider.newInstance());
}
else if (PacketExtension.class.isAssignableFrom(
provider))
{
extensionProviders.put(key, provider);
}
}
catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}
} }
} }
} }
else if (parser.getName().equals("extensionProvider")) { eventType = parser.next();
parser.next(); } while (eventType != XmlPullParser.END_DOCUMENT);
parser.next(); }
String elementName = parser.nextText(); finally {
parser.next(); try { providerStream.close(); }
parser.next(); catch (Exception e) { }
String namespace = parser.nextText(); }
parser.next();
parser.next();
String className = parser.nextText();
// Only add the provider for the namespace if one isn't
// already registered.
String key = getProviderKey(elementName, namespace);
if (!extensionProviders.containsKey(key)) {
// Attempt to load the provider class and then create
// a new instance if it's a Provider. Otherwise, if it's
// a PacketExtension, add the class object itself and
// then we'll use reflection later to create instances
// of the class.
try {
// Add the provider to the map.
Class provider = Class.forName(className);
if (PacketExtensionProvider.class.isAssignableFrom(provider)) {
extensionProviders.put(key, provider.newInstance());
}
else if (PacketExtension.class.isAssignableFrom(provider)) {
extensionProviders.put(key, provider);
}
}
catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}
}
}
}
eventType = parser.next();
} while (eventType != XmlPullParser.END_DOCUMENT);
}
finally {
try { providerStream.close(); }
catch (Exception e) { }
} }
} }
} }
@ -344,4 +352,16 @@ public class ProviderManager {
buf.append("<").append(elementName).append("/><").append(namespace).append("/>"); buf.append("<").append(elementName).append("/><").append(namespace).append("/>");
return buf.toString(); return buf.toString();
} }
private static ClassLoader[] getClassLoaders() {
ClassLoader[] classLoaders = new ClassLoader[3];
classLoaders[0] = new ProviderManager().getClass().getClassLoader();
classLoaders[1] = Thread.currentThread().getContextClassLoader();
classLoaders[2] = Object.class.getClassLoader();
return classLoaders;
}
private ProviderManager() {
}
} }