mirror of
https://github.com/igniterealtime/Spark.git
synced 2025-10-29 19:57:28 +00:00
Added a check for smack.providers in plugin class loader.
git-svn-id: http://svn.igniterealtime.org/svn/repos/spark/trunk@4626 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
parent
403a1ce399
commit
d50a01b1ac
@ -10,11 +10,25 @@
|
||||
|
||||
package org.jivesoftware.spark.plugin;
|
||||
|
||||
import org.jivesoftware.smack.packet.IQ;
|
||||
import org.jivesoftware.smack.packet.PacketExtension;
|
||||
import org.jivesoftware.smack.provider.IQProvider;
|
||||
import org.jivesoftware.smack.provider.PacketExtensionProvider;
|
||||
import org.jivesoftware.smack.provider.ProviderManager;
|
||||
import org.jivesoftware.spark.util.URLFileSystem;
|
||||
import org.jivesoftware.spark.util.log.Log;
|
||||
import org.xmlpull.mxp1.MXParser;
|
||||
import org.xmlpull.v1.XmlPullParser;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FilenameFilter;
|
||||
import java.io.InputStream;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
import java.util.jar.JarFile;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipFile;
|
||||
|
||||
/**
|
||||
* A simple classloader to extend the classpath to
|
||||
@ -67,8 +81,117 @@ public class PluginClassLoader extends URLClassLoader {
|
||||
|
||||
for (int i = 0; i < jars.length; i++) {
|
||||
if (jars[i].isFile()) {
|
||||
addURL(jars[i].toURL());
|
||||
final URL url = jars[i].toURL();
|
||||
addURL(url);
|
||||
try {
|
||||
checkForSmackProviders(url);
|
||||
}
|
||||
catch (Exception e) {
|
||||
Log.error(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void checkForSmackProviders(URL jarURL) throws Exception {
|
||||
ZipFile zipFile = new JarFile(URLFileSystem.url2File(jarURL));
|
||||
|
||||
ZipEntry entry = zipFile.getEntry("META-INF/smack.providers");
|
||||
if (entry != null) {
|
||||
InputStream zin = zipFile.getInputStream(entry);
|
||||
loadSmackProvider(zin);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void loadSmackProvider(InputStream providerStream) throws Exception {
|
||||
|
||||
// Get an array of class loaders to try loading the providers files from.
|
||||
|
||||
try {
|
||||
XmlPullParser parser = new MXParser();
|
||||
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
|
||||
parser.setInput(providerStream, "UTF-8");
|
||||
int eventType = parser.getEventType();
|
||||
do {
|
||||
if (eventType == XmlPullParser.START_TAG) {
|
||||
if (parser.getName().equals("iqProvider")) {
|
||||
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.
|
||||
|
||||
// Attempt to load the provider class and then create
|
||||
// a new instance if it's an IQProvider. Otherwise, if it's
|
||||
// an IQ class, add the class object itself, then we'll use
|
||||
// reflection later to create instances of the class.
|
||||
try {
|
||||
// Add the provider to the map.
|
||||
Class provider = this.loadClass(className);
|
||||
if (IQProvider.class.isAssignableFrom(provider)) {
|
||||
ProviderManager.addIQProvider(elementName, namespace, provider.newInstance());
|
||||
}
|
||||
else if (IQ.class.isAssignableFrom(provider)) {
|
||||
ProviderManager.addIQProvider(elementName, namespace, provider.newInstance());
|
||||
}
|
||||
}
|
||||
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.
|
||||
// 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 = this.loadClass(className);
|
||||
if (PacketExtensionProvider.class.isAssignableFrom(
|
||||
provider)) {
|
||||
ProviderManager.addExtensionProvider(elementName, namespace, provider.newInstance());
|
||||
}
|
||||
else if (PacketExtension.class.isAssignableFrom(
|
||||
provider)) {
|
||||
ProviderManager.addExtensionProvider(elementName, namespace, provider.newInstance());
|
||||
}
|
||||
}
|
||||
catch (ClassNotFoundException cnfe) {
|
||||
cnfe.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
eventType = parser.next();
|
||||
}
|
||||
while (eventType != XmlPullParser.END_DOCUMENT);
|
||||
}
|
||||
finally {
|
||||
try {
|
||||
providerStream.close();
|
||||
}
|
||||
catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user