1 package org.apache.maven.archiva.consumers.core.repository;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
22 import org.apache.archiva.repository.events.RepositoryListener;
23 import org.apache.commons.lang.time.DateUtils;
24 import org.apache.maven.archiva.common.utils.VersionComparator;
25 import org.apache.maven.archiva.common.utils.VersionUtil;
26 import org.apache.maven.archiva.model.ArtifactReference;
27 import org.apache.maven.archiva.model.VersionedReference;
28 import org.apache.maven.archiva.repository.ContentNotFoundException;
29 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
30 import org.apache.maven.archiva.repository.layout.LayoutException;
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;
41 import java.util.regex.Matcher;
44 * Purge from repository all snapshots older than the specified days in the repository configuration.
47 public class DaysOldRepositoryPurge
48 extends AbstractRepositoryPurge
50 private SimpleDateFormat timestampParser;
52 private int daysOlder;
54 private int retentionCount;
56 public DaysOldRepositoryPurge( ManagedRepositoryContent repository, int daysOlder,
57 int retentionCount, List<RepositoryListener> listeners )
59 super( repository, listeners );
60 this.daysOlder = daysOlder;
61 this.retentionCount = retentionCount;
62 timestampParser = new SimpleDateFormat( "yyyyMMdd.HHmmss" );
63 timestampParser.setTimeZone( DateUtils.UTC_TIME_ZONE );
66 public void process( String path )
67 throws RepositoryPurgeException
71 File artifactFile = new File( repository.getRepoRoot(), path );
73 if ( !artifactFile.exists() )
78 ArtifactReference artifact = repository.toArtifactReference( path );
80 Calendar olderThanThisDate = Calendar.getInstance( DateUtils.UTC_TIME_ZONE );
81 olderThanThisDate.add( Calendar.DATE, -daysOlder );
83 // respect retention count
84 VersionedReference reference = new VersionedReference();
85 reference.setGroupId( artifact.getGroupId() );
86 reference.setArtifactId( artifact.getArtifactId() );
87 reference.setVersion( artifact.getVersion() );
89 List<String> versions = new ArrayList<String>( repository.getVersions( reference ) );
91 Collections.sort( versions, VersionComparator.getInstance() );
93 if ( retentionCount > versions.size() )
95 // Done. nothing to do here. skip it.
99 int countToPurge = versions.size() - retentionCount;
101 for ( String version : versions )
103 if ( countToPurge-- <= 0 )
108 ArtifactReference newArtifactReference =
109 repository.toArtifactReference( artifactFile.getAbsolutePath() );
110 newArtifactReference.setVersion( version );
112 File newArtifactFile = repository.toFile( newArtifactReference );
114 // Is this a generic snapshot "1.0-SNAPSHOT" ?
115 if ( VersionUtil.isGenericSnapshot( newArtifactReference.getVersion() ) )
117 if ( newArtifactFile.lastModified() < olderThanThisDate.getTimeInMillis() )
119 doPurgeAllRelated( newArtifactReference );
122 // Is this a timestamp snapshot "1.0-20070822.123456-42" ?
123 else if ( VersionUtil.isUniqueSnapshot( newArtifactReference.getVersion() ) )
125 Calendar timestampCal = uniqueSnapshotToCalendar( newArtifactReference.getVersion() );
127 if ( timestampCal.getTimeInMillis() < olderThanThisDate.getTimeInMillis() )
129 doPurgeAllRelated( newArtifactReference );
134 catch ( ContentNotFoundException e )
136 throw new RepositoryPurgeException( e.getMessage(), e );
138 catch ( LayoutException e )
140 log.debug( "Not processing file that is not an artifact: " + e.getMessage() );
144 private Calendar uniqueSnapshotToCalendar( String version )
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}
149 Matcher m = VersionUtil.UNIQUE_SNAPSHOT_PATTERN.matcher( version );
152 Matcher mtimestamp = VersionUtil.TIMESTAMP_PATTERN.matcher( m.group( 2 ) );
153 if ( mtimestamp.matches() )
155 String tsDate = mtimestamp.group( 1 );
156 String tsTime = mtimestamp.group( 2 );
161 versionDate = timestampParser.parse( tsDate + "." + tsTime );
162 Calendar cal = Calendar.getInstance( DateUtils.UTC_TIME_ZONE );
163 cal.setTime( versionDate );
167 catch ( ParseException e )
177 private void doPurgeAllRelated( ArtifactReference reference )
181 Set<ArtifactReference> related = repository.getRelatedArtifacts( reference );
184 catch ( ContentNotFoundException e )
186 // Nothing to do here - it means the repository would have been constructed incorrectly
187 log.debug( e.getMessage(), e );