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