View Javadoc

1   package org.apache.continuum.purge.executor;
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.continuum.purge.repository.content.RepositoryManagedContent;
23  import org.apache.maven.archiva.common.utils.VersionComparator;
24  import org.apache.maven.archiva.common.utils.VersionUtil;
25  import org.apache.maven.archiva.model.ArtifactReference;
26  import org.apache.maven.archiva.model.VersionedReference;
27  import org.apache.maven.archiva.repository.ContentNotFoundException;
28  import org.apache.maven.archiva.repository.layout.LayoutException;
29  
30  import java.io.File;
31  import java.util.ArrayList;
32  import java.util.Collections;
33  import java.util.List;
34  import java.util.Set;
35  
36  /**
37   * Codes were taken from Archiva's RetentionCountRepository Purge and made some few changes.
38   *
39   * @author Maria Catherine Tan
40   */
41  public class RetentionCountRepositoryPurgeExecutor
42      extends AbstractContinuumPurgeExecutor
43      implements ContinuumPurgeExecutor
44  {
45      private final int retentionCount;
46  
47      private final RepositoryManagedContent repository;
48  
49      public RetentionCountRepositoryPurgeExecutor( RepositoryManagedContent repository, int retentionCount )
50      {
51          this.repository = repository;
52          this.retentionCount = retentionCount;
53      }
54  
55      public void purge( String path )
56          throws ContinuumPurgeExecutorException
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<String>( repository.getVersions( reference ) );
77  
78                  Collections.sort( versions, VersionComparator.getInstance() );
79  
80                  if ( retentionCount > versions.size() )
81                  {
82                      // Done. nothing to do here. skip it.
83                      return;
84                  }
85  
86                  int countToPurge = versions.size() - retentionCount;
87  
88                  for ( String version : versions )
89                  {
90                      if ( countToPurge-- <= 0 )
91                      {
92                          break;
93                      }
94  
95                      doPurgeAllRelated( artifact, version );
96                  }
97              }
98          }
99          catch ( LayoutException e )
100         {
101             throw new ContinuumPurgeExecutorException( e.getMessage(), e );
102         }
103         catch ( ContentNotFoundException e )
104         {
105             // Nothing to do here.
106             // TODO: Log this condition?
107         }
108     }
109 
110     private void doPurgeAllRelated( ArtifactReference reference, String version )
111         throws LayoutException
112     {
113         ArtifactReference artifact = new ArtifactReference();
114         artifact.setGroupId( reference.getGroupId() );
115         artifact.setArtifactId( reference.getArtifactId() );
116         artifact.setVersion( version );
117         artifact.setClassifier( reference.getClassifier() );
118         artifact.setType( reference.getType() );
119 
120         try
121         {
122             Set<ArtifactReference> related = repository.getRelatedArtifacts( artifact );
123             purge( related, repository );
124         }
125         catch ( ContentNotFoundException e )
126         {
127             // Nothing to do here.
128             // TODO: Log this?
129         }
130     }
131 }