This project has retired. For details please refer to its Attic page.
CleanupReleasedSnapshotsRepositoryPurge xref
View Javadoc
1   package org.apache.archiva.consumers.core.repository;
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.archiva.common.utils.VersionComparator;
23  import org.apache.archiva.common.utils.VersionUtil;
24  import org.apache.archiva.metadata.repository.MetadataRepository;
25  import org.apache.archiva.metadata.repository.MetadataRepositoryException;
26  import org.apache.archiva.metadata.repository.RepositorySession;
27  import org.apache.archiva.model.ArtifactReference;
28  import org.apache.archiva.model.ProjectReference;
29  import org.apache.archiva.model.VersionedReference;
30  import org.apache.archiva.repository.ContentNotFoundException;
31  import org.apache.archiva.repository.LayoutException;
32  import org.apache.archiva.repository.ManagedRepositoryContent;
33  import org.apache.archiva.repository.ReleaseScheme;
34  import org.apache.archiva.repository.RepositoryException;
35  import org.apache.archiva.repository.RepositoryRegistry;
36  import org.apache.archiva.metadata.audit.RepositoryListener;
37  import org.apache.archiva.repository.metadata.base.MetadataTools;
38  import org.apache.archiva.repository.metadata.RepositoryMetadataException;
39  
40  import java.io.IOException;
41  import java.nio.file.Files;
42  import java.nio.file.Path;
43  import java.nio.file.Paths;
44  import java.util.ArrayList;
45  import java.util.Collection;
46  import java.util.Collections;
47  import java.util.List;
48  
49  /**
50   * <p>
51   * This will look in a single managed repository, and purge any snapshots that are present
52   * that have a corresponding released version on the same repository.
53   * </p>
54   * <p>
55   * So, if you have the following (presented in the m2/default layout form) ...
56   * <pre>
57   *   /com/foo/foo-tool/1.0-SNAPSHOT/foo-tool-1.0-SNAPSHOT.jar
58   *   /com/foo/foo-tool/1.1-SNAPSHOT/foo-tool-1.1-SNAPSHOT.jar
59   *   /com/foo/foo-tool/1.2.1-SNAPSHOT/foo-tool-1.2.1-SNAPSHOT.jar
60   *   /com/foo/foo-tool/1.2.1/foo-tool-1.2.1.jar
61   *   /com/foo/foo-tool/2.0-SNAPSHOT/foo-tool-2.0-SNAPSHOT.jar
62   *   /com/foo/foo-tool/2.0/foo-tool-2.0.jar
63   *   /com/foo/foo-tool/2.1-SNAPSHOT/foo-tool-2.1-SNAPSHOT.jar
64   * </pre>
65   * then the current highest ranked released (non-snapshot) version is 2.0, which means
66   * the snapshots from 1.0-SNAPSHOT, 1.1-SNAPSHOT, 1.2.1-SNAPSHOT, and 2.0-SNAPSHOT can
67   * be purged.  Leaving 2.1-SNAPSHOT in alone.
68   */
69  public class CleanupReleasedSnapshotsRepositoryPurge
70      extends AbstractRepositoryPurge
71  {
72      private MetadataTools metadataTools;
73  
74      private RepositoryRegistry repositoryRegistry;
75  
76      public CleanupReleasedSnapshotsRepositoryPurge( ManagedRepositoryContent repository, MetadataTools metadataTools,
77                                                      RepositoryRegistry repositoryRegistry,
78                                                      RepositorySession repositorySession,
79                                                      List<RepositoryListener> listeners )
80      {
81          super( repository, repositorySession, listeners );
82          this.metadataTools = metadataTools;
83          this.repositoryRegistry = repositoryRegistry;
84      }
85  
86      @Override
87      public void process( String path )
88          throws RepositoryPurgeException
89      {
90          try
91          {
92              Path artifactFile = Paths.get( repository.getRepoRoot( ), path );
93  
94              if ( !Files.exists(artifactFile) )
95              {
96                  // Nothing to do here, file doesn't exist, skip it.
97                  return;
98              }
99  
100             ArtifactReference artifactRef = repository.toArtifactReference( path );
101 
102             if ( !VersionUtil.isSnapshot( artifactRef.getVersion( ) ) )
103             {
104                 // Nothing to do here, not a snapshot, skip it.
105                 return;
106             }
107 
108             ProjectReferenceerence.html#ProjectReference">ProjectReference reference = new ProjectReference( );
109             reference.setGroupId( artifactRef.getGroupId( ) );
110             reference.setArtifactId( artifactRef.getArtifactId( ) );
111 
112             // Gether the released versions
113             List<String> releasedVersions = new ArrayList<>( );
114 
115             Collection<org.apache.archiva.repository.ManagedRepository> repos = repositoryRegistry.getManagedRepositories( );
116             for ( org.apache.archiva.repository.ManagedRepository repo : repos )
117             {
118 
119                 if ( repo.getActiveReleaseSchemes().contains( ReleaseScheme.RELEASE ))
120                 {
121                     try
122                     {
123                         ManagedRepositoryContent repoContent = repo.getContent();
124                         for ( String version : repoContent.getVersions( reference ) )
125                         {
126                             if ( !VersionUtil.isSnapshot( version ) )
127                             {
128                                 releasedVersions.add( version );
129                             }
130                         }
131                     }
132                     catch ( RepositoryException e )
133                     {
134                         // swallow
135                     }
136                 }
137             }
138 
139             Collections.sort( releasedVersions, VersionComparator.getInstance( ) );
140 
141             // Now clean out any version that is earlier than the highest released version.
142             boolean needsMetadataUpdate = false;
143 
144             VersionedReferencerence.html#VersionedReference">VersionedReference versionRef = new VersionedReference( );
145             versionRef.setGroupId( artifactRef.getGroupId( ) );
146             versionRef.setArtifactId( artifactRef.getArtifactId( ) );
147 
148             MetadataRepository metadataRepository = repositorySession.getRepository( );
149 
150             if ( releasedVersions.contains( VersionUtil.getReleaseVersion( artifactRef.getVersion( ) ) ) )
151             {
152                 versionRef.setVersion( artifactRef.getVersion( ) );
153                 repository.deleteVersion( versionRef );
154 
155                 for ( RepositoryListener listener : listeners )
156                 {
157                     listener.deleteArtifact( metadataRepository, repository.getId( ), artifactRef.getGroupId( ),
158                         artifactRef.getArtifactId( ), artifactRef.getVersion( ),
159                         artifactFile.getFileName().toString() );
160                 }
161                 metadataRepository.removeProjectVersion( repositorySession, repository.getId( ),
162                     artifactRef.getGroupId( ), artifactRef.getArtifactId( ), artifactRef.getVersion( ) );
163 
164                 needsMetadataUpdate = true;
165             }
166 
167             if ( needsMetadataUpdate )
168             {
169                 updateMetadata( artifactRef );
170             }
171         }
172         catch ( LayoutException e )
173         {
174             log.debug( "Not processing file that is not an artifact: {}", e.getMessage( ) );
175         }
176         catch ( ContentNotFoundException e )
177         {
178             throw new RepositoryPurgeException( e.getMessage( ), e );
179         }
180         catch ( MetadataRepositoryException e )
181         {
182             log.error( "Could not remove metadata during cleanup of released snapshots of {}", path, e );
183         }
184     }
185 
186 
187     /*
188      * TODO: Uses a deprecated API, but if we use the API with location string, it does not work as expected
189      * -> not sure what needs to be changed here.
190      */
191     @SuppressWarnings( "deprecation" )
192     private void updateMetadata( ArtifactReference artifact )
193     {
194         VersionedReferencerence.html#VersionedReference">VersionedReference versionRef = new VersionedReference( );
195         versionRef.setGroupId( artifact.getGroupId( ) );
196         versionRef.setArtifactId( artifact.getArtifactId( ) );
197         versionRef.setVersion( artifact.getVersion( ) );
198 
199         ProjectReferencerence.html#ProjectReference">ProjectReference projectRef = new ProjectReference( );
200         projectRef.setGroupId( artifact.getGroupId( ) );
201         projectRef.setArtifactId( artifact.getArtifactId( ) );
202 
203         try
204         {
205             metadataTools.updateMetadata( repository, versionRef );
206         }
207         catch ( ContentNotFoundException e )
208         {
209             // Ignore. (Just means we have no snapshot versions left to reference).
210         }
211         catch ( RepositoryMetadataException e )
212         {
213             // Ignore.
214         }
215         catch ( IOException e )
216         {
217             // Ignore.
218         }
219         catch ( LayoutException e )
220         {
221             // Ignore.
222         }
223 
224         try
225         {
226             metadataTools.updateMetadata( repository, projectRef );
227         }
228         catch ( ContentNotFoundException e )
229         {
230             // Ignore. (Just means we have no snapshot versions left to reference).
231         }
232         catch ( RepositoryMetadataException e )
233         {
234             // Ignore.
235         }
236         catch ( IOException e )
237         {
238             // Ignore.
239         }
240         catch ( LayoutException e )
241         {
242             // Ignore.
243         }
244     }
245 }