merge with branch 1.x

This commit is contained in:
Sebastian Sdorra
2014-06-04 15:51:19 +02:00
42 changed files with 1689 additions and 102 deletions

View File

@@ -71,6 +71,7 @@ import sonia.scm.plugin.PluginManager;
import sonia.scm.repository.DefaultRepositoryManager;
import sonia.scm.repository.DefaultRepositoryProvider;
import sonia.scm.repository.HealthCheckContextListener;
import sonia.scm.repository.LastModifiedUpdateListener;
import sonia.scm.repository.Repository;
import sonia.scm.repository.RepositoryDAO;
import sonia.scm.repository.RepositoryManager;
@@ -141,6 +142,7 @@ import com.sun.jersey.spi.container.servlet.ServletContainer;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
*
@@ -356,10 +358,12 @@ public class ScmServletModule extends JerseyServletModule
bind(TemplateEngine.class).annotatedWith(DefaultEngine.class).to(
MustacheTemplateEngine.class);
bind(TemplateEngineFactory.class);
// bind events
bind(LastModifiedUpdateListener.class);
// jersey
Map<String, String> params = new HashMap<String, String>();
/*
* params.put("com.sun.jersey.spi.container.ContainerRequestFilters",
* "com.sun.jersey.api.container.filter.LoggingFilter");
@@ -370,14 +374,14 @@ public class ScmServletModule extends JerseyServletModule
*/
params.put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE.toString());
params.put(ResourceConfig.FEATURE_REDIRECT, Boolean.TRUE.toString());
params.put(ResourceConfig.FEATURE_DISABLE_WADL, Boolean.TRUE.toString());
/*
* TODO remove UriExtensionsConfig and PackagesResourceConfig
* to stop jersey classpath scanning
*/
params.put(ServletContainer.RESOURCE_CONFIG_CLASS,
UriExtensionsConfig.class.getName());
UriExtensionsConfig.class.getName());
params.put(PackagesResourceConfig.PROPERTY_PACKAGES, "unbound");
serve(PATTERN_RESTAPI).with(GuiceContainer.class, params);
}

View File

@@ -30,18 +30,30 @@
*/
package sonia.scm.api.rest;
//~--- JDK imports ------------------------------------------------------------
import javax.xml.bind.annotation.XmlRootElement;
/**
*
* @author Sebastian Sdorra
*/
@XmlRootElement(name="result")
@XmlRootElement(name = "result")
public class RestActionUploadResult extends RestActionResult
{
/**
* Constructs ...
*
*/
public RestActionUploadResult()
{
this(true);
}
/**
* Constructs ...
*

View File

@@ -196,7 +196,7 @@ public abstract class AbstractManagerResource<T extends ModelObject,
}
catch (Exception ex)
{
logger.error("error during create", ex);
logger.error("error during delete", ex);
response = createErrorResonse(ex);
}
}
@@ -230,7 +230,7 @@ public abstract class AbstractManagerResource<T extends ModelObject,
}
catch (ScmSecurityException ex)
{
logger.warn("delete not allowd", ex);
logger.warn("update not allowd", ex);
response = Response.status(Response.Status.FORBIDDEN).build();
}
catch (Exception ex)

View File

@@ -72,12 +72,23 @@ public class BootstrapFilter implements Filter
@Override
public void destroy()
{
if (classLoader != null)
{
Thread.currentThread().setContextClassLoader(classLoader);
}
ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
guiceFilter.destroy();
try
{
if (classLoader != null)
{
Thread.currentThread().setContextClassLoader(classLoader);
}
logger.debug("destroy guice filter");
guiceFilter.destroy();
}
finally
{
Thread.currentThread().setContextClassLoader(oldClassLoader);
}
}
/**
@@ -93,15 +104,24 @@ public class BootstrapFilter implements Filter
*/
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException
FilterChain chain)
throws IOException, ServletException
{
if (classLoader != null)
{
Thread.currentThread().setContextClassLoader(classLoader);
}
ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
guiceFilter.doFilter(request, response, chain);
try
{
if (classLoader != null)
{
Thread.currentThread().setContextClassLoader(classLoader);
}
guiceFilter.doFilter(request, response, chain);
}
finally
{
Thread.currentThread().setContextClassLoader(oldClassLoader);
}
}
/**

View File

@@ -35,22 +35,27 @@ 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.LinkedList;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
@@ -89,8 +94,28 @@ public class BootstrapListener implements ServletContextListener
{
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");
}
}
/**
@@ -110,6 +135,44 @@ public class BootstrapListener implements ServletContextListener
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);
@@ -144,31 +207,7 @@ public class BootstrapListener implements ServletContextListener
logger.debug("no plugin directory found");
}
if (classLoader != null)
{
if (logger.isInfoEnabled())
{
logger.info("try to use ScmBootstrapClassLoader");
}
scmContextListener = BootstrapUtil.loadClass(classLoader,
ServletContextListener.class, LISTENER);
Thread.currentThread().setContextClassLoader(classLoader);
BootstrapUtil.setClassLoader(sce.getServletContext(), classLoader);
}
if (scmContextListener == null)
{
if (logger.isWarnEnabled())
{
logger.warn("fallback to default classloader");
}
scmContextListener =
BootstrapUtil.loadClass(ServletContextListener.class, LISTENER);
}
scmContextListener.contextInitialized(sce);
return classLoader;
}
/**
@@ -188,7 +227,7 @@ public class BootstrapListener implements ServletContextListener
logger.debug("create classloader from plugin classpath");
}
List<URL> classpathURLs = new LinkedList<URL>();
List<URL> classpathURLs = Lists.newLinkedList();
for (String path : classpath)
{
@@ -224,32 +263,36 @@ public class BootstrapListener implements ServletContextListener
}
return BootstrapUtil.createClassLoader(classpathURLs,
getParentClassLoader());
ClassLoaders.getContextClassLoader(BootstrapListener.class));
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @return
* @param classLoader
* @param listener
* @param sce
*/
private ClassLoader getParentClassLoader()
private void initializeContext(ClassLoader classLoader,
ServletContextListener listener, ServletContextEvent sce)
{
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
if (classLoader == null)
try
{
if (logger.isWarnEnabled())
if (classLoader != null)
{
logger.warn("could not use context classloader, try to use default");
Thread.currentThread().setContextClassLoader(classLoader);
}
classLoader = BootstrapListener.class.getClassLoader();
logger.info("initialize scm context listener");
listener.contextInitialized(sce);
}
finally
{
Thread.currentThread().setContextClassLoader(oldClassLoader);
}
return classLoader;
}
//~--- fields ---------------------------------------------------------------

