implemented a child first plugin classloader strategy

This commit is contained in:
Sebastian Sdorra
2013-03-13 20:51:44 +01:00
parent 15906a7e62
commit 19c48f0a6c
3 changed files with 252 additions and 7 deletions

View File

@@ -47,7 +47,6 @@ import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.LinkedList;
import java.util.List;
@@ -108,12 +107,12 @@ public class BootstrapListener implements ServletContextListener
if (logger.isInfoEnabled())
{
logger.info("start scm-manager {} in stage: {}", context.getVersion(),
context.getStage());
context.getStage());
}
ClassLoader classLoader = null;
File pluginDirectory = new File(context.getBaseDirectory(),
PLUGIN_DIRECTORY);
PLUGIN_DIRECTORY);
if (pluginDirectory.exists())
{
@@ -153,7 +152,7 @@ public class BootstrapListener implements ServletContextListener
}
scmContextListener = BootstrapUtil.loadClass(classLoader,
ServletContextListener.class, LISTENER);
ServletContextListener.class, LISTENER);
Thread.currentThread().setContextClassLoader(classLoader);
BootstrapUtil.setClassLoader(sce.getServletContext(), classLoader);
}
@@ -182,7 +181,7 @@ public class BootstrapListener implements ServletContextListener
* @return
*/
private ClassLoader createClassLoader(File pluginDirectory,
Classpath classpath)
Classpath classpath)
{
if (logger.isDebugEnabled())
{
@@ -224,8 +223,8 @@ public class BootstrapListener implements ServletContextListener
}
}
return new URLClassLoader(classpathURLs.toArray(new URL[0]),
getParentClassLoader());
return BootstrapUtil.createClassLoader(classpathURLs,
getParentClassLoader());
}
//~--- get methods ----------------------------------------------------------

View File

@@ -35,11 +35,20 @@ package sonia.scm.boot;
//~--- non-JDK imports --------------------------------------------------------
import com.google.common.base.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.net.ChildFirstURLClassLoader;
//~--- JDK imports ------------------------------------------------------------
import java.net.URL;
import java.net.URLClassLoader;
import java.util.List;
import javax.servlet.ServletContext;
/**
@@ -52,6 +61,16 @@ public final class BootstrapUtil
/** Field description */
public static final String CLASSLOADER = "sonia.scm.BoostrapClassLoader";
/** Field description */
private static final String STRATEGY =
"sonia.scm.plugin.classloader.strategy";
/** Field description */
private static final String STRATEGY_CHILDFIRST = "child-first";
/** Field description */
private static final String STRATEGY_PARENTFIRST = "parent-first";
/** the logger for BootstrapUtil */
private static final Logger logger =
LoggerFactory.getLogger(BootstrapUtil.class);
@@ -66,6 +85,46 @@ public final class BootstrapUtil
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @param classpathURLs
* @param parent
*
* @return
*/
public static ClassLoader createClassLoader(List<URL> classpathURLs,
ClassLoader parent)
{
ClassLoader classLoader = null;
URL[] urls = classpathURLs.toArray(new URL[classpathURLs.size()]);
String strategy = System.getProperty(STRATEGY);
if (!Strings.isNullOrEmpty(strategy))
{
if (STRATEGY_CHILDFIRST.equals(strategy))
{
logger.info("using {} as plugin classloading strategy",
STRATEGY_CHILDFIRST);
classLoader = new ChildFirstURLClassLoader(urls, parent);
}
else if (!STRATEGY_PARENTFIRST.equals(strategy))
{
logger.warn("unknown plugin classloading strategy {}", strategy);
}
}
if (classLoader == null)
{
logger.info("using {} as plugin classloading strategy",
STRATEGY_PARENTFIRST);
classLoader = new URLClassLoader(urls, parent);
}
return classLoader;
}
/**
* Method description
*