2010-12-13 21:02:39 +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
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
package sonia.scm.plugin;
|
|
|
|
|
|
|
|
|
|
//~--- non-JDK imports --------------------------------------------------------
|
|
|
|
|
|
2013-05-23 19:36:21 +02:00
|
|
|
import com.google.common.collect.Lists;
|
2010-12-13 21:02:39 +01:00
|
|
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
|
|
import org.sonatype.aether.RepositorySystem;
|
|
|
|
|
import org.sonatype.aether.graph.Dependency;
|
|
|
|
|
import org.sonatype.aether.repository.LocalRepository;
|
|
|
|
|
import org.sonatype.aether.repository.RemoteRepository;
|
|
|
|
|
|
|
|
|
|
import sonia.scm.ConfigurationException;
|
|
|
|
|
import sonia.scm.SCMContextProvider;
|
|
|
|
|
import sonia.scm.boot.BootstrapListener;
|
|
|
|
|
import sonia.scm.boot.Classpath;
|
2011-05-09 15:08:42 +02:00
|
|
|
import sonia.scm.config.ScmConfiguration;
|
2010-12-13 21:02:39 +01:00
|
|
|
import sonia.scm.util.IOUtil;
|
2010-12-29 14:05:30 +01:00
|
|
|
import sonia.scm.util.Util;
|
2010-12-13 21:02:39 +01:00
|
|
|
|
|
|
|
|
//~--- JDK imports ------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
|
|
|
|
|
import java.util.Collection;
|
2010-12-29 14:05:30 +01:00
|
|
|
import java.util.LinkedHashSet;
|
2010-12-13 21:02:39 +01:00
|
|
|
import java.util.List;
|
2010-12-29 14:05:30 +01:00
|
|
|
import java.util.Set;
|
2010-12-13 21:02:39 +01:00
|
|
|
|
|
|
|
|
import javax.xml.bind.JAXBContext;
|
|
|
|
|
import javax.xml.bind.JAXBException;
|
|
|
|
|
import javax.xml.bind.Marshaller;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @author Sebastian Sdorra
|
|
|
|
|
*/
|
|
|
|
|
public class AetherPluginHandler
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/** the logger for AetherPluginHandler */
|
|
|
|
|
private static final Logger logger =
|
|
|
|
|
LoggerFactory.getLogger(AetherPluginHandler.class);
|
|
|
|
|
|
|
|
|
|
//~--- constructors ---------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Constructs ...
|
|
|
|
|
*
|
|
|
|
|
*
|
2010-12-29 14:05:30 +01:00
|
|
|
*
|
|
|
|
|
* @param pluginManager
|
2010-12-13 21:02:39 +01:00
|
|
|
* @param context
|
2011-05-09 15:08:42 +02:00
|
|
|
* @param configuration
|
2010-12-13 21:02:39 +01:00
|
|
|
*/
|
2010-12-29 14:05:30 +01:00
|
|
|
public AetherPluginHandler(PluginManager pluginManager,
|
2012-08-09 20:18:44 +02:00
|
|
|
SCMContextProvider context, ScmConfiguration configuration)
|
2010-12-13 21:02:39 +01:00
|
|
|
{
|
2010-12-29 14:05:30 +01:00
|
|
|
this.pluginManager = pluginManager;
|
2011-05-09 15:08:42 +02:00
|
|
|
this.configuration = configuration;
|
2010-12-13 21:02:39 +01:00
|
|
|
localRepositoryDirectory = new File(context.getBaseDirectory(),
|
2012-08-09 20:18:44 +02:00
|
|
|
BootstrapListener.PLUGIN_DIRECTORY);
|
2010-12-13 21:02:39 +01:00
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
jaxbContext = JAXBContext.newInstance(Classpath.class);
|
|
|
|
|
}
|
|
|
|
|
catch (JAXBException ex)
|
|
|
|
|
{
|
2013-05-23 14:10:01 +02:00
|
|
|
throw new ConfigurationException(
|
|
|
|
|
"could not create jaxb context for classpath file", ex);
|
2010-12-13 21:02:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
classpathFile = new File(localRepositoryDirectory,
|
2012-08-09 20:18:44 +02:00
|
|
|
BootstrapListener.PLUGIN_CLASSPATHFILE);
|
2010-12-13 21:02:39 +01:00
|
|
|
|
|
|
|
|
if (classpathFile.exists())
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
classpath =
|
|
|
|
|
(Classpath) jaxbContext.createUnmarshaller().unmarshal(classpathFile);
|
|
|
|
|
}
|
|
|
|
|
catch (JAXBException ex)
|
|
|
|
|
{
|
2013-05-23 14:10:01 +02:00
|
|
|
logger.error("could not read classpath file", ex);
|
2010-12-13 21:02:39 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IOUtil.mkdirs(localRepositoryDirectory);
|
2013-05-23 19:36:21 +02:00
|
|
|
repositorySystem = Aether.createRepositorySystem();
|
2010-12-13 21:02:39 +01:00
|
|
|
localRepository = new LocalRepository(localRepositoryDirectory);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//~--- methods --------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @param gav
|
|
|
|
|
*/
|
|
|
|
|
public void install(String gav)
|
|
|
|
|
{
|
|
|
|
|
if (logger.isInfoEnabled())
|
|
|
|
|
{
|
|
|
|
|
logger.info("try to install plugin with gav: {}", gav);
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-23 19:36:21 +02:00
|
|
|
Dependency dependency = Aether.createDependency(gav);
|
2010-12-29 14:05:30 +01:00
|
|
|
List<Dependency> dependencies = getInstalledDependencies(null);
|
2010-12-13 21:02:39 +01:00
|
|
|
|
2010-12-29 14:05:30 +01:00
|
|
|
collectDependencies(dependency, dependencies);
|
2010-12-13 21:02:39 +01:00
|
|
|
}
|
|
|
|
|
|
2010-12-18 16:25:57 +01:00
|
|
|
/**
|
|
|
|
|
* TODO: remove dependencies and remove files
|
|
|
|
|
*
|
|
|
|
|
*
|
2010-12-29 14:05:30 +01:00
|
|
|
*
|
|
|
|
|
* @param id
|
2010-12-18 16:25:57 +01:00
|
|
|
*/
|
2010-12-29 14:05:30 +01:00
|
|
|
public void uninstall(String id)
|
2010-12-18 16:25:57 +01:00
|
|
|
{
|
|
|
|
|
if (logger.isInfoEnabled())
|
|
|
|
|
{
|
2010-12-29 14:05:30 +01:00
|
|
|
logger.info("try to uninstall plugin: {}", id);
|
2010-12-18 16:25:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (classpath != null)
|
|
|
|
|
{
|
|
|
|
|
synchronized (Classpath.class)
|
|
|
|
|
{
|
2010-12-29 14:05:30 +01:00
|
|
|
List<Dependency> dependencies = getInstalledDependencies(id);
|
2010-12-18 16:25:57 +01:00
|
|
|
|
2010-12-29 14:05:30 +01:00
|
|
|
collectDependencies(null, dependencies);
|
2010-12-18 16:25:57 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-13 21:02:39 +01:00
|
|
|
//~--- set methods ----------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @param repositories
|
|
|
|
|
*/
|
|
|
|
|
public void setPluginRepositories(Collection<PluginRepository> repositories)
|
|
|
|
|
{
|
2013-05-23 19:36:21 +02:00
|
|
|
remoteRepositories = Lists.newArrayList();
|
2010-12-13 21:02:39 +01:00
|
|
|
|
|
|
|
|
for (PluginRepository repository : repositories)
|
|
|
|
|
{
|
2013-05-23 19:36:21 +02:00
|
|
|
remoteRepositories.add(Aether.createRemoteRepository(configuration,
|
|
|
|
|
repository));
|
2010-12-13 21:02:39 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//~--- methods --------------------------------------------------------------
|
|
|
|
|
|
2010-12-29 14:05:30 +01:00
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @param dependency
|
|
|
|
|
* @param dependencies
|
2013-05-23 19:36:21 +02:00
|
|
|
* @param localDependencies
|
2010-12-29 14:05:30 +01:00
|
|
|
*/
|
|
|
|
|
private void collectDependencies(Dependency dependency,
|
2013-05-23 19:36:21 +02:00
|
|
|
List<Dependency> localDependencies)
|
2010-12-29 14:05:30 +01:00
|
|
|
{
|
2013-05-23 19:36:21 +02:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
AetherDependencyResolver resolver =
|
|
|
|
|
new AetherDependencyResolver(configuration, repositorySystem,
|
|
|
|
|
localRepository, remoteRepositories);
|
2010-12-29 14:05:30 +01:00
|
|
|
|
2013-05-23 19:36:21 +02:00
|
|
|
resolver.resolveRemoteDependency(dependency);
|
2012-08-09 20:18:44 +02:00
|
|
|
|
2013-05-23 19:36:21 +02:00
|
|
|
for (Dependency localDependency : localDependencies)
|
2012-04-19 17:02:55 +02:00
|
|
|
{
|
2013-05-23 19:36:21 +02:00
|
|
|
resolver.resolveLocalDependency(localDependency);
|
2012-04-19 17:02:55 +02:00
|
|
|
}
|
|
|
|
|
|
2013-05-23 19:36:21 +02:00
|
|
|
if (classpath == null)
|
|
|
|
|
{
|
|
|
|
|
classpath = new Classpath();
|
|
|
|
|
}
|
2010-12-29 14:05:30 +01:00
|
|
|
|
|
|
|
|
synchronized (Classpath.class)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
Set<String> classpathSet =
|
2013-05-23 19:36:21 +02:00
|
|
|
createClasspathSet(resolver.createClassPath());
|
2010-12-29 14:05:30 +01:00
|
|
|
|
|
|
|
|
classpath.setPathSet(classpathSet);
|
|
|
|
|
storeClasspath();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2013-05-23 14:10:01 +02:00
|
|
|
throw new PluginException(
|
|
|
|
|
"could not collect dependencies or store classpath file", ex);
|
2010-12-29 14:05:30 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @param classpathString
|
|
|
|
|
*
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
private Set<String> createClasspathSet(String classpathString)
|
|
|
|
|
{
|
|
|
|
|
if (logger.isDebugEnabled())
|
|
|
|
|
{
|
|
|
|
|
logger.debug("set new plugin classpath: {}", classpathString);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Set<String> classpathSet = new LinkedHashSet<String>();
|
|
|
|
|
|
|
|
|
|
if (Util.isNotEmpty(classpathString))
|
|
|
|
|
{
|
|
|
|
|
String[] classpathParts = classpathString.split(File.pathSeparator);
|
|
|
|
|
int prefixLength = localRepositoryDirectory.getAbsolutePath().length();
|
|
|
|
|
|
|
|
|
|
for (String classpathPart : classpathParts)
|
|
|
|
|
{
|
|
|
|
|
classpathSet.add(classpathPart.substring(prefixLength));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return classpathSet;
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-18 16:25:57 +01:00
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @throws JAXBException
|
|
|
|
|
*/
|
|
|
|
|
private void storeClasspath() throws JAXBException
|
|
|
|
|
{
|
|
|
|
|
Marshaller marshaller = jaxbContext.createMarshaller();
|
|
|
|
|
|
|
|
|
|
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
|
|
|
|
|
marshaller.marshal(classpath, classpathFile);
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-29 14:05:30 +01:00
|
|
|
//~--- get methods ----------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @param skipId
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
private List<Dependency> getInstalledDependencies(String skipId)
|
|
|
|
|
{
|
2013-05-23 19:36:21 +02:00
|
|
|
List<Dependency> dependencies = Lists.newArrayList();
|
2010-12-29 14:05:30 +01:00
|
|
|
Collection<PluginInformation> installed =
|
|
|
|
|
pluginManager.get(new StatePluginFilter(PluginState.INSTALLED));
|
|
|
|
|
|
|
|
|
|
if (installed != null)
|
|
|
|
|
{
|
|
|
|
|
for (PluginInformation plugin : installed)
|
|
|
|
|
{
|
|
|
|
|
String id = plugin.getId();
|
|
|
|
|
|
2011-01-30 15:06:58 +01:00
|
|
|
if (Util.isNotEmpty(id) && ((skipId == null) ||!id.equals(skipId)))
|
2010-12-29 14:05:30 +01:00
|
|
|
{
|
2013-05-23 19:36:21 +02:00
|
|
|
dependencies.add(Aether.createDependency(id));
|
2010-12-29 14:05:30 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return dependencies;
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-13 21:02:39 +01:00
|
|
|
//~--- fields ---------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
/** Field description */
|
|
|
|
|
private Classpath classpath;
|
|
|
|
|
|
|
|
|
|
/** Field description */
|
|
|
|
|
private File classpathFile;
|
|
|
|
|
|
2011-05-09 15:08:42 +02:00
|
|
|
/** Field description */
|
|
|
|
|
private ScmConfiguration configuration;
|
|
|
|
|
|
2010-12-13 21:02:39 +01:00
|
|
|
/** Field description */
|
|
|
|
|
private JAXBContext jaxbContext;
|
|
|
|
|
|
|
|
|
|
/** Field description */
|
|
|
|
|
private LocalRepository localRepository;
|
|
|
|
|
|
|
|
|
|
/** Field description */
|
|
|
|
|
private File localRepositoryDirectory;
|
|
|
|
|
|
2010-12-29 14:05:30 +01:00
|
|
|
/** Field description */
|
|
|
|
|
private PluginManager pluginManager;
|
|
|
|
|
|
2010-12-13 21:02:39 +01:00
|
|
|
/** Field description */
|
|
|
|
|
private List<RemoteRepository> remoteRepositories;
|
|
|
|
|
|
|
|
|
|
/** Field description */
|
|
|
|
|
private RepositorySystem repositorySystem;
|
|
|
|
|
}
|