View Javadoc

1   package org.apache.continuum.purge.executor;
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 java.io.File;
23  import java.util.ArrayList;
24  import java.util.Collections;
25  import java.util.List;
26  
27  import org.apache.continuum.purge.repository.content.RepositoryManagedContent;
28  import org.apache.continuum.purge.ContinuumPurgeConstants;
29  import org.apache.maven.archiva.common.utils.VersionComparator;
30  import org.apache.maven.archiva.common.utils.VersionUtil;
31  import org.apache.maven.archiva.model.ArtifactReference;
32  import org.apache.maven.archiva.model.ProjectReference;
33  import org.apache.maven.archiva.model.VersionedReference;
34  import org.apache.maven.archiva.repository.ContentNotFoundException;
35  import org.apache.maven.archiva.repository.layout.LayoutException;
36  
37  import org.slf4j.Logger;
38  import org.slf4j.LoggerFactory;
39  
40  /**
41   * Codes were taken from Archiva's CleanupReleasedSnapshotsRepositoryPurge and just made some few changes
42   *
43   * @author Maria Catherine Tan
44   */
45  public class ReleasedSnapshotsRepositoryPurgeExecutor
46      extends AbstractContinuumPurgeExecutor
47  {
48      private final RepositoryManagedContent repository;
49      
50      private Logger log = LoggerFactory.getLogger( ReleasedSnapshotsRepositoryPurgeExecutor.class );
51  
52      public ReleasedSnapshotsRepositoryPurgeExecutor( RepositoryManagedContent repository )
53      {
54          this.repository = repository;
55      }
56  
57      public void purge( String path )
58          throws ContinuumPurgeExecutorException
59      {
60          try
61          {
62              File artifactFile = new File( repository.getRepoRoot(), path );
63  
64              if ( !artifactFile.exists() )
65              {
66                  // Nothing to do here, file doesn't exist, skip it.
67                  return;
68              }
69  
70              ArtifactReference artifact = repository.toArtifactReference( path );
71  
72              if ( !VersionUtil.isSnapshot( artifact.getVersion() ) )
73              {
74                  // Nothing to do here, not a snapshot, skip it.
75                  return;
76              }
77  
78              ProjectReference reference = new ProjectReference();
79              reference.setGroupId( artifact.getGroupId() );
80              reference.setArtifactId( artifact.getArtifactId() );
81  
82              // Gather up all of the versions.
83              List<String> allVersions = new ArrayList<String>( repository.getVersions( reference ) );
84  
85              // Split the versions into released and snapshots.
86              List<String> releasedVersions = new ArrayList<String>();
87              List<String> snapshotVersions = new ArrayList<String>();
88  
89              for ( String version : allVersions )
90              {
91                  if ( VersionUtil.isSnapshot( version ) )
92                  {
93                      snapshotVersions.add( version );
94                  }
95                  else
96                  {
97                      releasedVersions.add( version );
98                  }
99              }
100 
101             Collections.sort( allVersions, VersionComparator.getInstance() );
102             Collections.sort( releasedVersions, VersionComparator.getInstance() );
103             Collections.sort( snapshotVersions, VersionComparator.getInstance() );
104 
105             VersionedReference versionRef = new VersionedReference();
106             versionRef.setGroupId( artifact.getGroupId() );
107             versionRef.setArtifactId( artifact.getArtifactId() );
108 
109             for ( String version : snapshotVersions )
110             {
111                 if ( releasedVersions.contains( VersionUtil.getReleaseVersion( version ) ) )
112                 {
113                     versionRef.setVersion( version );
114                     repository.deleteVersion( versionRef );
115                     
116                     log.info( ContinuumPurgeConstants.PURGE_PROJECT + " - " + VersionedReference.toKey( versionRef ) );
117 
118                     removeMetadata( versionRef );
119                 }
120             }
121         }
122         catch ( LayoutException e )
123         {
124             throw new ContinuumPurgeExecutorException( e.getMessage(), e );
125         }
126         catch ( ContentNotFoundException e )
127         {
128             throw new ContinuumPurgeExecutorException( e.getMessage(), e );
129         }
130     }
131 
132     private void removeMetadata( VersionedReference versionRef )
133         throws ContinuumPurgeExecutorException
134     {
135         String path = repository.toMetadataPath( versionRef );
136         File projectPath = new File( repository.getRepoRoot(), path );
137 
138         File projectDir = projectPath.getParentFile();
139 
140         purgeSupportFiles( projectDir, "maven-metadata" );
141     }
142 }