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.metadata.repository.RepositorySession;
23  import org.apache.archiva.repository.events.RepositoryListener;
24  import org.apache.archiva.common.utils.VersionComparator;
25  import org.apache.archiva.common.utils.VersionUtil;
26  import org.apache.archiva.model.ArtifactReference;
27  import org.apache.archiva.model.VersionedReference;
28  import org.apache.archiva.repository.ContentNotFoundException;
29  import org.apache.archiva.repository.ManagedRepositoryContent;
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.List;
36  import java.util.Set;
37  
38  /**
39   * Purge the repository by retention count. Retain only the specified number of snapshots.
40   */
41  public class RetentionCountRepositoryPurge
42      extends AbstractRepositoryPurge
43  {
44      private int retentionCount;
45  
46      public RetentionCountRepositoryPurge( ManagedRepositoryContent repository, int retentionCount,
47                                            RepositorySession repositorySession, List<RepositoryListener> listeners )
48      {
49          super( repository, repositorySession, listeners );
50          this.retentionCount = retentionCount;
51      }
52  
53      @Override
54      public void process( String path )
55          throws RepositoryPurgeException
56      {
57          try
58          {
59              File artifactFile = new File( repository.getRepoRoot(), path );
60  
61              if ( !artifactFile.exists() )
62              {
63                  return;
64              }
65  
66              ArtifactReference artifact = repository.toArtifactReference( path );
67  
68              if ( VersionUtil.isSnapshot( artifact.getVersion() ) )
69              {
70                  VersionedReference reference = new VersionedReference();
71                  reference.setGroupId( artifact.getGroupId() );
72                  reference.setArtifactId( artifact.getArtifactId() );
73                  reference.setVersion( artifact.getVersion() );
74  
75                  List<String> versions = new ArrayList<>( repository.getVersions( reference ) );
76  
77                  Collections.sort( versions, VersionComparator.getInstance() );
78  
79                  if ( retentionCount > versions.size() )
80                  {
81                      // Done. nothing to do here. skip it.
82                      return;
83                  }
84  
85                  int countToPurge = versions.size() - retentionCount;
86  
87                  for ( String version : versions )
88                  {
89                      if ( countToPurge-- <= 0 )
90                      {
91                          break;
92                      }
93  
94                      doPurgeAllRelated( artifact, version );
95                  }
96              }
97          }
98          catch ( LayoutException le )
99          {
100             throw new RepositoryPurgeException( le.getMessage(), le );
101         }
102         catch ( ContentNotFoundException e )
103         {
104             // Nothing to do here.
105             // TODO: Log this condition?
106         }
107     }
108 
109     private void doPurgeAllRelated( ArtifactReference reference, String version )
110         throws LayoutException
111     {
112         ArtifactReference artifact = new ArtifactReference();
113         artifact.setGroupId( reference.getGroupId() );
114         artifact.setArtifactId( reference.getArtifactId() );
115         artifact.setVersion( version );
116         artifact.setClassifier( reference.getClassifier() );
117         artifact.setType( reference.getType() );
118 
119         try
120         {
121             Set<ArtifactReference> related = repository.getRelatedArtifacts( artifact );
122             purge( related );
123         }
124         catch ( ContentNotFoundException e )
125         {
126             // Nothing to do here.
127             // TODO: Log this?
128         }
129     }
130 }