mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-11 16:05:44 +01:00
start implementation of isolated classloaders
This commit is contained in:
@@ -0,0 +1,263 @@
|
||||
/**
|
||||
* Copyright (c) 2010, Sebastian Sdorra All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer. 2. Redistributions in
|
||||
* binary form must reproduce the above copyright notice, this list of
|
||||
* conditions and the following disclaimer in the documentation and/or other
|
||||
* materials provided with the distribution. 3. Neither the name of SCM-Manager;
|
||||
* nor the names of its contributors may be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
|
||||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* http://bitbucket.org/sdorra/scm-manager
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
|
||||
package sonia.scm.boot;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import com.google.common.base.Charsets;
|
||||
import com.google.common.base.Strings;
|
||||
import com.google.common.io.Resources;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import sonia.scm.SCMContext;
|
||||
import sonia.scm.ScmContextListener;
|
||||
import sonia.scm.plugin.PluginException;
|
||||
import sonia.scm.plugin.PluginLoadException;
|
||||
import sonia.scm.plugin.PluginWrapper;
|
||||
import sonia.scm.plugin.Plugins;
|
||||
import sonia.scm.util.ClassLoaders;
|
||||
import sonia.scm.util.IOUtil;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
|
||||
import java.net.URL;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.ServletContextEvent;
|
||||
import javax.servlet.ServletContextListener;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
public class BootstrapContextListener implements ServletContextListener
|
||||
{
|
||||
|
||||
/** Field description */
|
||||
private static final String DIRECTORY_PLUGINS = "plugins";
|
||||
|
||||
/** Field description */
|
||||
private static final String PLUGIN_DIRECTORY = "/WEB-INF/plugins/";
|
||||
|
||||
/**
|
||||
* the logger for BootstrapContextListener
|
||||
*/
|
||||
private static final Logger logger =
|
||||
LoggerFactory.getLogger(BootstrapContextListener.class);
|
||||
|
||||
/** Field description */
|
||||
private static final String PLUGIN_COREINDEX =
|
||||
PLUGIN_DIRECTORY.concat("plugin.idx");
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param sce
|
||||
*/
|
||||
@Override
|
||||
public void contextDestroyed(ServletContextEvent sce)
|
||||
{
|
||||
contextListener.contextDestroyed(sce);
|
||||
|
||||
for (PluginWrapper plugin : contextListener.getPlugins())
|
||||
{
|
||||
ClassLoader pcl = plugin.getClassLoader();
|
||||
|
||||
if (pcl instanceof Closeable)
|
||||
{
|
||||
try
|
||||
{
|
||||
((Closeable) pcl).close();
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
logger.warn("could not close plugin classloader", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
contextListener = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param sce
|
||||
*/
|
||||
@Override
|
||||
public void contextInitialized(ServletContextEvent sce)
|
||||
{
|
||||
ServletContext context = sce.getServletContext();
|
||||
List<String> lines = readCorePluginIndex(context);
|
||||
|
||||
File pluginDirectory = getPluginDirectory();
|
||||
|
||||
copyCorePlugins(context, pluginDirectory, lines);
|
||||
|
||||
ClassLoader cl =
|
||||
ClassLoaders.getContextClassLoader(BootstrapContextListener.class);
|
||||
|
||||
try
|
||||
{
|
||||
Set<PluginWrapper> plugins = Plugins.collectPlugins(cl,
|
||||
pluginDirectory.toPath());
|
||||
|
||||
contextListener = new ScmContextListener(cl, plugins);
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
throw new PluginLoadException("could not load plugins", ex);
|
||||
}
|
||||
|
||||
contextListener.contextInitialized(sce);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param context
|
||||
* @param pluginDirectory
|
||||
* @param name
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
private void copyCorePlugin(ServletContext context, File pluginDirectory,
|
||||
String name)
|
||||
throws IOException
|
||||
{
|
||||
URL url = context.getResource(PLUGIN_DIRECTORY.concat(name));
|
||||
File file = new File(pluginDirectory, name);
|
||||
|
||||
try (OutputStream output = new FileOutputStream(file))
|
||||
{
|
||||
Resources.copy(url, output);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param context
|
||||
* @param pluginDirectory
|
||||
* @param lines
|
||||
*/
|
||||
private void copyCorePlugins(ServletContext context, File pluginDirectory,
|
||||
List<String> lines)
|
||||
{
|
||||
IOUtil.mkdirs(pluginDirectory);
|
||||
|
||||
for (String line : lines)
|
||||
{
|
||||
line = line.trim();
|
||||
|
||||
if (!Strings.isNullOrEmpty(line))
|
||||
{
|
||||
try
|
||||
{
|
||||
copyCorePlugin(context, pluginDirectory, line);
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
logger.error("could not copy core plugin", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param context
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private List<String> readCorePluginIndex(ServletContext context)
|
||||
{
|
||||
List<String> lines;
|
||||
|
||||
try
|
||||
{
|
||||
URL index = context.getResource(PLUGIN_COREINDEX);
|
||||
|
||||
if (index == null)
|
||||
{
|
||||
throw new PluginException("no core plugin index found");
|
||||
}
|
||||
|
||||
lines = Resources.readLines(index, Charsets.UTF_8);
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
throw new PluginException("could not load core plugin index", ex);
|
||||
}
|
||||
|
||||
return lines;
|
||||
}
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private File getPluginDirectory()
|
||||
{
|
||||
File baseDirectory = SCMContext.getContext().getBaseDirectory();
|
||||
|
||||
return new File(baseDirectory, DIRECTORY_PLUGINS);
|
||||
}
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
private ScmContextListener contextListener;
|
||||
}
|
||||
@@ -1,164 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2010, Sebastian Sdorra
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of SCM-Manager; nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* http://bitbucket.org/sdorra/scm-manager
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
|
||||
package sonia.scm.boot;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.servlet.Filter;
|
||||
import javax.servlet.FilterChain;
|
||||
import javax.servlet.FilterConfig;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletRequest;
|
||||
import javax.servlet.ServletResponse;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
public class BootstrapFilter implements Filter
|
||||
{
|
||||
|
||||
/** Field description */
|
||||
public static final String FILTER = "com.google.inject.servlet.GuiceFilter";
|
||||
|
||||
/** the logger for BootstrapFilter */
|
||||
private static final Logger logger =
|
||||
LoggerFactory.getLogger(BootstrapFilter.class);
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*/
|
||||
@Override
|
||||
public void destroy()
|
||||
{
|
||||
ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
|
||||
|
||||
try
|
||||
{
|
||||
if (classLoader != null)
|
||||
{
|
||||
Thread.currentThread().setContextClassLoader(classLoader);
|
||||
}
|
||||
|
||||
logger.debug("destroy guice filter");
|
||||
|
||||
guiceFilter.destroy();
|
||||
}
|
||||
finally
|
||||
{
|
||||
Thread.currentThread().setContextClassLoader(oldClassLoader);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param request
|
||||
* @param response
|
||||
* @param chain
|
||||
*
|
||||
* @throws IOException
|
||||
* @throws ServletException
|
||||
*/
|
||||
@Override
|
||||
public void doFilter(ServletRequest request, ServletResponse response,
|
||||
FilterChain chain)
|
||||
throws IOException, ServletException
|
||||
{
|
||||
ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
|
||||
|
||||
try
|
||||
{
|
||||
if (classLoader != null)
|
||||
{
|
||||
Thread.currentThread().setContextClassLoader(classLoader);
|
||||
}
|
||||
|
||||
guiceFilter.doFilter(request, response, chain);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Thread.currentThread().setContextClassLoader(oldClassLoader);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param filterConfig
|
||||
*
|
||||
* @throws ServletException
|
||||
*/
|
||||
@Override
|
||||
public void init(FilterConfig filterConfig) throws ServletException
|
||||
{
|
||||
classLoader =
|
||||
BootstrapUtil.getClassLoader(filterConfig.getServletContext());
|
||||
|
||||
if (classLoader != null)
|
||||
{
|
||||
logger.info("loading GuiceFilter with ScmBootstrapClassLoader");
|
||||
Thread.currentThread().setContextClassLoader(classLoader);
|
||||
guiceFilter = BootstrapUtil.loadClass(classLoader, Filter.class, FILTER);
|
||||
}
|
||||
|
||||
if (guiceFilter == null)
|
||||
{
|
||||
logger.info("fallback to default classloader for GuiceFilter");
|
||||
guiceFilter = BootstrapUtil.loadClass(Filter.class, FILTER);
|
||||
}
|
||||
|
||||
guiceFilter.init(filterConfig);
|
||||
}
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
private ClassLoader classLoader;
|
||||
|
||||
/** Field description */
|
||||
private Filter guiceFilter;
|
||||
}
|
||||
@@ -1,302 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2010, Sebastian Sdorra
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of SCM-Manager; nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* http://bitbucket.org/sdorra/scm-manager
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
|
||||
package sonia.scm.boot;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import sonia.scm.SCMContext;
|
||||
import sonia.scm.SCMContextProvider;
|
||||
import sonia.scm.util.ClassLoaders;
|
||||
import sonia.scm.util.IOUtil;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.File;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.ServletContextEvent;
|
||||
import javax.servlet.ServletContextListener;
|
||||
|
||||
import javax.xml.bind.JAXB;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
public class BootstrapListener implements ServletContextListener
|
||||
{
|
||||
|
||||
/** Field description */
|
||||
public static final String LISTENER = "sonia.scm.ScmContextListener";
|
||||
|
||||
/** Field description */
|
||||
public static final String PLUGIN_CLASSPATHFILE = "classpath.xml";
|
||||
|
||||
/** Field description */
|
||||
public static final String PLUGIN_DIRECTORY = "plugins";
|
||||
|
||||
/** the logger for BootstrapListener */
|
||||
private static final Logger logger =
|
||||
LoggerFactory.getLogger(BootstrapListener.class);
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param sce
|
||||
*/
|
||||
@Override
|
||||
public void contextDestroyed(ServletContextEvent sce)
|
||||
{
|
||||
if (scmContextListener != null)
|
||||
{
|
||||
logger.info("destroy scm context listener");
|
||||
scmContextListener.contextDestroyed(sce);
|
||||
}
|
||||
|
||||
ServletContext servletContext = sce.getServletContext();
|
||||
ClassLoader classLoader = BootstrapUtil.getClassLoader(servletContext);
|
||||
|
||||
if (classLoader != null)
|
||||
{
|
||||
if (classLoader instanceof Closeable)
|
||||
{
|
||||
logger.info("close plugin class loader");
|
||||
IOUtil.close((Closeable) classLoader);
|
||||
}
|
||||
|
||||
logger.debug("remove plugin class loader from servlet context");
|
||||
BootstrapUtil.removeClassLoader(servletContext);
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.debug("plugin class loader is not available");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param sce
|
||||
*/
|
||||
@Override
|
||||
public void contextInitialized(ServletContextEvent sce)
|
||||
{
|
||||
SCMContextProvider context = SCMContext.getContext();
|
||||
|
||||
if (logger.isInfoEnabled())
|
||||
{
|
||||
logger.info("start scm-manager {} in stage: {}", context.getVersion(),
|
||||
context.getStage());
|
||||
}
|
||||
|
||||
ClassLoader classLoader = createClassLoader(context);
|
||||
|
||||
if (classLoader != null)
|
||||
{
|
||||
if (logger.isInfoEnabled())
|
||||
{
|
||||
logger.info("try to use ScmBootstrapClassLoader");
|
||||
}
|
||||
|
||||
scmContextListener = BootstrapUtil.loadClass(classLoader,
|
||||
ServletContextListener.class, LISTENER);
|
||||
BootstrapUtil.setClassLoader(sce.getServletContext(), classLoader);
|
||||
}
|
||||
|
||||
if (scmContextListener == null)
|
||||
{
|
||||
if (logger.isWarnEnabled())
|
||||
{
|
||||
logger.warn("fallback to default classloader");
|
||||
}
|
||||
|
||||
scmContextListener =
|
||||
BootstrapUtil.loadClass(ServletContextListener.class, LISTENER);
|
||||
}
|
||||
|
||||
initializeContext(classLoader, scmContextListener, sce);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param context
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private ClassLoader createClassLoader(SCMContextProvider context)
|
||||
{
|
||||
ClassLoader classLoader = null;
|
||||
File pluginDirectory = new File(context.getBaseDirectory(),
|
||||
PLUGIN_DIRECTORY);
|
||||
|
||||
if (pluginDirectory.exists())
|
||||
{
|
||||
File classpathFile = new File(pluginDirectory, PLUGIN_CLASSPATHFILE);
|
||||
|
||||
if (classpathFile.exists())
|
||||
{
|
||||
try
|
||||
{
|
||||
Classpath classpath = JAXB.unmarshal(classpathFile, Classpath.class);
|
||||
|
||||
if (classpath != null)
|
||||
{
|
||||
classLoader = createClassLoader(pluginDirectory, classpath);
|
||||
}
|
||||
else if (logger.isErrorEnabled())
|
||||
{
|
||||
logger.error("classloader is null");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.error("could not load classpath from plugin folder", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (logger.isDebugEnabled())
|
||||
{
|
||||
logger.debug("no plugin directory found");
|
||||
}
|
||||
|
||||
return classLoader;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param pluginDirectory
|
||||
* @param classpath
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private ClassLoader createClassLoader(File pluginDirectory,
|
||||
Classpath classpath)
|
||||
{
|
||||
if (logger.isDebugEnabled())
|
||||
{
|
||||
logger.debug("create classloader from plugin classpath");
|
||||
}
|
||||
|
||||
List<URL> classpathURLs = Lists.newLinkedList();
|
||||
|
||||
for (String path : classpath)
|
||||
{
|
||||
if (path.startsWith("/"))
|
||||
{
|
||||
path = path.substring(1);
|
||||
}
|
||||
|
||||
File file = new File(pluginDirectory, path);
|
||||
|
||||
if (file.exists())
|
||||
{
|
||||
try
|
||||
{
|
||||
URL url = file.toURI().toURL();
|
||||
|
||||
if (logger.isDebugEnabled())
|
||||
{
|
||||
logger.debug("append {} to classpath", url.toExternalForm());
|
||||
}
|
||||
|
||||
classpathURLs.add(url);
|
||||
}
|
||||
catch (MalformedURLException ex)
|
||||
{
|
||||
logger.error("could not append url to classpath", ex);
|
||||
}
|
||||
}
|
||||
else if (logger.isErrorEnabled())
|
||||
{
|
||||
logger.error("plugin file {} does not exists", file);
|
||||
}
|
||||
}
|
||||
|
||||
return BootstrapUtil.createClassLoader(classpathURLs,
|
||||
ClassLoaders.getContextClassLoader(BootstrapListener.class));
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param classLoader
|
||||
* @param listener
|
||||
* @param sce
|
||||
*/
|
||||
private void initializeContext(ClassLoader classLoader,
|
||||
ServletContextListener listener, ServletContextEvent sce)
|
||||
{
|
||||
ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
|
||||
|
||||
try
|
||||
{
|
||||
if (classLoader != null)
|
||||
{
|
||||
Thread.currentThread().setContextClassLoader(classLoader);
|
||||
}
|
||||
|
||||
logger.info("initialize scm context listener");
|
||||
listener.contextInitialized(sce);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Thread.currentThread().setContextClassLoader(oldClassLoader);
|
||||
}
|
||||
}
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
private ServletContextListener scmContextListener;
|
||||
}
|
||||
@@ -1,254 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2010, Sebastian Sdorra
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of SCM-Manager; nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* http://bitbucket.org/sdorra/scm-manager
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
|
||||
package sonia.scm.boot;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import com.google.common.base.Strings;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import sonia.scm.plugin.ChildFirstPluginClassLoader;
|
||||
import sonia.scm.plugin.DefaultPluginClassLoader;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.net.URL;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
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);
|
||||
|
||||
//~--- constructors ---------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Constructs ...
|
||||
*
|
||||
*/
|
||||
private 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 ChildFirstPluginClassLoader(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 DefaultPluginClassLoader(urls, parent);
|
||||
}
|
||||
|
||||
return classLoader;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param classLoader
|
||||
* @param clazz
|
||||
* @param <T>
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> T loadClass(ClassLoader classLoader, Class<T> clazz)
|
||||
{
|
||||
T instance = null;
|
||||
|
||||
try
|
||||
{
|
||||
instance = (T) classLoader.loadClass(clazz.getName()).newInstance();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.error("could not load class ".concat(clazz.getName()), ex);
|
||||
}
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param classLoader
|
||||
* @param clazz
|
||||
* @param className
|
||||
* @param <T>
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> T loadClass(ClassLoader classLoader, Class<T> clazz,
|
||||
String className)
|
||||
{
|
||||
T instance = null;
|
||||
|
||||
try
|
||||
{
|
||||
instance = (T) classLoader.loadClass(className).newInstance();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.error("could not load class ".concat(className), ex);
|
||||
}
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
*
|
||||
* @param clazz
|
||||
* @param className
|
||||
* @param <T>
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> T loadClass(Class<T> clazz, String className)
|
||||
{
|
||||
T instance = null;
|
||||
|
||||
try
|
||||
{
|
||||
instance = (T) Class.forName(className).newInstance();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.error("could not load class ".concat(className), ex);
|
||||
}
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param context
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static ClassLoader getClassLoader(ServletContext context)
|
||||
{
|
||||
return (ClassLoader) context.getAttribute(CLASSLOADER);
|
||||
}
|
||||
|
||||
//~--- set methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param context
|
||||
* @param classLoader
|
||||
*/
|
||||
public static void setClassLoader(ServletContext context,
|
||||
ClassLoader classLoader)
|
||||
{
|
||||
context.setAttribute(CLASSLOADER, classLoader);
|
||||
}
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param context
|
||||
*/
|
||||
public static void removeClassLoader(ServletContext context)
|
||||
{
|
||||
context.removeAttribute(CLASSLOADER);
|
||||
}
|
||||
}
|
||||
@@ -1,131 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2010, Sebastian Sdorra
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of SCM-Manager; nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* http://bitbucket.org/sdorra/scm-manager
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
|
||||
package sonia.scm.boot;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.xml.bind.annotation.XmlAccessType;
|
||||
import javax.xml.bind.annotation.XmlAccessorType;
|
||||
import javax.xml.bind.annotation.XmlElement;
|
||||
import javax.xml.bind.annotation.XmlRootElement;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
@XmlRootElement(name = "classpath")
|
||||
@XmlAccessorType(XmlAccessType.FIELD)
|
||||
public class Classpath implements Iterable<String>
|
||||
{
|
||||
|
||||
/**
|
||||
* Constructs ...
|
||||
*
|
||||
*/
|
||||
public Classpath() {}
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param path
|
||||
*/
|
||||
public void add(String path)
|
||||
{
|
||||
pathSet.add(path);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param file
|
||||
*/
|
||||
public void add(File file)
|
||||
{
|
||||
pathSet.add(file.getPath());
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Iterator<String> iterator()
|
||||
{
|
||||
return pathSet.iterator();
|
||||
}
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Set<String> getPathSet()
|
||||
{
|
||||
return pathSet;
|
||||
}
|
||||
|
||||
//~--- set methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param pathSet
|
||||
*/
|
||||
public void setPathSet(Set<String> pathSet)
|
||||
{
|
||||
this.pathSet = pathSet;
|
||||
}
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
@XmlElement(name = "path")
|
||||
private Set<String> pathSet = new LinkedHashSet<String>();
|
||||
}
|
||||
Reference in New Issue
Block a user