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;
36 import java.util.HashSet;
40 * ManagedLegacyRepositoryContent
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 ManagedLegacyRepositoryContent
51 extends AbstractLegacyRepositoryContent
52 implements ManagedRepositoryContent
57 private FileTypes filetypes;
59 private ManagedRepositoryConfiguration repository;
61 public void deleteVersion( VersionedReference reference )
62 throws ContentNotFoundException
64 File groupDir = new File( repository.getLocation(), reference.getGroupId() );
66 if ( !groupDir.exists() )
68 throw new ContentNotFoundException( "Unable to get versions using a non-existant groupId directory: "
69 + groupDir.getAbsolutePath() );
72 if ( !groupDir.isDirectory() )
74 throw new ContentNotFoundException( "Unable to get versions using a non-directory: "
75 + groupDir.getAbsolutePath() );
78 // First gather up the versions found as artifacts in the managed repository.
79 File typeDirs[] = groupDir.listFiles();
80 for ( File typeDir : typeDirs )
82 if ( !typeDir.isDirectory() )
84 // Skip it, we only care about directories.
88 if ( !typeDir.getName().endsWith( "s" ) )
90 // Skip it, we only care about directories that end in "s".
93 deleteVersions( typeDir, reference );
97 private void deleteVersions( File typeDir, VersionedReference reference )
99 File repoFiles[] = typeDir.listFiles();
100 for ( File repoFile : repoFiles )
102 if ( repoFile.isDirectory() )
104 // Skip it. it's a directory.
108 String relativePath = PathUtil.getRelative( repository.getLocation(), repoFile );
110 if ( filetypes.matchesArtifactPattern( relativePath ) )
114 ArtifactReference artifact = toArtifactReference( relativePath );
115 if ( StringUtils.equals( artifact.getArtifactId(), reference.getArtifactId() )
116 && StringUtils.equals( artifact.getVersion(), reference.getVersion() ) )
119 deleteSupportFiles( repoFile );
122 catch ( LayoutException e )
124 /* don't fail the process if there is a bad artifact within the directory. */
130 private void deleteSupportFiles( File repoFile )
132 deleteSupportFile( repoFile, ".sha1" );
133 deleteSupportFile( repoFile, ".md5" );
134 deleteSupportFile( repoFile, ".asc" );
135 deleteSupportFile( repoFile, ".gpg" );
138 private void deleteSupportFile( File repoFile, String supportExtension )
140 File supportFile = new File( repoFile.getAbsolutePath() + supportExtension );
141 if ( supportFile.exists() && supportFile.isFile() )
143 supportFile.delete();
147 public String getId()
149 return repository.getId();
152 public Set<ArtifactReference> getRelatedArtifacts( ArtifactReference reference )
153 throws ContentNotFoundException, LayoutException
155 File artifactFile = toFile( reference );
156 File repoDir = artifactFile.getParentFile();
158 if ( !repoDir.exists() )
160 throw new ContentNotFoundException( "Unable to get related artifacts using a non-existant directory: "
161 + repoDir.getAbsolutePath() );
164 if ( !repoDir.isDirectory() )
166 throw new ContentNotFoundException( "Unable to get related artifacts using a non-directory: "
167 + repoDir.getAbsolutePath() );
170 Set<ArtifactReference> foundArtifacts = new HashSet<ArtifactReference>();
172 // First gather up the versions found as artifacts in the managed repository.
173 File projectParentDir = repoDir.getParentFile();
174 File typeDirs[] = projectParentDir.listFiles();
175 for ( File typeDir : typeDirs )
177 if ( !typeDir.isDirectory() )
179 // Skip it, we only care about directories.
183 if ( !typeDir.getName().endsWith( "s" ) )
185 // Skip it, we only care about directories that end in "s".
188 getRelatedArtifacts( typeDir, reference, foundArtifacts );
191 return foundArtifacts;
194 public String getRepoRoot()
196 return repository.getLocation();
199 public ManagedRepositoryConfiguration getRepository()
204 public Set<String> getVersions( ProjectReference reference )
205 throws ContentNotFoundException
207 File groupDir = new File( repository.getLocation(), reference.getGroupId() );
209 if ( !groupDir.exists() )
211 throw new ContentNotFoundException( "Unable to get versions using a non-existant groupId directory: "
212 + groupDir.getAbsolutePath() );
215 if ( !groupDir.isDirectory() )
217 throw new ContentNotFoundException( "Unable to get versions using a non-directory: "
218 + groupDir.getAbsolutePath() );
221 Set<String> foundVersions = new HashSet<String>();
223 // First gather up the versions found as artifacts in the managed repository.
224 File typeDirs[] = groupDir.listFiles();
225 for ( File typeDir : typeDirs )
227 if ( !typeDir.isDirectory() )
229 // Skip it, we only care about directories.
233 if ( !typeDir.getName().endsWith( "s" ) )
235 // Skip it, we only care about directories that end in "s".
238 getProjectVersions( typeDir, reference, foundVersions );
241 return foundVersions;
244 public Set<String> getVersions( VersionedReference reference )
245 throws ContentNotFoundException
247 File groupDir = new File( repository.getLocation(), reference.getGroupId() );
249 if ( !groupDir.exists() )
251 throw new ContentNotFoundException( "Unable to get versions using a non-existant groupId directory: "
252 + groupDir.getAbsolutePath() );
255 if ( !groupDir.isDirectory() )
257 throw new ContentNotFoundException( "Unable to get versions using a non-directory: "
258 + groupDir.getAbsolutePath() );
261 Set<String> foundVersions = new HashSet<String>();
263 // First gather up the versions found as artifacts in the managed repository.
264 File typeDirs[] = groupDir.listFiles();
265 for ( File typeDir : typeDirs )
267 if ( !typeDir.isDirectory() )
269 // Skip it, we only care about directories.
273 if ( !typeDir.getName().endsWith( "s" ) )
275 // Skip it, we only care about directories that end in "s".
278 getVersionedVersions( typeDir, reference, foundVersions );
281 return foundVersions;
284 public boolean hasContent( ArtifactReference reference )
286 File artifactFile = toFile( reference );
287 return artifactFile.exists() && artifactFile.isFile();
290 public boolean hasContent( ProjectReference reference )
294 Set<String> versions = getVersions( reference );
295 return CollectionUtils.isNotEmpty( versions );
297 catch ( ContentNotFoundException e )
303 public boolean hasContent( VersionedReference reference )
307 Set<String> versions = getVersions( reference );
308 return CollectionUtils.isNotEmpty( versions );
310 catch ( ContentNotFoundException e )
316 public void setRepository( ManagedRepositoryConfiguration repository )
318 this.repository = repository;
322 * Convert a path to an artifact reference.
324 * @param path the path to convert. (relative or full location path)
325 * @throws LayoutException if the path cannot be converted to an artifact reference.
328 public ArtifactReference toArtifactReference( String path )
329 throws LayoutException
331 if ( ( path != null ) && path.startsWith( repository.getLocation() ) )
333 return super.toArtifactReference( path.substring( repository.getLocation().length() ) );
336 return super.toArtifactReference( path );
339 public File toFile( ArchivaArtifact reference )
341 return new File( repository.getLocation(), toPath( reference ) );
344 public File toFile( ArtifactReference reference )
346 return new File( repository.getLocation(), toPath( reference ) );
349 public String toMetadataPath( ProjectReference reference )
351 // No metadata present in legacy repository.
355 public String toMetadataPath( VersionedReference reference )
357 // No metadata present in legacy repository.
361 private void getProjectVersions( File typeDir, ProjectReference reference, Set<String> foundVersions )
363 File repoFiles[] = typeDir.listFiles();
364 for ( File repoFile : repoFiles )
366 if ( repoFile.isDirectory() )
368 // Skip it. it's a directory.
372 String relativePath = PathUtil.getRelative( repository.getLocation(), repoFile );
374 if ( filetypes.matchesArtifactPattern( relativePath ) )
378 ArtifactReference artifact = toArtifactReference( relativePath );
379 if ( StringUtils.equals( artifact.getArtifactId(), reference.getArtifactId() ) )
381 foundVersions.add( artifact.getVersion() );
384 catch ( LayoutException e )
386 /* don't fail the process if there is a bad artifact within the directory. */
392 private void getRelatedArtifacts( File typeDir, ArtifactReference reference, Set<ArtifactReference> foundArtifacts )
394 File repoFiles[] = typeDir.listFiles();
395 for ( int i = 0; i < repoFiles.length; i++ )
397 if ( repoFiles[i].isDirectory() )
399 // Skip it. it's a directory.
403 String relativePath = PathUtil.getRelative( repository.getLocation(), repoFiles[i] );
405 if ( filetypes.matchesArtifactPattern( relativePath ) )
409 ArtifactReference artifact = toArtifactReference( relativePath );
410 if ( StringUtils.equals( artifact.getArtifactId(), reference.getArtifactId() )
411 && artifact.getVersion().startsWith( reference.getVersion() ) )
413 foundArtifacts.add( artifact );
416 catch ( LayoutException e )
418 /* don't fail the process if there is a bad artifact within the directory. */
424 private void getVersionedVersions( File typeDir, VersionedReference reference, Set<String> foundVersions )
426 File repoFiles[] = typeDir.listFiles();
427 for ( int i = 0; i < repoFiles.length; i++ )
429 if ( repoFiles[i].isDirectory() )
431 // Skip it. it's a directory.
435 String relativePath = PathUtil.getRelative( repository.getLocation(), repoFiles[i] );
437 if ( filetypes.matchesArtifactPattern( relativePath ) )
441 ArtifactReference artifact = toArtifactReference( relativePath );
442 if ( StringUtils.equals( artifact.getArtifactId(), reference.getArtifactId() )
443 && artifact.getVersion().startsWith( reference.getVersion() ) )
445 foundVersions.add( artifact.getVersion() );
448 catch ( LayoutException e )
450 /* don't fail the process if there is a bad artifact within the directory. */