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
44 * @todo no need to be a component when filetypes is not
47 * role="org.apache.maven.archiva.repository.ManagedRepositoryContent"
49 * instantiation-strategy="per-lookup"
51 public class ManagedDefaultRepositoryContent
52 extends AbstractDefaultRepositoryContent
53 implements ManagedRepositoryContent
58 private FileTypes filetypes;
60 private ManagedRepositoryConfiguration repository;
62 public void deleteVersion( VersionedReference reference )
63 throws ContentNotFoundException
65 String path = toMetadataPath( reference );
66 File projectPath = new File( getRepoRoot(), path );
68 File projectDir = projectPath.getParentFile();
69 if( projectDir.exists() && projectDir.isDirectory() )
73 FileUtils.deleteDirectory( projectDir );
75 catch ( IOException e )
77 // TODO: log this somewhere?
82 throw new ContentNotFoundException( "Unable to delete non-existing project directory." );
88 return repository.getId();
91 public Set<ArtifactReference> getRelatedArtifacts( ArtifactReference reference )
92 throws ContentNotFoundException, LayoutException
94 File artifactFile = toFile( reference );
95 File repoDir = artifactFile.getParentFile();
97 if ( !repoDir.exists() )
99 throw new ContentNotFoundException( "Unable to get related artifacts using a non-existant directory: "
100 + repoDir.getAbsolutePath() );
103 if ( !repoDir.isDirectory() )
105 throw new ContentNotFoundException( "Unable to get related artifacts using a non-directory: "
106 + repoDir.getAbsolutePath() );
109 Set<ArtifactReference> foundArtifacts = new HashSet<ArtifactReference>();
111 // First gather up the versions found as artifacts in the managed repository.
112 File repoFiles[] = repoDir.listFiles();
113 for ( int i = 0; i < repoFiles.length; i++ )
115 if ( repoFiles[i].isDirectory() )
117 // Skip it. it's a directory.
121 String relativePath = PathUtil.getRelative( repository.getLocation(), repoFiles[i] );
123 if ( filetypes.matchesArtifactPattern( relativePath ) )
125 ArtifactReference artifact = toArtifactReference( relativePath );
127 // Test for related, groupId / artifactId / version must match.
128 if ( artifact.getGroupId().equals( reference.getGroupId() )
129 && artifact.getArtifactId().equals( reference.getArtifactId() )
130 && artifact.getVersion().equals( reference.getVersion() ) )
132 foundArtifacts.add( artifact );
137 return foundArtifacts;
140 public String getRepoRoot()
142 return repository.getLocation();
145 public ManagedRepositoryConfiguration getRepository()
151 * Gather the Available Versions (on disk) for a specific Project Reference, based on filesystem
154 * @return the Set of available versions, based on the project reference.
155 * @throws LayoutException
156 * @throws LayoutException
158 public Set<String> getVersions( ProjectReference reference )
159 throws ContentNotFoundException, LayoutException
161 String path = toMetadataPath( reference );
163 int idx = path.lastIndexOf( '/' );
166 path = path.substring( 0, idx );
169 File repoDir = new File( repository.getLocation(), path );
171 if ( !repoDir.exists() )
173 throw new ContentNotFoundException( "Unable to get Versions on a non-existant directory: "
174 + repoDir.getAbsolutePath() );
177 if ( !repoDir.isDirectory() )
179 throw new ContentNotFoundException( "Unable to get Versions on a non-directory: "
180 + repoDir.getAbsolutePath() );
183 Set<String> foundVersions = new HashSet<String>();
184 VersionedReference versionRef = new VersionedReference();
185 versionRef.setGroupId( reference.getGroupId() );
186 versionRef.setArtifactId( reference.getArtifactId() );
188 File repoFiles[] = repoDir.listFiles();
189 for ( int i = 0; i < repoFiles.length; i++ )
191 if ( !repoFiles[i].isDirectory() )
193 // Skip it. not a directory.
197 // Test if dir has an artifact, which proves to us that it is a valid version directory.
198 String version = repoFiles[i].getName();
199 versionRef.setVersion( version );
201 if ( hasArtifact( versionRef ) )
203 // Found an artifact, must be a valid version.
204 foundVersions.add( version );
208 return foundVersions;
211 public Set<String> getVersions( VersionedReference reference )
212 throws ContentNotFoundException, LayoutException
214 String path = toMetadataPath( reference );
216 int idx = path.lastIndexOf( '/' );
219 path = path.substring( 0, idx );
222 File repoDir = new File( repository.getLocation(), path );
224 if ( !repoDir.exists() )
226 throw new ContentNotFoundException( "Unable to get versions on a non-existant directory: "
227 + repoDir.getAbsolutePath() );
230 if ( !repoDir.isDirectory() )
232 throw new ContentNotFoundException( "Unable to get versions on a non-directory: "
233 + repoDir.getAbsolutePath() );
236 Set<String> foundVersions = new HashSet<String>();
238 // First gather up the versions found as artifacts in the managed repository.
239 File repoFiles[] = repoDir.listFiles();
240 for ( int i = 0; i < repoFiles.length; i++ )
242 if ( repoFiles[i].isDirectory() )
244 // Skip it. it's a directory.
248 String relativePath = PathUtil.getRelative( repository.getLocation(), repoFiles[i] );
250 if ( filetypes.matchesDefaultExclusions( relativePath ) )
252 // Skip it, it's metadata or similar
256 if ( filetypes.matchesArtifactPattern( relativePath ) )
258 ArtifactReference artifact = toArtifactReference( relativePath );
260 foundVersions.add( artifact.getVersion() );
264 return foundVersions;
267 public boolean hasContent( ArtifactReference reference )
269 File artifactFile = toFile( reference );
270 return artifactFile.exists() && artifactFile.isFile();
273 public boolean hasContent( ProjectReference reference )
277 Set<String> versions = getVersions( reference );
278 return !versions.isEmpty();
280 catch ( ContentNotFoundException e )
284 catch ( LayoutException e )
290 public boolean hasContent( VersionedReference reference )
294 return ( getFirstArtifact( reference ) != null );
296 catch ( IOException e )
300 catch ( LayoutException e )
306 public void setRepository( ManagedRepositoryConfiguration repository )
308 this.repository = repository;
312 * Convert a path to an artifact reference.
314 * @param path the path to convert. (relative or full location path)
315 * @throws LayoutException if the path cannot be converted to an artifact reference.
318 public ArtifactReference toArtifactReference( String path )
319 throws LayoutException
321 if ( ( path != null ) && path.startsWith( repository.getLocation() ) )
323 return super.toArtifactReference( path.substring( repository.getLocation().length() ) );
326 return super.toArtifactReference( path );
329 public File toFile( ArtifactReference reference )
331 return new File( repository.getLocation(), toPath( reference ) );
334 public File toFile( ArchivaArtifact reference )
336 return new File( repository.getLocation(), toPath( reference ) );
340 * Get the first Artifact found in the provided VersionedReference location.
342 * @param managedRepository the repository to search within.
343 * @param reference the reference to the versioned reference to search within
344 * @return the ArtifactReference to the first artifact located within the versioned reference. or null if
345 * no artifact was found within the versioned reference.
346 * @throws IOException if the versioned reference is invalid (example: doesn't exist, or isn't a directory)
347 * @throws LayoutException
349 private ArtifactReference getFirstArtifact( VersionedReference reference )
350 throws LayoutException, IOException
352 String path = toMetadataPath( reference );
354 int idx = path.lastIndexOf( '/' );
357 path = path.substring( 0, idx );
360 File repoDir = new File( repository.getLocation(), path );
362 if ( !repoDir.exists() )
364 throw new IOException( "Unable to gather the list of snapshot versions on a non-existant directory: "
365 + repoDir.getAbsolutePath() );
368 if ( !repoDir.isDirectory() )
370 throw new IOException( "Unable to gather the list of snapshot versions on a non-directory: "
371 + repoDir.getAbsolutePath() );
374 File repoFiles[] = repoDir.listFiles();
375 for ( int i = 0; i < repoFiles.length; i++ )
377 if ( repoFiles[i].isDirectory() )
379 // Skip it. it's a directory.
383 String relativePath = PathUtil.getRelative( repository.getLocation(), repoFiles[i] );
385 if ( filetypes.matchesArtifactPattern( relativePath ) )
387 ArtifactReference artifact = toArtifactReference( relativePath );
393 // No artifact was found.
397 private boolean hasArtifact( VersionedReference reference )
398 throws LayoutException
402 return ( getFirstArtifact( reference ) != null );
404 catch ( IOException e )