Files
SCM-Manager/scm-webapp/src/main/java/sonia/scm/plugin/DefaultPluginLoader.java

295 lines
7.1 KiB
Java
Raw Normal View History

2010-10-31 19:22:53 +01:00
/**
* 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
*
2010-10-12 09:16:40 +02:00
*/
2010-12-01 14:26:29 +01:00
package sonia.scm.plugin;
2010-10-12 09:16:40 +02:00
//~--- non-JDK imports --------------------------------------------------------
2010-10-16 11:03:54 +02:00
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2010-12-01 15:15:09 +01:00
import sonia.scm.SCMContext;
2010-12-01 14:26:29 +01:00
import sonia.scm.plugin.ext.ExtensionObject;
import sonia.scm.plugin.ext.ExtensionProcessor;
import sonia.scm.plugin.ext.JARExtensionScanner;
import sonia.scm.util.IOUtil;
2010-10-12 09:16:40 +02:00
//~--- JDK imports ------------------------------------------------------------
import java.io.File;
2010-12-01 14:26:29 +01:00
import java.io.FileInputStream;
2010-10-12 09:16:40 +02:00
import java.io.IOException;
2010-12-01 14:26:29 +01:00
import java.io.InputStream;
2010-10-12 09:16:40 +02:00
import java.net.URL;
import java.net.URLDecoder;
2010-10-12 09:16:40 +02:00
2010-12-01 14:26:29 +01:00
import java.util.Collection;
2010-10-12 09:16:40 +02:00
import java.util.Enumeration;
2010-12-01 14:26:29 +01:00
import java.util.HashSet;
2010-10-12 09:16:40 +02:00
import java.util.Set;
import javax.xml.bind.JAXB;
/**
*
* @author Sebastian Sdorra
*/
2010-12-13 18:59:00 +01:00
public class DefaultPluginLoader implements PluginLoader
2010-10-12 09:16:40 +02:00
{
/** Field description */
public static final String ENCODING = "UTF-8";
2010-10-12 09:16:40 +02:00
/** Field description */
2010-12-01 14:26:29 +01:00
public static final String PATH_PLUGINCONFIG = "META-INF/scm/plugin.xml";
2010-12-18 13:37:34 +01:00
/** Field description */
public static final String REGE_COREPLUGIN =
"^.*(?:/|\\\\)WEB-INF(?:/|\\\\)lib(?:/|\\\\).*\\.jar$";
2010-12-13 18:59:00 +01:00
/** the logger for DefaultPluginLoader */
2010-10-12 09:16:40 +02:00
private static final Logger logger =
2010-12-13 18:59:00 +01:00
LoggerFactory.getLogger(DefaultPluginLoader.class);
2010-10-12 09:16:40 +02:00
2010-12-01 14:26:29 +01:00
//~--- constructors ---------------------------------------------------------
2010-10-12 09:16:40 +02:00
/**
2010-12-01 14:26:29 +01:00
* Constructs ...
2010-10-12 09:16:40 +02:00
*
*/
2010-12-13 18:59:00 +01:00
public DefaultPluginLoader()
2010-10-12 09:16:40 +02:00
{
2010-12-01 14:26:29 +01:00
ClassLoader classLoader = getClassLoader();
2010-10-12 09:16:40 +02:00
2010-12-01 14:26:29 +01:00
try
2010-10-12 09:16:40 +02:00
{
2010-12-01 14:26:29 +01:00
load(classLoader);
}
catch (IOException ex)
{
throw new RuntimeException(ex);
2010-10-12 09:16:40 +02:00
}
}
2010-12-01 14:26:29 +01:00
//~--- methods --------------------------------------------------------------
2010-10-12 09:16:40 +02:00
/**
* Method description
*
*
2010-12-01 14:26:29 +01:00
* @param processor
2010-10-12 09:16:40 +02:00
*/
2010-12-01 14:26:29 +01:00
@Override
public void processExtensions(ExtensionProcessor processor)
2010-10-12 09:16:40 +02:00
{
2010-12-01 14:26:29 +01:00
Set<ExtensionObject> extensions = new HashSet<ExtensionObject>();
ClassLoader classLoader = getClassLoader();
JARExtensionScanner scanner = new JARExtensionScanner();
2010-10-12 09:16:40 +02:00
2010-12-13 18:59:00 +01:00
for (Plugin plugin : installedPlugins)
2010-10-12 09:16:40 +02:00
{
2010-12-01 14:26:29 +01:00
InputStream input = null;
try
2010-10-12 09:16:40 +02:00
{
2010-12-01 14:26:29 +01:00
Set<String> packageSet = plugin.getPackageSet();
2010-10-12 09:16:40 +02:00
2010-12-01 14:26:29 +01:00
if (packageSet == null)
{
packageSet = new HashSet<String>();
}
2010-12-01 15:15:09 +01:00
packageSet.add(SCMContext.DEFAULT_PACKAGE);
2010-12-01 14:26:29 +01:00
input = new FileInputStream(plugin.getPath());
scanner.processExtensions(classLoader, extensions, input, packageSet);
}
catch (IOException ex)
{
logger.error(ex.getMessage(), ex);
2010-10-12 09:16:40 +02:00
}
2010-12-01 14:26:29 +01:00
finally
{
IOUtil.close(input);
}
}
for (ExtensionObject exo : extensions)
{
processor.processExtension(exo.getExtension(), exo.getExtensionClass());
2010-10-12 09:16:40 +02:00
}
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @return
*/
2010-12-01 14:26:29 +01:00
@Override
2010-12-13 18:59:00 +01:00
public Collection<Plugin> getInstalledPlugins()
2010-10-12 09:16:40 +02:00
{
2010-12-13 18:59:00 +01:00
return installedPlugins;
2010-10-12 09:16:40 +02:00
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @param path
*
* @return
*/
private String decodePath(String path)
{
File file = new File(path);
if (!file.exists())
{
try
{
path = URLDecoder.decode(path, ENCODING);
}
catch (IOException ex)
{
logger.error(ex.getMessage(), ex);
}
}
return path;
}
2010-12-01 14:26:29 +01:00
/**
* 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);
}
}
}
2010-10-12 09:16:40 +02:00
/**
* Method description
*
*
* @param url
*/
private void loadPlugin(URL url)
{
try
{
2010-12-01 14:26:29 +01:00
// jar:file:/some/path/file.jar!/META-INF/scm/plugin.xml
String path = url.toExternalForm();
path = path.substring("jar:file:".length(), path.lastIndexOf("!"));
path = decodePath(path);
2010-10-12 09:16:40 +02:00
2010-12-18 13:37:34 +01:00
boolean corePlugin = path.matches(REGE_COREPLUGIN);
2010-10-31 13:07:43 +01:00
if (logger.isInfoEnabled())
2010-10-12 09:16:40 +02:00
{
2010-12-18 13:37:34 +01:00
logger.info("load {}plugin {}", corePlugin
? "core "
: " ", path);
2010-10-12 09:16:40 +02:00
}
2010-10-31 13:07:43 +01:00
2010-12-01 14:26:29 +01:00
Plugin plugin = JAXB.unmarshal(url, Plugin.class);
2010-12-18 13:37:34 +01:00
PluginInformation info = plugin.getInformation();
2011-03-12 16:14:17 +01:00
PluginCondition condition = plugin.getCondition();
if (condition != null)
{
info.setCondition(condition);
}
2010-12-18 13:37:34 +01:00
if (info != null)
{
info.setState(corePlugin
? PluginState.CORE
: PluginState.INSTALLED);
}
2010-12-01 14:26:29 +01:00
plugin.setPath(path);
2010-12-13 18:59:00 +01:00
installedPlugins.add(plugin);
2010-10-12 09:16:40 +02:00
}
catch (Exception ex)
{
2010-10-16 11:03:54 +02:00
logger.error(ex.getMessage(), ex);
2010-10-12 09:16:40 +02:00
}
}
2010-12-01 14:26:29 +01:00
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @return
*/
private ClassLoader getClassLoader()
{
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
if (classLoader == null)
{
classLoader = DefaultPluginManager.class.getClassLoader();
}
return classLoader;
}
2010-10-12 09:16:40 +02:00
//~--- fields ---------------------------------------------------------------
/** Field description */
2010-12-13 18:59:00 +01:00
private Set<Plugin> installedPlugins = new HashSet<Plugin>();
2010-10-12 09:16:40 +02:00
}