From 341f030f5cc58081b07f7f579fb62d3493e738fd Mon Sep 17 00:00:00 2001 From: Benjamin Bentmann Date: Tue, 22 Sep 2009 14:17:30 +0000 Subject: [PATCH] o Generalized plugin realm cache git-svn-id: https://svn.apache.org/repos/asf/maven/maven-3/trunk@817669 13f79535-47bb-0310-9956-ffa450edef68 --- .../plugin/DefaultBuildPluginManager.java | 29 +++-------------- .../maven/plugin/DefaultPluginRealmCache.java | 31 +++++++++++-------- .../apache/maven/plugin/PluginRealmCache.java | 16 +++++++--- .../internal/DefaultMavenPluginManager.java | 29 +++++++++++++++++ 4 files changed, 64 insertions(+), 41 deletions(-) diff --git a/maven-core/src/main/java/org/apache/maven/plugin/DefaultBuildPluginManager.java b/maven-core/src/main/java/org/apache/maven/plugin/DefaultBuildPluginManager.java index a24e4f9949..ba1a18703b 100644 --- a/maven-core/src/main/java/org/apache/maven/plugin/DefaultBuildPluginManager.java +++ b/maven-core/src/main/java/org/apache/maven/plugin/DefaultBuildPluginManager.java @@ -46,9 +46,6 @@ public class DefaultBuildPluginManager @Requirement private PlexusContainer container; - @Requirement - private PluginRealmCache pluginCache; - @Requirement private MavenPluginManager mavenPluginManager; @@ -154,30 +151,14 @@ public class DefaultBuildPluginManager Plugin plugin = pluginDescriptor.getPlugin(); - MavenProject project = session.getCurrentProject(); - ArtifactRepository localRepository = session.getLocalRepository(); - List remoteRepositories = project.getPluginArtifactRepositories(); - - PluginRealmCache.CacheRecord cacheRecord = pluginCache.get( plugin, project, localRepository, remoteRepositories ); - - if ( cacheRecord != null ) + try { - pluginDescriptor.setClassRealm( cacheRecord.realm ); - pluginDescriptor.setArtifacts( new ArrayList( cacheRecord.artifacts ) ); + mavenPluginManager.setupPluginRealm( pluginDescriptor, session, + session.getCurrentProject().getClassRealm(), null ); } - else + catch ( PluginResolutionException e ) { - try - { - mavenPluginManager.setupPluginRealm( pluginDescriptor, session, project.getClassRealm(), null ); - } - catch ( PluginResolutionException e ) - { - throw new PluginManagerException( plugin, e.getMessage(), e ); - } - - pluginCache.put( plugin, project, localRepository, remoteRepositories, pluginDescriptor.getClassRealm(), - pluginDescriptor.getArtifacts() ); + throw new PluginManagerException( plugin, e.getMessage(), e ); } return pluginDescriptor.getClassRealm(); diff --git a/maven-core/src/main/java/org/apache/maven/plugin/DefaultPluginRealmCache.java b/maven-core/src/main/java/org/apache/maven/plugin/DefaultPluginRealmCache.java index d08d1eac99..62e19c1849 100644 --- a/maven-core/src/main/java/org/apache/maven/plugin/DefaultPluginRealmCache.java +++ b/maven-core/src/main/java/org/apache/maven/plugin/DefaultPluginRealmCache.java @@ -20,6 +20,7 @@ package org.apache.maven.plugin; */ import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; import java.util.Iterator; import java.util.List; @@ -30,7 +31,6 @@ import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.model.Dependency; import org.apache.maven.model.Exclusion; import org.apache.maven.model.Plugin; -import org.apache.maven.project.MavenProject; import org.codehaus.plexus.classworlds.realm.ClassRealm; import org.codehaus.plexus.component.annotations.Component; @@ -48,22 +48,26 @@ public class DefaultPluginRealmCache private final List repositories = new ArrayList(); - private final ClassRealm projectRealm; + private final ClassLoader parentRealm; + + private final List parentImports; private final int hashCode; - public CacheKey( Plugin plugin, MavenProject project, ArtifactRepository localRepository, + public CacheKey( Plugin plugin, ClassLoader parentRealm, List parentImports, ArtifactRepository localRepository, List remoteRepositories ) { this.plugin = plugin.clone(); this.repositories.add( localRepository ); this.repositories.addAll( remoteRepositories ); - this.projectRealm = project.getClassRealm(); + this.parentRealm = parentRealm; + this.parentImports = ( parentImports != null ) ? parentImports : Collections. emptyList(); int hash = 17; hash = hash * 31 + pluginHashCode( plugin ); hash = hash * 31 + repositories.hashCode(); - hash = hash * 31 + ( projectRealm != null ? projectRealm.hashCode() : 0 ); + hash = hash * 31 + ( parentRealm != null ? parentRealm.hashCode() : 0 ); + hash = hash * 31 + this.parentImports.hashCode(); this.hashCode = hash; } @@ -88,32 +92,33 @@ public class DefaultPluginRealmCache CacheKey other = (CacheKey) o; - return projectRealm == other.projectRealm && pluginEquals( plugin, other.plugin ) + return parentRealm == other.parentRealm && pluginEquals( plugin, other.plugin ) && eq( repositories, other.repositories ); } } protected final Map cache = new HashMap(); - public CacheRecord get( Plugin plugin, MavenProject project, ArtifactRepository localRepository, - List remoteRepositories ) + public CacheRecord get( Plugin plugin, ClassLoader parentRealm, List parentImports, + ArtifactRepository localRepository, List remoteRepositories ) { - return cache.get( new CacheKey( plugin, project, localRepository, remoteRepositories ) ); + return cache.get( new CacheKey( plugin, parentRealm, parentImports, localRepository, remoteRepositories ) ); } - public void put( Plugin plugin, MavenProject project, ArtifactRepository localRepository, - List remoteRepositories, ClassRealm pluginRealm, List pluginArtifacts ) + public void put( Plugin plugin, ClassLoader parentRealm, List parentImports, + ArtifactRepository localRepository, List remoteRepositories, + ClassRealm pluginRealm, List pluginArtifacts ) { if ( pluginRealm == null || pluginArtifacts == null ) { throw new NullPointerException(); } - CacheKey key = new CacheKey( plugin, project, localRepository, remoteRepositories ); + CacheKey key = new CacheKey( plugin, parentRealm, parentImports, localRepository, remoteRepositories ); if ( cache.containsKey( key ) ) { - throw new IllegalStateException( "Duplicate plugin realm for plugin " + plugin ); + throw new IllegalStateException( "Duplicate plugin realm for plugin " + plugin.getId() ); } CacheRecord record = new CacheRecord( pluginRealm, pluginArtifacts ); diff --git a/maven-core/src/main/java/org/apache/maven/plugin/PluginRealmCache.java b/maven-core/src/main/java/org/apache/maven/plugin/PluginRealmCache.java index 31ac90e84d..26b8aeac7a 100644 --- a/maven-core/src/main/java/org/apache/maven/plugin/PluginRealmCache.java +++ b/maven-core/src/main/java/org/apache/maven/plugin/PluginRealmCache.java @@ -24,9 +24,16 @@ import java.util.List; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.model.Plugin; -import org.apache.maven.project.MavenProject; import org.codehaus.plexus.classworlds.realm.ClassRealm; +/** + * Caches plugin class realms. Warning: This is an internal utility interface that is only public for + * technical reasons, it is not part of the public API. In particular, this interface can be changed or deleted without + * prior notice. + * + * @author Igor Fedorenko + * @author Benjamin Bentmann + */ public interface PluginRealmCache { @@ -42,11 +49,12 @@ public interface PluginRealmCache } } - CacheRecord get( Plugin plugin, MavenProject project, ArtifactRepository localRepository, - List remoteRepositories ); + CacheRecord get( Plugin plugin, ClassLoader parentRealm, List parentImports, + ArtifactRepository localRepository, List remoteRepositories ); - void put( Plugin plugin, MavenProject project, ArtifactRepository localRepository, + void put( Plugin plugin, ClassLoader parentRealm, List parentImports, ArtifactRepository localRepository, List remoteRepositories, ClassRealm pluginRealm, List pluginArtifacts ); void flush(); + } diff --git a/maven-core/src/main/java/org/apache/maven/plugin/internal/DefaultMavenPluginManager.java b/maven-core/src/main/java/org/apache/maven/plugin/internal/DefaultMavenPluginManager.java index 5a1e0f7baf..d569bdf695 100644 --- a/maven-core/src/main/java/org/apache/maven/plugin/internal/DefaultMavenPluginManager.java +++ b/maven-core/src/main/java/org/apache/maven/plugin/internal/DefaultMavenPluginManager.java @@ -63,6 +63,7 @@ import org.apache.maven.plugin.PluginDescriptorCache; import org.apache.maven.plugin.PluginDescriptorParsingException; import org.apache.maven.plugin.PluginManagerException; import org.apache.maven.plugin.PluginParameterExpressionEvaluator; +import org.apache.maven.plugin.PluginRealmCache; import org.apache.maven.plugin.PluginResolutionException; import org.apache.maven.plugin.descriptor.MojoDescriptor; import org.apache.maven.plugin.descriptor.PluginDescriptor; @@ -125,6 +126,9 @@ public class DefaultMavenPluginManager @Requirement private PluginDescriptorCache pluginDescriptorCache; + @Requirement + private PluginRealmCache pluginRealmCache; + private PluginDescriptorBuilder builder = new PluginDescriptorBuilder(); public synchronized PluginDescriptor getPluginDescriptor( Plugin plugin, RepositoryRequest repositoryRequest ) @@ -293,6 +297,31 @@ public class DefaultMavenPluginManager { Plugin plugin = pluginDescriptor.getPlugin(); + PluginRealmCache.CacheRecord cacheRecord = + pluginRealmCache.get( plugin, parent, imports, session.getLocalRepository(), + session.getCurrentProject().getPluginArtifactRepositories() ); + + if ( cacheRecord != null ) + { + pluginDescriptor.setClassRealm( cacheRecord.realm ); + pluginDescriptor.setArtifacts( new ArrayList( cacheRecord.artifacts ) ); + } + else + { + createPluginRealm( pluginDescriptor, session, parent, imports ); + + pluginRealmCache.put( plugin, parent, imports, session.getLocalRepository(), + session.getCurrentProject().getPluginArtifactRepositories(), + pluginDescriptor.getClassRealm(), pluginDescriptor.getArtifacts() ); + } + } + + private void createPluginRealm( PluginDescriptor pluginDescriptor, MavenSession session, ClassLoader parent, + List imports ) + throws PluginResolutionException, PluginManagerException + { + Plugin plugin = pluginDescriptor.getPlugin(); + if ( plugin == null ) { throw new IllegalArgumentException( "incomplete plugin descriptor, plugin missing" );