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.archiva.metadata.repository.storage.maven2.DefaultArtifactMappingProvider;
23 import org.apache.commons.io.FileUtils;
24 import org.apache.maven.archiva.common.utils.PathUtil;
25 import org.apache.maven.archiva.configuration.FileTypes;
26 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
27 import org.apache.maven.archiva.model.ArchivaArtifact;
28 import org.apache.maven.archiva.model.ArtifactReference;
29 import org.apache.maven.archiva.model.ProjectReference;
30 import org.apache.maven.archiva.model.VersionedReference;
31 import org.apache.maven.archiva.repository.ContentNotFoundException;
32 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
33 import org.apache.maven.archiva.repository.layout.LayoutException;
34 import org.springframework.context.annotation.Scope;
35 import org.springframework.stereotype.Service;
37 import javax.inject.Inject;
38 import javax.inject.Named;
40 import java.io.IOException;
41 import java.util.Collections;
42 import java.util.HashSet;
46 * ManagedDefaultRepositoryContent
51 * role="org.apache.maven.archiva.repository.ManagedRepositoryContent"
53 * instantiation-strategy="per-lookup"
55 @Service("managedRepositoryContent#default")
57 public class ManagedDefaultRepositoryContent
58 extends AbstractDefaultRepositoryContent
59 implements ManagedRepositoryContent
62 @Named(value = "fileTypes" )
63 private FileTypes filetypes;
65 private ManagedRepositoryConfiguration repository;
67 public ManagedDefaultRepositoryContent()
69 // default to use if there are none supplied as components
70 this.artifactMappingProviders = Collections.singletonList( new DefaultArtifactMappingProvider() );
73 public void deleteVersion( VersionedReference reference )
74 throws ContentNotFoundException
76 String path = toMetadataPath( reference );
77 File projectPath = new File( getRepoRoot(), path );
79 File projectDir = projectPath.getParentFile();
80 if( projectDir.exists() && projectDir.isDirectory() )
82 FileUtils.deleteQuietly( projectDir );
86 throw new ContentNotFoundException( "Unable to delete non-existing project directory." );
92 return repository.getId();
95 public Set<ArtifactReference> getRelatedArtifacts( ArtifactReference reference )
96 throws ContentNotFoundException
98 File artifactFile = toFile( reference );
99 File repoDir = artifactFile.getParentFile();
101 if ( !repoDir.exists() )
103 throw new ContentNotFoundException( "Unable to get related artifacts using a non-existant directory: "
104 + repoDir.getAbsolutePath() );
107 if ( !repoDir.isDirectory() )
109 throw new ContentNotFoundException( "Unable to get related artifacts using a non-directory: "
110 + repoDir.getAbsolutePath() );
113 Set<ArtifactReference> foundArtifacts = new HashSet<ArtifactReference>();
115 // First gather up the versions found as artifacts in the managed repository.
116 File repoFiles[] = repoDir.listFiles();
117 for ( int i = 0; i < repoFiles.length; i++ )
119 if ( repoFiles[i].isDirectory() )
121 // Skip it. it's a directory.
125 String relativePath = PathUtil.getRelative( repository.getLocation(), repoFiles[i] );
127 if ( filetypes.matchesArtifactPattern( relativePath ) )
131 ArtifactReference artifact = toArtifactReference( relativePath );
133 // Test for related, groupId / artifactId / version must match.
134 if ( artifact.getGroupId().equals( reference.getGroupId() )
135 && artifact.getArtifactId().equals( reference.getArtifactId() )
136 && artifact.getVersion().equals( reference.getVersion() ) )
138 foundArtifacts.add( artifact );
141 catch ( LayoutException e )
143 log.debug( "Not processing file that is not an artifact: {}", e.getMessage() );
148 return foundArtifacts;
151 public String getRepoRoot()
153 return repository.getLocation();
156 public ManagedRepositoryConfiguration getRepository()
162 * Gather the Available Versions (on disk) for a specific Project Reference, based on filesystem
165 * @return the Set of available versions, based on the project reference.
166 * @throws LayoutException
167 * @throws LayoutException
169 public Set<String> getVersions( ProjectReference reference )
170 throws ContentNotFoundException, LayoutException
172 String path = toMetadataPath( reference );
174 int idx = path.lastIndexOf( '/' );
177 path = path.substring( 0, idx );
180 File repoDir = new File( repository.getLocation(), path );
182 if ( !repoDir.exists() )
184 throw new ContentNotFoundException( "Unable to get Versions on a non-existant directory: "
185 + repoDir.getAbsolutePath() );
188 if ( !repoDir.isDirectory() )
190 throw new ContentNotFoundException( "Unable to get Versions on a non-directory: "
191 + repoDir.getAbsolutePath() );
194 Set<String> foundVersions = new HashSet<String>();
195 VersionedReference versionRef = new VersionedReference();
196 versionRef.setGroupId( reference.getGroupId() );
197 versionRef.setArtifactId( reference.getArtifactId() );
199 File repoFiles[] = repoDir.listFiles();
200 for ( int i = 0; i < repoFiles.length; i++ )
202 if ( !repoFiles[i].isDirectory() )
204 // Skip it. not a directory.
208 // Test if dir has an artifact, which proves to us that it is a valid version directory.
209 String version = repoFiles[i].getName();
210 versionRef.setVersion( version );
212 if ( hasArtifact( versionRef ) )
214 // Found an artifact, must be a valid version.
215 foundVersions.add( version );
219 return foundVersions;
222 public Set<String> getVersions( VersionedReference reference )
223 throws ContentNotFoundException
225 String path = toMetadataPath( reference );
227 int idx = path.lastIndexOf( '/' );
230 path = path.substring( 0, idx );
233 File repoDir = new File( repository.getLocation(), path );
235 if ( !repoDir.exists() )
237 throw new ContentNotFoundException( "Unable to get versions on a non-existant directory: "
238 + repoDir.getAbsolutePath() );
241 if ( !repoDir.isDirectory() )
243 throw new ContentNotFoundException( "Unable to get versions on a non-directory: "
244 + repoDir.getAbsolutePath() );
247 Set<String> foundVersions = new HashSet<String>();
249 // First gather up the versions found as artifacts in the managed repository.
250 File repoFiles[] = repoDir.listFiles();
251 for ( int i = 0; i < repoFiles.length; i++ )
253 if ( repoFiles[i].isDirectory() )
255 // Skip it. it's a directory.
259 String relativePath = PathUtil.getRelative( repository.getLocation(), repoFiles[i] );
261 if ( filetypes.matchesDefaultExclusions( relativePath ) )
263 // Skip it, it's metadata or similar
267 if ( filetypes.matchesArtifactPattern( relativePath ) )
271 ArtifactReference artifact = toArtifactReference( relativePath );
273 foundVersions.add( artifact.getVersion() );
275 catch ( LayoutException e )
277 log.debug( "Not processing file that is not an artifact: {}", e.getMessage() );
282 return foundVersions;
285 public boolean hasContent( ArtifactReference reference )
287 File artifactFile = toFile( reference );
288 return artifactFile.exists() && artifactFile.isFile();
291 public boolean hasContent( ProjectReference reference )
295 Set<String> versions = getVersions( reference );
296 return !versions.isEmpty();
298 catch ( ContentNotFoundException e )
302 catch ( LayoutException e )
308 public boolean hasContent( VersionedReference reference )
312 return ( getFirstArtifact( reference ) != null );
314 catch ( IOException e )
318 catch ( LayoutException e )
324 public void setRepository( ManagedRepositoryConfiguration repository )
326 this.repository = repository;
330 * Convert a path to an artifact reference.
332 * @param path the path to convert. (relative or full location path)
333 * @throws LayoutException if the path cannot be converted to an artifact reference.
336 public ArtifactReference toArtifactReference( String path )
337 throws LayoutException
339 if ( ( path != null ) && path.startsWith( repository.getLocation() ) && repository.getLocation().length() > 0 )
341 return super.toArtifactReference( path.substring( repository.getLocation().length() + 1 ) );
344 return super.toArtifactReference( path );
347 public File toFile( ArtifactReference reference )
349 return new File( repository.getLocation(), toPath( reference ) );
352 public File toFile( ArchivaArtifact reference )
354 return new File( repository.getLocation(), toPath( reference ) );
358 * Get the first Artifact found in the provided VersionedReference location.
360 * @param reference the reference to the versioned reference to search within
361 * @return the ArtifactReference to the first artifact located within the versioned reference. or null if
362 * no artifact was found within the versioned reference.
363 * @throws IOException if the versioned reference is invalid (example: doesn't exist, or isn't a directory)
364 * @throws LayoutException
366 private ArtifactReference getFirstArtifact( VersionedReference reference )
367 throws LayoutException, IOException
369 String path = toMetadataPath( reference );
371 int idx = path.lastIndexOf( '/' );
374 path = path.substring( 0, idx );
377 File repoDir = new File( repository.getLocation(), path );
379 if ( !repoDir.exists() )
381 throw new IOException( "Unable to gather the list of snapshot versions on a non-existant directory: "
382 + repoDir.getAbsolutePath() );
385 if ( !repoDir.isDirectory() )
387 throw new IOException( "Unable to gather the list of snapshot versions on a non-directory: "
388 + repoDir.getAbsolutePath() );
391 File repoFiles[] = repoDir.listFiles();
392 for ( int i = 0; i < repoFiles.length; i++ )
394 if ( repoFiles[i].isDirectory() )
396 // Skip it. it's a directory.
400 String relativePath = PathUtil.getRelative( repository.getLocation(), repoFiles[i] );
402 if ( filetypes.matchesArtifactPattern( relativePath ) )
404 ArtifactReference artifact = toArtifactReference( relativePath );
410 // No artifact was found.
414 private boolean hasArtifact( VersionedReference reference )
415 throws LayoutException
419 return ( getFirstArtifact( reference ) != null );
421 catch ( IOException e )
427 public void setFiletypes( FileTypes filetypes )
429 this.filetypes = filetypes;