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>
45 * @todo no need to be a component when filetypes is not
48 * role="org.apache.maven.archiva.repository.ManagedRepositoryContent"
50 * instantiation-strategy="per-lookup"
52 public class ManagedDefaultRepositoryContent
53 extends AbstractDefaultRepositoryContent
54 implements ManagedRepositoryContent
59 private FileTypes filetypes;
61 private ManagedRepositoryConfiguration repository;
63 public void deleteVersion( VersionedReference reference )
64 throws ContentNotFoundException
66 String path = toMetadataPath( reference );
67 File projectPath = new File( getRepoRoot(), path );
69 File projectDir = projectPath.getParentFile();
70 if( projectDir.exists() && projectDir.isDirectory() )
74 FileUtils.deleteDirectory( projectDir );
76 catch ( IOException e )
78 // TODO: log this somewhere?
85 return repository.getId();
88 public Set<ArtifactReference> getRelatedArtifacts( ArtifactReference reference )
89 throws ContentNotFoundException, LayoutException
91 File artifactFile = toFile( reference );
92 File repoDir = artifactFile.getParentFile();
94 if ( !repoDir.exists() )
96 throw new ContentNotFoundException( "Unable to get related artifacts using a non-existant directory: "
97 + repoDir.getAbsolutePath() );
100 if ( !repoDir.isDirectory() )
102 throw new ContentNotFoundException( "Unable to get related artifacts using a non-directory: "
103 + repoDir.getAbsolutePath() );
106 Set<ArtifactReference> foundArtifacts = new HashSet<ArtifactReference>();
108 // First gather up the versions found as artifacts in the managed repository.
109 File repoFiles[] = repoDir.listFiles();
110 for ( int i = 0; i < repoFiles.length; i++ )
112 if ( repoFiles[i].isDirectory() )
114 // Skip it. it's a directory.
118 String relativePath = PathUtil.getRelative( repository.getLocation(), repoFiles[i] );
120 if ( filetypes.matchesArtifactPattern( relativePath ) )
122 ArtifactReference artifact = toArtifactReference( relativePath );
124 // Test for related, groupId / artifactId / version must match.
125 if ( artifact.getGroupId().equals( reference.getGroupId() )
126 && artifact.getArtifactId().equals( reference.getArtifactId() )
127 && artifact.getVersion().equals( reference.getVersion() ) )
129 foundArtifacts.add( artifact );
134 return foundArtifacts;
137 public String getRepoRoot()
139 return repository.getLocation();
142 public ManagedRepositoryConfiguration getRepository()
148 * Gather the Available Versions (on disk) for a specific Project Reference, based on filesystem
151 * @return the Set of available versions, based on the project reference.
152 * @throws LayoutException
153 * @throws LayoutException
155 public Set<String> getVersions( ProjectReference reference )
156 throws ContentNotFoundException, LayoutException
158 String path = toMetadataPath( reference );
160 int idx = path.lastIndexOf( '/' );
163 path = path.substring( 0, idx );
166 File repoDir = new File( repository.getLocation(), path );
168 if ( !repoDir.exists() )
170 throw new ContentNotFoundException( "Unable to get Versions on a non-existant directory: "
171 + repoDir.getAbsolutePath() );
174 if ( !repoDir.isDirectory() )
176 throw new ContentNotFoundException( "Unable to get Versions on a non-directory: "
177 + repoDir.getAbsolutePath() );
180 Set<String> foundVersions = new HashSet<String>();
181 VersionedReference versionRef = new VersionedReference();
182 versionRef.setGroupId( reference.getGroupId() );
183 versionRef.setArtifactId( reference.getArtifactId() );
185 File repoFiles[] = repoDir.listFiles();
186 for ( int i = 0; i < repoFiles.length; i++ )
188 if ( !repoFiles[i].isDirectory() )
190 // Skip it. not a directory.
194 // Test if dir has an artifact, which proves to us that it is a valid version directory.
195 String version = repoFiles[i].getName();
196 versionRef.setVersion( version );
198 if ( hasArtifact( versionRef ) )
200 // Found an artifact, must be a valid version.
201 foundVersions.add( version );
205 return foundVersions;
208 public Set<String> getVersions( VersionedReference reference )
209 throws ContentNotFoundException, LayoutException
211 String path = toMetadataPath( reference );
213 int idx = path.lastIndexOf( '/' );
216 path = path.substring( 0, idx );
219 File repoDir = new File( repository.getLocation(), path );
221 if ( !repoDir.exists() )
223 throw new ContentNotFoundException( "Unable to get versions on a non-existant directory: "
224 + repoDir.getAbsolutePath() );
227 if ( !repoDir.isDirectory() )
229 throw new ContentNotFoundException( "Unable to get versions on a non-directory: "
230 + repoDir.getAbsolutePath() );
233 Set<String> foundVersions = new HashSet<String>();
235 // First gather up the versions found as artifacts in the managed repository.
236 File repoFiles[] = repoDir.listFiles();
237 for ( int i = 0; i < repoFiles.length; i++ )
239 if ( repoFiles[i].isDirectory() )
241 // Skip it. it's a directory.
245 String relativePath = PathUtil.getRelative( repository.getLocation(), repoFiles[i] );
247 if ( filetypes.matchesDefaultExclusions( relativePath ) )
249 // Skip it, it's metadata or similar
253 if ( filetypes.matchesArtifactPattern( relativePath ) )
255 ArtifactReference artifact = toArtifactReference( relativePath );
257 foundVersions.add( artifact.getVersion() );
261 return foundVersions;
264 public boolean hasContent( ArtifactReference reference )
266 File artifactFile = toFile( reference );
267 return artifactFile.exists() && artifactFile.isFile();
270 public boolean hasContent( ProjectReference reference )
274 Set<String> versions = getVersions( reference );
275 return !versions.isEmpty();
277 catch ( ContentNotFoundException e )
281 catch ( LayoutException e )
287 public boolean hasContent( VersionedReference reference )
291 return ( getFirstArtifact( reference ) != null );
293 catch ( IOException e )
297 catch ( LayoutException e )
303 public void setRepository( ManagedRepositoryConfiguration repository )
305 this.repository = repository;
309 * Convert a path to an artifact reference.
311 * @param path the path to convert. (relative or full location path)
312 * @throws LayoutException if the path cannot be converted to an artifact reference.
315 public ArtifactReference toArtifactReference( String path )
316 throws LayoutException
318 if ( ( path != null ) && path.startsWith( repository.getLocation() ) )
320 return super.toArtifactReference( path.substring( repository.getLocation().length() ) );
323 return super.toArtifactReference( path );
326 public File toFile( ArtifactReference reference )
328 return new File( repository.getLocation(), toPath( reference ) );
331 public File toFile( ArchivaArtifact reference )
333 return new File( repository.getLocation(), toPath( reference ) );
337 * Get the first Artifact found in the provided VersionedReference location.
339 * @param managedRepository the repository to search within.
340 * @param reference the reference to the versioned reference to search within
341 * @return the ArtifactReference to the first artifact located within the versioned reference. or null if
342 * no artifact was found within the versioned reference.
343 * @throws IOException if the versioned reference is invalid (example: doesn't exist, or isn't a directory)
344 * @throws LayoutException
346 private ArtifactReference getFirstArtifact( VersionedReference reference )
347 throws LayoutException, IOException
349 String path = toMetadataPath( reference );
351 int idx = path.lastIndexOf( '/' );
354 path = path.substring( 0, idx );
357 File repoDir = new File( repository.getLocation(), path );
359 if ( !repoDir.exists() )
361 throw new IOException( "Unable to gather the list of snapshot versions on a non-existant directory: "
362 + repoDir.getAbsolutePath() );
365 if ( !repoDir.isDirectory() )
367 throw new IOException( "Unable to gather the list of snapshot versions on a non-directory: "
368 + repoDir.getAbsolutePath() );
371 File repoFiles[] = repoDir.listFiles();
372 for ( int i = 0; i < repoFiles.length; i++ )
374 if ( repoFiles[i].isDirectory() )
376 // Skip it. it's a directory.
380 String relativePath = PathUtil.getRelative( repository.getLocation(), repoFiles[i] );
382 if ( filetypes.matchesArtifactPattern( relativePath ) )
384 ArtifactReference artifact = toArtifactReference( relativePath );
390 // No artifact was found.
394 private boolean hasArtifact( VersionedReference reference )
395 throws LayoutException
399 return ( getFirstArtifact( reference ) != null );
401 catch ( IOException e )