This project has retired. For details please refer to its Attic page.
RetentionCountRepositoryPurge 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.RepositorySession;
25  import org.apache.archiva.model.ArtifactReference;
26  import org.apache.archiva.model.VersionedReference;
27  import org.apache.archiva.repository.ContentNotFoundException;
28  import org.apache.archiva.repository.ManagedRepositoryContent;
29  import org.apache.archiva.repository.events.RepositoryListener;
30  import org.apache.archiva.repository.layout.LayoutException;
31  
32  import java.io.File;
33  import java.util.ArrayList;
34  import java.util.Collections;
35  import java.util.HashSet;
36  import java.util.List;
37  import java.util.Set;
38  
39  /**
40   * Purge the repository by retention count. Retain only the specified number of snapshots.
41   */
42  public class RetentionCountRepositoryPurge
43      extends AbstractRepositoryPurge
44  {
45      private int retentionCount;
46  
47      public RetentionCountRepositoryPurge( ManagedRepositoryContent repository, int retentionCount,
48                                            RepositorySession repositorySession, List<RepositoryListener> listeners )
49      {
50          super( repository, repositorySession, listeners );
51          this.retentionCount = retentionCount;
52      }
53  
54      @Override
55      public void process( String path )
56          throws RepositoryPurgeException
57      {
58          try
59          {
60              File artifactFile = new File( repository.getRepoRoot( ), path );
61  
62              if ( !artifactFile.exists( ) )
63              {
64                  return;
65              }
66  
67              ArtifactReference artifact = repository.toArtifactReference( path );
68  
69              if ( VersionUtil.isSnapshot( artifact.getVersion( ) ) )
70              {
71                  VersionedReference reference = new VersionedReference( );
72                  reference.setGroupId( artifact.getGroupId( ) );
73                  reference.setArtifactId( artifact.getArtifactId( ) );
74                  reference.setVersion( artifact.getVersion( ) );
75  
76                  List<String> versions = new ArrayList<>( repository.getVersions( reference ) );
77  
78                  Collections.sort( versions, VersionComparator.getInstance( ) );
79  
80                  if ( retentionCount > versions.size( ) )
81                  {
82                      log.trace( "No deletion, because retention count is higher than actual number of artifacts." );
83                      // Done. nothing to do here. skip it.
84                      return;
85                  }
86  
87                  int countToPurge = versions.size( ) - retentionCount;
88                  Set<ArtifactReference> artifactsToDelete = new HashSet<>( );
89                  for ( String version : versions )
90                  {
91                      if ( countToPurge-- <= 0 )
92                      {
93                          break;
94                      }
95                      artifactsToDelete.addAll( repository.getRelatedArtifacts( getNewArtifactReference( artifact, version ) ) );
96                  }
97                  purge( artifactsToDelete );
98              }
99          }
100         catch ( LayoutException le )
101         {
102             throw new RepositoryPurgeException( le.getMessage( ), le );
103         }
104         catch ( ContentNotFoundException e )
105         {
106             log.error( "Repostory artifact not found {}", path );
107         }
108     }
109 
110     /*
111      * Returns a new artifact reference with different version
112      */
113     private ArtifactReference getNewArtifactReference( ArtifactReference reference, String version )
114         throws LayoutException
115     {
116         ArtifactReference artifact = new ArtifactReference( );
117         artifact.setGroupId( reference.getGroupId( ) );
118         artifact.setArtifactId( reference.getArtifactId( ) );
119         artifact.setVersion( version );
120         artifact.setClassifier( reference.getClassifier( ) );
121         artifact.setType( reference.getType( ) );
122         return artifact;
123 
124     }
125 }