2019-06-25 09:49:52 +02:00
|
|
|
package sonia.scm.lifecycle.classloading;
|
2019-06-19 11:52:20 +02:00
|
|
|
|
|
|
|
|
import com.google.common.annotations.VisibleForTesting;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
2019-06-25 09:49:52 +02:00
|
|
|
import sonia.scm.lifecycle.LifeCycle;
|
2019-06-19 11:52:20 +02:00
|
|
|
import sonia.scm.plugin.ChildFirstPluginClassLoader;
|
|
|
|
|
import sonia.scm.plugin.DefaultPluginClassLoader;
|
|
|
|
|
|
|
|
|
|
import java.net.URL;
|
|
|
|
|
|
|
|
|
|
import static com.google.common.base.Preconditions.checkState;
|
|
|
|
|
|
|
|
|
|
/**
|
2020-02-04 15:59:11 +01:00
|
|
|
* Base class for ClassLoader LifeCycle implementation in SCM-Manager.
|
2019-06-19 11:52:20 +02:00
|
|
|
*/
|
2020-02-04 15:59:11 +01:00
|
|
|
public abstract class ClassLoaderLifeCycle implements LifeCycle {
|
2019-06-19 11:52:20 +02:00
|
|
|
|
|
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(ClassLoaderLifeCycle.class);
|
|
|
|
|
|
|
|
|
|
@VisibleForTesting
|
2020-02-04 15:59:11 +01:00
|
|
|
static final String PROPERTY = "sonia.scm.classloading.lifecycle";
|
|
|
|
|
|
2019-06-19 11:52:20 +02:00
|
|
|
public static ClassLoaderLifeCycle create() {
|
2019-11-21 16:16:15 +01:00
|
|
|
ClassLoader webappClassLoader = Thread.currentThread().getContextClassLoader();
|
2020-02-04 15:59:11 +01:00
|
|
|
String implementation = System.getProperty(PROPERTY);
|
|
|
|
|
if (SimpleClassLoaderLifeCycle.NAME.equalsIgnoreCase(implementation)) {
|
|
|
|
|
LOG.info("create new simple ClassLoaderLifeCycle");
|
|
|
|
|
return new SimpleClassLoaderLifeCycle(webappClassLoader);
|
|
|
|
|
}
|
|
|
|
|
LOG.info("create new ClassLoaderLifeCycle with leak prevention");
|
|
|
|
|
return new ClassLoaderLifeCycleWithLeakPrevention(webappClassLoader);
|
2019-06-19 11:52:20 +02:00
|
|
|
}
|
|
|
|
|
|
2020-02-04 15:59:11 +01:00
|
|
|
private final ClassLoader webappClassLoader;
|
2019-11-21 16:16:15 +01:00
|
|
|
|
2020-02-04 15:59:11 +01:00
|
|
|
private BootstrapClassLoader bootstrapClassLoader;
|
2019-11-21 16:16:15 +01:00
|
|
|
|
2020-02-04 15:59:11 +01:00
|
|
|
ClassLoaderLifeCycle(ClassLoader webappClassLoader) {
|
|
|
|
|
this.webappClassLoader = webappClassLoader;
|
2019-11-21 16:16:15 +01:00
|
|
|
}
|
|
|
|
|
|
2020-02-04 15:59:11 +01:00
|
|
|
@Override
|
2019-06-25 09:49:52 +02:00
|
|
|
public void initialize() {
|
2019-06-19 11:52:20 +02:00
|
|
|
bootstrapClassLoader = initAndAppend(new BootstrapClassLoader(webappClassLoader));
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-04 15:59:11 +01:00
|
|
|
protected abstract <T extends ClassLoader> T initAndAppend(T classLoader);
|
2019-06-19 11:52:20 +02:00
|
|
|
|
|
|
|
|
public ClassLoader getBootstrapClassLoader() {
|
|
|
|
|
checkState(bootstrapClassLoader != null, "%s was not initialized", ClassLoaderLifeCycle.class.getName());
|
|
|
|
|
return bootstrapClassLoader;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ClassLoader createChildFirstPluginClassLoader(URL[] urls, ClassLoader parent, String plugin) {
|
|
|
|
|
LOG.debug("create new ChildFirstPluginClassLoader for {}", plugin);
|
|
|
|
|
ChildFirstPluginClassLoader pluginClassLoader = new ChildFirstPluginClassLoader(urls, parent, plugin);
|
|
|
|
|
return initAndAppend(pluginClassLoader);
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-04 15:59:11 +01:00
|
|
|
public ClassLoader createPluginClassLoader(URL[] urls, ClassLoader parent, String plugin) {
|
|
|
|
|
LOG.debug("create new PluginClassLoader for {}", plugin);
|
|
|
|
|
DefaultPluginClassLoader pluginClassLoader = new DefaultPluginClassLoader(urls, parent, plugin);
|
|
|
|
|
return initAndAppend(pluginClassLoader);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2019-06-25 09:49:52 +02:00
|
|
|
public void shutdown() {
|
2019-06-19 11:52:20 +02:00
|
|
|
LOG.info("shutdown classloader infrastructure");
|
2020-02-04 15:59:11 +01:00
|
|
|
shutdownClassLoaders();
|
2019-11-21 16:16:15 +01:00
|
|
|
|
|
|
|
|
bootstrapClassLoader.markAsShutdown();
|
2019-06-19 11:52:20 +02:00
|
|
|
bootstrapClassLoader = null;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-04 15:59:11 +01:00
|
|
|
protected abstract void shutdownClassLoaders();
|
2019-06-19 11:52:20 +02:00
|
|
|
}
|