mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-07 22:15:45 +01:00
improve plugin dependency handling
This commit is contained in:
@@ -83,9 +83,19 @@ public class AetherDependencyFilter implements DependencyFilter
|
|||||||
@Override
|
@Override
|
||||||
public boolean accept(DependencyNode node, List<DependencyNode> parents)
|
public boolean accept(DependencyNode node, List<DependencyNode> parents)
|
||||||
{
|
{
|
||||||
Artifact artifact = node.getDependency().getArtifact();
|
boolean result = true;
|
||||||
|
|
||||||
return !exludeSet.contains(getId(artifact));
|
if ((node != null) && (node.getDependency() != null))
|
||||||
|
{
|
||||||
|
Artifact artifact = node.getDependency().getArtifact();
|
||||||
|
|
||||||
|
if (artifact != null)
|
||||||
|
{
|
||||||
|
result = !exludeSet.contains(getId(artifact));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -51,21 +51,23 @@ import org.sonatype.aether.connector.wagon.WagonProvider;
|
|||||||
import org.sonatype.aether.connector.wagon.WagonRepositoryConnectorFactory;
|
import org.sonatype.aether.connector.wagon.WagonRepositoryConnectorFactory;
|
||||||
import org.sonatype.aether.graph.Dependency;
|
import org.sonatype.aether.graph.Dependency;
|
||||||
import org.sonatype.aether.graph.DependencyFilter;
|
import org.sonatype.aether.graph.DependencyFilter;
|
||||||
|
import org.sonatype.aether.graph.DependencyNode;
|
||||||
import org.sonatype.aether.impl.ArtifactDescriptorReader;
|
import org.sonatype.aether.impl.ArtifactDescriptorReader;
|
||||||
import org.sonatype.aether.impl.VersionRangeResolver;
|
import org.sonatype.aether.impl.VersionRangeResolver;
|
||||||
import org.sonatype.aether.impl.VersionResolver;
|
import org.sonatype.aether.impl.VersionResolver;
|
||||||
import org.sonatype.aether.impl.internal.DefaultServiceLocator;
|
import org.sonatype.aether.impl.internal.DefaultServiceLocator;
|
||||||
import org.sonatype.aether.repository.LocalRepository;
|
import org.sonatype.aether.repository.LocalRepository;
|
||||||
import org.sonatype.aether.repository.RemoteRepository;
|
import org.sonatype.aether.repository.RemoteRepository;
|
||||||
import org.sonatype.aether.resolution.ArtifactResult;
|
|
||||||
import org.sonatype.aether.spi.connector.RepositoryConnectorFactory;
|
import org.sonatype.aether.spi.connector.RepositoryConnectorFactory;
|
||||||
import org.sonatype.aether.util.artifact.DefaultArtifact;
|
import org.sonatype.aether.util.artifact.DefaultArtifact;
|
||||||
|
import org.sonatype.aether.util.graph.PreorderNodeListGenerator;
|
||||||
|
|
||||||
import sonia.scm.ConfigurationException;
|
import sonia.scm.ConfigurationException;
|
||||||
import sonia.scm.SCMContextProvider;
|
import sonia.scm.SCMContextProvider;
|
||||||
import sonia.scm.boot.BootstrapListener;
|
import sonia.scm.boot.BootstrapListener;
|
||||||
import sonia.scm.boot.Classpath;
|
import sonia.scm.boot.Classpath;
|
||||||
import sonia.scm.util.IOUtil;
|
import sonia.scm.util.IOUtil;
|
||||||
|
import sonia.scm.util.Util;
|
||||||
|
|
||||||
//~--- JDK imports ------------------------------------------------------------
|
//~--- JDK imports ------------------------------------------------------------
|
||||||
|
|
||||||
@@ -73,8 +75,9 @@ import java.io.File;
|
|||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Iterator;
|
import java.util.LinkedHashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
import javax.xml.bind.JAXBContext;
|
import javax.xml.bind.JAXBContext;
|
||||||
import javax.xml.bind.JAXBException;
|
import javax.xml.bind.JAXBException;
|
||||||
@@ -103,10 +106,14 @@ public class AetherPluginHandler
|
|||||||
* Constructs ...
|
* Constructs ...
|
||||||
*
|
*
|
||||||
*
|
*
|
||||||
|
*
|
||||||
|
* @param pluginManager
|
||||||
* @param context
|
* @param context
|
||||||
*/
|
*/
|
||||||
public AetherPluginHandler(SCMContextProvider context)
|
public AetherPluginHandler(PluginManager pluginManager,
|
||||||
|
SCMContextProvider context)
|
||||||
{
|
{
|
||||||
|
this.pluginManager = pluginManager;
|
||||||
localRepositoryDirectory = new File(context.getBaseDirectory(),
|
localRepositoryDirectory = new File(context.getBaseDirectory(),
|
||||||
BootstrapListener.PLUGIN_DIRECTORY);
|
BootstrapListener.PLUGIN_DIRECTORY);
|
||||||
|
|
||||||
@@ -157,96 +164,34 @@ public class AetherPluginHandler
|
|||||||
|
|
||||||
Dependency dependency = new Dependency(new DefaultArtifact(gav),
|
Dependency dependency = new Dependency(new DefaultArtifact(gav),
|
||||||
PLUGIN_SCOPE);
|
PLUGIN_SCOPE);
|
||||||
CollectRequest request = new CollectRequest(dependency, remoteRepositories);
|
List<Dependency> dependencies = getInstalledDependencies(null);
|
||||||
MavenRepositorySystemSession session = new MavenRepositorySystemSession();
|
|
||||||
|
|
||||||
session.setLocalRepositoryManager(
|
collectDependencies(dependency, dependencies);
|
||||||
repositorySystem.newLocalRepositoryManager(localRepository));
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
|
|
||||||
/*
|
|
||||||
* DependencyNode node = repositorySystem.collectDependencies(session,
|
|
||||||
* request).getRoot();
|
|
||||||
*/
|
|
||||||
List<ArtifactResult> artifacts =
|
|
||||||
repositorySystem.resolveDependencies(session, request, FILTER);
|
|
||||||
|
|
||||||
synchronized (Classpath.class)
|
|
||||||
{
|
|
||||||
if (classpath == null)
|
|
||||||
{
|
|
||||||
classpath = new Classpath();
|
|
||||||
}
|
|
||||||
|
|
||||||
for (ArtifactResult result : artifacts)
|
|
||||||
{
|
|
||||||
File file = result.getArtifact().getFile();
|
|
||||||
|
|
||||||
if (logger.isDebugEnabled())
|
|
||||||
{
|
|
||||||
logger.debug("added {} to classpath", file.getPath());
|
|
||||||
}
|
|
||||||
|
|
||||||
classpath.add(
|
|
||||||
file.getAbsolutePath().substring(
|
|
||||||
localRepositoryDirectory.getAbsolutePath().length()));
|
|
||||||
}
|
|
||||||
|
|
||||||
storeClasspath();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
throw new PluginException(ex);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TODO: remove dependencies and remove files
|
* TODO: remove dependencies and remove files
|
||||||
*
|
*
|
||||||
*
|
*
|
||||||
* @param pluginPath
|
*
|
||||||
|
* @param id
|
||||||
*/
|
*/
|
||||||
public void uninstall(String pluginPath)
|
public void uninstall(String id)
|
||||||
{
|
{
|
||||||
if (logger.isInfoEnabled())
|
if (logger.isInfoEnabled())
|
||||||
{
|
{
|
||||||
logger.info("try to uninstall plugin: {}", pluginPath);
|
logger.info("try to uninstall plugin: {}", id);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (classpath != null)
|
if (classpath != null)
|
||||||
{
|
{
|
||||||
synchronized (Classpath.class)
|
synchronized (Classpath.class)
|
||||||
{
|
{
|
||||||
Iterator<String> iterator = classpath.iterator();
|
List<Dependency> dependencies = getInstalledDependencies(id);
|
||||||
|
|
||||||
while (iterator.hasNext())
|
collectDependencies(null, dependencies);
|
||||||
{
|
|
||||||
String path = iterator.next();
|
|
||||||
|
|
||||||
if (pluginPath.contains(path))
|
|
||||||
{
|
|
||||||
if (logger.isInfoEnabled())
|
|
||||||
{
|
|
||||||
logger.info("remove {} from classpath", path);
|
|
||||||
}
|
|
||||||
|
|
||||||
iterator.remove();
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
storeClasspath();
|
|
||||||
}
|
|
||||||
catch (JAXBException ex)
|
|
||||||
{
|
|
||||||
throw new PluginException(ex);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -272,6 +217,86 @@ public class AetherPluginHandler
|
|||||||
|
|
||||||
//~--- methods --------------------------------------------------------------
|
//~--- methods --------------------------------------------------------------
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method description
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @param dependency
|
||||||
|
* @param dependencies
|
||||||
|
*/
|
||||||
|
private void collectDependencies(Dependency dependency,
|
||||||
|
List<Dependency> dependencies)
|
||||||
|
{
|
||||||
|
CollectRequest request = new CollectRequest(dependency, dependencies,
|
||||||
|
remoteRepositories);
|
||||||
|
MavenRepositorySystemSession session = new MavenRepositorySystemSession();
|
||||||
|
|
||||||
|
session.setLocalRepositoryManager(
|
||||||
|
repositorySystem.newLocalRepositoryManager(localRepository));
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
DependencyNode node = repositorySystem.collectDependencies(session,
|
||||||
|
request).getRoot();
|
||||||
|
|
||||||
|
repositorySystem.resolveDependencies(session, node, FILTER);
|
||||||
|
|
||||||
|
synchronized (Classpath.class)
|
||||||
|
{
|
||||||
|
if (classpath == null)
|
||||||
|
{
|
||||||
|
classpath = new Classpath();
|
||||||
|
}
|
||||||
|
|
||||||
|
PreorderNodeListGenerator nodeListGenerator =
|
||||||
|
new PreorderNodeListGenerator();
|
||||||
|
|
||||||
|
node.accept(nodeListGenerator);
|
||||||
|
|
||||||
|
Set<String> classpathSet =
|
||||||
|
createClasspathSet(nodeListGenerator.getClassPath());
|
||||||
|
|
||||||
|
classpath.setPathSet(classpathSet);
|
||||||
|
storeClasspath();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
throw new PluginException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method description
|
* Method description
|
||||||
*
|
*
|
||||||
@@ -317,6 +342,42 @@ public class AetherPluginHandler
|
|||||||
marshaller.marshal(classpath, classpathFile);
|
marshaller.marshal(classpath, classpathFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//~--- get methods ----------------------------------------------------------
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method description
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @param skipId
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private List<Dependency> getInstalledDependencies(String skipId)
|
||||||
|
{
|
||||||
|
List<Dependency> dependencies = new ArrayList<Dependency>();
|
||||||
|
Collection<PluginInformation> installed =
|
||||||
|
pluginManager.get(new StatePluginFilter(PluginState.INSTALLED));
|
||||||
|
|
||||||
|
if (installed != null)
|
||||||
|
{
|
||||||
|
for (PluginInformation plugin : installed)
|
||||||
|
{
|
||||||
|
String id = plugin.getId();
|
||||||
|
|
||||||
|
if ((skipId == null) ||!id.equals(skipId))
|
||||||
|
{
|
||||||
|
if (Util.isNotEmpty(id))
|
||||||
|
{
|
||||||
|
dependencies.add(new Dependency(new DefaultArtifact(id),
|
||||||
|
PLUGIN_SCOPE));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return dependencies;
|
||||||
|
}
|
||||||
|
|
||||||
//~--- fields ---------------------------------------------------------------
|
//~--- fields ---------------------------------------------------------------
|
||||||
|
|
||||||
/** Field description */
|
/** Field description */
|
||||||
@@ -334,6 +395,9 @@ public class AetherPluginHandler
|
|||||||
/** Field description */
|
/** Field description */
|
||||||
private File localRepositoryDirectory;
|
private File localRepositoryDirectory;
|
||||||
|
|
||||||
|
/** Field description */
|
||||||
|
private PluginManager pluginManager;
|
||||||
|
|
||||||
/** Field description */
|
/** Field description */
|
||||||
private List<RemoteRepository> remoteRepositories;
|
private List<RemoteRepository> remoteRepositories;
|
||||||
|
|
||||||
|
|||||||
@@ -50,6 +50,7 @@ import sonia.scm.config.ScmConfiguration;
|
|||||||
import sonia.scm.security.SecurityContext;
|
import sonia.scm.security.SecurityContext;
|
||||||
import sonia.scm.util.AssertUtil;
|
import sonia.scm.util.AssertUtil;
|
||||||
import sonia.scm.util.SecurityUtil;
|
import sonia.scm.util.SecurityUtil;
|
||||||
|
import sonia.scm.util.Util;
|
||||||
|
|
||||||
//~--- JDK imports ------------------------------------------------------------
|
//~--- JDK imports ------------------------------------------------------------
|
||||||
|
|
||||||
@@ -154,12 +155,25 @@ public class DefaultPluginManager implements PluginManager
|
|||||||
{
|
{
|
||||||
SecurityUtil.assertIsAdmin(securityContextProvicer);
|
SecurityUtil.assertIsAdmin(securityContextProvicer);
|
||||||
|
|
||||||
if (pluginHandler == null)
|
PluginCenter center = getPluginCenter();
|
||||||
{
|
|
||||||
getPluginCenter();
|
|
||||||
}
|
|
||||||
|
|
||||||
pluginHandler.install(id);
|
pluginHandler.install(id);
|
||||||
|
|
||||||
|
for (PluginInformation plugin : center.getPlugins())
|
||||||
|
{
|
||||||
|
String pluginId = plugin.getId();
|
||||||
|
|
||||||
|
if (Util.isNotEmpty(pluginId) && pluginId.equals(id))
|
||||||
|
{
|
||||||
|
plugin.setState(PluginState.INSTALLED);
|
||||||
|
|
||||||
|
// ugly workaround
|
||||||
|
Plugin newPlugin = new Plugin();
|
||||||
|
|
||||||
|
newPlugin.setInformation(plugin);
|
||||||
|
installedPlugins.put(id, newPlugin);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -182,10 +196,12 @@ public class DefaultPluginManager implements PluginManager
|
|||||||
|
|
||||||
if (pluginHandler == null)
|
if (pluginHandler == null)
|
||||||
{
|
{
|
||||||
pluginHandler = new AetherPluginHandler(SCMContext.getContext());
|
getPluginCenter();
|
||||||
}
|
}
|
||||||
|
|
||||||
pluginHandler.uninstall(plugin.getPath());
|
pluginHandler.uninstall(id);
|
||||||
|
installedPlugins.remove(id);
|
||||||
|
preparePlugins(getPluginCenter());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -465,7 +481,8 @@ public class DefaultPluginManager implements PluginManager
|
|||||||
|
|
||||||
if (pluginHandler == null)
|
if (pluginHandler == null)
|
||||||
{
|
{
|
||||||
pluginHandler = new AetherPluginHandler(SCMContext.getContext());
|
pluginHandler = new AetherPluginHandler(this,
|
||||||
|
SCMContext.getContext());
|
||||||
}
|
}
|
||||||
|
|
||||||
pluginHandler.setPluginRepositories(center.getRepositories());
|
pluginHandler.setPluginRepositories(center.getRepositories());
|
||||||
|
|||||||
@@ -46,6 +46,8 @@ Sonia.plugin.Store = Ext.extend(Sonia.rest.JsonStore, {
|
|||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Sonia.plugin.StoreInstance = null;
|
||||||
|
|
||||||
Sonia.plugin.GetPluginId = function(data){
|
Sonia.plugin.GetPluginId = function(data){
|
||||||
return data.groupId + ':' + data.artifactId + ':' + data.version;
|
return data.groupId + ':' + data.artifactId + ':' + data.version;
|
||||||
}
|
}
|
||||||
@@ -84,6 +86,7 @@ Sonia.plugin.installPlugin = function(pluginId){
|
|||||||
loadingBox.hide();
|
loadingBox.hide();
|
||||||
Ext.MessageBox.alert('Plugin successfully installed',
|
Ext.MessageBox.alert('Plugin successfully installed',
|
||||||
'Restart the applicationserver to activate the plugin.');
|
'Restart the applicationserver to activate the plugin.');
|
||||||
|
Sonia.plugin.StoreInstance.reload();
|
||||||
},
|
},
|
||||||
failure: function(){
|
failure: function(){
|
||||||
if ( debug ){
|
if ( debug ){
|
||||||
@@ -126,6 +129,7 @@ Sonia.plugin.uninstallPlugin = function(pluginId){
|
|||||||
loadingBox.hide();
|
loadingBox.hide();
|
||||||
Ext.MessageBox.alert('Plugin successfully uninstalled',
|
Ext.MessageBox.alert('Plugin successfully uninstalled',
|
||||||
'Restart the applicationserver to activate the changes.');
|
'Restart the applicationserver to activate the changes.');
|
||||||
|
Sonia.plugin.StoreInstance.reload();
|
||||||
},
|
},
|
||||||
failure: function(){
|
failure: function(){
|
||||||
if ( debug ){
|
if ( debug ){
|
||||||
@@ -168,6 +172,7 @@ Sonia.plugin.updatePlugin = function(pluginId){
|
|||||||
loadingBox.hide();
|
loadingBox.hide();
|
||||||
Ext.MessageBox.alert('Plugin successfully updated',
|
Ext.MessageBox.alert('Plugin successfully updated',
|
||||||
'Restart the applicationserver to activate the changes.');
|
'Restart the applicationserver to activate the changes.');
|
||||||
|
Sonia.plugin.StoreInstance.reload();
|
||||||
},
|
},
|
||||||
failure: function(){
|
failure: function(){
|
||||||
if ( debug ){
|
if ( debug ){
|
||||||
@@ -192,10 +197,11 @@ Sonia.plugin.Grid = Ext.extend(Sonia.rest.Grid, {
|
|||||||
|
|
||||||
initComponent: function(){
|
initComponent: function(){
|
||||||
|
|
||||||
var pluginStore = new Sonia.plugin.Store({
|
Sonia.plugin.StoreInstance = new Sonia.plugin.Store({
|
||||||
url: restUrl + 'plugins.json'
|
url: restUrl + 'plugins.json'
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
var pluginColModel = new Ext.grid.ColumnModel({
|
var pluginColModel = new Ext.grid.ColumnModel({
|
||||||
defaults: {
|
defaults: {
|
||||||
sortable: true,
|
sortable: true,
|
||||||
@@ -215,7 +221,7 @@ Sonia.plugin.Grid = Ext.extend(Sonia.rest.Grid, {
|
|||||||
|
|
||||||
var config = {
|
var config = {
|
||||||
autoExpandColumn: 'description',
|
autoExpandColumn: 'description',
|
||||||
store: pluginStore,
|
store: Sonia.plugin.StoreInstance,
|
||||||
colModel: pluginColModel,
|
colModel: pluginColModel,
|
||||||
emptyText: 'No plugins avaiable'
|
emptyText: 'No plugins avaiable'
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user