Coverage Report - org.apache.maven.archiva.consumers.core.repository.DaysOldRepositoryPurge
 
Classes in this File Line Coverage Branch Coverage Complexity
DaysOldRepositoryPurge
0%
0/60
0%
0/20
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 org.apache.commons.lang.time.DateUtils;
 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.ManagedRepositoryContent;
 29  
 import org.apache.maven.archiva.repository.events.RepositoryListener;
 30  
 import org.apache.maven.archiva.repository.layout.LayoutException;
 31  
 
 32  
 import java.io.File;
 33  
 import java.text.ParseException;
 34  
 import java.text.SimpleDateFormat;
 35  
 import java.util.ArrayList;
 36  
 import java.util.Calendar;
 37  
 import java.util.Collections;
 38  
 import java.util.Date;
 39  
 import java.util.List;
 40  
 import java.util.Set;
 41  
 import java.util.regex.Matcher;
 42  
 
 43  
 /**
 44  
  * Purge from repository all snapshots older than the specified days in the repository configuration.
 45  
  * 
 46  
  */
 47  
 public class DaysOldRepositoryPurge
 48  
     extends AbstractRepositoryPurge
 49  
 {
 50  
     private SimpleDateFormat timestampParser;
 51  
 
 52  
     private int daysOlder;
 53  
 
 54  
     private int retentionCount;
 55  
 
 56  
     public DaysOldRepositoryPurge( ManagedRepositoryContent repository, int daysOlder,
 57  
                                    int retentionCount, List<RepositoryListener> listeners )
 58  
     {
 59  0
         super( repository, listeners );
 60  0
         this.daysOlder = daysOlder;
 61  0
         this.retentionCount = retentionCount;
 62  0
         timestampParser = new SimpleDateFormat( "yyyyMMdd.HHmmss" );
 63  0
         timestampParser.setTimeZone( DateUtils.UTC_TIME_ZONE );
 64  0
     }
 65  
 
 66  
     public void process( String path )
 67  
         throws RepositoryPurgeException
 68  
     {
 69  
         try
 70  
         {
 71  0
             File artifactFile = new File( repository.getRepoRoot(), path );
 72  
 
 73  0
             if ( !artifactFile.exists() )
 74  
             {
 75  0
                 return;
 76  
             }
 77  
 
 78  0
             ArtifactReference artifact = repository.toArtifactReference( path );
 79  
 
 80  0
             Calendar olderThanThisDate = Calendar.getInstance( DateUtils.UTC_TIME_ZONE );
 81  0
             olderThanThisDate.add( Calendar.DATE, -daysOlder );
 82  
 
 83  
             // respect retention count
 84  0
             VersionedReference reference = new VersionedReference();
 85  0
             reference.setGroupId( artifact.getGroupId() );
 86  0
             reference.setArtifactId( artifact.getArtifactId() );
 87  0
             reference.setVersion( artifact.getVersion() );
 88  
 
 89  0
             List<String> versions = new ArrayList<String>( repository.getVersions( reference ) );
 90  
 
 91  0
             Collections.sort( versions, VersionComparator.getInstance() );
 92  
 
 93  0
             if ( retentionCount > versions.size() )
 94  
             {
 95  
                 // Done. nothing to do here. skip it.
 96  0
                 return;
 97  
             }
 98  
 
 99  0
             int countToPurge = versions.size() - retentionCount;
 100  
 
 101  0
             for ( String version : versions )
 102  
             {
 103  0
                 if ( countToPurge-- <= 0 )
 104  
                 {
 105  0
                     break;
 106  
                 }
 107  
 
 108  0
                 ArtifactReference newArtifactReference =
 109  
                     repository.toArtifactReference( artifactFile.getAbsolutePath() );
 110  0
                 newArtifactReference.setVersion( version );
 111  
 
 112  0
                 File newArtifactFile = repository.toFile( newArtifactReference );
 113  
 
 114  
                 // Is this a generic snapshot "1.0-SNAPSHOT" ?
 115  0
                 if ( VersionUtil.isGenericSnapshot( newArtifactReference.getVersion() ) )
 116  
                 {
 117  0
                     if ( newArtifactFile.lastModified() < olderThanThisDate.getTimeInMillis() )
 118  
                     {
 119  0
                         doPurgeAllRelated( newArtifactReference );
 120  
                     }
 121  
                 }
 122  
                 // Is this a timestamp snapshot "1.0-20070822.123456-42" ?
 123  0
                 else if ( VersionUtil.isUniqueSnapshot( newArtifactReference.getVersion() ) )
 124  
                 {
 125  0
                     Calendar timestampCal = uniqueSnapshotToCalendar( newArtifactReference.getVersion() );
 126  
 
 127  0
                     if ( timestampCal.getTimeInMillis() < olderThanThisDate.getTimeInMillis() )
 128  
                     {
 129  0
                         doPurgeAllRelated( newArtifactReference );
 130  
                     }
 131  
                 }
 132  0
             }
 133  
         }
 134  0
         catch ( ContentNotFoundException e )
 135  
         {
 136  0
             throw new RepositoryPurgeException( e.getMessage(), e );
 137  
         }
 138  0
         catch ( LayoutException e )
 139  
         {
 140  0
             log.debug( "Not processing file that is not an artifact: " + e.getMessage() );
 141  0
         }
 142  0
     }
 143  
 
 144  
     private Calendar uniqueSnapshotToCalendar( String version )
 145  
     {
 146  
         // The latestVersion will contain the full version string "1.0-alpha-5-20070821.213044-8"
 147  
         // This needs to be broken down into ${base}-${timestamp}-${build_number}
 148  
 
 149  0
         Matcher m = VersionUtil.UNIQUE_SNAPSHOT_PATTERN.matcher( version );
 150  0
         if ( m.matches() )
 151  
         {
 152  0
             Matcher mtimestamp = VersionUtil.TIMESTAMP_PATTERN.matcher( m.group( 2 ) );
 153  0
             if ( mtimestamp.matches() )
 154  
             {
 155  0
                 String tsDate = mtimestamp.group( 1 );
 156  0
                 String tsTime = mtimestamp.group( 2 );
 157  
 
 158  
                 Date versionDate;
 159  
                 try
 160  
                 {
 161  0
                     versionDate = timestampParser.parse( tsDate + "." + tsTime );
 162  0
                     Calendar cal = Calendar.getInstance( DateUtils.UTC_TIME_ZONE );
 163  0
                     cal.setTime( versionDate );
 164  
 
 165  0
                     return cal;
 166  
                 }
 167  0
                 catch ( ParseException e )
 168  
                 {
 169  
                     // Invalid Date/Time
 170  0
                     return null;
 171  
                 }
 172  
             }
 173  
         }
 174  0
         return null;
 175  
     }
 176  
 
 177  
     private void doPurgeAllRelated( ArtifactReference reference )
 178  
     {
 179  
         try
 180  
         {
 181  0
             Set<ArtifactReference> related = repository.getRelatedArtifacts( reference );
 182  0
             purge( related );
 183  
         }
 184  0
         catch ( ContentNotFoundException e )
 185  
         {
 186  
             // Nothing to do here - it means the repository would have been constructed incorrectly
 187  0
             log.debug( e.getMessage(), e );
 188  0
         }
 189  0
     }
 190  
 }