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