o Revised extension caching to be insensitive to POM repos

git-svn-id: https://svn.apache.org/repos/asf/maven/maven-3/trunk@819868 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Benjamin Bentmann 2009-09-29 09:38:12 +00:00
parent 663f9db7f4
commit 42884072b2
5 changed files with 369 additions and 152 deletions

View File

@ -19,18 +19,13 @@ package org.apache.maven.plugin;
* under the License.
*/
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.RepositoryRequest;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.Exclusion;
import org.apache.maven.model.Plugin;
import org.apache.maven.project.ExtensionDescriptor;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.classworlds.realm.ClassRealm;
@ -47,22 +42,19 @@ public class DefaultExtensionRealmCache
private static class CacheKey
{
private final Plugin extension;
private final List<ArtifactRepository> repositories = new ArrayList<ArtifactRepository>();
private final List<File> files;
private final int hashCode;
public CacheKey( Plugin extension, RepositoryRequest repositoryRequest )
public CacheKey( List<? extends Artifact> extensionArtifacts )
{
this.extension = extension.clone();
this.repositories.add( repositoryRequest.getLocalRepository() );
this.repositories.addAll( repositoryRequest.getRemoteRepositories() );
this.files = new ArrayList<File>( extensionArtifacts.size() );
for ( Artifact artifact : extensionArtifacts )
{
files.add( artifact.getFile() );
}
int hash = 17;
hash = hash * 31 + extensionHashCode( extension );
hash = hash * 31 + repositories.hashCode();
this.hashCode = hash;
this.hashCode = files.hashCode();
}
@Override
@ -86,35 +78,38 @@ public class DefaultExtensionRealmCache
CacheKey other = (CacheKey) o;
return extensionEquals( extension, other.extension ) && eq( repositories, other.repositories );
return files.equals( other.files );
}
}
private final Map<CacheKey, CacheRecord> cache = new HashMap<CacheKey, CacheRecord>();
public CacheRecord get( Plugin extension, RepositoryRequest repositoryRequest )
public CacheRecord get( List<? extends Artifact> extensionArtifacts )
{
return cache.get( new CacheKey( extension, repositoryRequest ) );
return cache.get( new CacheKey( extensionArtifacts ) );
}
public void put( Plugin extension, RepositoryRequest repositoryRequest, ClassRealm extensionRealm,
List<Artifact> extensionArtifacts, ExtensionDescriptor extensionDescriptor )
public CacheRecord put( List<? extends Artifact> extensionArtifacts, ClassRealm extensionRealm,
ExtensionDescriptor extensionDescriptor )
{
if ( extensionRealm == null || extensionArtifacts == null )
if ( extensionRealm == null )
{
throw new NullPointerException();
}
CacheKey key = new CacheKey( extension, repositoryRequest );
CacheKey key = new CacheKey( extensionArtifacts );
if ( cache.containsKey( key ) )
{
throw new IllegalStateException( "Duplicate extension realm for extension " + extension.getId() );
throw new IllegalStateException( "Duplicate extension realm for extension " + extensionArtifacts );
}
CacheRecord record = new CacheRecord( extensionRealm, extensionArtifacts, extensionDescriptor );
CacheRecord record = new CacheRecord( extensionRealm, extensionDescriptor );
cache.put( key, record );
return record;
}
public void flush()
@ -122,108 +117,7 @@ public class DefaultExtensionRealmCache
cache.clear();
}
protected static int extensionHashCode( Plugin extension )
{
int hash = 17;
hash = hash * 31 + extension.getGroupId().hashCode();
hash = hash * 31 + extension.getArtifactId().hashCode();
hash = hash * 31 + extension.getVersion().hashCode();
for ( Dependency dependency : extension.getDependencies() )
{
hash = hash * 31 + dependency.getGroupId().hashCode();
hash = hash * 31 + dependency.getArtifactId().hashCode();
hash = hash * 31 + dependency.getVersion().hashCode();
hash = hash * 31 + dependency.getType().hashCode();
hash = hash * 31 + ( dependency.getClassifier() != null ? dependency.getClassifier().hashCode() : 0 );
hash = hash * 31 + ( dependency.getScope() != null ? dependency.getScope().hashCode() : 0 );
for ( Exclusion exclusion : dependency.getExclusions() )
{
hash = hash * 31 + exclusion.getGroupId().hashCode();
hash = hash * 31 + exclusion.getArtifactId().hashCode();
}
}
return hash;
}
private static boolean extensionEquals( Plugin a, Plugin b )
{
return eq( a.getGroupId(), b.getGroupId() ) //
&& eq( a.getArtifactId(), b.getArtifactId() ) //
&& eq( a.getVersion(), b.getVersion() ) //
&& a.isExtensions() == b.isExtensions() //
&& dependenciesEquals( a.getDependencies(), b.getDependencies() );
}
private static boolean dependenciesEquals( List<Dependency> a, List<Dependency> b )
{
if ( a.size() != b.size() )
{
return false;
}
Iterator<Dependency> aI = a.iterator();
Iterator<Dependency> bI = b.iterator();
while ( aI.hasNext() )
{
Dependency aD = aI.next();
Dependency bD = bI.next();
boolean r = eq( aD.getGroupId(), bD.getGroupId() ) //
&& eq( aD.getArtifactId(), bD.getArtifactId() ) //
&& eq( aD.getVersion(), bD.getVersion() ) //
&& eq( aD.getType(), bD.getType() ) //
&& eq( aD.getClassifier(), bD.getClassifier() ) //
&& eq( aD.getScope(), bD.getScope() );
r &= exclusionsEquals( aD.getExclusions(), bD.getExclusions() );
if ( !r )
{
return false;
}
}
return true;
}
private static boolean exclusionsEquals( List<Exclusion> a, List<Exclusion> b )
{
if ( a.size() != b.size() )
{
return false;
}
Iterator<Exclusion> aI = a.iterator();
Iterator<Exclusion> bI = b.iterator();
while ( aI.hasNext() )
{
Exclusion aD = aI.next();
Exclusion bD = bI.next();
boolean r = eq( aD.getGroupId(), bD.getGroupId() ) //
&& eq( aD.getArtifactId(), bD.getArtifactId() );
if ( !r )
{
return false;
}
}
return true;
}
private static <T> boolean eq( T s1, T s2 )
{
return s1 != null ? s1.equals( s2 ) : s2 == null;
}
public void register( MavenProject project, ClassRealm extensionRealm )
public void register( MavenProject project, CacheRecord record )
{
// default cache does not track extension usage
}

