mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-11 16:05:44 +01:00
implemented a child first plugin classloader strategy
This commit is contained in:
@@ -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 ----------------------------------------------------------
|
||||
|
||||
@@ -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
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user