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

@@ -1,239 +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 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 DefaultPluginManager implements PluginManager
{
/** Field description */
public static final String PATH_PLUGINCONFIG = "META-INF/scm/plugin.xml";
/** the logger for DefaultPluginManager */
private static final Logger logger =
LoggerFactory.getLogger(DefaultPluginManager.class);
//~--- constructors ---------------------------------------------------------
/**
* Constructs ...
*
*/
public DefaultPluginManager()
{
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 : plugins)
{
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> getPlugins()
{
return plugins;
}
//~--- 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);
plugins.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> plugins = new HashSet<Plugin>();
}

View File

@@ -0,0 +1,120 @@
/**
* 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.io.Serializable;
import java.util.HashSet;
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.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
/**
*
* @author Sebastian Sdorra
*/
@XmlRootElement(name = "plugin-center")
@XmlAccessorType(XmlAccessType.FIELD)
public class PluginCenter implements Serializable
{
/** Field description */
private static final long serialVersionUID = -6414175308610267397L;
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @return
*/
public Set<PluginInformation> getPlugins()
{
return plugins;
}
/**
* Method description
*
*
* @return
*/
public Set<PluginRepository> getRepositories()
{
return repositories;
}
//~--- set methods ----------------------------------------------------------
/**
* Method description
*
*
* @param plugins
*/
public void setPlugins(Set<PluginInformation> plugins)
{
this.plugins = plugins;
}
/**
* Method description
*
*
* @param repositories
*/
public void setRepositories(Set<PluginRepository> repositories)
{
this.repositories = repositories;
}
//~--- fields ---------------------------------------------------------------
/** Field description */
@XmlElement(name = "plugin")
@XmlElementWrapper(name = "plugins")
private Set<PluginInformation> plugins = new HashSet<PluginInformation>();
/** Field description */
@XmlElement(name = "repository")
@XmlElementWrapper(name = "repositories")
private Set<PluginRepository> repositories = new HashSet<PluginRepository>();
}

View File

@@ -45,6 +45,17 @@ import javax.xml.bind.annotation.XmlRootElement;
public class PluginInformation
{
/**
* Method description
*
*
* @return
*/
public String getArtifactId()
{
return artifactId;
}
/**
* Method description
*
@@ -67,6 +78,17 @@ public class PluginInformation
return description;
}
/**
* Method description
*
*
* @return
*/
public String getGroupId()
{
return groupId;
}
/**
* Method description
*
@@ -102,6 +124,17 @@ public class PluginInformation
//~--- set methods ----------------------------------------------------------
/**
* Method description
*
*
* @param artifactId
*/
public void setArtifactId(String artifactId)
{
this.artifactId = artifactId;
}
/**
* Method description
*
@@ -124,6 +157,17 @@ public class PluginInformation
this.description = description;
}
/**
* Method description
*
*
* @param groupId
*/
public void setGroupId(String groupId)
{
this.groupId = groupId;
}
/**
* Method description
*
@@ -159,12 +203,18 @@ public class PluginInformation
//~--- fields ---------------------------------------------------------------
/** Field description */
private String artifactId;
/** Field description */
private String author;
/** Field description */
private String description;
/** Field description */
private String groupId;
/** Field description */
private String name;

View File

@@ -0,0 +1,90 @@
/**
* 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;
/**
*
* @author Sebastian Sdorra
*/
public class PluginLoadException extends RuntimeException
{
/** Field description */
private static final long serialVersionUID = -8683647962850692519L;
//~--- constructors ---------------------------------------------------------
/**
* Constructs ...
*
*/
public PluginLoadException()
{
super();
}
/**
* Constructs ...
*
*
* @param message
*/
public PluginLoadException(String message)
{
super(message);
}
/**
* Constructs ...
*
*
* @param cause
*/
public PluginLoadException(Throwable cause)
{
super(cause);
}
/**
* Constructs ...
*
*
* @param message
* @param cause
*/
public PluginLoadException(String message, Throwable cause)
{
super(message, cause);
}
}

View File

@@ -0,0 +1,68 @@
/**
* 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.plugin.ext.ExtensionProcessor;
//~--- JDK imports ------------------------------------------------------------
import java.util.Collection;
/**
*
* @author Sebastian Sdorra
*/
public interface PluginLoader
{
/**
* Method description
*
*
* @param processor
*/
public void processExtensions(ExtensionProcessor processor);
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @return
*/
public Collection<Plugin> getInstalledPlugins();
}

View File

@@ -33,14 +33,9 @@
package sonia.scm.plugin;
//~--- non-JDK imports --------------------------------------------------------
import com.google.inject.Binder;
//~--- JDK imports ------------------------------------------------------------
import java.util.Collection;
import sonia.scm.plugin.ext.ExtensionProcessor;
/**
*
@@ -53,10 +48,17 @@ public interface PluginManager
* Method description
*
*
* @param binder
* @param id
*/
public void processExtensions(ExtensionProcessor processor);
public void install(String id);
/**
* Method description
*
*
* @param id
*/
public void uninstall(String id);
//~--- get methods ----------------------------------------------------------
@@ -64,7 +66,25 @@ public interface PluginManager
* Method description
*
*
* @param id
*
* @return
*/
public Collection<Plugin> getPlugins();
public PluginInformation get(String id);
/**
* Method description
*
*
* @return
*/
public Collection<PluginInformation> getAvailable();
/**
* Method description
*
*
* @return
*/
public Collection<PluginInformation> getInstalled();
}

View File

@@ -0,0 +1,196 @@
/**
* 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;
/**
*
* @author Sebastian Sdorra
*/
public class PluginRepository
{
/**
* Constructs ...
*
*/
public PluginRepository() {}
/**
* Constructs ...
*
*
* @param id
* @param url
*/
public PluginRepository(String id, String url)
{
this.id = id;
this.url = url;
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @param obj
*
* @return
*/
@Override
public boolean equals(Object obj)
{
if (obj == null)
{
return false;
}
if (getClass() != obj.getClass())
{
return false;
}
final PluginRepository other = (PluginRepository) obj;
if ((this.id == null)
? (other.id != null)
: !this.id.equals(other.id))
{
return false;
}
if ((this.url == null)
? (other.url != null)
: !this.url.equals(other.url))
{
return false;
}
return true;
}
/**
* Method description
*
*
* @return
*/
@Override
public int hashCode()
{
int hash = 7;
hash = 37 * hash + ((this.id != null)
? this.id.hashCode()
: 0);
hash = 37 * hash + ((this.url != null)
? this.url.hashCode()
: 0);
return hash;
}
/**
* Method description
*
*
* @return
*/
@Override
public String toString()
{
StringBuilder out = new StringBuilder("PluginRepository{id=");
out.append(id).append(", url=").append(url).append(")");
return out.toString();
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @return
*/
public String getId()
{
return id;
}
/**
* Method description
*
*
* @return
*/
public String getUrl()
{
return url;
}
//~--- set methods ----------------------------------------------------------
/**
* Method description
*
*
* @param id
*/
public void setId(String id)
{
this.id = id;
}
/**
* Method description
*
*
* @param url
*/
public void setUrl(String url)
{
this.url = url;
}
//~--- fields ---------------------------------------------------------------
/** Field description */
private String id;
/** Field description */
private String url;
}