View File

@ -0,0 +1,240 @@
package org.apache.maven.plugin;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.RepositoryRequest;
import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
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.component.annotations.Component;
/**
* @author Igor Fedorenko
* @author Benjamin Bentmann
*/
@Component( role = PluginArtifactsCache.class )
public class DefaultPluginArtifactsCache
implements PluginArtifactsCache
{
private static class CacheKey
{
private final Plugin plugin;
private final List<ArtifactRepository> repositories = new ArrayList<ArtifactRepository>();
private final ArtifactFilter extensionArtifactFilter;
private final int hashCode;
public CacheKey( Plugin plugin, RepositoryRequest repositoryRequest, ArtifactFilter extensionArtifactFilter )
{
this.plugin = plugin.clone();
this.repositories.add( repositoryRequest.getLocalRepository() );
this.repositories.addAll( repositoryRequest.getRemoteRepositories() );
this.extensionArtifactFilter = extensionArtifactFilter;
int hash = 17;
hash = hash * 31 + pluginHashCode( plugin );
hash = hash * 31 + repositories.hashCode();
hash = hash * 31 + ( extensionArtifactFilter != null ? extensionArtifactFilter.hashCode() : 0 );
this.hashCode = hash;
}
@Override
public int hashCode()
{
return hashCode;
}
@Override
public boolean equals( Object o )
{
if ( o == this )
{
return true;
}
if ( !( o instanceof CacheKey ) )
{
return false;
}
CacheKey other = (CacheKey) o;
return pluginEquals( plugin, other.plugin ) && eq( repositories, other.repositories )
&& eq( extensionArtifactFilter, other.extensionArtifactFilter );
}
}
protected final Map<CacheKey, CacheRecord> cache = new HashMap<CacheKey, CacheRecord>();
public CacheRecord get( Plugin plugin, RepositoryRequest repositoryRequest, ArtifactFilter extensionArtifactFilter )
{
return cache.get( new CacheKey( plugin, repositoryRequest, extensionArtifactFilter ) );
}
public CacheRecord put( Plugin plugin, RepositoryRequest repositoryRequest, ArtifactFilter extensionArtifactFilter,
List<Artifact> pluginArtifacts )
{
if ( pluginArtifacts == null )
{
throw new NullPointerException();
}
CacheKey key = new CacheKey( plugin, repositoryRequest, extensionArtifactFilter );
if ( cache.containsKey( key ) )
{
throw new IllegalStateException( "Duplicate artifact resolution result for plugin " + plugin.getId() );
}
CacheRecord record =
new CacheRecord( Collections.unmodifiableList( new ArrayList<Artifact>( pluginArtifacts ) ) );
cache.put( key, record );
return record;
}
public void flush()
{
cache.clear();
}
protected static int pluginHashCode( Plugin plugin )
{
int hash = 17;
hash = hash * 31 + plugin.getGroupId().hashCode();
hash = hash * 31 + plugin.getArtifactId().hashCode();
hash = hash * 31 + plugin.getVersion().hashCode();
for ( Dependency dependency : plugin.getDependencies() )
{
hash = hash * 31 + dependency.getGroupId().hashCode();
hash = hash * 31 + dependency.getArtifactId().hashCode();
hash = hash * 31 + dependency.getVersion().hashCode();
hash = hash * 31 + dependency.getType().hashCode();
hash = hash * 31 + ( dependency.getClassifier() != null ? dependency.getClassifier().hashCode() : 0 );
hash = hash * 31 + ( dependency.getScope() != null ? dependency.getScope().hashCode() : 0 );
for ( Exclusion exclusion : dependency.getExclusions() )
{
hash = hash * 31 + exclusion.getGroupId().hashCode();
hash = hash * 31 + exclusion.getArtifactId().hashCode();
}
}
return hash;
}
protected static boolean pluginEquals( Plugin a, Plugin b )
{
return eq( a.getGroupId(), b.getGroupId() ) //
&& eq( a.getArtifactId(), b.getArtifactId() ) //
&& eq( a.getVersion(), b.getVersion() ) //
&& dependenciesEquals( a.getDependencies(), b.getDependencies() );
}
private static boolean dependenciesEquals( List<Dependency> a, List<Dependency> b )
{
if ( a.size() != b.size() )
{
return false;
}
Iterator<Dependency> aI = a.iterator();
Iterator<Dependency> bI = b.iterator();
while ( aI.hasNext() )
{
Dependency aD = aI.next();
Dependency bD = bI.next();
boolean r = eq( aD.getGroupId(), bD.getGroupId() ) //
&& eq( aD.getArtifactId(), bD.getArtifactId() ) //
&& eq( aD.getVersion(), bD.getVersion() ) //
&& eq( aD.getType(), bD.getType() ) //
&& eq( aD.getClassifier(), bD.getClassifier() ) //
&& eq( aD.getScope(), bD.getScope() );
r &= exclusionsEquals( aD.getExclusions(), bD.getExclusions() );
if ( !r )
{
return false;
}
}
return true;
}
private static boolean exclusionsEquals( List<Exclusion> a, List<Exclusion> b )
{
if ( a.size() != b.size() )
{
return false;
}
Iterator<Exclusion> aI = a.iterator();
Iterator<Exclusion> bI = b.iterator();
while ( aI.hasNext() )
{
Exclusion aD = aI.next();
Exclusion bD = bI.next();
boolean r = eq( aD.getGroupId(), bD.getGroupId() ) //
&& eq( aD.getArtifactId(), bD.getArtifactId() );
if ( !r )
{
return false;
}
}
return true;
}
private static <T> boolean eq( T s1, T s2 )
{
return s1 != null ? s1.equals( s2 ) : s2 == null;
}
public void register( MavenProject project, CacheRecord record )
{
// default cache does not track record usage
}
}

