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      public void process( String path )
54          throws RepositoryPurgeException
55      {
56          try
57          {
58              File artifactFile = new File( repository.getRepoRoot(), path );
59  
60              if ( !artifactFile.exists() )
61              {
62                  return;
63              }
64  
65              ArtifactReference artifact = repository.toArtifactReference( path );
66  
67              if ( VersionUtil.isSnapshot( artifact.getVersion() ) )
68              {
69                  VersionedReference reference = new VersionedReference();
70                  reference.setGroupId( artifact.getGroupId() );
71                  reference.setArtifactId( artifact.getArtifactId() );
72                  reference.setVersion( artifact.getVersion() );
73  
74                  List<String> versions = new ArrayList<String>( repository.getVersions( reference ) );
75  
76                  Collections.sort( versions, VersionComparator.getInstance() );
77  
78                  if ( retentionCount > versions.size() )
79                  {
80                      // Done. nothing to do here. skip it.
81                      return;
82                  }
83  
84                  int countToPurge = versions.size() - retentionCount;
85  
86                  for ( String version : versions )
87                  {
88                      if ( countToPurge-- <= 0 )
89                      {
90                          break;
91                      }
92  
93                      doPurgeAllRelated( artifact, version );
94                  }
95              }
96          }
97          catch ( LayoutException le )
98          {
99              throw new RepositoryPurgeException( le.getMessage(), le );
100         }
101         catch ( ContentNotFoundException e )
102         {
103             // Nothing to do here.
104             // TODO: Log this condition?
105         }
106     }
107 
108     private void doPurgeAllRelated( ArtifactReference reference, String version )
109         throws LayoutException
110     {
111         ArtifactReference artifact = new ArtifactReference();
112         artifact.setGroupId( reference.getGroupId() );
113         artifact.setArtifactId( reference.getArtifactId() );
114         artifact.setVersion( version );
115         artifact.setClassifier( reference.getClassifier() );
116         artifact.setType( reference.getType() );
117 
118         try
119         {
120             Set<ArtifactReference> related = repository.getRelatedArtifacts( artifact );
121             purge( related );
122         }
123         catch ( ContentNotFoundException e )
124         {
125             // Nothing to do here.
126             // TODO: Log this?
127         }
128     }
129 }