imporve plugin system

This commit is contained in:
Sebastian Sdorra
2010-12-13 18:59:00 +01:00
parent ab00eabdd4
commit 2df3034309
22 changed files with 1010 additions and 59 deletions

View File

@@ -40,8 +40,8 @@ import com.google.inject.Injector;
import com.google.inject.Module;
import com.google.inject.servlet.GuiceServletContextListener;
import sonia.scm.plugin.DefaultPluginManager;
import sonia.scm.plugin.PluginManager;
import sonia.scm.plugin.DefaultPluginLoader;
import sonia.scm.plugin.PluginLoader;
import sonia.scm.repository.RepositoryManager;
import sonia.scm.store.StoreFactory;
import sonia.scm.user.UserManager;
@@ -101,13 +101,14 @@ public class ScmContextListener extends GuiceServletContextListener
@Override
protected Injector getInjector()
{
PluginManager manager = new DefaultPluginManager();
PluginLoader pluginLoader = new DefaultPluginLoader();
BindingExtensionProcessor bindExtProcessor =
new BindingExtensionProcessor();
manager.processExtensions(bindExtProcessor);
pluginLoader.processExtensions(bindExtProcessor);
ScmServletModule main = new ScmServletModule(manager, bindExtProcessor);
ScmServletModule main = new ScmServletModule(pluginLoader,
bindExtProcessor);
List<Module> moduleList =
new ArrayList<Module>(bindExtProcessor.getModuleSet());

View File

@@ -45,7 +45,9 @@ import sonia.scm.cache.CacheManager;
import sonia.scm.cache.EhCacheManager;
import sonia.scm.config.ScmConfiguration;
import sonia.scm.filter.SecurityFilter;
import sonia.scm.plugin.DefaultPluginManager;
import sonia.scm.plugin.Plugin;
import sonia.scm.plugin.PluginLoader;
import sonia.scm.plugin.PluginManager;
import sonia.scm.plugin.ScriptResourceServlet;
import sonia.scm.repository.RepositoryManager;
@@ -129,13 +131,13 @@ public class ScmServletModule extends ServletModule
* Constructs ...
*
*
* @param manager
* @param pluginLoader
* @param bindExtProcessor
*/
ScmServletModule(PluginManager manager,
ScmServletModule(PluginLoader pluginLoader,
BindingExtensionProcessor bindExtProcessor)
{
this.pluginManager = manager;
this.pluginLoader = pluginLoader;
this.bindExtProcessor = bindExtProcessor;
}
@@ -156,7 +158,8 @@ public class ScmServletModule extends ServletModule
bind(StoreFactory.class).to(JAXBStoreFactory.class);
bind(ScmConfiguration.class).toInstance(config);
bind(PluginManager.class).toInstance(pluginManager);
bind(PluginLoader.class).toInstance(pluginLoader);
bind(PluginManager.class).to(DefaultPluginManager.class);
bind(EncryptionHandler.class).to(MessageDigestEncryptionHandler.class);
bindExtProcessor.bindExtensions(binder());
@@ -276,7 +279,7 @@ public class ScmServletModule extends ServletModule
packageSet.add(SCMContext.DEFAULT_PACKAGE);
Collection<Plugin> plugins = pluginManager.getPlugins();
Collection<Plugin> plugins = pluginLoader.getInstalledPlugins();
if (plugins != null)
{
@@ -341,5 +344,5 @@ public class ScmServletModule extends ServletModule
private BindingExtensionProcessor bindExtProcessor;
/** Field description */
private PluginManager pluginManager;
private PluginLoader pluginLoader;
}

View File

@@ -0,0 +1,106 @@
/**
* 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.api.rest.resources;
//~--- non-JDK imports --------------------------------------------------------
import com.google.inject.Inject;
import com.google.inject.Singleton;
import sonia.scm.plugin.PluginInformation;
import sonia.scm.plugin.PluginManager;
//~--- JDK imports ------------------------------------------------------------
import java.util.Collection;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
/**
*
* @author Sebastian Sdorra
*/
@Singleton
@Path("plugins/available")
public class AvailablePluginResource
{
/**
* Constructs ...
*
*
* @param pluginManager
*/
@Inject
public AvailablePluginResource(PluginManager pluginManager)
{
this.pluginManager = pluginManager;
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @return
*/
@GET
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public PluginInformation[] getAvailablePlugins()
{
Collection<PluginInformation> pluginCollection =
pluginManager.getAvailable();
PluginInformation[] plugins = null;
if (pluginCollection != null)
{
plugins = pluginCollection.toArray(new PluginInformation[0]);
}
else
{
plugins = new PluginInformation[0];
}
return plugins;
}
//~--- fields ---------------------------------------------------------------
/** Field description */
private PluginManager pluginManager;
}

View File

@@ -38,15 +38,12 @@ package sonia.scm.api.rest.resources;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import sonia.scm.plugin.Plugin;
import sonia.scm.plugin.PluginInformation;
import sonia.scm.plugin.PluginManager;
//~--- JDK imports ------------------------------------------------------------
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
@@ -58,8 +55,8 @@ import javax.ws.rs.core.MediaType;
* @author Sebastian Sdorra
*/
@Singleton
@Path("plugins")
public class PluginResource
@Path("plugins/installed")
public class InstalledPluginResource
{
/**
@@ -69,25 +66,19 @@ public class PluginResource
* @param pluginManager
*/
@Inject
public PluginResource(PluginManager pluginManager)
public InstalledPluginResource(PluginManager pluginManager)
{
Collection<Plugin> pluginCollection = pluginManager.getPlugins();
List<PluginInformation> informations = new ArrayList<PluginInformation>();
Collection<PluginInformation> pluginCollection =
pluginManager.getInstalled();
if (pluginCollection != null)
{
for (Plugin plugin : pluginCollection)
{
PluginInformation pluginInfo = plugin.getInformation();
if (pluginInfo != null)
{
informations.add(pluginInfo);
}
}
plugins = pluginCollection.toArray(new PluginInformation[0]);
}
else
{
plugins = new PluginInformation[0];
}
plugins = informations.toArray(new PluginInformation[0]);
}
//~--- get methods ----------------------------------------------------------

View File

@@ -43,6 +43,7 @@ import java.io.File;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
/**
@@ -55,6 +56,10 @@ import javax.xml.bind.annotation.XmlRootElement;
public class ScmConfiguration
{
/** Field description */
public static final String DEFAULT_PLUGINURL =
"http://plugins.scm-manager.org/plugins.xml";
/** Field description */
public static final String PATH =
"config".concat(File.separator).concat("config.xml");
@@ -74,6 +79,17 @@ public class ScmConfiguration
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @return
*/
public String getPluginUrl()
{
return pluginUrl;
}
/**
* Method description
*
@@ -87,6 +103,17 @@ public class ScmConfiguration
//~--- set methods ----------------------------------------------------------
/**
* Method description
*
*
* @param pluginUrl
*/
public void setPluginUrl(String pluginUrl)
{
this.pluginUrl = pluginUrl;
}
/**
* Method description
*
@@ -100,6 +127,10 @@ public class ScmConfiguration
//~--- fields ---------------------------------------------------------------
/** Field description */
@XmlElement(name = "plugin-url")
private String pluginUrl = DEFAULT_PLUGINURL;
/** Field description */
private String servername = "localhost";
}

View File

@@ -0,0 +1,239 @@
/**
* 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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.SCMContext;
import sonia.scm.plugin.ext.ExtensionObject;
import sonia.scm.plugin.ext.ExtensionProcessor;
import sonia.scm.plugin.ext.JARExtensionScanner;
import sonia.scm.util.IOUtil;
//~--- JDK imports ------------------------------------------------------------
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Set;
import javax.xml.bind.JAXB;
/**
*
* @author Sebastian Sdorra
*/
public class DefaultPluginLoader implements PluginLoader
{
/** Field description */
public static final String PATH_PLUGINCONFIG = "META-INF/scm/plugin.xml";
/** the logger for DefaultPluginLoader */
private static final Logger logger =
LoggerFactory.getLogger(DefaultPluginLoader.class);
//~--- constructors ---------------------------------------------------------
/**
* Constructs ...
*
*/
public DefaultPluginLoader()
{
ClassLoader classLoader = getClassLoader();
try
{
load(classLoader);
}
catch (IOException ex)
{
throw new RuntimeException(ex);
}
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @param processor
*/
@Override
public void processExtensions(ExtensionProcessor processor)
{
Set<ExtensionObject> extensions = new HashSet<ExtensionObject>();
ClassLoader classLoader = getClassLoader();
JARExtensionScanner scanner = new JARExtensionScanner();
for (Plugin plugin : installedPlugins)
{
InputStream input = null;
try
{
Set<String> packageSet = plugin.getPackageSet();
if (packageSet == null)
{
packageSet = new HashSet<String>();
}
packageSet.add(SCMContext.DEFAULT_PACKAGE);
input = new FileInputStream(plugin.getPath());
scanner.processExtensions(classLoader, extensions, input, packageSet);
}
catch (IOException ex)
{
logger.error(ex.getMessage(), ex);
}
finally
{
IOUtil.close(input);
}
}
for (ExtensionObject exo : extensions)
{
processor.processExtension(exo.getExtension(), exo.getExtensionClass());
}
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @return
*/
@Override
public Collection<Plugin> getInstalledPlugins()
{
return installedPlugins;
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @param classLoader
*
* @throws IOException
*/
private void load(ClassLoader classLoader) throws IOException
{
Enumeration<URL> urlEnum = classLoader.getResources(PATH_PLUGINCONFIG);
if (urlEnum != null)
{
while (urlEnum.hasMoreElements())
{
URL url = urlEnum.nextElement();
loadPlugin(url);
}
}
}
/**
* Method description
*
*
* @param url
*/
private void loadPlugin(URL url)
{
try
{
// jar:file:/some/path/file.jar!/META-INF/scm/plugin.xml
String path = url.toExternalForm();
path = path.substring("jar:file:".length(), path.lastIndexOf("!"));
if (logger.isInfoEnabled())
{
logger.info("load plugin {}", path);
}
Plugin plugin = JAXB.unmarshal(url, Plugin.class);
plugin.setPath(path);
installedPlugins.add(plugin);
}
catch (Exception ex)
{
logger.error(ex.getMessage(), ex);
}
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @return
*/
private ClassLoader getClassLoader()
{
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
if (classLoader == null)
{
classLoader = DefaultPluginManager.class.getClassLoader();
}
return classLoader;
}
//~--- fields ---------------------------------------------------------------
/** Field description */
private Set<Plugin> installedPlugins = new HashSet<Plugin>();
}

View File

@@ -0,0 +1,249 @@
/**
* 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 com.google.inject.Inject;
import com.google.inject.Singleton;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.ConfigurationException;
import sonia.scm.cache.CacheManager;
import sonia.scm.cache.SimpleCache;
import sonia.scm.config.ScmConfiguration;
//~--- JDK imports ------------------------------------------------------------
import java.net.URL;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
/**
*
* @author Sebastian Sdorra
*/
@Singleton
public class DefaultPluginManager implements PluginManager
{
/** Field description */
public static final String CACHE_NAME = "sonia.cache.plugins";
/** the logger for DefaultPluginManager */
private static final Logger logger =
LoggerFactory.getLogger(DefaultPluginManager.class);
//~--- constructors ---------------------------------------------------------
/**
* Constructs ...
*
*
*
* @param configuration
* @param pluginLoader
* @param cacheManager
*/
@Inject
public DefaultPluginManager(ScmConfiguration configuration,
PluginLoader pluginLoader,
CacheManager cacheManager)
{
this.configuration = configuration;
this.cache = cacheManager.getSimpleCache(String.class, PluginCenter.class,
CACHE_NAME);
installedPlugins = new HashMap<String, PluginInformation>();
for (Plugin plugin : pluginLoader.getInstalledPlugins())
{
PluginInformation info = plugin.getInformation();
if (info != null)
{
String id = getPluginId(info);
installedPlugins.put(id, plugin.getInformation());
}
}
try
{
unmarshaller =
JAXBContext.newInstance(PluginCenter.class).createUnmarshaller();
}
catch (JAXBException ex)
{
throw new ConfigurationException(ex);
}
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @param id
*/
@Override
public void install(String id)
{
throw new UnsupportedOperationException("Not supported yet.");
}
/**
* Method description
*
*
* @param id
*/
@Override
public void uninstall(String id)
{
throw new UnsupportedOperationException("Not supported yet.");
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @param id
*
* @return
*/
@Override
public PluginInformation get(String id)
{
throw new UnsupportedOperationException("Not supported yet.");
}
/**
* Method description
*
*
* @return
*/
@Override
public Collection<PluginInformation> getAvailable()
{
return getPluginCenter().getPlugins();
}
/**
* Method description
*
*
* @return
*/
@Override
public Collection<PluginInformation> getInstalled()
{
return installedPlugins.values();
}
/**
* Method description
*
*
* @return
*/
private PluginCenter getPluginCenter()
{
PluginCenter center = cache.get(PluginCenter.class.getName());
if (center == null)
{
if (logger.isInfoEnabled())
{
logger.info("fetch plugin informations from {}",
configuration.getPluginUrl());
}
try
{
center = (PluginCenter) unmarshaller.unmarshal(
new URL(configuration.getPluginUrl()));
cache.put(PluginCenter.class.getName(), center);
}
catch (Exception ex)
{
throw new PluginLoadException(ex);
}
}
return center;
}
/**
* Method description
*
*
* @param info
*
* @return
*/
private String getPluginId(PluginInformation info)
{
StringBuilder id = new StringBuilder(info.getGroupId());
id.append(":").append(info.getArtifactId()).append(":");
return id.append(info.getVersion()).toString();
}
//~--- fields ---------------------------------------------------------------
/** Field description */
private SimpleCache<String, PluginCenter> cache;
/** Field description */
private ScmConfiguration configuration;
/** Field description */
private Map<String, PluginInformation> installedPlugins;
/** Field description */
private Unmarshaller unmarshaller;
}

View File

@@ -74,12 +74,12 @@ public class ScriptResourceServlet extends AbstractResourceServlet
* Constructs ...
*
*
* @param manager
* @param pluginLoader
*/
@Inject
public ScriptResourceServlet(PluginManager manager)
public ScriptResourceServlet(PluginLoader pluginLoader)
{
this.manager = manager;
this.pluginLoader = pluginLoader;
}
//~--- methods --------------------------------------------------------------
@@ -222,7 +222,7 @@ public class ScriptResourceServlet extends AbstractResourceServlet
private Collection<String> getScriptResources()
{
Set<String> resources = new TreeSet<String>();
Collection<Plugin> plugins = manager.getPlugins();
Collection<Plugin> plugins = pluginLoader.getInstalledPlugins();
if (plugins != null)
{
@@ -238,5 +238,5 @@ public class ScriptResourceServlet extends AbstractResourceServlet
//~--- fields ---------------------------------------------------------------
/** Field description */
private PluginManager manager;
private PluginLoader pluginLoader;
}