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.ArtifactRepositoryMetadata;
22 import org.apache.maven.artifact.repository.metadata.Metadata;
23 import org.apache.maven.artifact.repository.metadata.Snapshot;
24 import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader;
25 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
28 import java.io.FileNotFoundException;
29 import java.io.FileReader;
30 import java.io.IOException;
31 import java.util.List;
36 public abstract class AbstractRepositoryQueryLayer
37 implements RepositoryQueryLayer
39 protected ArtifactRepository repository;
41 public boolean containsArtifact( Artifact artifact )
43 File f = new File( repository.getBasedir(), repository.pathOf( artifact ) );
47 public boolean containsArtifact( Artifact artifact, Snapshot snapshot )
49 String artifactPath = getSnapshotArtifactRepositoryPath( artifact, snapshot );
50 File artifactFile = new File( artifactPath );
51 return artifactFile.exists();
54 public List getVersions( Artifact artifact )
55 throws RepositoryQueryLayerException
57 Metadata metadata = getMetadata( artifact );
59 return metadata.getVersioning().getVersions();
62 protected String getSnapshotArtifactRepositoryPath( Artifact artifact, Snapshot snapshot )
64 File f = new File( repository.getBasedir(), repository.pathOf( artifact ) );
65 String snapshotInfo = artifact.getVersion().replaceFirst( "SNAPSHOT", snapshot.getTimestamp() + "-" +
66 snapshot.getBuildNumber() + ".pom" );
67 File snapshotFile = new File( f.getParentFile(), artifact.getArtifactId() + "-" + snapshotInfo );
68 return snapshotFile.getAbsolutePath();
71 protected Metadata getMetadata( Artifact artifact )
72 throws RepositoryQueryLayerException
76 ArtifactRepositoryMetadata repositoryMetadata = new ArtifactRepositoryMetadata( artifact );
77 String path = repository.pathOfRemoteRepositoryMetadata( repositoryMetadata );
78 File metadataFile = new File( repository.getBasedir(), path );
79 if ( metadataFile.exists() )
81 MetadataXpp3Reader reader = new MetadataXpp3Reader();
84 metadata = reader.read( new FileReader( metadataFile ) );
86 catch ( FileNotFoundException e )
88 throw new RepositoryQueryLayerException( "Error occurred while attempting to read metadata file", e );
90 catch ( IOException e )
92 throw new RepositoryQueryLayerException( "Error occurred while attempting to read metadata file", e );
94 catch ( XmlPullParserException e )
96 throw new RepositoryQueryLayerException( "Error occurred while attempting to read metadata file", e );
101 throw new RepositoryQueryLayerException( "Metadata not found: " + metadataFile.getAbsolutePath() );