1 package org.apache.maven.archiva.layer;
4 * Copyright 2005-2006 The Apache Software Foundation.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 import org.apache.maven.artifact.Artifact;
20 import org.apache.maven.artifact.repository.ArtifactRepository;
21 import org.apache.maven.artifact.repository.metadata.Metadata;
22 import org.apache.maven.artifact.repository.metadata.Snapshot;
28 public class CachedRepositoryQueryLayer
29 extends AbstractRepositoryQueryLayer
33 public static final double CACHE_HIT_RATIO = 0.5;
35 public CachedRepositoryQueryLayer( ArtifactRepository repository )
37 this.repository = repository;
39 cache = new Cache( CACHE_HIT_RATIO );
42 public double getCacheHitRate()
44 return cache.getHitRate();
47 public boolean containsArtifact( Artifact artifact )
49 boolean artifactFound = true;
51 String artifactPath = repository.getBasedir() + "/" + repository.pathOf( artifact );
53 if ( cache.get( artifactPath ) == null )
55 artifactFound = super.containsArtifact( artifact );
58 cache.put( artifactPath, artifactPath );
65 public boolean containsArtifact( Artifact artifact, Snapshot snapshot )
67 boolean artifactFound = true;
69 String path = getSnapshotArtifactRepositoryPath( artifact, snapshot );
71 if ( cache.get( path ) == null )
73 artifactFound = super.containsArtifact( artifact, snapshot );
76 cache.put( path, path );
84 * Override method to utilize the cache
86 protected Metadata getMetadata( Artifact artifact )
87 throws RepositoryQueryLayerException
89 Metadata metadata = (Metadata) cache.get( artifact.getId() );
91 if ( metadata == null )
93 metadata = super.getMetadata( artifact );
94 cache.put( artifact.getId(), metadata );