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