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;
import java.net.MalformedURLException;
import java.net.URL;
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
......@@ -35,23 +31,14 @@ import java.util.List;
*
* @author Derek DeMoro
*/
public class PluginClassLoader {
public class PluginClassLoader extends URLClassLoader {
private URLClassLoader classLoader;
private final List<URL> list = new ArrayList<URL>();
/**
* Constructs a plugin loader for the given plugin directory.
*
* @throws SecurityException if the created class loader violates
* existing security constraints.
*/
public PluginClassLoader() throws SecurityException {
public PluginClassLoader() {
super(new URL[] {}, findParentClassLoader());
}
/**
* Adds a directory to the class loader. The {@link #initialize()} method should be called
* after adding the directory to make the change take effect.
* Adds a directory to the class loader.
*
* @param directory the directory.
* @param developmentMode true if the plugin is running in development mode. This
......@@ -63,25 +50,25 @@ public class PluginClassLoader {
// Add classes directory to classpath.
File classesDir = new File(directory, "classes");
if (classesDir.exists()) {
list.add(classesDir.toURL());
addURL(classesDir.toURL());
}
// Add i18n directory to classpath.
File databaseDir = new File(directory, "database");
if(databaseDir.exists()){
list.add(databaseDir.toURL());
addURL(databaseDir.toURL());
}
// Add i18n directory to classpath.
File i18nDir = new File(directory, "i18n");
if(i18nDir.exists()){
list.add(i18nDir.toURL());
addURL(i18nDir.toURL());
}
// Add web directory to classpath.
File webDir = new File(directory, "web");
if(webDir.exists()){
list.add(webDir.toURL());
addURL(webDir.toURL());
}
// Add lib directory to classpath.
......@@ -97,11 +84,11 @@ public class PluginClassLoader {
if (developmentMode) {
// Do not add plugin-pluginName.jar to classpath.
if (!jars[i].getName().equals("plugin-" + directory.getName() + ".jar")) {
list.add(jars[i].toURL());
addURL(jars[i].toURL());
}
}
else {
list.add(jars[i].toURL());
addURL(jars[i].toURL());
}
}
}
......@@ -112,61 +99,8 @@ public class PluginClassLoader {
}
}
public Collection<URL> getURLS() {
return list;
}
/**
* 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;
public void addURLFile(URL file) {
addURL(file);
}
/**
......@@ -174,23 +108,14 @@ public class PluginClassLoader {
*
* @return the best parent classloader to use.
*/
private ClassLoader findParentClassLoader() {
private static ClassLoader findParentClassLoader() {
ClassLoader parent = XMPPServer.class.getClassLoader();
if (parent == null) {
parent = this.getClass().getClassLoader();
parent = PluginClassLoader.class.getClassLoader();
}
if (parent == null) {
parent = ClassLoader.getSystemClassLoader();
}
return parent;
}
/**
* Returns the URLClassloader used.
*
* @return the URLClassLoader used.
*/
public ClassLoader getClassLoader() {
return classLoader;
}
}
......@@ -347,13 +347,11 @@ public class PluginManager {
if (classes.exists()) {
dev.setClassesDir(classes);
pluginLoader.addURL(classes.getAbsoluteFile().toURL());
pluginLoader.addURLFile(classes.getAbsoluteFile().toURL());
}
}
}
pluginLoader.initialize();
String className = pluginXML.selectSingleNode("/plugin/class").getText();
plugin = (Plugin)pluginLoader.loadClass(className).newInstance();
if (parentPluginNode != null) {
......@@ -537,11 +535,6 @@ public class PluginManager {
catch (Exception 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);
pluginDirs.remove(plugin);
classloaders.remove(plugin);
......
......@@ -20,18 +20,6 @@ import org.jivesoftware.util.Log;
import org.jivesoftware.util.StringUtils;
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.ServletConfig;
import javax.servlet.ServletException;
......@@ -39,6 +27,12 @@ import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
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)
......@@ -481,9 +475,7 @@ public class PluginServlet extends HttpServlet {
PluginClassLoader pluginClassloader = pluginManager.getPluginClassloader(plugin);
Collection col = pluginClassloader.getURLS();
for (Object aCol : col) {
URL url = (URL)aCol;
for (URL url : pluginClassloader.getURLs()) {
File file = new File(url.getFile());
classpath.append(file.getAbsolutePath()).append(";");
......
......@@ -15,11 +15,7 @@ import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.container.Plugin;
import org.jivesoftware.openfire.container.PluginManager;
import java.text.DateFormat;
import java.text.Format;
import java.text.MessageFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.*;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
......@@ -430,7 +426,7 @@ public class LocaleUtils {
throw new NullPointerException("Plugin could not be located: " + pluginName);
}
ClassLoader pluginClassLoader = pluginManager.getPluginClassloader(plugin).getClassLoader();
ClassLoader pluginClassLoader = pluginManager.getPluginClassloader(plugin);
try {
ResourceBundle bundle = ResourceBundle.getBundle(i18nFile, locale, pluginClassLoader);
return getLocalizedString(key, locale, arguments, bundle);
......@@ -461,7 +457,7 @@ public class LocaleUtils {
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);
}
......
......@@ -348,7 +348,7 @@ public class CacheFactory {
Plugin enterprisePlugin = pluginManager.getPlugin(pluginName);
PluginClassLoader pluginLoader = pluginManager.getPluginClassloader(enterprisePlugin);
if (pluginLoader != null) {
return pluginLoader.getClassLoader();
return pluginLoader;
}
else {
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