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
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 ) )
127 ArtifactReference artifact = toArtifactReference( relativePath );
129 // Test for related, groupId / artifactId / version must match.
130 if ( artifact.getGroupId().equals( reference.getGroupId() )
131 && artifact.getArtifactId().equals( reference.getArtifactId() )
132 && artifact.getVersion().equals( reference.getVersion() ) )
134 foundArtifacts.add( artifact );
137 catch ( LayoutException e )
139 log.debug( "Not processing file that is not an artifact: " + e.getMessage() );
144 return foundArtifacts;
147 public String getRepoRoot()
149 return repository.getLocation();
152 public ManagedRepositoryConfiguration getRepository()
158 * Gather the Available Versions (on disk) for a specific Project Reference, based on filesystem
161 * @return the Set of available versions, based on the project reference.
162 * @throws LayoutException
163 * @throws LayoutException
165 public Set<String> getVersions( ProjectReference reference )
166 throws ContentNotFoundException, LayoutException
168 String path = toMetadataPath( reference );
170 int idx = path.lastIndexOf( '/' );
173 path = path.substring( 0, idx );
176 File repoDir = new File( repository.getLocation(), path );
178 if ( !repoDir.exists() )
180 throw new ContentNotFoundException( "Unable to get Versions on a non-existant directory: "
181 + repoDir.getAbsolutePath() );
184 if ( !repoDir.isDirectory() )
186 throw new ContentNotFoundException( "Unable to get Versions on a non-directory: "
187 + repoDir.getAbsolutePath() );
190 Set<String> foundVersions = new HashSet<String>();
191 VersionedReference versionRef = new VersionedReference();
192 versionRef.setGroupId( reference.getGroupId() );
193 versionRef.setArtifactId( reference.getArtifactId() );
195 File repoFiles[] = repoDir.listFiles();
196 for ( int i = 0; i < repoFiles.length; i++ )
198 if ( !repoFiles[i].isDirectory() )
200 // Skip it. not a directory.
204 // Test if dir has an artifact, which proves to us that it is a valid version directory.
205 String version = repoFiles[i].getName();
206 versionRef.setVersion( version );
208 if ( hasArtifact( versionRef ) )
210 // Found an artifact, must be a valid version.
211 foundVersions.add( version );
215 return foundVersions;
218 public Set<String> getVersions( VersionedReference reference )
219 throws ContentNotFoundException
221 String path = toMetadataPath( reference );
223 int idx = path.lastIndexOf( '/' );
226 path = path.substring( 0, idx );
229 File repoDir = new File( repository.getLocation(), path );
231 if ( !repoDir.exists() )
233 throw new ContentNotFoundException( "Unable to get versions on a non-existant directory: "
234 + repoDir.getAbsolutePath() );
237 if ( !repoDir.isDirectory() )
239 throw new ContentNotFoundException( "Unable to get versions on a non-directory: "
240 + repoDir.getAbsolutePath() );
243 Set<String> foundVersions = new HashSet<String>();
245 // First gather up the versions found as artifacts in the managed repository.
246 File repoFiles[] = repoDir.listFiles();
247 for ( int i = 0; i < repoFiles.length; i++ )
249 if ( repoFiles[i].isDirectory() )
251 // Skip it. it's a directory.
255 String relativePath = PathUtil.getRelative( repository.getLocation(), repoFiles[i] );
257 if ( filetypes.matchesDefaultExclusions( relativePath ) )
259 // Skip it, it's metadata or similar
263 if ( filetypes.matchesArtifactPattern( relativePath ) )
267 ArtifactReference artifact = toArtifactReference( relativePath );
269 foundVersions.add( artifact.getVersion() );
271 catch ( LayoutException e )
273 log.debug( "Not processing file that is not an artifact: " + e.getMessage() );
278 return foundVersions;
281 public boolean hasContent( ArtifactReference reference )
283 File artifactFile = toFile( reference );
284 return artifactFile.exists() && artifactFile.isFile();
287 public boolean hasContent( ProjectReference reference )
291 Set<String> versions = getVersions( reference );
292 return !versions.isEmpty();
294 catch ( ContentNotFoundException e )
298 catch ( LayoutException e )
304 public boolean hasContent( VersionedReference reference )
308 return ( getFirstArtifact( reference ) != null );
310 catch ( IOException e )
314 catch ( LayoutException e )
320 public void setRepository( ManagedRepositoryConfiguration repository )
322 this.repository = repository;
326 * Convert a path to an artifact reference.
328 * @param path the path to convert. (relative or full location path)
329 * @throws LayoutException if the path cannot be converted to an artifact reference.
332 public ArtifactReference toArtifactReference( String path )
333 throws LayoutException
335 if ( ( path != null ) && path.startsWith( repository.getLocation() ) && repository.getLocation().length() > 0 )
337 return super.toArtifactReference( path.substring( repository.getLocation().length() + 1 ) );
340 return super.toArtifactReference( path );
343 public File toFile( ArtifactReference reference )
345 return new File( repository.getLocation(), toPath( reference ) );
348 public File toFile( ArchivaArtifact reference )
350 return new File( repository.getLocation(), toPath( reference ) );
354 * Get the first Artifact found in the provided VersionedReference location.
356 * @param managedRepository the repository to search within.
357 * @param reference the reference to the versioned reference to search within
358 * @return the ArtifactReference to the first artifact located within the versioned reference. or null if
359 * no artifact was found within the versioned reference.
360 * @throws IOException if the versioned reference is invalid (example: doesn't exist, or isn't a directory)
361 * @throws LayoutException
363 private ArtifactReference getFirstArtifact( VersionedReference reference )
364 throws LayoutException, IOException
366 String path = toMetadataPath( reference );
368 int idx = path.lastIndexOf( '/' );
371 path = path.substring( 0, idx );
374 File repoDir = new File( repository.getLocation(), path );
376 if ( !repoDir.exists() )
378 throw new IOException( "Unable to gather the list of snapshot versions on a non-existant directory: "
379 + repoDir.getAbsolutePath() );
382 if ( !repoDir.isDirectory() )
384 throw new IOException( "Unable to gather the list of snapshot versions on a non-directory: "
385 + repoDir.getAbsolutePath() );
388 File repoFiles[] = repoDir.listFiles();
389 for ( int i = 0; i < repoFiles.length; i++ )
391 if ( repoFiles[i].isDirectory() )
393 // Skip it. it's a directory.
397 String relativePath = PathUtil.getRelative( repository.getLocation(), repoFiles[i] );
399 if ( filetypes.matchesArtifactPattern( relativePath ) )
401 ArtifactReference artifact = toArtifactReference( relativePath );
407 // No artifact was found.
411 private boolean hasArtifact( VersionedReference reference )
412 throws LayoutException
416 return ( getFirstArtifact( reference ) != null );
418 catch ( IOException e )
424 public void setFiletypes( FileTypes filetypes )
426 this.filetypes = filetypes;