Commit 8ab44dbb authored by Gaston Dombiak's avatar Gaston Dombiak Committed by gato

Refactoring work. PluginClassLoader is now a ClassLoader.

git-svn-id: http://svn.igniterealtime.org/svn/repos/openfire/trunk@8194 b35dd754-fafc-0310-a699-88a17e54d16e
parent ee9c9e6c
...@@ -19,10 +19,6 @@ import java.io.FilenameFilter; ...@@ -19,10 +19,6 @@ import java.io.FilenameFilter;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.net.URLClassLoader; import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
/** /**
* ClassLoader for plugins. It searches the plugin directory for classes * ClassLoader for plugins. It searches the plugin directory for classes
...@@ -35,23 +31,14 @@ import java.util.List; ...@@ -35,23 +31,14 @@ import java.util.List;
* *
* @author Derek DeMoro * @author Derek DeMoro
*/ */
public class PluginClassLoader { public class PluginClassLoader extends URLClassLoader {
private URLClassLoader classLoader; public PluginClassLoader() {
private final List<URL> list = new ArrayList<URL>(); super(new URL[] {}, findParentClassLoader());
/**
* Constructs a plugin loader for the given plugin directory.
*
* @throws SecurityException if the created class loader violates
* existing security constraints.
*/
public PluginClassLoader() throws SecurityException {
} }
/** /**
* Adds a directory to the class loader. The {@link #initialize()} method should be called * Adds a directory to the class loader.
* after adding the directory to make the change take effect.
* *
* @param directory the directory. * @param directory the directory.
* @param developmentMode true if the plugin is running in development mode. This * @param developmentMode true if the plugin is running in development mode. This
...@@ -63,25 +50,25 @@ public class PluginClassLoader { ...@@ -63,25 +50,25 @@ public class PluginClassLoader {
// Add classes directory to classpath. // Add classes directory to classpath.
File classesDir = new File(directory, "classes"); File classesDir = new File(directory, "classes");
if (classesDir.exists()) { if (classesDir.exists()) {
list.add(classesDir.toURL()); addURL(classesDir.toURL());
} }
// Add i18n directory to classpath. // Add i18n directory to classpath.
File databaseDir = new File(directory, "database"); File databaseDir = new File(directory, "database");
if(databaseDir.exists()){ if(databaseDir.exists()){
list.add(databaseDir.toURL()); addURL(databaseDir.toURL());
} }
// Add i18n directory to classpath. // Add i18n directory to classpath.
File i18nDir = new File(directory, "i18n"); File i18nDir = new File(directory, "i18n");
if(i18nDir.exists()){ if(i18nDir.exists()){
list.add(i18nDir.toURL()); addURL(i18nDir.toURL());
} }
// Add web directory to classpath. // Add web directory to classpath.
File webDir = new File(directory, "web"); File webDir = new File(directory, "web");
if(webDir.exists()){ if(webDir.exists()){
list.add(webDir.toURL()); addURL(webDir.toURL());
} }
// Add lib directory to classpath. // Add lib directory to classpath.
...@@ -97,11 +84,11 @@ public class PluginClassLoader { ...@@ -97,11 +84,11 @@ public class PluginClassLoader {
if (developmentMode) { if (developmentMode) {
// Do not add plugin-pluginName.jar to classpath. // Do not add plugin-pluginName.jar to classpath.
if (!jars[i].getName().equals("plugin-" + directory.getName() + ".jar")) { if (!jars[i].getName().equals("plugin-" + directory.getName() + ".jar")) {
list.add(jars[i].toURL()); addURL(jars[i].toURL());
} }
} }
else { else {
list.add(jars[i].toURL()); addURL(jars[i].toURL());
} }
} }
} }
...@@ -112,61 +99,8 @@ public class PluginClassLoader { ...@@ -112,61 +99,8 @@ public class PluginClassLoader {
} }
} }
public Collection<URL> getURLS() { public void addURLFile(URL file) {
return list; addURL(file);
}
/**
* Adds a URL to the class loader. The {@link #initialize()} method should be called
* after adding the URL to make the change take effect.
*
* @param url the url.
*/
public void addURL(URL url) {
list.add(url);
}
/**
* Initializes the class loader with all configured classpath URLs. This method
* can be called multiple times if the list of URLs changes.
*/
public void initialize() {
Iterator urls = list.iterator();
URL[] urlArray = new URL[list.size()];
for (int i = 0; urls.hasNext(); i++) {
urlArray[i] = (URL)urls.next();
}
// If the classloader is to be used by a child plugin, we should
// never use the ContextClassLoader, but only reuse the plugin classloader itself.
if (classLoader != null) {
classLoader = new URLClassLoader(urlArray, classLoader);
}
else {
classLoader = new URLClassLoader(urlArray, findParentClassLoader());
}
}
/**
* Load a class using this plugin class loader.
*
* @param name the fully qualified name of the class to load.
* @return The module object loaded
* @throws ClassNotFoundException if the class could not be loaded by this class loader.
* @throws IllegalAccessException if the class constructor was private or protected.
* @throws InstantiationException if the class could not be instantiated (initialization error).
* @throws SecurityException if the custom class loader not allowed.
*/
public Class loadClass(String name) throws ClassNotFoundException, IllegalAccessException,
InstantiationException, SecurityException {
return classLoader.loadClass(name);
}
/**
* Destroys this class loader.
*/
public void destroy() {
classLoader = null;
} }
/** /**
...@@ -174,23 +108,14 @@ public class PluginClassLoader { ...@@ -174,23 +108,14 @@ public class PluginClassLoader {
* *
* @return the best parent classloader to use. * @return the best parent classloader to use.
*/ */
private ClassLoader findParentClassLoader() { private static ClassLoader findParentClassLoader() {
ClassLoader parent = XMPPServer.class.getClassLoader(); ClassLoader parent = XMPPServer.class.getClassLoader();
if (parent == null) { if (parent == null) {
parent = this.getClass().getClassLoader(); parent = PluginClassLoader.class.getClassLoader();
} }
if (parent == null) { if (parent == null) {
parent = ClassLoader.getSystemClassLoader(); parent = ClassLoader.getSystemClassLoader();
} }
return parent; return parent;
} }
/**
* Returns the URLClassloader used.
*
* @return the URLClassLoader used.
*/
public ClassLoader getClassLoader() {
return classLoader;
}
} }
...@@ -347,13 +347,11 @@ public class PluginManager { ...@@ -347,13 +347,11 @@ public class PluginManager {
if (classes.exists()) { if (classes.exists()) {
dev.setClassesDir(classes); dev.setClassesDir(classes);
pluginLoader.addURL(classes.getAbsoluteFile().toURL()); pluginLoader.addURLFile(classes.getAbsoluteFile().toURL());
} }
} }
} }
pluginLoader.initialize();
String className = pluginXML.selectSingleNode("/plugin/class").getText(); String className = pluginXML.selectSingleNode("/plugin/class").getText();
plugin = (Plugin)pluginLoader.loadClass(className).newInstance(); plugin = (Plugin)pluginLoader.loadClass(className).newInstance();
if (parentPluginNode != null) { if (parentPluginNode != null) {
...@@ -537,11 +535,6 @@ public class PluginManager { ...@@ -537,11 +535,6 @@ public class PluginManager {
catch (Exception e) { catch (Exception e) {
Log.error(e); Log.error(e);
} }
PluginClassLoader classLoader = classloaders.get(plugin);
// Destroy class loader if defined, which it won't be if this is a child plugin.
if (classLoader != null) {
classLoader.destroy();
}
plugins.remove(pluginName); plugins.remove(pluginName);
pluginDirs.remove(plugin); pluginDirs.remove(plugin);
classloaders.remove(plugin); classloaders.remove(plugin);
......
...@@ -20,18 +20,6 @@ import org.jivesoftware.util.Log; ...@@ -20,18 +20,6 @@ import org.jivesoftware.util.Log;
import org.jivesoftware.util.StringUtils; import org.jivesoftware.util.StringUtils;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.servlet.GenericServlet; import javax.servlet.GenericServlet;
import javax.servlet.ServletConfig; import javax.servlet.ServletConfig;
import javax.servlet.ServletException; import javax.servlet.ServletException;
...@@ -39,6 +27,12 @@ import javax.servlet.ServletOutputStream; ...@@ -39,6 +27,12 @@ import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/** /**
* The plugin servlet acts as a proxy for web requests (in the admin console) * The plugin servlet acts as a proxy for web requests (in the admin console)
...@@ -481,9 +475,7 @@ public class PluginServlet extends HttpServlet { ...@@ -481,9 +475,7 @@ public class PluginServlet extends HttpServlet {
PluginClassLoader pluginClassloader = pluginManager.getPluginClassloader(plugin); PluginClassLoader pluginClassloader = pluginManager.getPluginClassloader(plugin);
Collection col = pluginClassloader.getURLS(); for (URL url : pluginClassloader.getURLs()) {
for (Object aCol : col) {
URL url = (URL)aCol;
File file = new File(url.getFile()); File file = new File(url.getFile());
classpath.append(file.getAbsolutePath()).append(";"); classpath.append(file.getAbsolutePath()).append(";");
......
...@@ -15,11 +15,7 @@ import org.jivesoftware.openfire.XMPPServer; ...@@ -15,11 +15,7 @@ import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.container.Plugin; import org.jivesoftware.openfire.container.Plugin;
import org.jivesoftware.openfire.container.PluginManager; import org.jivesoftware.openfire.container.PluginManager;
import java.text.DateFormat; import java.text.*;
import java.text.Format;
import java.text.MessageFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.*; import java.util.*;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
...@@ -430,7 +426,7 @@ public class LocaleUtils { ...@@ -430,7 +426,7 @@ public class LocaleUtils {
throw new NullPointerException("Plugin could not be located: " + pluginName); throw new NullPointerException("Plugin could not be located: " + pluginName);
} }
ClassLoader pluginClassLoader = pluginManager.getPluginClassloader(plugin).getClassLoader(); ClassLoader pluginClassLoader = pluginManager.getPluginClassloader(plugin);
try { try {
ResourceBundle bundle = ResourceBundle.getBundle(i18nFile, locale, pluginClassLoader); ResourceBundle bundle = ResourceBundle.getBundle(i18nFile, locale, pluginClassLoader);
return getLocalizedString(key, locale, arguments, bundle); return getLocalizedString(key, locale, arguments, bundle);
...@@ -461,7 +457,7 @@ public class LocaleUtils { ...@@ -461,7 +457,7 @@ public class LocaleUtils {
throw new NullPointerException("Plugin could not be located."); throw new NullPointerException("Plugin could not be located.");
} }
ClassLoader pluginClassLoader = pluginManager.getPluginClassloader(plugin).getClassLoader(); ClassLoader pluginClassLoader = pluginManager.getPluginClassloader(plugin);
return ResourceBundle.getBundle(i18nFile, locale, pluginClassLoader); return ResourceBundle.getBundle(i18nFile, locale, pluginClassLoader);
} }
......
...@@ -348,7 +348,7 @@ public class CacheFactory { ...@@ -348,7 +348,7 @@ public class CacheFactory {
Plugin enterprisePlugin = pluginManager.getPlugin(pluginName); Plugin enterprisePlugin = pluginManager.getPlugin(pluginName);
PluginClassLoader pluginLoader = pluginManager.getPluginClassloader(enterprisePlugin); PluginClassLoader pluginLoader = pluginManager.getPluginClassloader(enterprisePlugin);
if (pluginLoader != null) { if (pluginLoader != null) {
return pluginLoader.getClassLoader(); return pluginLoader;
} }
else { else {
Log.warn("Unable to find PluginClassloader for plugin: " + pluginName + " in CacheFactory."); Log.warn("Unable to find PluginClassloader for plugin: " + pluginName + " in CacheFactory.");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment