improve plugin management

This commit is contained in:
Sebastian Sdorra
2010-10-31 13:07:43 +01:00
parent eda1c353b9
commit 0a9ed23b28
18 changed files with 526 additions and 65 deletions

View File

@@ -9,6 +9,9 @@ package sonia.scm.web;
//~--- non-JDK imports --------------------------------------------------------
import sonia.scm.web.plugin.ClasspathWebResource;
import sonia.scm.web.plugin.ScmWebPlugin;
import sonia.scm.web.plugin.ScmWebPluginContext;
import com.google.inject.servlet.ServletModule;
import sonia.scm.web.filter.BasicAuthenticationFilter;

View File

@@ -10,8 +10,10 @@
<plugin-config>
<repository-handlers>
<handler>sonia.scm.repository.GitRepositoryHandler</handler>
</repository-handlers>
<web-plugin>sonia.scm.web.GitWebPlugin</web-plugin>
<repository-handlers>
<repository-handler>sonia.scm.repository.GitRepositoryHandler</repository-handler>
</repository-handlers>
</plugin-config>

View File

@@ -7,6 +7,10 @@
package sonia.scm.web;
import sonia.scm.web.plugin.ScmWebPluginContext;
import sonia.scm.web.plugin.ScmWebPlugin;
import sonia.scm.web.plugin.ClasspathWebResource;
/**
*
* @author Sebastian Sdorra

View File

@@ -10,8 +10,10 @@
<plugin-config>
<repository-handlers>
<handler>sonia.scm.repository.HgRepositoryHandler</handler>
</repository-handlers>
<web-plugin>sonia.scm.web.HgWebPlugin</web-plugin>
<repository-handlers>
<repository-handler>sonia.scm.repository.HgRepositoryHandler</repository-handler>
</repository-handlers>
</plugin-config>

View File

@@ -7,6 +7,10 @@
package sonia.scm.web;
import sonia.scm.web.plugin.ScmWebPlugin;
import sonia.scm.web.plugin.ScmWebPluginContext;
import sonia.scm.web.plugin.ClasspathWebResource;
/**
*
* @author Sebastian Sdorra

View File

@@ -10,8 +10,10 @@
<plugin-config>
<web-plugin>sonia.scm.web.SvnWebPlugin</web-plugin>
<repository-handlers>
<handler>sonia.scm.repository.SvnRepositoryHandler</handler>
<repository-handler>sonia.scm.repository.SvnRepositoryHandler</repository-handler>
</repository-handlers>
</plugin-config>

View File

@@ -5,7 +5,7 @@
package sonia.scm.web;
package sonia.scm.web.plugin;
//~--- JDK imports ------------------------------------------------------------

View File

@@ -5,7 +5,7 @@
package sonia.scm.plugin;
package sonia.scm.web.plugin;
//~--- non-JDK imports --------------------------------------------------------
@@ -82,10 +82,64 @@ public class SCMPlugin
return handlers;
}
/**
* Method description
*
*
* @return
*/
public SecurityConfig getSecurityConfig()
{
return securityConfig;
}
/**
* Method description
*
*
* @return
*/
public Class<? extends ScmWebPlugin> getWebPlugin()
{
return webPlugin;
}
//~--- set methods ----------------------------------------------------------
/**
* Method description
*
*
* @param securityConfig
*/
public void setSecurityConfig(SecurityConfig securityConfig)
{
this.securityConfig = securityConfig;
}
/**
* Method description
*
*
* @param webPlugin
*/
public void setWebPlugin(Class<? extends ScmWebPlugin> webPlugin)
{
this.webPlugin = webPlugin;
}
//~--- fields ---------------------------------------------------------------
/** Field description */
@XmlElementWrapper(name = "repository-handlers")
@XmlElement(name = "handler")
@XmlElement(name = "repository-handler")
private Set<Class<? extends RepositoryHandler>> handlers;
/** Field description */
@XmlElement(name = "security")
private SecurityConfig securityConfig;
/** Field description */
@XmlElement(name = "web-plugin")
private Class<? extends ScmWebPlugin> webPlugin;
}

View File

@@ -5,25 +5,21 @@
package sonia.scm.plugin;
package sonia.scm.web.plugin;
//~--- non-JDK imports --------------------------------------------------------
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.repository.RepositoryHandler;
import sonia.scm.util.Util;
//~--- JDK imports ------------------------------------------------------------
import java.io.IOException;
import java.net.URL;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;
import javax.xml.bind.JAXB;
@@ -42,17 +38,6 @@ public class SCMPluginManager
private static final Logger logger =
LoggerFactory.getLogger(SCMPluginManager.class);
//~--- constructors ---------------------------------------------------------
/**
* Constructs ...
*
*/
public SCMPluginManager()
{
repositoryHandlers = new HashSet<Class<? extends RepositoryHandler>>();
}
//~--- methods --------------------------------------------------------------
/**
@@ -104,9 +89,9 @@ public class SCMPluginManager
*
* @return
*/
public Set<Class<? extends RepositoryHandler>> getRepositoryHandlers()
public Set<SCMPlugin> getPlugins()
{
return repositoryHandlers;
return plugins;
}
//~--- methods --------------------------------------------------------------
@@ -122,13 +107,13 @@ public class SCMPluginManager
try
{
SCMPlugin plugin = JAXB.unmarshal(url, SCMPlugin.class);
Collection<Class<? extends RepositoryHandler>> handlers =
plugin.getHandlers();
if (Util.isNotEmpty(handlers))
if (logger.isInfoEnabled())
{
repositoryHandlers.addAll(handlers);
logger.info("load plugin {}", url.toExternalForm());
}
plugins.add(plugin);
}
catch (Exception ex)
{
@@ -139,5 +124,5 @@ public class SCMPluginManager
//~--- fields ---------------------------------------------------------------
/** Field description */
private Set<Class<? extends RepositoryHandler>> repositoryHandlers;
private Set<SCMPlugin> plugins = new LinkedHashSet<SCMPlugin>();
}

View File

@@ -5,7 +5,7 @@
package sonia.scm.web;
package sonia.scm.web.plugin;
/**
*

View File

@@ -5,7 +5,7 @@
package sonia.scm.web;
package sonia.scm.web.plugin;
//~--- non-JDK imports --------------------------------------------------------

View File

@@ -0,0 +1,85 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package sonia.scm.web.plugin;
//~--- non-JDK imports --------------------------------------------------------
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import sonia.scm.security.EncryptionHandler;
import sonia.scm.web.security.Authenticator;
//~--- JDK imports ------------------------------------------------------------
import javax.xml.bind.annotation.XmlElement;
/**
*
* @author Sebastian Sdorra
*/
@XmlAccessorType(XmlAccessType.FIELD)
public class SecurityConfig
{
/**
* Method description
*
*
* @return
*/
public Class<? extends Authenticator> getAuthenticator()
{
return authenticator;
}
/**
* Method description
*
*
* @return
*/
public Class<? extends EncryptionHandler> getEncryptionHandler()
{
return encryptionHandler;
}
//~--- set methods ----------------------------------------------------------
/**
* Method description
*
*
* @param authenticator
*/
public void setAuthenticator(Class<? extends Authenticator> authenticator)
{
this.authenticator = authenticator;
}
/**
* Method description
*
*
* @param encryptionHandler
*/
public void setEncryptionHandler(
Class<? extends EncryptionHandler> encryptionHandler)
{
this.encryptionHandler = encryptionHandler;
}
//~--- fields ---------------------------------------------------------------
/** Field description */
@XmlElement(name = "authenticator")
private Class<? extends Authenticator> authenticator;
/** Field description */
@XmlElement(name = "encryption-handler")
private Class<? extends EncryptionHandler> encryptionHandler;
}

View File

@@ -5,7 +5,7 @@
package sonia.scm.web;
package sonia.scm.web.plugin;
//~--- JDK imports ------------------------------------------------------------

View File

@@ -0,0 +1,228 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package sonia.test;
import java.io.File;
import java.io.IOException;
import java.util.Collection;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.JAXB;
import org.junit.Test;
import sonia.scm.ConfigChangedListener;
import sonia.scm.SCMContextProvider;
import sonia.scm.User;
import sonia.scm.repository.Repository;
import sonia.scm.repository.RepositoryException;
import sonia.scm.repository.RepositoryHandler;
import sonia.scm.repository.RepositoryType;
import sonia.scm.security.EncryptionHandler;
import sonia.scm.web.plugin.SCMPlugin;
import sonia.scm.web.plugin.SecurityConfig;
import sonia.scm.web.security.Authenticator;
/**
*
* @author Sebastian Sdorra
*/
public class PluginTest {
@Test
public void test()
{
SCMPlugin plugin = new SCMPlugin();
plugin.addHandler( DemoRepositoryHandler.class );
plugin.addHandler(OtherRepositoryHandler.class);
SecurityConfig config = new SecurityConfig();
config.setAuthenticator( DemoAuthenticator.class );
config.setEncryptionHandler( EncryptionHandler.class );
plugin.setSecurityConfig( config );
JAXB.marshal(plugin, new File("/tmp/test.xml"));
}
static class DemoAuthenticator implements Authenticator {
@Override
public User authenticate(HttpServletRequest request, HttpServletResponse response, String username, String password)
{
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void init(SCMContextProvider context)
{
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void close() throws IOException
{
throw new UnsupportedOperationException("Not supported yet.");
}
}
static class DemoEncryptionHander implements EncryptionHandler {
@Override
public String encrypt(String value)
{
throw new UnsupportedOperationException("Not supported yet.");
}
}
static class OtherRepositoryHandler implements RepositoryHandler {
@Override
public RepositoryType getType()
{
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public boolean isConfigured()
{
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void create(Repository object) throws RepositoryException, IOException
{
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void delete(Repository object) throws RepositoryException, IOException
{
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void modify(Repository object) throws RepositoryException, IOException
{
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void refresh(Repository object) throws RepositoryException, IOException
{
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public Repository get(String id)
{
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public Collection<Repository> getAll()
{
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void init(SCMContextProvider context)
{
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void close() throws IOException
{
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void addListener(ConfigChangedListener listener)
{
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void removeListener(ConfigChangedListener listener)
{
throw new UnsupportedOperationException("Not supported yet.");
}
}
static class DemoRepositoryHandler implements RepositoryHandler {
@Override
public RepositoryType getType()
{
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public boolean isConfigured()
{
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void create(Repository object) throws RepositoryException, IOException
{
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void delete(Repository object) throws RepositoryException, IOException
{
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void modify(Repository object) throws RepositoryException, IOException
{
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void refresh(Repository object) throws RepositoryException, IOException
{
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public Repository get(String id)
{
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public Collection<Repository> getAll()
{
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void init(SCMContextProvider context)
{
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void close() throws IOException
{
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void addListener(ConfigChangedListener listener)
{
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void removeListener(ConfigChangedListener listener)
{
throw new UnsupportedOperationException("Not supported yet.");
}
}
}

View File

@@ -14,20 +14,29 @@ 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.repository.RepositoryManager;
import sonia.scm.util.ServiceUtil;
import sonia.scm.util.Util;
import sonia.scm.web.ScmWebPlugin;
import sonia.scm.web.ScmWebPluginContext;
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;
import sonia.scm.web.security.Authenticator;
/**
*
@@ -36,6 +45,12 @@ import sonia.scm.web.security.Authenticator;
public class ContextListener extends GuiceServletContextListener
{
/** the logger for ContextListener */
private static final Logger logger =
LoggerFactory.getLogger(ContextListener.class);
//~--- methods --------------------------------------------------------------
/**
* Method description
*
@@ -45,9 +60,7 @@ public class ContextListener extends GuiceServletContextListener
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent)
{
List<ScmWebPlugin> plugins = ServiceUtil.getServices(ScmWebPlugin.class);
for (ScmWebPlugin plugin : plugins)
for (ScmWebPlugin plugin : webPluginSet)
{
plugin.contextDestroyed(webPluginContext);
}
@@ -64,14 +77,38 @@ public class ContextListener extends GuiceServletContextListener
@Override
public void contextInitialized(ServletContextEvent servletContextEvent)
{
webPluginContext =
new ScmWebPluginContext(servletContextEvent.getServletContext());
pluginManager = new SCMPluginManager();
List<ScmWebPlugin> plugins = ServiceUtil.getServices(ScmWebPlugin.class);
for (ScmWebPlugin plugin : plugins)
try
{
plugin.contextInitialized(webPluginContext);
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);
@@ -112,6 +149,12 @@ public class ContextListener extends GuiceServletContextListener
//~--- fields ---------------------------------------------------------------
/** Field description */
private SCMPluginManager pluginManager;
/** Field description */
private ScmWebPluginContext webPluginContext;
/** Field description */
private Set<ScmWebPlugin> webPluginSet = new LinkedHashSet<ScmWebPlugin>();
}

View File

@@ -20,7 +20,6 @@ import sonia.scm.cache.CacheManager;
import sonia.scm.cache.CacheRepositoryManagerDecorator;
import sonia.scm.cache.EhCacheManager;
import sonia.scm.filter.SecurityFilter;
import sonia.scm.plugin.SCMPluginManager;
import sonia.scm.plugin.ScriptResourceServlet;
import sonia.scm.repository.BasicRepositoryManager;
import sonia.scm.repository.RepositoryHandler;
@@ -28,7 +27,10 @@ import sonia.scm.repository.RepositoryManager;
import sonia.scm.security.EncryptionHandler;
import sonia.scm.security.MessageDigestEncryptionHandler;
import sonia.scm.util.DebugServlet;
import sonia.scm.web.ScmWebPluginContext;
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.security.Authenticator;
import sonia.scm.web.security.BasicSecurityContext;
import sonia.scm.web.security.SecurityContext;
@@ -43,8 +45,11 @@ import com.sun.jersey.spi.container.servlet.ServletContainer;
import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
/**
*
@@ -117,8 +122,6 @@ public class ScmServletModule extends ServletModule
bind(Authenticator.class).to(XmlAuthenticator.class);
bind(SecurityContext.class).to(BasicSecurityContext.class);
Multibinder<RepositoryHandler> repositoryHandlerBinder =
Multibinder.newSetBinder(binder(), RepositoryHandler.class);
SCMPluginManager pluginManager = new SCMPluginManager();
try
@@ -130,13 +133,7 @@ public class ScmServletModule extends ServletModule
logger.error(ex.getMessage(), ex);
}
for (Class<? extends RepositoryHandler> handler :
pluginManager.getRepositoryHandlers())
{
bind(handler);
repositoryHandlerBinder.addBinding().to(handler);
}
loadPlugins(pluginManager);
bind(CacheManager.class).to(EhCacheManager.class);
bind(RepositoryManager.class).annotatedWith(Undecorated.class).to(
BasicRepositoryManager.class);
@@ -175,6 +172,61 @@ 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))
{
Multibinder<RepositoryHandler> repositoryHandlerBinder =
Multibinder.newSetBinder(binder(), RepositoryHandler.class);
Set<Class<? extends RepositoryHandler>> handlerSet =
new LinkedHashSet<Class<? extends RepositoryHandler>>();
for (SCMPlugin plugin : pluginSet)
{
Collection<Class<? extends RepositoryHandler>> handlers =
plugin.getHandlers();
if (Util.isNotEmpty(handlers))
{
handlerSet.addAll(handlers);
}
}
bindRepositoryHandlers(repositoryHandlerBinder, handlerSet);
}
}
//~--- fields ---------------------------------------------------------------
/** Field description */

View File

@@ -14,8 +14,8 @@ import com.google.inject.Singleton;
import sonia.scm.util.IOUtil;
import sonia.scm.util.Util;
import sonia.scm.web.ScmWebPluginContext;
import sonia.scm.web.WebResource;
import sonia.scm.web.plugin.ScmWebPluginContext;
import sonia.scm.web.plugin.WebResource;
//~--- JDK imports ------------------------------------------------------------

View File

@@ -71,9 +71,6 @@ public class XmlAuthenticator implements Authenticator
String encryptedPassword = encryptionHandler.encrypt(password);
System.out.println(encryptedPassword);
System.out.println(user.getPassword());
if (!encryptedPassword.equalsIgnoreCase(user.getPassword()))
{
user = null;