View File

@ -22,8 +22,6 @@ package org.apache.maven.plugin;
import java.util.List;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.repository.RepositoryRequest;
import org.apache.maven.model.Plugin;
import org.apache.maven.project.ExtensionDescriptor;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.classworlds.realm.ClassRealm;
@ -44,34 +42,31 @@ public interface ExtensionRealmCache
public final ClassRealm realm;
public final List<Artifact> artifacts;
public final ExtensionDescriptor desciptor;
public CacheRecord( ClassRealm realm, List<Artifact> artifacts, ExtensionDescriptor descriptor )
public CacheRecord( ClassRealm realm, ExtensionDescriptor descriptor )
{
this.realm = realm;
this.artifacts = artifacts;
this.desciptor = descriptor;
}
}
CacheRecord get( Plugin extension, RepositoryRequest repositoryRequest );
CacheRecord get( List<? extends Artifact> extensionArtifacts );
void put( Plugin extension, RepositoryRequest repositoryRequest, ClassRealm extensionRealm,
List<Artifact> extensionArtifacts, ExtensionDescriptor extensionDescriptor );
CacheRecord put( List<? extends Artifact> extensionArtifacts, ClassRealm extensionRealm,
ExtensionDescriptor extensionDescriptor );
void flush();
/**
* Registers the specified extension realm for usage with the given project. Integrators can use the information
* collected from this method in combination with a custom cache implementation to dispose unused extension realms
* from the cache.
* Registers the specified cache record for usage with the given project. Integrators can use the information
* collected from this method in combination with a custom cache implementation to dispose unused records from the
* cache.
*
* @param project The project that employs the extension realm, must not be {@code null}.
* @param extensionRealm The extension realm being used for the project, must not be {@code null}.
* @param project The project that employs the plugin realm, must not be {@code null}.
* @param record The cache record being used for the project, must not be {@code null}.
*/
void register( MavenProject project, ClassRealm extensionRealm );
void register( MavenProject project, CacheRecord record );
}

