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.matchesArtifactPattern( relativePath ) )
247 ArtifactReference artifact = toArtifactReference( relativePath );
249 foundVersions.add( artifact.getVersion() );
253 return foundVersions;
256 public boolean hasContent( ArtifactReference reference )
258 File artifactFile = toFile( reference );
259 return artifactFile.exists() && artifactFile.isFile();
262 public boolean hasContent( ProjectReference reference )
266 Set<String> versions = getVersions( reference );
267 return !versions.isEmpty();
269 catch ( ContentNotFoundException e )
273 catch ( LayoutException e )
279 public boolean hasContent( VersionedReference reference )
283 return ( getFirstArtifact( reference ) != null );
285 catch ( IOException e )
289 catch ( LayoutException e )
295 public void setRepository( ManagedRepositoryConfiguration repository )
297 this.repository = repository;
301 * Convert a path to an artifact reference.
303 * @param path the path to convert. (relative or full location path)
304 * @throws LayoutException if the path cannot be converted to an artifact reference.
307 public ArtifactReference toArtifactReference( String path )
308 throws LayoutException
310 if ( ( path != null ) && path.startsWith( repository.getLocation() ) )
312 return super.toArtifactReference( path.substring( repository.getLocation().length() ) );
315 return super.toArtifactReference( path );
318 public File toFile( ArtifactReference reference )
320 return new File( repository.getLocation(), toPath( reference ) );
323 public File toFile( ArchivaArtifact reference )
325 return new File( repository.getLocation(), toPath( reference ) );
329 * Get the first Artifact found in the provided VersionedReference location.
331 * @param managedRepository the repository to search within.
332 * @param reference the reference to the versioned reference to search within
333 * @return the ArtifactReference to the first artifact located within the versioned reference. or null if
334 * no artifact was found within the versioned reference.
335 * @throws IOException if the versioned reference is invalid (example: doesn't exist, or isn't a directory)
336 * @throws LayoutException
338 private ArtifactReference getFirstArtifact( VersionedReference reference )
339 throws LayoutException, IOException
341 String path = toMetadataPath( reference );
343 int idx = path.lastIndexOf( '/' );
346 path = path.substring( 0, idx );
349 File repoDir = new File( repository.getLocation(), path );
351 if ( !repoDir.exists() )
353 throw new IOException( "Unable to gather the list of snapshot versions on a non-existant directory: "
354 + repoDir.getAbsolutePath() );
357 if ( !repoDir.isDirectory() )
359 throw new IOException( "Unable to gather the list of snapshot versions on a non-directory: "
360 + repoDir.getAbsolutePath() );
363 File repoFiles[] = repoDir.listFiles();
364 for ( int i = 0; i < repoFiles.length; i++ )
366 if ( repoFiles[i].isDirectory() )
368 // Skip it. it's a directory.
372 String relativePath = PathUtil.getRelative( repository.getLocation(), repoFiles[i] );
374 if ( filetypes.matchesArtifactPattern( relativePath ) )
376 ArtifactReference artifact = toArtifactReference( relativePath );
382 // No artifact was found.
386 private boolean hasArtifact( VersionedReference reference )
387 throws LayoutException
391 return ( getFirstArtifact( reference ) != null );
393 catch ( IOException e )