rebuild plugin system

This commit is contained in:
Sebastian Sdorra
2010-12-01 14:26:29 +01:00
parent 11faa205e8
commit 3f1b6e1160
28 changed files with 1026 additions and 710 deletions

View File

@@ -0,0 +1,226 @@
/**
* 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;
//~--- non-JDK imports --------------------------------------------------------
import com.google.inject.Binder;
import com.google.inject.Module;
import com.google.inject.multibindings.Multibinder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.plugin.ext.Extension;
import sonia.scm.plugin.ext.ExtensionProcessor;
import sonia.scm.repository.RepositoryHandler;
import sonia.scm.security.EncryptionHandler;
import sonia.scm.web.security.Authenticator;
//~--- JDK imports ------------------------------------------------------------
import java.util.HashSet;
import java.util.Set;
/**
*
* @author Sebastian Sdorra
*/
public class BindingExtensionProcessor implements ExtensionProcessor
{
/** the logger for BindingExtensionProcessor */
private static final Logger logger =
LoggerFactory.getLogger(BindingExtensionProcessor.class);
//~--- constructors ---------------------------------------------------------
/**
* Constructs ...
*
*
*/
public BindingExtensionProcessor()
{
this.moduleSet = new HashSet<Module>();
this.extensions = new HashSet<Class<?>>();
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @param binder
*/
@SuppressWarnings("unchecked")
public void bindExtensions(Binder binder)
{
Multibinder<RepositoryHandler> repositoryHandlers =
Multibinder.newSetBinder(binder, RepositoryHandler.class);
for (Class extensionClass : extensions)
{
try
{
if (RepositoryHandler.class.isAssignableFrom(extensionClass))
{
if (logger.isInfoEnabled())
{
logger.info("bind RepositoryHandler {}", extensionClass.getName());
}
binder.bind(extensionClass);
repositoryHandlers.addBinding().to(extensionClass);
}
else if (EncryptionHandler.class.isAssignableFrom(extensionClass))
{
bind(binder, EncryptionHandler.class, extensionClass);
}
else if (Authenticator.class.isAssignableFrom(extensionClass))
{
bind(binder, Authenticator.class, extensionClass);
}
else
{
if (logger.isInfoEnabled())
{
logger.info("bind {}", extensionClass.getName());
}
binder.bind(extensionClass);
}
}
catch (Exception ex)
{
logger.error(ex.getMessage(), ex);
}
}
}
/**
* Method description
*
*
* @param extension
* @param extensionClass
*/
@Override
public void processExtension(Extension extension, Class extensionClass)
{
if (Module.class.isAssignableFrom(extensionClass))
{
if (logger.isInfoEnabled())
{
logger.info("add GuiceModule {}", extensionClass.getName());
}
addModuleClass(extensionClass);
}
else
{
extensions.add(extensionClass);
}
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @return
*/
public Set<Module> getModuleSet()
{
return moduleSet;
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @param extensionClass
*/
private void addModuleClass(Class<? extends Module> extensionClass)
{
try
{
Module module = extensionClass.newInstance();
moduleSet.add(module);
}
catch (Exception ex)
{
logger.error(ex.getMessage(), ex);
}
}
/**
* Method description
*
*
*
* @param binder
* @param type
* @param bindingType
* @param <T>
*
* @return
*/
private <T> void bind(Binder binder, Class<T> type,
Class<? extends T> bindingType)
{
if (logger.isDebugEnabled())
{
logger.debug("bind Authenticator {}", type.getName(),
bindingType.getName());
}
binder.bind(type).to(bindingType);
}
//~--- fields ---------------------------------------------------------------
/** Field description */
private Set<Class<?>> extensions;
/** Field description */
private Set<Module> moduleSet;
}

View File

@@ -40,29 +40,16 @@ import com.google.inject.Injector;
import com.google.inject.Module;
import com.google.inject.servlet.GuiceServletContextListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.plugin.DefaultPluginManager;
import sonia.scm.plugin.PluginManager;
import sonia.scm.repository.RepositoryManager;
import sonia.scm.user.UserManager;
import sonia.scm.util.Util;
import sonia.scm.web.plugin.SCMPlugin;
import sonia.scm.web.plugin.SCMPluginManager;
import sonia.scm.web.plugin.ScmWebPlugin;
import sonia.scm.web.plugin.ScmWebPluginContext;
import sonia.scm.web.security.Authenticator;
//~--- JDK imports ------------------------------------------------------------
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import javax.servlet.ServletContextEvent;
/**
*
@@ -71,77 +58,6 @@ import javax.servlet.ServletContextEvent;
public class ContextListener extends GuiceServletContextListener
{
/** the logger for ContextListener */
private static final Logger logger =
LoggerFactory.getLogger(ContextListener.class);
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @param servletContextEvent
*/
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent)
{
for (ScmWebPlugin plugin : webPluginSet)
{
plugin.contextDestroyed(webPluginContext);
}
super.contextDestroyed(servletContextEvent);
}
/**
* Method description
*
*
* @param servletContextEvent
*/
@Override
public void contextInitialized(ServletContextEvent servletContextEvent)
{
pluginManager = new SCMPluginManager();
try
{
pluginManager.load();
webPluginContext =
new ScmWebPluginContext(servletContextEvent.getServletContext());
for (SCMPlugin plugin : pluginManager.getPlugins())
{
try
{
webPluginSet.add(plugin.getWebPlugin().newInstance());
}
catch (InstantiationException ex)
{
logger.error(ex.getMessage(), ex);
}
catch (IllegalAccessException ex)
{
logger.error(ex.getMessage(), ex);
}
}
for (ScmWebPlugin plugin : webPluginSet)
{
plugin.contextInitialized(webPluginContext);
}
}
catch (IOException ex)
{
throw new RuntimeException(ex);
}
super.contextInitialized(servletContextEvent);
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
@@ -151,18 +67,19 @@ public class ContextListener extends GuiceServletContextListener
@Override
protected Injector getInjector()
{
List<Module> modules = new ArrayList<Module>();
PluginManager manager = new DefaultPluginManager();
BindingExtensionProcessor bindExtProcessor =
new BindingExtensionProcessor();
modules.add(new ScmServletModule(pluginManager, webPluginContext));
manager.processExtensions(bindExtProcessor);
Collection<Module> pluginModules = webPluginContext.getInjectModules();
ScmServletModule main = new ScmServletModule(manager, bindExtProcessor);
List<Module> moduleList =
new ArrayList<Module>(bindExtProcessor.getModuleSet());
if (Util.isNotEmpty(pluginModules))
{
modules.addAll(pluginModules);
}
moduleList.add(0, main);
Injector injector = Guice.createInjector(modules);
Injector injector = Guice.createInjector(moduleList);
// init RepositoryManager
injector.getInstance(RepositoryManager.class).init(SCMContext.getContext());
@@ -175,15 +92,4 @@ public class ContextListener extends GuiceServletContextListener
return injector;
}
//~--- fields ---------------------------------------------------------------
/** Field description */
private SCMPluginManager pluginManager;
/** Field description */
private ScmWebPluginContext webPluginContext;
/** Field description */
private Set<ScmWebPlugin> webPluginSet = new LinkedHashSet<ScmWebPlugin>();
}

View File

@@ -35,7 +35,6 @@ package sonia.scm;
//~--- non-JDK imports --------------------------------------------------------
import com.google.inject.multibindings.Multibinder;
import com.google.inject.servlet.ServletModule;
import org.slf4j.Logger;
@@ -46,20 +45,16 @@ import sonia.scm.cache.CacheManager;
import sonia.scm.cache.EhCacheManager;
import sonia.scm.config.ScmConfiguration;
import sonia.scm.filter.SecurityFilter;
import sonia.scm.plugin.PluginManager;
import sonia.scm.plugin.ScriptResourceServlet;
import sonia.scm.repository.RepositoryHandler;
import sonia.scm.repository.RepositoryManager;
import sonia.scm.repository.xml.XmlRepositoryManager;
import sonia.scm.security.EncryptionHandler;
import sonia.scm.security.MessageDigestEncryptionHandler;
import sonia.scm.security.SecurityContext;
import sonia.scm.user.UserManager;
import sonia.scm.user.xml.XmlUserManager;
import sonia.scm.util.DebugServlet;
import sonia.scm.util.Util;
import sonia.scm.web.plugin.SCMPlugin;
import sonia.scm.web.plugin.SCMPluginManager;
import sonia.scm.web.plugin.ScmWebPluginContext;
import sonia.scm.web.plugin.SecurityConfig;
import sonia.scm.web.security.Authenticator;
import sonia.scm.web.security.BasicSecurityContext;
import sonia.scm.web.security.WebSecurityContext;
@@ -74,14 +69,10 @@ import com.sun.jersey.spi.container.servlet.ServletContainer;
import java.io.File;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import javax.xml.bind.JAXB;
import sonia.scm.security.SecurityContext;
/**
*
@@ -131,15 +122,14 @@ public class ScmServletModule extends ServletModule
* Constructs ...
*
*
*
* @param pluginManager
* @param webPluginContext
* @param manager
* @param bindExtProcessor
*/
ScmServletModule(SCMPluginManager pluginManager,
ScmWebPluginContext webPluginContext)
ScmServletModule(PluginManager manager,
BindingExtensionProcessor bindExtProcessor)
{
this.pluginManager = pluginManager;
this.webPluginContext = webPluginContext;
this.pluginManager = manager;
this.bindExtProcessor = bindExtProcessor;
}
//~--- methods --------------------------------------------------------------
@@ -158,19 +148,18 @@ public class ScmServletModule extends ServletModule
ScmConfiguration config = getScmConfiguration(context);
bind(ScmConfiguration.class).toInstance(config);
// bind(EncryptionHandler.class).to(MessageDigestEncryptionHandler.class);
// bind(Authenticator.class).to(XmlAuthenticator.class);
bind(PluginManager.class).toInstance(pluginManager);
bind(EncryptionHandler.class).to(MessageDigestEncryptionHandler.class);
bind(Authenticator.class).to(XmlAuthenticator.class);
bind(SecurityContext.class).to(BasicSecurityContext.class);
bind(WebSecurityContext.class).to(BasicSecurityContext.class);
loadPlugins(pluginManager);
bindExtProcessor.bindExtensions(binder());
bind(CacheManager.class).to(EhCacheManager.class);
// bind(RepositoryManager.class).annotatedWith(Undecorated.class).to(
// BasicRepositoryManager.class);
bind(RepositoryManager.class).to(XmlRepositoryManager.class);
bind(UserManager.class).to(XmlUserManager.class);
bind(ScmWebPluginContext.class).toInstance(webPluginContext);
// filter(PATTERN_RESTAPI).through(LoggingFilter.class);
@@ -206,91 +195,6 @@ public class ScmServletModule extends ServletModule
serve(PATTERN_RESTAPI).with(GuiceContainer.class, params);
}
/**
* Method description
*
*
* @param repositoryHandlerBinder
* @param handlerSet
*/
private void bindRepositoryHandlers(
Multibinder<RepositoryHandler> repositoryHandlerBinder,
Set<Class<? extends RepositoryHandler>> handlerSet)
{
for (Class<? extends RepositoryHandler> handlerClass : handlerSet)
{
if (logger.isInfoEnabled())
{
logger.info("load RepositoryHandler {}", handlerClass.getName());
}
bind(handlerClass);
repositoryHandlerBinder.addBinding().to(handlerClass);
}
}
/**
* Method description
*
*
* @param pluginManager
*/
private void loadPlugins(SCMPluginManager pluginManager)
{
Set<SCMPlugin> pluginSet = pluginManager.getPlugins();
if (Util.isNotEmpty(pluginSet))
{
// repository handlers
Multibinder<RepositoryHandler> repositoryHandlerBinder =
Multibinder.newSetBinder(binder(), RepositoryHandler.class);
Set<Class<? extends RepositoryHandler>> repositoryHandlerSet =
new LinkedHashSet<Class<? extends RepositoryHandler>>();
// security stuff
Class<? extends EncryptionHandler> encryptionHandler =
MessageDigestEncryptionHandler.class;
Class<? extends Authenticator> authenticator = XmlAuthenticator.class;
for (SCMPlugin plugin : pluginSet)
{
Collection<Class<? extends RepositoryHandler>> pluginRepositoryHandlers =
plugin.getRepositoryHandlers();
if (Util.isNotEmpty(pluginRepositoryHandlers))
{
repositoryHandlerSet.addAll(pluginRepositoryHandlers);
}
SecurityConfig securityConfig = plugin.getSecurityConfig();
if (securityConfig != null)
{
Class<? extends EncryptionHandler> enc =
securityConfig.getEncryptionHandler();
if (enc != null)
{
encryptionHandler = enc;
}
Class<? extends Authenticator> auth =
securityConfig.getAuthenticator();
if (auth != null)
{
authenticator = auth;
}
}
}
bind(EncryptionHandler.class).to(encryptionHandler);
bind(Authenticator.class).to(authenticator);
bindRepositoryHandlers(repositoryHandlerBinder, repositoryHandlerSet);
}
}
//~--- get methods ----------------------------------------------------------
/**
@@ -329,8 +233,8 @@ public class ScmServletModule extends ServletModule
//~--- fields ---------------------------------------------------------------
/** Field description */
private SCMPluginManager pluginManager;
private BindingExtensionProcessor bindExtProcessor;
/** Field description */
private ScmWebPluginContext webPluginContext;
private PluginManager pluginManager;
}

