2014-06-06 08:57:41 +02: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
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
package sonia.scm.plugin;
|
|
|
|
|
|
|
|
|
|
//~--- non-JDK imports --------------------------------------------------------
|
|
|
|
|
|
2014-07-13 09:47:14 +02:00
|
|
|
import com.google.common.collect.ImmutableList;
|
|
|
|
|
import com.google.common.collect.ImmutableList.Builder;
|
2014-06-06 08:57:41 +02:00
|
|
|
import com.google.common.collect.ImmutableSet;
|
2014-07-13 09:47:14 +02:00
|
|
|
import com.google.common.collect.Lists;
|
2014-06-06 08:57:41 +02:00
|
|
|
import com.google.common.collect.Sets;
|
2014-07-12 17:17:24 +02:00
|
|
|
import com.google.common.hash.Hashing;
|
2014-06-06 08:57:41 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
2014-07-13 09:47:14 +02:00
|
|
|
import sonia.scm.plugin.ExplodedSmp.PathTransformer;
|
|
|
|
|
|
2018-08-31 09:55:45 +02:00
|
|
|
import javax.xml.bind.JAXBContext;
|
|
|
|
|
import javax.xml.bind.JAXBException;
|
2014-07-12 17:17:24 +02:00
|
|
|
import java.io.File;
|
2014-06-06 08:57:41 +02:00
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.net.URL;
|
|
|
|
|
import java.nio.file.DirectoryStream;
|
|
|
|
|
import java.nio.file.DirectoryStream.Filter;
|
|
|
|
|
import java.nio.file.Files;
|
|
|
|
|
import java.nio.file.Path;
|
2014-07-12 17:17:24 +02:00
|
|
|
import java.text.SimpleDateFormat;
|
2014-06-06 08:57:41 +02:00
|
|
|
import java.util.ArrayList;
|
2014-07-12 17:17:24 +02:00
|
|
|
import java.util.Date;
|
2014-06-06 08:57:41 +02:00
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
2018-08-31 09:55:45 +02:00
|
|
|
//~--- JDK imports ------------------------------------------------------------
|
2014-06-06 08:57:41 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @author Sebastian Sdorra
|
2014-07-13 09:47:14 +02:00
|
|
|
*
|
|
|
|
|
* TODO don't mix nio and io
|
2014-06-06 08:57:41 +02:00
|
|
|
*/
|
|
|
|
|
public final class PluginProcessor
|
|
|
|
|
{
|
|
|
|
|
|
2014-12-07 18:28:24 +01:00
|
|
|
/** Field description */
|
|
|
|
|
private static final String INSTALLEDNAME_FORMAT = "%s.%03d";
|
|
|
|
|
|
2014-06-06 08:57:41 +02:00
|
|
|
/** Field description */
|
|
|
|
|
private static final String DIRECTORY_CLASSES = "classes";
|
|
|
|
|
|
|
|
|
|
/** Field description */
|
|
|
|
|
private static final String DIRECTORY_DEPENDENCIES = "lib";
|
|
|
|
|
|
2014-07-12 17:17:24 +02:00
|
|
|
/** Field description */
|
|
|
|
|
private static final String DIRECTORY_INSTALLED = ".installed";
|
|
|
|
|
|
2014-06-06 08:57:41 +02:00
|
|
|
/** Field description */
|
|
|
|
|
private static final String DIRECTORY_METAINF = "META-INF";
|
|
|
|
|
|
|
|
|
|
/** Field description */
|
2014-08-29 10:50:14 +02:00
|
|
|
private static final String DIRECTORY_WEBAPP = "webapp";
|
2014-06-06 08:57:41 +02:00
|
|
|
|
|
|
|
|
/** Field description */
|
|
|
|
|
private static final String EXTENSION_PLUGIN = ".smp";
|
|
|
|
|
|
2014-07-12 17:17:24 +02:00
|
|
|
/** Field description */
|
2014-07-13 09:47:14 +02:00
|
|
|
private static final String FORMAT_DATE = "yyyy-MM-dd";
|
2014-07-12 17:17:24 +02:00
|
|
|
|
2014-06-06 08:57:41 +02:00
|
|
|
/** Field description */
|
|
|
|
|
private static final String GLOB_JAR = "*.jar";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* the logger for PluginProcessor
|
|
|
|
|
*/
|
|
|
|
|
private static final Logger logger =
|
|
|
|
|
LoggerFactory.getLogger(PluginProcessor.class);
|
|
|
|
|
|
|
|
|
|
//~--- constructors ---------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Constructs ...
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @param pluginDirectory
|
|
|
|
|
*/
|
|
|
|
|
public PluginProcessor(Path pluginDirectory)
|
|
|
|
|
{
|
|
|
|
|
this.pluginDirectory = pluginDirectory;
|
2014-07-12 17:17:24 +02:00
|
|
|
this.installedDirectory = findInstalledDirectory();
|
2014-06-06 08:57:41 +02:00
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
this.context = JAXBContext.newInstance(Plugin.class);
|
|
|
|
|
}
|
|
|
|
|
catch (JAXBException ex)
|
|
|
|
|
{
|
|
|
|
|
throw new PluginLoadException("could not create jaxb context", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//~--- methods --------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @param directory
|
|
|
|
|
* @param filter
|
|
|
|
|
*
|
|
|
|
|
* @return
|
|
|
|
|
*
|
|
|
|
|
* @throws IOException
|
|
|
|
|
*/
|
|
|
|
|
private static DirectoryStream<Path> stream(Path directory,
|
|
|
|
|
Filter<Path> filter)
|
|
|
|
|
throws IOException
|
|
|
|
|
{
|
|
|
|
|
return Files.newDirectoryStream(directory, filter);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @param classLoader
|
|
|
|
|
* @return
|
|
|
|
|
*
|
|
|
|
|
* @throws IOException
|
|
|
|
|
*/
|
|
|
|
|
public Set<PluginWrapper> collectPlugins(ClassLoader classLoader)
|
|
|
|
|
throws IOException
|
|
|
|
|
{
|
|
|
|
|
logger.info("collect plugins");
|
|
|
|
|
|
|
|
|
|
Set<Path> archives = collect(pluginDirectory, new PluginArchiveFilter());
|
|
|
|
|
|
2019-01-10 12:09:07 +01:00
|
|
|
logger.debug("extract {} archives", archives.size());
|
2014-06-06 08:57:41 +02:00
|
|
|
|
|
|
|
|
extract(archives);
|
|
|
|
|
|
2014-07-13 09:47:14 +02:00
|
|
|
List<Path> dirs = collectPluginDirectories(pluginDirectory);
|
2014-06-06 08:57:41 +02:00
|
|
|
|
2019-01-10 12:09:07 +01:00
|
|
|
logger.debug("process {} directories: {}", dirs.size(), dirs);
|
2014-06-06 08:57:41 +02:00
|
|
|
|
2014-07-13 09:47:14 +02:00
|
|
|
List<ExplodedSmp> smps = Lists.transform(dirs, new PathTransformer());
|
|
|
|
|
|
2014-08-19 19:30:13 +02:00
|
|
|
logger.trace("start building plugin tree");
|
|
|
|
|
|
2019-01-10 12:09:07 +01:00
|
|
|
PluginTree pluginTree = new PluginTree(smps);
|
|
|
|
|
|
|
|
|
|
logger.trace("build plugin tree: {}", pluginTree);
|
|
|
|
|
|
|
|
|
|
List<PluginNode> rootNodes = pluginTree.getRootNodes();
|
2014-08-19 19:30:13 +02:00
|
|
|
|
|
|
|
|
logger.trace("create plugin wrappers and build classloaders");
|
|
|
|
|
|
|
|
|
|
Set<PluginWrapper> wrappers = createPluginWrappers(classLoader, rootNodes);
|
2014-06-06 08:57:41 +02:00
|
|
|
|
2019-01-10 12:09:07 +01:00
|
|
|
logger.debug("collected {} plugins", wrappers.size());
|
2014-08-19 19:30:13 +02:00
|
|
|
|
|
|
|
|
return ImmutableSet.copyOf(wrappers);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @param plugins
|
|
|
|
|
* @param classLoader
|
|
|
|
|
* @param node
|
|
|
|
|
*
|
|
|
|
|
* @throws IOException
|
|
|
|
|
*/
|
|
|
|
|
private void appendPluginWrapper(Set<PluginWrapper> plugins,
|
|
|
|
|
ClassLoader classLoader, PluginNode node)
|
|
|
|
|
throws IOException
|
|
|
|
|
{
|
2019-01-10 12:09:07 +01:00
|
|
|
if (node.getWrapper() != null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2014-08-19 19:30:13 +02:00
|
|
|
ExplodedSmp smp = node.getPlugin();
|
|
|
|
|
|
|
|
|
|
List<ClassLoader> parents = Lists.newArrayList();
|
|
|
|
|
|
|
|
|
|
for (PluginNode parent : node.getParents())
|
|
|
|
|
{
|
|
|
|
|
PluginWrapper wrapper = parent.getWrapper();
|
|
|
|
|
|
|
|
|
|
if (wrapper != null)
|
|
|
|
|
{
|
|
|
|
|
parents.add(wrapper.getClassLoader());
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
//J-
|
|
|
|
|
throw new PluginLoadException(
|
|
|
|
|
String.format(
|
|
|
|
|
"parent %s of plugin %s is not ready", parent.getId(), node.getId()
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
//J+
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PluginWrapper plugin =
|
|
|
|
|
createPluginWrapper(createParentPluginClassLoader(classLoader, parents),
|
2014-08-19 21:01:11 +02:00
|
|
|
smp);
|
2014-08-19 19:30:13 +02:00
|
|
|
|
|
|
|
|
if (plugin != null)
|
|
|
|
|
{
|
2014-08-23 17:39:29 +02:00
|
|
|
node.setWrapper(plugin);
|
2014-08-19 19:30:13 +02:00
|
|
|
plugins.add(plugin);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @param plugins
|
|
|
|
|
* @param classLoader
|
|
|
|
|
* @param nodes
|
|
|
|
|
*
|
|
|
|
|
* @throws IOException
|
|
|
|
|
*/
|
|
|
|
|
private void appendPluginWrappers(Set<PluginWrapper> plugins,
|
|
|
|
|
ClassLoader classLoader, List<PluginNode> nodes)
|
|
|
|
|
throws IOException
|
|
|
|
|
{
|
2014-08-19 21:01:11 +02:00
|
|
|
|
2014-08-19 19:30:13 +02:00
|
|
|
// TODO fix plugin loading order
|
|
|
|
|
for (PluginNode node : nodes)
|
|
|
|
|
{
|
|
|
|
|
appendPluginWrapper(plugins, classLoader, node);
|
2014-06-06 08:57:41 +02:00
|
|
|
}
|
|
|
|
|
|
2014-08-19 19:30:13 +02:00
|
|
|
for (PluginNode node : nodes)
|
|
|
|
|
{
|
|
|
|
|
appendPluginWrappers(plugins, classLoader, node.getChildren());
|
|
|
|
|
}
|
2014-06-06 08:57:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @param directory
|
|
|
|
|
* @param filter
|
|
|
|
|
*
|
|
|
|
|
* @return
|
|
|
|
|
*
|
|
|
|
|
* @throws IOException
|
|
|
|
|
*/
|
|
|
|
|
private Set<Path> collect(Path directory, Filter<Path> filter)
|
|
|
|
|
throws IOException
|
|
|
|
|
{
|
|
|
|
|
Set<Path> paths;
|
|
|
|
|
|
|
|
|
|
try (DirectoryStream<Path> stream = stream(directory, filter))
|
|
|
|
|
{
|
|
|
|
|
paths = ImmutableSet.copyOf(stream);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return paths;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-12 16:41:20 +02:00
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @param directory
|
|
|
|
|
*
|
|
|
|
|
* @return
|
|
|
|
|
*
|
|
|
|
|
* @throws IOException
|
|
|
|
|
*/
|
2014-07-13 09:47:14 +02:00
|
|
|
private List<Path> collectPluginDirectories(Path directory) throws IOException
|
2014-07-12 16:41:20 +02:00
|
|
|
{
|
2014-07-13 09:47:14 +02:00
|
|
|
Builder<Path> paths = ImmutableList.builder();
|
2014-07-12 16:41:20 +02:00
|
|
|
|
|
|
|
|
Filter<Path> filter = new DirectoryFilter();
|
|
|
|
|
|
|
|
|
|
try (DirectoryStream<Path> parentStream = stream(directory, filter))
|
|
|
|
|
{
|
|
|
|
|
for (Path parent : parentStream)
|
|
|
|
|
{
|
|
|
|
|
try (DirectoryStream<Path> direcotries = stream(parent, filter))
|
|
|
|
|
{
|
|
|
|
|
paths.addAll(direcotries);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return paths.build();
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-12 17:17:24 +02:00
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
2014-08-19 21:01:11 +02:00
|
|
|
* @param parentClassLoader
|
|
|
|
|
* @param directory
|
|
|
|
|
* @param smp
|
2014-06-06 08:57:41 +02:00
|
|
|
*
|
|
|
|
|
* @return
|
2014-08-19 21:01:11 +02:00
|
|
|
*
|
|
|
|
|
* @throws IOException
|
2014-06-06 08:57:41 +02:00
|
|
|
*/
|
2014-08-19 21:01:11 +02:00
|
|
|
private ClassLoader createClassLoader(ClassLoader parentClassLoader,
|
|
|
|
|
ExplodedSmp smp)
|
|
|
|
|
throws IOException
|
2014-06-06 08:57:41 +02:00
|
|
|
{
|
2014-08-19 21:01:11 +02:00
|
|
|
List<URL> urls = new ArrayList<>();
|
2014-06-06 08:57:41 +02:00
|
|
|
|
2014-08-29 10:50:14 +02:00
|
|
|
Path directory = smp.getPath();
|
|
|
|
|
|
|
|
|
|
Path metaDir = directory.resolve(DIRECTORY_METAINF);
|
2014-06-06 08:57:41 +02:00
|
|
|
|
2014-08-19 21:01:11 +02:00
|
|
|
if (!Files.exists(metaDir))
|
2014-06-06 08:57:41 +02:00
|
|
|
{
|
2014-08-19 21:01:11 +02:00
|
|
|
throw new FileNotFoundException("could not find META-INF directory");
|
2014-06-06 08:57:41 +02:00
|
|
|
}
|
2014-08-19 21:01:11 +02:00
|
|
|
|
2014-08-29 10:50:14 +02:00
|
|
|
Path classesDir = directory.resolve(DIRECTORY_CLASSES);
|
2014-08-19 21:01:11 +02:00
|
|
|
|
2014-08-29 10:50:14 +02:00
|
|
|
if (Files.exists(classesDir))
|
2014-08-19 21:01:11 +02:00
|
|
|
{
|
2014-08-29 10:50:14 +02:00
|
|
|
urls.add(classesDir.toUri().toURL());
|
|
|
|
|
}
|
2014-08-19 21:01:11 +02:00
|
|
|
|
2014-08-29 10:50:14 +02:00
|
|
|
Path libDir = directory.resolve(DIRECTORY_DEPENDENCIES);
|
2014-08-19 21:01:11 +02:00
|
|
|
|
2014-08-29 10:50:14 +02:00
|
|
|
if (Files.exists(libDir))
|
|
|
|
|
{
|
2018-08-31 09:55:45 +02:00
|
|
|
try (DirectoryStream<Path> pathDirectoryStream = Files.newDirectoryStream(libDir, GLOB_JAR)) {
|
|
|
|
|
for (Path f : pathDirectoryStream) {
|
|
|
|
|
urls.add(f.toUri().toURL());
|
|
|
|
|
}
|
2014-08-19 21:01:11 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ClassLoader classLoader;
|
|
|
|
|
URL[] urlArray = urls.toArray(new URL[urls.size()]);
|
|
|
|
|
Plugin plugin = smp.getPlugin();
|
|
|
|
|
|
|
|
|
|
if (smp.getPlugin().isChildFirstClassLoader())
|
|
|
|
|
{
|
|
|
|
|
logger.debug("create child fist classloader for plugin {}",
|
|
|
|
|
plugin.getInformation().getId());
|
|
|
|
|
classLoader = new ChildFirstPluginClassLoader(urlArray,
|
|
|
|
|
parentClassLoader);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
logger.debug("create parent fist classloader for plugin {}",
|
|
|
|
|
plugin.getInformation().getId());
|
|
|
|
|
classLoader = new DefaultPluginClassLoader(urlArray, parentClassLoader);
|
2014-06-06 08:57:41 +02:00
|
|
|
}
|
2014-08-19 21:01:11 +02:00
|
|
|
|
|
|
|
|
return classLoader;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
private String createDate()
|
|
|
|
|
{
|
|
|
|
|
return new SimpleDateFormat(FORMAT_DATE).format(new Date());
|
2014-06-06 08:57:41 +02:00
|
|
|
}
|
|
|
|
|
|
2014-08-19 19:30:13 +02:00
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @param root
|
|
|
|
|
* @param parents
|
|
|
|
|
*
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
private ClassLoader createParentPluginClassLoader(ClassLoader root,
|
|
|
|
|
List<ClassLoader> parents)
|
|
|
|
|
{
|
|
|
|
|
ClassLoader result;
|
|
|
|
|
int size = parents.size();
|
|
|
|
|
|
|
|
|
|
if (size == 0)
|
|
|
|
|
{
|
|
|
|
|
result = root;
|
|
|
|
|
}
|
|
|
|
|
else if (size == 1)
|
|
|
|
|
{
|
|
|
|
|
result = parents.get(0);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
result = new MultiParentClassLoader(parents);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-19 21:01:11 +02:00
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @param classLoader
|
|
|
|
|
* @param descriptor
|
|
|
|
|
*
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
private Plugin createPlugin(ClassLoader classLoader, Path descriptor)
|
|
|
|
|
{
|
|
|
|
|
ClassLoader ctxcl = Thread.currentThread().getContextClassLoader();
|
|
|
|
|
|
|
|
|
|
Thread.currentThread().setContextClassLoader(classLoader);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return (Plugin) context.createUnmarshaller().unmarshal(
|
|
|
|
|
descriptor.toFile());
|
|
|
|
|
}
|
|
|
|
|
catch (JAXBException ex)
|
|
|
|
|
{
|
|
|
|
|
throw new PluginLoadException(
|
|
|
|
|
"could not load plugin desriptor ".concat(descriptor.toString()), ex);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
Thread.currentThread().setContextClassLoader(ctxcl);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-06 08:57:41 +02:00
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @param classLoader
|
|
|
|
|
* @param directory
|
2014-08-19 21:01:11 +02:00
|
|
|
* @param smp
|
2014-06-06 08:57:41 +02:00
|
|
|
*
|
|
|
|
|
* @return
|
|
|
|
|
*
|
|
|
|
|
* @throws IOException
|
|
|
|
|
*/
|
|
|
|
|
private PluginWrapper createPluginWrapper(ClassLoader classLoader,
|
2014-08-19 21:01:11 +02:00
|
|
|
ExplodedSmp smp)
|
2014-06-06 08:57:41 +02:00
|
|
|
throws IOException
|
|
|
|
|
{
|
|
|
|
|
PluginWrapper wrapper = null;
|
2014-08-27 21:01:05 +02:00
|
|
|
Path directory = smp.getPath();
|
|
|
|
|
Path descriptor = directory.resolve(PluginConstants.FILE_DESCRIPTOR);
|
2014-06-06 08:57:41 +02:00
|
|
|
|
|
|
|
|
if (Files.exists(descriptor))
|
|
|
|
|
{
|
2014-08-19 21:01:11 +02:00
|
|
|
ClassLoader cl = createClassLoader(classLoader, smp);
|
2014-06-06 08:57:41 +02:00
|
|
|
|
|
|
|
|
Plugin plugin = createPlugin(cl, descriptor);
|
|
|
|
|
|
2014-08-29 10:50:14 +02:00
|
|
|
WebResourceLoader resourceLoader = createWebResourceLoader(directory);
|
|
|
|
|
|
|
|
|
|
wrapper = new PluginWrapper(plugin, cl, resourceLoader, directory);
|
2014-06-06 08:57:41 +02:00
|
|
|
}
|
2014-07-12 17:17:24 +02:00
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
logger.warn("found plugin directory without plugin descriptor");
|
|
|
|
|
}
|
2014-06-06 08:57:41 +02:00
|
|
|
|
|
|
|
|
return wrapper;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @param classLoader
|
2014-07-13 09:47:14 +02:00
|
|
|
* @param smps
|
2014-08-19 19:30:13 +02:00
|
|
|
* @param rootNodes
|
2014-06-06 08:57:41 +02:00
|
|
|
*
|
|
|
|
|
* @return
|
|
|
|
|
*
|
|
|
|
|
* @throws IOException
|
|
|
|
|
*/
|
|
|
|
|
private Set<PluginWrapper> createPluginWrappers(ClassLoader classLoader,
|
2014-08-19 19:30:13 +02:00
|
|
|
List<PluginNode> rootNodes)
|
2014-06-06 08:57:41 +02:00
|
|
|
throws IOException
|
|
|
|
|
{
|
|
|
|
|
Set<PluginWrapper> plugins = Sets.newHashSet();
|
|
|
|
|
|
2014-08-19 19:30:13 +02:00
|
|
|
appendPluginWrappers(plugins, classLoader, rootNodes);
|
2014-06-06 08:57:41 +02:00
|
|
|
|
|
|
|
|
return plugins;
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-29 10:50:14 +02:00
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @param directory
|
|
|
|
|
*
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
private WebResourceLoader createWebResourceLoader(Path directory)
|
|
|
|
|
{
|
|
|
|
|
WebResourceLoader resourceLoader;
|
|
|
|
|
Path webapp = directory.resolve(DIRECTORY_WEBAPP);
|
|
|
|
|
|
|
|
|
|
if (Files.exists(webapp))
|
|
|
|
|
{
|
|
|
|
|
logger.debug("create WebResourceLoader for path {}", webapp);
|
|
|
|
|
resourceLoader = new PathWebResourceLoader(webapp);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
logger.debug("create empty WebResourceLoader");
|
|
|
|
|
resourceLoader = new EmptyWebResourceLoader();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return resourceLoader;
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-06 08:57:41 +02:00
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @param archives
|
|
|
|
|
*
|
|
|
|
|
* @throws IOException
|
|
|
|
|
*/
|
|
|
|
|
private void extract(Iterable<Path> archives) throws IOException
|
|
|
|
|
{
|
|
|
|
|
logger.debug("extract archives");
|
|
|
|
|
|
|
|
|
|
for (Path archive : archives)
|
|
|
|
|
{
|
2014-07-12 17:17:24 +02:00
|
|
|
File archiveFile = archive.toFile();
|
|
|
|
|
|
2014-06-06 08:57:41 +02:00
|
|
|
logger.trace("extract archive {}", archive);
|
|
|
|
|
|
2014-07-12 17:17:24 +02:00
|
|
|
SmpArchive smp = SmpArchive.create(archive);
|
|
|
|
|
|
2014-07-13 13:47:35 +02:00
|
|
|
logger.debug("extract plugin {}", smp.getPlugin());
|
2014-07-12 17:17:24 +02:00
|
|
|
|
2014-07-13 13:47:35 +02:00
|
|
|
File directory =
|
|
|
|
|
PluginsInternal.createPluginDirectory(pluginDirectory.toFile(),
|
|
|
|
|
smp.getPlugin());
|
2014-07-12 17:17:24 +02:00
|
|
|
|
|
|
|
|
String checksum = com.google.common.io.Files.hash(archiveFile,
|
|
|
|
|
Hashing.sha256()).toString();
|
2014-07-13 13:47:35 +02:00
|
|
|
File checksumFile = PluginsInternal.getChecksumFile(directory);
|
2014-07-12 17:17:24 +02:00
|
|
|
|
2014-07-13 13:47:35 +02:00
|
|
|
PluginsInternal.extract(smp, checksum, directory, checksumFile, false);
|
2014-07-12 17:17:24 +02:00
|
|
|
moveArchive(archive);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
private Path findInstalledDirectory()
|
|
|
|
|
{
|
|
|
|
|
Path directory = null;
|
|
|
|
|
Path installed = pluginDirectory.resolve(DIRECTORY_INSTALLED);
|
|
|
|
|
Path date = installed.resolve(createDate());
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < 999; i++)
|
|
|
|
|
{
|
|
|
|
|
Path dir = date.resolve(String.format("%03d", i));
|
|
|
|
|
|
|
|
|
|
if (!Files.exists(dir))
|
|
|
|
|
{
|
|
|
|
|
directory = dir;
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (directory == null)
|
|
|
|
|
{
|
|
|
|
|
throw new PluginException("could not find installed directory");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return directory;
|
|
|
|
|
}
|
2014-06-06 08:57:41 +02:00
|
|
|
|
2014-07-12 17:17:24 +02:00
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @param archive
|
|
|
|
|
*
|
|
|
|
|
* @throws IOException
|
|
|
|
|
*/
|
|
|
|
|
private void moveArchive(Path archive) throws IOException
|
|
|
|
|
{
|
|
|
|
|
if (!Files.exists(installedDirectory))
|
|
|
|
|
{
|
|
|
|
|
logger.debug("create installed directory {}", installedDirectory);
|
|
|
|
|
Files.createDirectories(installedDirectory);
|
2014-06-06 08:57:41 +02:00
|
|
|
}
|
2014-07-12 17:17:24 +02:00
|
|
|
|
2014-12-07 18:28:24 +01:00
|
|
|
Path installed = null;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < 1000; i++)
|
|
|
|
|
{
|
|
|
|
|
String name = String.format(INSTALLEDNAME_FORMAT, archive.getFileName(), i);
|
|
|
|
|
|
|
|
|
|
installed = installedDirectory.resolve(name);
|
|
|
|
|
|
|
|
|
|
if (!Files.exists(installed))
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-08-31 09:55:45 +02:00
|
|
|
|
2014-12-07 18:28:24 +01:00
|
|
|
logger.debug("move installed archive to {}", installed);
|
|
|
|
|
|
|
|
|
|
Files.move(archive, installed);
|
2014-06-06 08:57:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//~--- inner classes --------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @version Enter version here..., 14/06/04
|
|
|
|
|
* @author Enter your name here...
|
|
|
|
|
*/
|
|
|
|
|
private static class DirectoryFilter implements DirectoryStream.Filter<Path>
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @param entry
|
|
|
|
|
*
|
|
|
|
|
* @return
|
|
|
|
|
*
|
|
|
|
|
* @throws IOException
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public boolean accept(Path entry) throws IOException
|
|
|
|
|
{
|
2014-07-13 09:47:14 +02:00
|
|
|
return Files.isDirectory(entry)
|
|
|
|
|
&&!entry.getFileName().toString().startsWith(".");
|
2014-06-06 08:57:41 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @version Enter version here..., 14/06/04
|
|
|
|
|
* @author Enter your name here...
|
|
|
|
|
*/
|
|
|
|
|
private static class PluginArchiveFilter
|
|
|
|
|
implements DirectoryStream.Filter<Path>
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @param entry
|
|
|
|
|
*
|
|
|
|
|
* @return
|
|
|
|
|
*
|
|
|
|
|
* @throws IOException
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public boolean accept(Path entry) throws IOException
|
|
|
|
|
{
|
|
|
|
|
return Files.isRegularFile(entry)
|
|
|
|
|
&& entry.getFileName().toString().endsWith(EXTENSION_PLUGIN);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//~--- fields ---------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
/** Field description */
|
|
|
|
|
private final JAXBContext context;
|
|
|
|
|
|
2014-07-12 17:17:24 +02:00
|
|
|
/** Field description */
|
|
|
|
|
private final Path installedDirectory;
|
|
|
|
|
|
2014-06-06 08:57:41 +02:00
|
|
|
/** Field description */
|
|
|
|
|
private final Path pluginDirectory;
|
|
|
|
|
}
|