View Javadoc

1   package org.apache.continuum.purge.repository.content;
2   
3   /*
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
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
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
19   * under the License.
20   */
21  
22  import org.apache.commons.io.FileUtils;
23  import org.apache.commons.lang.StringUtils;
24  import org.apache.continuum.model.repository.LocalRepository;
25  import org.apache.continuum.purge.repository.utils.FileTypes;
26  import org.apache.maven.archiva.common.utils.PathUtil;
27  import org.apache.maven.archiva.common.utils.VersionUtil;
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.content.ArtifactExtensionMapping;
33  import org.apache.maven.archiva.repository.content.DefaultPathParser;
34  import org.apache.maven.archiva.repository.content.PathParser;
35  import org.apache.maven.archiva.repository.layout.LayoutException;
36  
37  import java.io.File;
38  import java.io.IOException;
39  import java.util.HashSet;
40  import java.util.Set;
41  
42  /**
43   * Taken from Archiva's ManagedDefaultRepositoryContent and made some few changes.
44   *
45   * @plexus.component role="org.apache.continuum.purge.repository.content.RepositoryManagedContent"
46   * role-hint="default"
47   * instantiation-strategy="per-lookup"
48   */
49  public class ManagedDefaultRepositoryContent
50      implements RepositoryManagedContent
51  {
52      private static final String MAVEN_METADATA = "maven-metadata.xml";
53  
54      private static final char PATH_SEPARATOR = '/';
55  
56      private static final char GROUP_SEPARATOR = '.';
57  
58      private static final char ARTIFACT_SEPARATOR = '-';
59  
60      private final PathParser defaultPathParser = new DefaultPathParser();
61  
62      /**
63       * @plexus.requirement role-hint="file-types"
64       */
65      private FileTypes filetypes;
66  
67      private LocalRepository repository;
68  
69      public void deleteVersion( VersionedReference reference )
70          throws ContentNotFoundException
71      {
72          String path = toMetadataPath( reference );
73          File projectPath = new File( getRepoRoot(), path );
74  
75          File projectDir = projectPath.getParentFile();
76          if ( projectDir.exists() && projectDir.isDirectory() )
77          {
78              try
79              {
80                  FileUtils.deleteDirectory( projectDir );
81              }
82              catch ( IOException e )
83              {
84                  // TODO: log this somewhere?
85              }
86          }
87      }
88  
89      public int getId()
90      {
91          return repository.getId();
92      }
93  
94      public Set<ArtifactReference> getRelatedArtifacts( ArtifactReference reference )
95          throws ContentNotFoundException, LayoutException
96      {
97          File artifactFile = toFile( reference );
98          File repoDir = artifactFile.getParentFile();
99  
100         if ( !repoDir.exists() )
101         {
102             throw new ContentNotFoundException(
103                 "Unable to get related artifacts using a non-existant directory: " + repoDir.getAbsolutePath() );
104         }
105 
106         if ( !repoDir.isDirectory() )
107         {
108             throw new ContentNotFoundException(
109                 "Unable to get related artifacts using a non-directory: " + repoDir.getAbsolutePath() );
110         }
111 
112         Set<ArtifactReference> foundArtifacts = new HashSet<ArtifactReference>();
113 
114         // First gather up the versions found as artifacts in the managed repository.
115         for ( File repoFile : repoDir.listFiles() )
116         {
117             if ( repoFile.isDirectory() )
118             {
119                 // Skip it. it's a directory.
120                 continue;
121             }
122 
123             String relativePath = PathUtil.getRelative( repository.getLocation(), repoFile );
124 
125             if ( filetypes.matchesArtifactPattern( relativePath ) )
126             {
127                 ArtifactReference artifact = toArtifactReference( relativePath );
128 
129                 // Test for related, groupId / artifactId / version must match.
130                 if ( artifact.getGroupId().equals( reference.getGroupId() ) &&
131                     artifact.getArtifactId().equals( reference.getArtifactId() ) &&
132                     artifact.getVersion().equals( reference.getVersion() ) )
133                 {
134                     foundArtifacts.add( artifact );
135                 }
136             }
137         }
138 
139         return foundArtifacts;
140     }
141 
142     public String getRepoRoot()
143     {
144         return repository.getLocation();
145     }
146 
147     public LocalRepository getRepository()
148     {
149         return repository;
150     }
151 
152     /**
153      * Gather the Available Versions (on disk) for a specific Project Reference, based on filesystem
154      * information.
155      *
156      * @return the Set of available versions, based on the project reference.
157      * @throws ContentNotFoundException
158      * @throws LayoutException
159      */
160     public Set<String> getVersions( ProjectReference reference )
161         throws ContentNotFoundException, LayoutException
162     {
163         String path = toMetadataPath( reference );
164 
165         int idx = path.lastIndexOf( '/' );
166         if ( idx > 0 )
167         {
168             path = path.substring( 0, idx );
169         }
170 
171         File repoDir = new File( repository.getLocation(), path );
172 
173         if ( !repoDir.exists() )
174         {
175             throw new ContentNotFoundException(
176                 "Unable to get Versions on a non-existant directory: " + repoDir.getAbsolutePath() );
177         }
178 
179         if ( !repoDir.isDirectory() )
180         {
181             throw new ContentNotFoundException(
182                 "Unable to get Versions on a non-directory: " + repoDir.getAbsolutePath() );
183         }
184 
185         Set<String> foundVersions = new HashSet<String>();
186         VersionedReference versionRef = new VersionedReference();
187         versionRef.setGroupId( reference.getGroupId() );
188         versionRef.setArtifactId( reference.getArtifactId() );
189 
190         for ( File repoFile : repoDir.listFiles() )
191         {
192             if ( !repoFile.isDirectory() )
193             {
194                 // Skip it. not a directory.
195                 continue;
196             }
197 
198             // Test if dir has an artifact, which proves to us that it is a valid version directory.
199             String version = repoFile.getName();
200             versionRef.setVersion( version );
201 
202             if ( hasArtifact( versionRef ) )
203             {
204                 // Found an artifact, must be a valid version.
205                 foundVersions.add( version );
206             }
207         }
208 
209         return foundVersions;
210     }
211 
212     public Set<String> getVersions( VersionedReference reference )
213         throws ContentNotFoundException, LayoutException
214     {
215         String path = toMetadataPath( reference );
216 
217         int idx = path.lastIndexOf( '/' );
218         if ( idx > 0 )
219         {
220             path = path.substring( 0, idx );
221         }
222 
223         File repoDir = new File( repository.getLocation(), path );
224 
225         if ( !repoDir.exists() )
226         {
227             throw new ContentNotFoundException(
228                 "Unable to get versions on a non-existant directory: " + repoDir.getAbsolutePath() );
229         }
230 
231         if ( !repoDir.isDirectory() )
232         {
233             throw new ContentNotFoundException(
234                 "Unable to get versions on a non-directory: " + repoDir.getAbsolutePath() );
235         }
236 
237         Set<String> foundVersions = new HashSet<String>();
238 
239         // First gather up the versions found as artifacts in the managed repository.
240         for ( File repoFile : repoDir.listFiles() )
241         {
242             if ( repoFile.isDirectory() )
243             {
244                 // Skip it. it's a directory.
245                 continue;
246             }
247 
248             String relativePath = PathUtil.getRelative( repository.getLocation(), repoFile );
249 
250             if ( filetypes.matchesDefaultExclusions( relativePath ) )
251             {
252                 // Skip it, it's metadata or similar
253                 continue;
254             }
255 
256             if ( filetypes.matchesArtifactPattern( relativePath ) )
257             {
258                 ArtifactReference artifact = toArtifactReference( relativePath );
259 
260                 foundVersions.add( artifact.getVersion() );
261             }
262         }
263 
264         return foundVersions;
265     }
266 
267 
268     public String toMetadataPath( ProjectReference reference )
269     {
270         StringBuffer path = new StringBuffer();
271 
272         path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
273         path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
274         path.append( MAVEN_METADATA );
275 
276         return path.toString();
277     }
278 
279     public String toMetadataPath( VersionedReference reference )
280     {
281         StringBuffer path = new StringBuffer();
282 
283         path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
284         path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
285         if ( reference.getVersion() != null )
286         {
287             // add the version only if it is present
288             path.append( VersionUtil.getBaseVersion( reference.getVersion() ) ).append( PATH_SEPARATOR );
289         }
290         path.append( MAVEN_METADATA );
291 
292         return path.toString();
293     }
294 
295     public String toPath( ArtifactReference reference )
296     {
297         if ( reference == null )
298         {
299             throw new IllegalArgumentException( "Artifact reference cannot be null" );
300         }
301 
302         String baseVersion = VersionUtil.getBaseVersion( reference.getVersion() );
303         return toPath( reference.getGroupId(), reference.getArtifactId(), baseVersion, reference.getVersion(),
304                        reference.getClassifier(), reference.getType() );
305     }
306 
307     public void setRepository( LocalRepository repository )
308     {
309         this.repository = repository;
310     }
311 
312     /**
313      * Convert a path to an artifact reference.
314      *
315      * @param path the path to convert. (relative or full location path)
316      * @throws LayoutException if the path cannot be converted to an artifact reference.
317      */
318     public ArtifactReference toArtifactReference( String path )
319         throws LayoutException
320     {
321         if ( ( path != null ) && path.startsWith( repository.getLocation() ) )
322         {
323             return defaultPathParser.toArtifactReference( path.substring( repository.getLocation().length() ) );
324         }
325 
326         return defaultPathParser.toArtifactReference( path );
327     }
328 
329     public File toFile( ArtifactReference reference )
330     {
331         return new File( repository.getLocation(), toPath( reference ) );
332     }
333 
334     /**
335      * Get the first Artifact found in the provided VersionedReference location.
336      *
337      * @param reference the reference to the versioned reference to search within
338      * @return the ArtifactReference to the first artifact located within the versioned reference. or null if
339      *         no artifact was found within the versioned reference.
340      * @throws IOException     if the versioned reference is invalid (example: doesn't exist, or isn't a directory)
341      * @throws LayoutException if the path cannot be converted to an artifact reference.
342      */
343     private ArtifactReference getFirstArtifact( VersionedReference reference )
344         throws LayoutException, IOException
345     {
346         String path = toMetadataPath( reference );
347 
348         int idx = path.lastIndexOf( '/' );
349         if ( idx > 0 )
350         {
351             path = path.substring( 0, idx );
352         }
353 
354         File repoDir = new File( repository.getLocation(), path );
355 
356         if ( !repoDir.exists() )
357         {
358             throw new IOException( "Unable to gather the list of snapshot versions on a non-existant directory: " +
359                                        repoDir.getAbsolutePath() );
360         }
361 
362         if ( !repoDir.isDirectory() )
363         {
364             throw new IOException(
365                 "Unable to gather the list of snapshot versions on a non-directory: " + repoDir.getAbsolutePath() );
366         }
367 
368         for ( File repoFile : repoDir.listFiles() )
369         {
370             if ( repoFile.isDirectory() )
371             {
372                 // Skip it. it's a directory.
373                 continue;
374             }
375 
376             String relativePath = PathUtil.getRelative( repository.getLocation(), repoFile );
377 
378             if ( filetypes.matchesArtifactPattern( relativePath ) )
379             {
380                 return toArtifactReference( relativePath );
381             }
382         }
383 
384         // No artifact was found.
385         return null;
386     }
387 
388     private boolean hasArtifact( VersionedReference reference )
389         throws LayoutException
390     {
391         try
392         {
393             return ( getFirstArtifact( reference ) != null );
394         }
395         catch ( IOException e )
396         {
397             return false;
398         }
399     }
400 
401     private String formatAsDirectory( String directory )
402     {
403         return directory.replace( GROUP_SEPARATOR, PATH_SEPARATOR );
404     }
405 
406     private String toPath( String groupId, String artifactId, String baseVersion, String version, String classifier,
407                            String type )
408     {
409         StringBuffer path = new StringBuffer();
410 
411         path.append( formatAsDirectory( groupId ) ).append( PATH_SEPARATOR );
412         path.append( artifactId ).append( PATH_SEPARATOR );
413 
414         if ( baseVersion != null )
415         {
416             path.append( baseVersion ).append( PATH_SEPARATOR );
417             if ( ( version != null ) && ( type != null ) )
418             {
419                 path.append( artifactId ).append( ARTIFACT_SEPARATOR ).append( version );
420 
421                 if ( StringUtils.isNotBlank( classifier ) )
422                 {
423                     path.append( ARTIFACT_SEPARATOR ).append( classifier );
424                 }
425 
426                 path.append( GROUP_SEPARATOR ).append( ArtifactExtensionMapping.getExtension( type ) );
427             }
428         }
429 
430         return path.toString();
431     }
432 }