o adding a metadata cache

Submitted by: Igor Fedorenko


git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@780080 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Jason van Zyl 2009-05-29 19:22:03 +00:00
parent 192be4ec01
commit 318943638f
3 changed files with 212 additions and 1 deletions

View File

@ -0,0 +1,161 @@
package org.apache.maven.project.artifact;
/*
* 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.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.ArtifactUtils;
import org.apache.maven.artifact.metadata.ResolutionGroup;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.codehaus.plexus.component.annotations.Component;
@Component( role = MavenMetadataCache.class )
public class DefaultMavenMetadataCache
implements MavenMetadataCache
{
public static class CacheKey
{
Artifact artifact;
List<ArtifactRepository> repositories = new ArrayList<ArtifactRepository>();
CacheKey( Artifact artifact, ArtifactRepository localRepository, List<ArtifactRepository> remoteRepositories )
{
this.artifact = ArtifactUtils.copyArtifact( artifact );
this.repositories.add( localRepository );
this.repositories.addAll( remoteRepositories );
}
@Override
public int hashCode()
{
int hash = 17;
hash = hash * 31 + artifact.hashCode();
hash = hash * 31 + repositories.hashCode();
return hash;
}
@Override
public boolean equals( Object o )
{
if ( o == this )
{
return true;
}
if ( !(o instanceof CacheKey) )
{
return false;
}
CacheKey other = (CacheKey) o;
return artifact.equals( other.artifact ) && repositories.equals( other.repositories );
}
}
public class CacheRecord
{
Artifact pomArtifact;
List<Artifact> artifacts;
List<ArtifactRepository> remoteRepositories;
long length;
long timestamp;
CacheRecord(Artifact pomArtifact, Set<Artifact> artifacts, List<ArtifactRepository> remoteRepositories)
{
this.pomArtifact = ArtifactUtils.copyArtifact( pomArtifact );
this.artifacts = copyArtifacts( artifacts );
this.remoteRepositories = new ArrayList<ArtifactRepository>( remoteRepositories );
File pomFile = pomArtifact.getFile();
if ( pomFile != null && pomFile.canRead() )
{
this.length = pomFile.length();
this.timestamp = pomFile.lastModified();
}
else
{
this.length = -1;
this.timestamp = -1;
}
}
public boolean isStale()
{
File pomFile = pomArtifact.getFile();
if ( pomFile != null && pomFile.canRead() )
{
return length != pomFile.length() || timestamp != pomFile.lastModified();
}
return length != -1 || timestamp != -1;
}
}
private Map<CacheKey, CacheRecord> cache = new HashMap<CacheKey, CacheRecord>();
public ResolutionGroup get( Artifact artifact, ArtifactRepository localRepository,
List<ArtifactRepository> remoteRepositories )
{
CacheKey cacheKey = new CacheKey( artifact, localRepository, remoteRepositories );
CacheRecord cacheRecord = cache.get( cacheKey );
if ( cacheRecord != null && !cacheRecord.isStale() )
{
Artifact pomArtifact = ArtifactUtils.copyArtifact( cacheRecord.pomArtifact );
Set<Artifact> artifacts = new LinkedHashSet<Artifact>( copyArtifacts( cacheRecord.artifacts ) );
return new ResolutionGroup( pomArtifact, artifacts , cacheRecord.remoteRepositories );
}
cache.remove( cacheKey );
return null;
}
public void put( Artifact artifact, ArtifactRepository localRepository,
List<ArtifactRepository> remoteRepositories, ResolutionGroup result )
{
CacheKey cacheKey = new CacheKey( artifact, localRepository, remoteRepositories );
CacheRecord cacheRecord = new CacheRecord( result.getPomArtifact(), result.getArtifacts(), result.getResolutionRepositories() );
cache.put( cacheKey, cacheRecord );
}
public static List<Artifact> copyArtifacts( Collection<Artifact> artifacts )
{
ArrayList<Artifact> result = new ArrayList<Artifact>();
for ( Artifact artifact : artifacts )
{
result.add( ArtifactUtils.copyArtifact( artifact ) );
}
return result;
}
}

View File

@ -0,0 +1,34 @@
package org.apache.maven.project.artifact;
import java.util.List;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.metadata.ResolutionGroup;
import org.apache.maven.artifact.repository.ArtifactRepository;
/*
* 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.
*/
public interface MavenMetadataCache
{
ResolutionGroup get( Artifact artifact, ArtifactRepository localRepository,
List<ArtifactRepository> remoteRepositories );
void put( Artifact artifact, ArtifactRepository localRepository,
List<ArtifactRepository> remoteRepositories, ResolutionGroup result );
}

View File

@ -71,9 +71,21 @@ public class MavenMetadataSource
@Requirement @Requirement
private Logger logger; private Logger logger;
@Requirement
private MavenMetadataCache cache;
public ResolutionGroup retrieve( Artifact artifact, ArtifactRepository localRepository, List<ArtifactRepository> remoteRepositories ) public ResolutionGroup retrieve( Artifact artifact, ArtifactRepository localRepository, List<ArtifactRepository> remoteRepositories )
throws ArtifactMetadataRetrievalException throws ArtifactMetadataRetrievalException
{ {
/*
ResolutionGroup cached = cache.get( artifact, localRepository, remoteRepositories );
if ( cached != null )
{
return cached;
}
*/
List<Dependency> dependencies; List<Dependency> dependencies;
Artifact pomArtifact; Artifact pomArtifact;
@ -155,7 +167,11 @@ public class MavenMetadataSource
} }
} }
return new ResolutionGroup( pomArtifact, artifacts, remoteRepositories ); ResolutionGroup result = new ResolutionGroup( pomArtifact, artifacts, remoteRepositories );
//cache.put( artifact, localRepository, remoteRepositories, result );
return result;
} }
private String getEffectiveScope( String originalScope, String inheritedScope ) private String getEffectiveScope( String originalScope, String inheritedScope )