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.collections.CollectionUtils;
23 import org.apache.commons.lang.StringUtils;
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.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
35 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
36 import org.codehaus.plexus.util.SelectorUtils;
39 import java.util.ArrayList;
40 import java.util.HashSet;
41 import java.util.Iterator;
42 import java.util.List;
46 * ManagedLegacyRepositoryContent
48 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
52 * role="org.apache.maven.archiva.repository.ManagedRepositoryContent"
54 * instantiation-strategy="per-lookup"
56 public class ManagedLegacyRepositoryContent
57 extends AbstractLegacyRepositoryContent
58 implements ManagedRepositoryContent, Initializable
63 private FileTypes filetypes;
65 private ManagedRepositoryConfiguration repository;
67 private List<String> artifactPatterns;
69 public void deleteVersion( VersionedReference reference )
70 throws ContentNotFoundException
72 File groupDir = new File( repository.getLocation(), reference.getGroupId() );
74 if ( !groupDir.exists() )
76 throw new ContentNotFoundException( "Unable to get versions using a non-existant groupId directory: "
77 + groupDir.getAbsolutePath() );
80 if ( !groupDir.isDirectory() )
82 throw new ContentNotFoundException( "Unable to get versions using a non-directory: "
83 + groupDir.getAbsolutePath() );
86 // First gather up the versions found as artifacts in the managed repository.
87 File typeDirs[] = groupDir.listFiles();
88 for ( File typeDir : typeDirs )
90 if ( !typeDir.isDirectory() )
92 // Skip it, we only care about directories.
96 if ( !typeDir.getName().endsWith( "s" ) )
98 // Skip it, we only care about directories that end in "s".
101 deleteVersions( typeDir, reference );
105 private void deleteVersions( File typeDir, VersionedReference reference )
107 File repoFiles[] = typeDir.listFiles();
108 for ( File repoFile : repoFiles )
110 if ( repoFile.isDirectory() )
112 // Skip it. it's a directory.
116 String relativePath = PathUtil.getRelative( repository.getLocation(), repoFile );
118 if ( matchesArtifactPattern( relativePath ) )
122 ArtifactReference artifact = toArtifactReference( relativePath );
123 if ( StringUtils.equals( artifact.getArtifactId(), reference.getArtifactId() )
124 && StringUtils.equals( artifact.getVersion(), reference.getVersion() ) )
127 deleteSupportFiles( repoFile );
130 catch ( LayoutException e )
132 /* don't fail the process if there is a bad artifact within the directory. */
138 private void deleteSupportFiles( File repoFile )
140 deleteSupportFile( repoFile, ".sha1" );
141 deleteSupportFile( repoFile, ".md5" );
142 deleteSupportFile( repoFile, ".asc" );
143 deleteSupportFile( repoFile, ".gpg" );
146 private void deleteSupportFile( File repoFile, String supportExtension )
148 File supportFile = new File( repoFile.getAbsolutePath() + supportExtension );
149 if ( supportFile.exists() && supportFile.isFile() )
151 supportFile.delete();
155 public String getId()
157 return repository.getId();
160 public Set<ArtifactReference> getRelatedArtifacts( ArtifactReference reference )
161 throws ContentNotFoundException, LayoutException
163 File artifactFile = toFile( reference );
164 File repoDir = artifactFile.getParentFile();
166 if ( !repoDir.exists() )
168 throw new ContentNotFoundException( "Unable to get related artifacts using a non-existant directory: "
169 + repoDir.getAbsolutePath() );
172 if ( !repoDir.isDirectory() )
174 throw new ContentNotFoundException( "Unable to get related artifacts using a non-directory: "
175 + repoDir.getAbsolutePath() );
178 Set<ArtifactReference> foundArtifacts = new HashSet<ArtifactReference>();
180 // First gather up the versions found as artifacts in the managed repository.
181 File projectParentDir = repoDir.getParentFile();
182 File typeDirs[] = projectParentDir.listFiles();
183 for ( File typeDir : typeDirs )
185 if ( !typeDir.isDirectory() )
187 // Skip it, we only care about directories.
191 if ( !typeDir.getName().endsWith( "s" ) )
193 // Skip it, we only care about directories that end in "s".
196 getRelatedArtifacts( typeDir, reference, foundArtifacts );
199 return foundArtifacts;
202 public String getRepoRoot()
204 return repository.getLocation();
207 public ManagedRepositoryConfiguration getRepository()
212 public Set<String> getVersions( ProjectReference reference )
213 throws ContentNotFoundException
215 File groupDir = new File( repository.getLocation(), reference.getGroupId() );
217 if ( !groupDir.exists() )
219 throw new ContentNotFoundException( "Unable to get versions using a non-existant groupId directory: "
220 + groupDir.getAbsolutePath() );
223 if ( !groupDir.isDirectory() )
225 throw new ContentNotFoundException( "Unable to get versions using a non-directory: "
226 + groupDir.getAbsolutePath() );
229 Set<String> foundVersions = new HashSet<String>();
231 // First gather up the versions found as artifacts in the managed repository.
232 File typeDirs[] = groupDir.listFiles();
233 for ( File typeDir : typeDirs )
235 if ( !typeDir.isDirectory() )
237 // Skip it, we only care about directories.
241 if ( !typeDir.getName().endsWith( "s" ) )
243 // Skip it, we only care about directories that end in "s".
246 getProjectVersions( typeDir, reference, foundVersions );
249 return foundVersions;
252 public Set<String> getVersions( VersionedReference reference )
253 throws ContentNotFoundException
255 File groupDir = new File( repository.getLocation(), reference.getGroupId() );
257 if ( !groupDir.exists() )
259 throw new ContentNotFoundException( "Unable to get versions using a non-existant groupId directory: "
260 + groupDir.getAbsolutePath() );
263 if ( !groupDir.isDirectory() )
265 throw new ContentNotFoundException( "Unable to get versions using a non-directory: "
266 + groupDir.getAbsolutePath() );
269 Set<String> foundVersions = new HashSet<String>();
271 // First gather up the versions found as artifacts in the managed repository.
272 File typeDirs[] = groupDir.listFiles();
273 for ( File typeDir : typeDirs )
275 if ( !typeDir.isDirectory() )
277 // Skip it, we only care about directories.
281 if ( !typeDir.getName().endsWith( "s" ) )
283 // Skip it, we only care about directories that end in "s".
286 getVersionedVersions( typeDir, reference, foundVersions );
289 return foundVersions;
292 public boolean hasContent( ArtifactReference reference )
294 File artifactFile = toFile( reference );
295 return artifactFile.exists() && artifactFile.isFile();
298 public boolean hasContent( ProjectReference reference )
302 Set<String> versions = getVersions( reference );
303 return CollectionUtils.isNotEmpty( versions );
305 catch ( ContentNotFoundException e )
311 public boolean hasContent( VersionedReference reference )
315 Set<String> versions = getVersions( reference );
316 return CollectionUtils.isNotEmpty( versions );
318 catch ( ContentNotFoundException e )
324 public void initialize()
325 throws InitializationException
327 this.artifactPatterns = new ArrayList<String>();
331 public void setRepository( ManagedRepositoryConfiguration repository )
333 this.repository = repository;
337 * Convert a path to an artifact reference.
339 * @param path the path to convert. (relative or full location path)
340 * @throws LayoutException if the path cannot be converted to an artifact reference.
343 public ArtifactReference toArtifactReference( String path )
344 throws LayoutException
346 if ( ( path != null ) && path.startsWith( repository.getLocation() ) )
348 return super.toArtifactReference( path.substring( repository.getLocation().length() ) );
351 return super.toArtifactReference( path );
354 public File toFile( ArchivaArtifact reference )
356 return new File( repository.getLocation(), toPath( reference ) );
359 public File toFile( ArtifactReference reference )
361 return new File( repository.getLocation(), toPath( reference ) );
364 public String toMetadataPath( ProjectReference reference )
366 // No metadata present in legacy repository.
370 public String toMetadataPath( VersionedReference reference )
372 // No metadata present in legacy repository.
376 private void getProjectVersions( File typeDir, ProjectReference reference, Set<String> foundVersions )
378 File repoFiles[] = typeDir.listFiles();
379 for ( File repoFile : repoFiles )
381 if ( repoFile.isDirectory() )
383 // Skip it. it's a directory.
387 String relativePath = PathUtil.getRelative( repository.getLocation(), repoFile );
389 if ( matchesArtifactPattern( relativePath ) )
393 ArtifactReference artifact = toArtifactReference( relativePath );
394 if ( StringUtils.equals( artifact.getArtifactId(), reference.getArtifactId() ) )
396 foundVersions.add( artifact.getVersion() );
399 catch ( LayoutException e )
401 /* don't fail the process if there is a bad artifact within the directory. */
407 private void getRelatedArtifacts( File typeDir, ArtifactReference reference, Set<ArtifactReference> foundArtifacts )
409 File repoFiles[] = typeDir.listFiles();
410 for ( int i = 0; i < repoFiles.length; i++ )
412 if ( repoFiles[i].isDirectory() )
414 // Skip it. it's a directory.
418 String relativePath = PathUtil.getRelative( repository.getLocation(), repoFiles[i] );
420 if ( matchesArtifactPattern( relativePath ) )
424 ArtifactReference artifact = toArtifactReference( relativePath );
425 if ( StringUtils.equals( artifact.getArtifactId(), reference.getArtifactId() )
426 && artifact.getVersion().startsWith( reference.getVersion() ) )
428 foundArtifacts.add( artifact );
431 catch ( LayoutException e )
433 /* don't fail the process if there is a bad artifact within the directory. */
439 private void getVersionedVersions( File typeDir, VersionedReference reference, Set<String> foundVersions )
441 File repoFiles[] = typeDir.listFiles();
442 for ( int i = 0; i < repoFiles.length; i++ )
444 if ( repoFiles[i].isDirectory() )
446 // Skip it. it's a directory.
450 String relativePath = PathUtil.getRelative( repository.getLocation(), repoFiles[i] );
452 if ( matchesArtifactPattern( relativePath ) )
456 ArtifactReference artifact = toArtifactReference( relativePath );
457 if ( StringUtils.equals( artifact.getArtifactId(), reference.getArtifactId() )
458 && artifact.getVersion().startsWith( reference.getVersion() ) )
460 foundVersions.add( artifact.getVersion() );
463 catch ( LayoutException e )
465 /* don't fail the process if there is a bad artifact within the directory. */
471 private void initVariables()
473 synchronized ( this.artifactPatterns )
475 this.artifactPatterns.clear();
477 this.artifactPatterns.addAll( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
481 private boolean matchesArtifactPattern( String relativePath )
483 // Correct the slash pattern.
484 relativePath = relativePath.replace( '\\', '/' );
486 Iterator<String> it = this.artifactPatterns.iterator();
487 while ( it.hasNext() )
489 String pattern = it.next();
491 if ( SelectorUtils.matchPath( pattern, relativePath, false ) )