View File

@ -0,0 +1,70 @@
package org.apache.maven.plugin;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import java.util.List;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.repository.RepositoryRequest;
import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
import org.apache.maven.model.Plugin;
import org.apache.maven.project.MavenProject;
/**
* Caches plugin artifacts. <strong>Warning:</strong> 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 PluginArtifactsCache
{
public static class CacheRecord
{
public final List<Artifact> artifacts;
public CacheRecord( List<Artifact> artifacts )
{
this.artifacts = artifacts;
}
}
CacheRecord get( Plugin plugin, RepositoryRequest repositoryRequest, ArtifactFilter extensionArtifactFilter );
CacheRecord put( Plugin plugin, RepositoryRequest repositoryRequest, ArtifactFilter extensionArtifactFilter,
List<Artifact> pluginArtifacts );
void flush();
/**
* Registers the specified cache record for usage with the given project. Integrators can use the information
* collected from this method in combination with a custom cache implementation to dispose unused records from the
* cache.
*
* @param project The project that employs the plugin realm, must not be {@code null}.
* @param record The cache record being used for the project, must not be {@code null}.
*/
void register( MavenProject project, CacheRecord record );
}

View File

@ -49,6 +49,7 @@ import org.apache.maven.model.Model;
import org.apache.maven.model.Plugin;
import org.apache.maven.model.Repository;
import org.apache.maven.plugin.ExtensionRealmCache;
import org.apache.maven.plugin.PluginArtifactsCache;
import org.apache.maven.plugin.version.DefaultPluginVersionRequest;
import org.apache.maven.plugin.version.PluginVersionRequest;
import org.apache.maven.plugin.version.PluginVersionResolutionException;
@ -81,6 +82,9 @@ public class DefaultProjectBuildingHelper
@Requirement
private ClassRealmManager classRealmManager;
@Requirement
private PluginArtifactsCache pluginArtifactsCache;
@Requirement
private ExtensionRealmCache extensionRealmCache;
@ -180,22 +184,36 @@ public class DefaultProjectBuildingHelper
plugin.setVersion( pluginVersionResolver.resolve( versionRequest ).getVersion() );
}
ClassRealm extensionRealm;
List<Artifact> artifacts;
ExtensionDescriptor extensionDescriptor = null;
ExtensionRealmCache.CacheRecord record = extensionRealmCache.get( plugin, repositoryRequest );
PluginArtifactsCache.CacheRecord recordArtifacts =
pluginArtifactsCache.get( plugin, repositoryRequest, null );
if ( record != null )
if ( recordArtifacts != null )
{
extensionRealm = record.realm;
artifacts = record.artifacts;
extensionDescriptor = record.desciptor;
artifacts = recordArtifacts.artifacts;
}
else
{
artifacts = resolveExtensionArtifacts( plugin, repositoryRequest );
recordArtifacts = pluginArtifactsCache.put( plugin, repositoryRequest, null, artifacts );
}
pluginArtifactsCache.register( project, recordArtifacts );
ClassRealm extensionRealm;
ExtensionDescriptor extensionDescriptor = null;
ExtensionRealmCache.CacheRecord recordRealm = extensionRealmCache.get( artifacts );
if ( recordRealm != null )
{
extensionRealm = recordRealm.realm;
extensionDescriptor = recordRealm.desciptor;
}
else
{
extensionRealm = classRealmManager.createExtensionRealm( plugin );
if ( logger.isDebugEnabled() )
@ -258,10 +276,10 @@ public class DefaultProjectBuildingHelper
}
}
extensionRealmCache.put( plugin, repositoryRequest, extensionRealm, artifacts, extensionDescriptor );
recordRealm = extensionRealmCache.put( artifacts, extensionRealm, extensionDescriptor );
}
extensionRealmCache.register( project, extensionRealm );
extensionRealmCache.register( project, recordRealm );
extensionRealms.add( extensionRealm );
if ( extensionDescriptor != null )