1 package org.apache.maven.archiva.repository.content;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.commons.io.FileUtils;
23 import org.apache.maven.archiva.common.utils.PathUtil;
24 import org.apache.maven.archiva.configuration.FileTypes;
25 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
26 import org.apache.maven.archiva.model.ArchivaArtifact;
27 import org.apache.maven.archiva.model.ArtifactReference;
28 import org.apache.maven.archiva.model.ProjectReference;
29 import org.apache.maven.archiva.model.VersionedReference;
30 import org.apache.maven.archiva.repository.ContentNotFoundException;
31 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
32 import org.apache.maven.archiva.repository.layout.LayoutException;
35 import java.io.IOException;
36 import java.util.HashSet;
40 * ManagedDefaultRepositoryContent
42 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
46 * role="org.apache.maven.archiva.repository.ManagedRepositoryContent"
48 * instantiation-strategy="per-lookup"
50 public class ManagedDefaultRepositoryContent
51 extends AbstractDefaultRepositoryContent
52 implements ManagedRepositoryContent
57 private FileTypes filetypes;
59 private ManagedRepositoryConfiguration repository;
61 public void deleteVersion( VersionedReference reference )
62 throws ContentNotFoundException
64 String path = toMetadataPath( reference );
65 File projectPath = new File( getRepoRoot(), path );
67 File projectDir = projectPath.getParentFile();
68 if( projectDir.exists() && projectDir.isDirectory() )
72 FileUtils.deleteDirectory( projectDir );
74 catch ( IOException e )
76 // TODO: log this somewhere?
83 return repository.getId();
86 public Set<ArtifactReference> getRelatedArtifacts( ArtifactReference reference )
87 throws ContentNotFoundException, LayoutException
89 File artifactFile = toFile( reference );
90 File repoDir = artifactFile.getParentFile();
92 if ( !repoDir.exists() )
94 throw new ContentNotFoundException( "Unable to get related artifacts using a non-existant directory: "
95 + repoDir.getAbsolutePath() );
98 if ( !repoDir.isDirectory() )
100 throw new ContentNotFoundException( "Unable to get related artifacts using a non-directory: "
101 + repoDir.getAbsolutePath() );
104 Set<ArtifactReference> foundArtifacts = new HashSet<ArtifactReference>();
106 // First gather up the versions found as artifacts in the managed repository.
107 File repoFiles[] = repoDir.listFiles();
108 for ( int i = 0; i < repoFiles.length; i++ )
110 if ( repoFiles[i].isDirectory() )
112 // Skip it. it's a directory.
116 String relativePath = PathUtil.getRelative( repository.getLocation(), repoFiles[i] );
118 if ( filetypes.matchesArtifactPattern( relativePath ) )
120 ArtifactReference artifact = toArtifactReference( relativePath );
122 // Test for related, groupId / artifactId / version must match.
123 if ( artifact.getGroupId().equals( reference.getGroupId() )
124 && artifact.getArtifactId().equals( reference.getArtifactId() )
125 && artifact.getVersion().equals( reference.getVersion() ) )
127 foundArtifacts.add( artifact );
132 return foundArtifacts;
135 public String getRepoRoot()
137 return repository.getLocation();
140 public ManagedRepositoryConfiguration getRepository()
146 * Gather the Available Versions (on disk) for a specific Project Reference, based on filesystem
149 * @return the Set of available versions, based on the project reference.
150 * @throws LayoutException
151 * @throws LayoutException
153 public Set<String> getVersions( ProjectReference reference )
154 throws ContentNotFoundException, LayoutException
156 String path = toMetadataPath( reference );
158 int idx = path.lastIndexOf( '/' );
161 path = path.substring( 0, idx );
164 File repoDir = new File( repository.getLocation(), path );
166 if ( !repoDir.exists() )
168 throw new ContentNotFoundException( "Unable to get Versions on a non-existant directory: "
169 + repoDir.getAbsolutePath() );
172 if ( !repoDir.isDirectory() )
174 throw new ContentNotFoundException( "Unable to get Versions on a non-directory: "
175 + repoDir.getAbsolutePath() );
178 Set<String> foundVersions = new HashSet<String>();
179 VersionedReference versionRef = new VersionedReference();
180 versionRef.setGroupId( reference.getGroupId() );
181 versionRef.setArtifactId( reference.getArtifactId() );
183 File repoFiles[] = repoDir.listFiles();
184 for ( int i = 0; i < repoFiles.length; i++ )
186 if ( !repoFiles[i].isDirectory() )
188 // Skip it. not a directory.
192 // Test if dir has an artifact, which proves to us that it is a valid version directory.
193 String version = repoFiles[i].getName();
194 versionRef.setVersion( version );
196 if ( hasArtifact( versionRef ) )
198 // Found an artifact, must be a valid version.
199 foundVersions.add( version );
203 return foundVersions;
206 public Set<String> getVersions( VersionedReference reference )
207 throws ContentNotFoundException, LayoutException
209 String path = toMetadataPath( reference );
211 int idx = path.lastIndexOf( '/' );
214 path = path.substring( 0, idx );
217 File repoDir = new File( repository.getLocation(), path );
219 if ( !repoDir.exists() )
221 throw new ContentNotFoundException( "Unable to get versions on a non-existant directory: "
222 + repoDir.getAbsolutePath() );
225 if ( !repoDir.isDirectory() )
227 throw new ContentNotFoundException( "Unable to get versions on a non-directory: "
228 + repoDir.getAbsolutePath() );
231 Set<String> foundVersions = new HashSet<String>();
233 // First gather up the versions found as artifacts in the managed repository.
234 File repoFiles[] = repoDir.listFiles();
235 for ( int i = 0; i < repoFiles.length; i++ )
237 if ( repoFiles[i].isDirectory() )
239 // Skip it. it's a directory.
243 String relativePath = PathUtil.getRelative( repository.getLocation(), repoFiles[i] );
245 if ( filetypes.matchesDefaultExclusions( relativePath ) )
247 // Skip it, it's metadata or similar
251 if ( filetypes.matchesArtifactPattern( relativePath ) )
253 ArtifactReference artifact = toArtifactReference( relativePath );
255 foundVersions.add( artifact.getVersion() );
259 return foundVersions;
262 public boolean hasContent( ArtifactReference reference )
264 File artifactFile = toFile( reference );
265 return artifactFile.exists() && artifactFile.isFile();
268 public boolean hasContent( ProjectReference reference )
272 Set<String> versions = getVersions( reference );
273 return !versions.isEmpty();
275 catch ( ContentNotFoundException e )
279 catch ( LayoutException e )
285 public boolean hasContent( VersionedReference reference )
289 return ( getFirstArtifact( reference ) != null );
291 catch ( IOException e )
295 catch ( LayoutException e )
301 public void setRepository( ManagedRepositoryConfiguration repository )
303 this.repository = repository;
307 * Convert a path to an artifact reference.
309 * @param path the path to convert. (relative or full location path)
310 * @throws LayoutException if the path cannot be converted to an artifact reference.
313 public ArtifactReference toArtifactReference( String path )
314 throws LayoutException
316 if ( ( path != null ) && path.startsWith( repository.getLocation() ) )
318 return super.toArtifactReference( path.substring( repository.getLocation().length() ) );
321 return super.toArtifactReference( path );
324 public File toFile( ArtifactReference reference )
326 return new File( repository.getLocation(), toPath( reference ) );
329 public File toFile( ArchivaArtifact reference )
331 return new File( repository.getLocation(), toPath( reference ) );
335 * Get the first Artifact found in the provided VersionedReference location.
337 * @param managedRepository the repository to search within.
338 * @param reference the reference to the versioned reference to search within
339 * @return the ArtifactReference to the first artifact located within the versioned reference. or null if
340 * no artifact was found within the versioned reference.
341 * @throws IOException if the versioned reference is invalid (example: doesn't exist, or isn't a directory)
342 * @throws LayoutException
344 private ArtifactReference getFirstArtifact( VersionedReference reference )
345 throws LayoutException, IOException
347 String path = toMetadataPath( reference );
349 int idx = path.lastIndexOf( '/' );
352 path = path.substring( 0, idx );
355 File repoDir = new File( repository.getLocation(), path );
357 if ( !repoDir.exists() )
359 throw new IOException( "Unable to gather the list of snapshot versions on a non-existant directory: "
360 + repoDir.getAbsolutePath() );
363 if ( !repoDir.isDirectory() )
365 throw new IOException( "Unable to gather the list of snapshot versions on a non-directory: "
366 + repoDir.getAbsolutePath() );
369 File repoFiles[] = repoDir.listFiles();
370 for ( int i = 0; i < repoFiles.length; i++ )
372 if ( repoFiles[i].isDirectory() )
374 // Skip it. it's a directory.
378 String relativePath = PathUtil.getRelative( repository.getLocation(), repoFiles[i] );
380 if ( filetypes.matchesArtifactPattern( relativePath ) )
382 ArtifactReference artifact = toArtifactReference( relativePath );
388 // No artifact was found.
392 private boolean hasArtifact( VersionedReference reference )
393 throws LayoutException
397 return ( getFirstArtifact( reference ) != null );
399 catch ( IOException e )