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
23 import java.text.ParseException;
24 import java.text.SimpleDateFormat;
25 import java.util.ArrayList;
26 import java.util.Calendar;
27 import java.util.Collections;
28 import java.util.Date;
29 import java.util.List;
31 import java.util.regex.Matcher;
33 import org.apache.commons.lang.time.DateUtils;
34 import org.apache.maven.archiva.common.utils.VersionComparator;
35 import org.apache.maven.archiva.common.utils.VersionUtil;
36 import org.apache.maven.archiva.model.ArtifactReference;
37 import org.apache.maven.archiva.model.VersionedReference;
38 import org.apache.maven.archiva.repository.ContentNotFoundException;
39 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
40 import org.apache.maven.archiva.repository.events.RepositoryListener;
41 import org.apache.maven.archiva.repository.layout.LayoutException;
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 );
131 else if ( newArtifactFile.lastModified() < olderThanThisDate.getTimeInMillis() )
133 doPurgeAllRelated( newArtifactReference );
138 catch ( LayoutException le )
140 throw new RepositoryPurgeException( le.getMessage(), le );
142 catch ( ContentNotFoundException e )
144 throw new RepositoryPurgeException( e.getMessage(), e );
148 private Calendar uniqueSnapshotToCalendar( String version )
150 // The latestVersion will contain the full version string "1.0-alpha-5-20070821.213044-8"
151 // This needs to be broken down into ${base}-${timestamp}-${build_number}
153 Matcher m = VersionUtil.UNIQUE_SNAPSHOT_PATTERN.matcher( version );
156 Matcher mtimestamp = VersionUtil.TIMESTAMP_PATTERN.matcher( m.group( 2 ) );
157 if ( mtimestamp.matches() )
159 String tsDate = mtimestamp.group( 1 );
160 String tsTime = mtimestamp.group( 2 );
165 versionDate = timestampParser.parse( tsDate + "." + tsTime );
166 Calendar cal = Calendar.getInstance( DateUtils.UTC_TIME_ZONE );
167 cal.setTime( versionDate );
171 catch ( ParseException e )
181 private void doPurgeAllRelated( ArtifactReference reference )
182 throws LayoutException
186 Set<ArtifactReference> related = repository.getRelatedArtifacts( reference );
189 catch ( ContentNotFoundException e )
191 // Nothing to do here.