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;
33 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
34 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
35 import org.codehaus.plexus.util.SelectorUtils;
38 import java.io.IOException;
39 import java.util.ArrayList;
40 import java.util.HashSet;
41 import java.util.Iterator;
42 import java.util.List;
46 * ManagedDefaultRepositoryContent
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 ManagedDefaultRepositoryContent
57 extends AbstractDefaultRepositoryContent
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 String path = toMetadataPath( reference );
73 File projectPath = new File( getRepoRoot(), path );
75 File projectDir = projectPath.getParentFile();
76 if( projectDir.exists() && projectDir.isDirectory() )
80 FileUtils.deleteDirectory( projectDir );
82 catch ( IOException e )
84 // TODO: log this somewhere?
91 return repository.getId();
94 public Set<ArtifactReference> getRelatedArtifacts( ArtifactReference reference )
95 throws ContentNotFoundException, LayoutException
97 File artifactFile = toFile( reference );
98 File repoDir = artifactFile.getParentFile();
100 if ( !repoDir.exists() )
102 throw new ContentNotFoundException( "Unable to get related artifacts using a non-existant directory: "
103 + repoDir.getAbsolutePath() );
106 if ( !repoDir.isDirectory() )
108 throw new ContentNotFoundException( "Unable to get related artifacts using a non-directory: "
109 + repoDir.getAbsolutePath() );
112 Set<ArtifactReference> foundArtifacts = new HashSet<ArtifactReference>();
114 // First gather up the versions found as artifacts in the managed repository.
115 File repoFiles[] = repoDir.listFiles();
116 for ( int i = 0; i < repoFiles.length; i++ )
118 if ( repoFiles[i].isDirectory() )
120 // Skip it. it's a directory.
124 String relativePath = PathUtil.getRelative( repository.getLocation(), repoFiles[i] );
126 if ( matchesArtifactPattern( relativePath ) )
128 ArtifactReference artifact = toArtifactReference( relativePath );
130 // Test for related, groupId / artifactId / version must match.
131 if ( artifact.getGroupId().equals( reference.getGroupId() )
132 && artifact.getArtifactId().equals( reference.getArtifactId() )
133 && artifact.getVersion().equals( reference.getVersion() ) )
135 foundArtifacts.add( artifact );
140 return foundArtifacts;
143 public String getRepoRoot()
145 return repository.getLocation();
148 public ManagedRepositoryConfiguration getRepository()
154 * Gather the Available Versions (on disk) for a specific Project Reference, based on filesystem
157 * @return the Set of available versions, based on the project reference.
158 * @throws LayoutException
159 * @throws LayoutException
161 public Set<String> getVersions( ProjectReference reference )
162 throws ContentNotFoundException, LayoutException
164 String path = toMetadataPath( reference );
166 int idx = path.lastIndexOf( '/' );
169 path = path.substring( 0, idx );
172 File repoDir = new File( repository.getLocation(), path );
174 if ( !repoDir.exists() )
176 throw new ContentNotFoundException( "Unable to get Versions on a non-existant directory: "
177 + repoDir.getAbsolutePath() );
180 if ( !repoDir.isDirectory() )
182 throw new ContentNotFoundException( "Unable to get Versions on a non-directory: "
183 + repoDir.getAbsolutePath() );
186 Set<String> foundVersions = new HashSet<String>();
187 VersionedReference versionRef = new VersionedReference();
188 versionRef.setGroupId( reference.getGroupId() );
189 versionRef.setArtifactId( reference.getArtifactId() );
191 File repoFiles[] = repoDir.listFiles();
192 for ( int i = 0; i < repoFiles.length; i++ )
194 if ( !repoFiles[i].isDirectory() )
196 // Skip it. not a directory.
200 // Test if dir has an artifact, which proves to us that it is a valid version directory.
201 String version = repoFiles[i].getName();
202 versionRef.setVersion( version );
204 if ( hasArtifact( versionRef ) )
206 // Found an artifact, must be a valid version.
207 foundVersions.add( version );
211 return foundVersions;
214 public Set<String> getVersions( VersionedReference reference )
215 throws ContentNotFoundException, LayoutException
217 String path = toMetadataPath( reference );
219 int idx = path.lastIndexOf( '/' );
222 path = path.substring( 0, idx );
225 File repoDir = new File( repository.getLocation(), path );
227 if ( !repoDir.exists() )
229 throw new ContentNotFoundException( "Unable to get versions on a non-existant directory: "
230 + repoDir.getAbsolutePath() );
233 if ( !repoDir.isDirectory() )
235 throw new ContentNotFoundException( "Unable to get versions on a non-directory: "
236 + repoDir.getAbsolutePath() );
239 Set<String> foundVersions = new HashSet<String>();
241 // First gather up the versions found as artifacts in the managed repository.
242 File repoFiles[] = repoDir.listFiles();
243 for ( int i = 0; i < repoFiles.length; i++ )
245 if ( repoFiles[i].isDirectory() )
247 // Skip it. it's a directory.
251 String relativePath = PathUtil.getRelative( repository.getLocation(), repoFiles[i] );
253 if ( 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 initialize()
304 throws InitializationException
306 this.artifactPatterns = new ArrayList<String>();
310 public void setRepository( ManagedRepositoryConfiguration repository )
312 this.repository = repository;
316 * Convert a path to an artifact reference.
318 * @param path the path to convert. (relative or full location path)
319 * @throws LayoutException if the path cannot be converted to an artifact reference.
322 public ArtifactReference toArtifactReference( String path )
323 throws LayoutException
325 if ( ( path != null ) && path.startsWith( repository.getLocation() ) )
327 return super.toArtifactReference( path.substring( repository.getLocation().length() ) );
330 return super.toArtifactReference( path );
333 public File toFile( ArtifactReference reference )
335 return new File( repository.getLocation(), toPath( reference ) );
338 public File toFile( ArchivaArtifact reference )
340 return new File( repository.getLocation(), toPath( reference ) );
344 * Get the first Artifact found in the provided VersionedReference location.
346 * @param managedRepository the repository to search within.
347 * @param reference the reference to the versioned reference to search within
348 * @return the ArtifactReference to the first artifact located within the versioned reference. or null if
349 * no artifact was found within the versioned reference.
350 * @throws IOException if the versioned reference is invalid (example: doesn't exist, or isn't a directory)
351 * @throws LayoutException
353 private ArtifactReference getFirstArtifact( VersionedReference reference )
354 throws LayoutException, IOException
356 String path = toMetadataPath( reference );
358 int idx = path.lastIndexOf( '/' );
361 path = path.substring( 0, idx );
364 File repoDir = new File( repository.getLocation(), path );
366 if ( !repoDir.exists() )
368 throw new IOException( "Unable to gather the list of snapshot versions on a non-existant directory: "
369 + repoDir.getAbsolutePath() );
372 if ( !repoDir.isDirectory() )
374 throw new IOException( "Unable to gather the list of snapshot versions on a non-directory: "
375 + repoDir.getAbsolutePath() );
378 File repoFiles[] = repoDir.listFiles();
379 for ( int i = 0; i < repoFiles.length; i++ )
381 if ( repoFiles[i].isDirectory() )
383 // Skip it. it's a directory.
387 String relativePath = PathUtil.getRelative( repository.getLocation(), repoFiles[i] );
389 if ( matchesArtifactPattern( relativePath ) )
391 ArtifactReference artifact = toArtifactReference( relativePath );
397 // No artifact was found.
401 private boolean hasArtifact( VersionedReference reference )
402 throws LayoutException
406 return ( getFirstArtifact( reference ) != null );
408 catch ( IOException e )
414 private void initVariables()
416 synchronized ( this.artifactPatterns )
418 this.artifactPatterns.clear();
420 this.artifactPatterns.addAll( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
424 private boolean matchesArtifactPattern( String relativePath )
426 // Correct the slash pattern.
427 relativePath = relativePath.replace( '\\', '/' );
429 Iterator<String> it = this.artifactPatterns.iterator();
430 while ( it.hasNext() )
432 String pattern = it.next();
434 if ( SelectorUtils.matchPath( pattern, relativePath, false ) )