View File

@@ -40,12 +40,12 @@ import com.google.common.base.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.net.ChildFirstURLClassLoader;
import sonia.scm.plugin.ChildFirstPluginClassLoader;
import sonia.scm.plugin.DefaultPluginClassLoader;
//~--- JDK imports ------------------------------------------------------------
import java.net.URL;
import java.net.URLClassLoader;
import java.util.List;
@@ -107,7 +107,7 @@ public final class BootstrapUtil
{
logger.info("using {} as plugin classloading strategy",
STRATEGY_CHILDFIRST);
classLoader = new ChildFirstURLClassLoader(urls, parent);
classLoader = new ChildFirstPluginClassLoader(urls, parent);
}
else if (!STRATEGY_PARENTFIRST.equals(strategy))
{
@@ -119,7 +119,7 @@ public final class BootstrapUtil
{
logger.info("using {} as plugin classloading strategy",
STRATEGY_PARENTFIRST);
classLoader = new URLClassLoader(urls, parent);
classLoader = new DefaultPluginClassLoader(urls, parent);
}
return classLoader;
@@ -238,4 +238,17 @@ public final class BootstrapUtil
{
context.setAttribute(CLASSLOADER, classLoader);
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @param context
*/
public static void removeClassLoader(ServletContext context)
{
context.removeAttribute(CLASSLOADER);
}
}

View File

@@ -0,0 +1,73 @@
/**
* 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.plugin;
//~--- non-JDK imports --------------------------------------------------------
import sonia.scm.net.ChildFirstURLClassLoader;
//~--- JDK imports ------------------------------------------------------------
import java.net.URL;
/**
* Child first {@link ClassLoader} for SCM-Manager plugins.
*
* @author Sebastian Sdorra
*/
public class ChildFirstPluginClassLoader extends ChildFirstURLClassLoader
implements PluginClassLoader
{
/**
* Constructs ...
*
*
* @param urls
*/
public ChildFirstPluginClassLoader(URL[] urls)
{
super(urls);
}
/**
* Constructs ...
*
*
* @param urls
* @param parent
*/
public ChildFirstPluginClassLoader(URL[] urls, ClassLoader parent)
{
super(urls, parent);
}
}

View File

@@ -0,0 +1,71 @@
/**
* 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.plugin;
//~--- JDK imports ------------------------------------------------------------
import java.net.URL;
import java.net.URLClassLoader;
/**
* Default {@link ClassLoader} for SCM-Manager plugins. This {@link ClassLoader}
* uses the default parent first strategy.
*
* @author Sebastian Sdorra
*/
public class DefaultPluginClassLoader extends URLClassLoader
implements PluginClassLoader
{
/**
* Constructs ...
*
*
* @param urls
*/
public DefaultPluginClassLoader(URL[] urls)
{
super(urls);
}
/**
* Constructs ...
*
*
* @param urls
* @param parent
*/
public DefaultPluginClassLoader(URL[] urls, ClassLoader parent)
{
super(urls, parent);
}
}

View File

@@ -0,0 +1,40 @@
/**
* 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.plugin;
/**
* The PluginClassLoader interface is mainly a marker to find the class loader
* in a memory dump. This should make it easier to find class loader leaks.
*
* @author Sebastian Sdorra
*/
public interface PluginClassLoader {}