View File

@@ -213,25 +213,27 @@ public class RepositoryResource extends AbstractResource<Repository>
*/
private void appendUrl(Repository repository)
{
StringBuilder url = new StringBuilder(request.getScheme());
url.append("://").append(configuration.getServername());
url.append(":").append(request.getLocalPort());
String ctxPath = request.getContextPath();
if (ctxPath.endsWith("/"))
{
ctxPath = ctxPath.substring(0, ctxPath.length() - 1);
}
url.append(ctxPath);
RepositoryHandler handler =
repositoryManager.getHandler(repository.getType());
url.append(handler.createResourcePath(repository));
repository.setUrl(url.toString());
if (handler != null)
{
StringBuilder url = new StringBuilder(request.getScheme());
url.append("://").append(configuration.getServername());
url.append(":").append(request.getLocalPort());
String ctxPath = request.getContextPath();
if (ctxPath.endsWith("/"))
{
ctxPath = ctxPath.substring(0, ctxPath.length() - 1);
}
url.append(ctxPath);
url.append(handler.createResourcePath(repository));
repository.setUrl(url.toString());
}
}
//~--- fields ---------------------------------------------------------------

View File

@@ -40,8 +40,6 @@ import com.google.inject.Singleton;
import sonia.scm.util.IOUtil;
import sonia.scm.util.Util;
import sonia.scm.web.plugin.ScmWebPluginContext;
import sonia.scm.web.plugin.WebResource;
//~--- JDK imports ------------------------------------------------------------
@@ -49,10 +47,9 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import javax.servlet.ServletException;
@@ -70,6 +67,20 @@ public class ScriptResourceServlet extends AbstractResourceServlet
/** Field description */
private static final long serialVersionUID = -5769146163848821050L;
//~--- constructors ---------------------------------------------------------
/**
* Constructs ...
*
*
* @param manager
*/
@Inject
public ScriptResourceServlet(PluginManager manager)
{
this.manager = manager;
}
//~--- methods --------------------------------------------------------------
/**
@@ -89,19 +100,13 @@ public class ScriptResourceServlet extends AbstractResourceServlet
"function sayPluginHello(){ alert('Plugin Hello !'); }".concat(
System.getProperty("line.separator")).getBytes());
Collection<WebResource> scriptResources =
webPluginContext.getScriptResources();
Collection<String> scriptResources = getScriptResources();
if (Util.isNotEmpty(scriptResources))
{
List<WebResource> resourceList =
new ArrayList<WebResource>(scriptResources);
Collections.sort(resourceList, new WebResourceComparator());
for (WebResource scriptResource : resourceList)
for (String resource : scriptResources)
{
appendResource(stream, scriptResource);
appendResource(stream, resource);
}
}
}
@@ -132,10 +137,10 @@ public class ScriptResourceServlet extends AbstractResourceServlet
* @throws IOException
* @throws ServletException
*/
private void appendResource(OutputStream stream, WebResource script)
private void appendResource(OutputStream stream, String script)
throws ServletException, IOException
{
InputStream input = script.getContent();
InputStream input = ScriptResourceServlet.class.getResourceAsStream(script);
if (input != null)
{
@@ -150,9 +155,54 @@ public class ScriptResourceServlet extends AbstractResourceServlet
}
}
/**
* Method description
*
*
* @param resources
* @param plugin
*/
private void processPlugin(Set<String> resources, Plugin plugin)
{
PluginResources pluginResources = plugin.getResources();
if (pluginResources != null)
{
Set<String> scriptResources = pluginResources.getScriptResources();
if (scriptResources != null)
{
resources.addAll(scriptResources);
}
}
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @return
*/
private Collection<String> getScriptResources()
{
Set<String> resources = new TreeSet<String>();
Collection<Plugin> plugins = manager.getPlugins();
if (plugins != null)
{
for (Plugin plugin : plugins)
{
processPlugin(resources, plugin);
}
}
return resources;
}
//~--- fields ---------------------------------------------------------------
/** Field description */
@Inject
private ScmWebPluginContext webPluginContext;
private PluginManager manager;
}

View File

@@ -1,73 +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.plugin;
//~--- non-JDK imports --------------------------------------------------------
import sonia.scm.web.plugin.WebResource;
//~--- JDK imports ------------------------------------------------------------
import java.io.Serializable;
import java.util.Comparator;
/**
*
* @author Sebastian Sdorra
*/
public class WebResourceComparator
implements Comparator<WebResource>, Serializable
{
/** Field description */
private static final long serialVersionUID = -5916499507958881372L;
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @param resource
* @param resource1
*
* @return
*/
@Override
public int compare(WebResource resource, WebResource resource1)
{
return resource.getId().compareTo(resource1.getId());
}
}