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.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.database.ArtifactDAO;
26 import org.apache.maven.archiva.indexer.RepositoryContentIndex;
27 import org.apache.maven.archiva.model.ArtifactReference;
28 import org.apache.maven.archiva.model.VersionedReference;
29 import org.apache.maven.archiva.repository.ContentNotFoundException;
30 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
31 import org.apache.maven.archiva.repository.layout.LayoutException;
34 import java.text.ParseException;
35 import java.text.SimpleDateFormat;
36 import java.util.ArrayList;
37 import java.util.Calendar;
38 import java.util.Collections;
39 import java.util.Date;
40 import java.util.List;
43 import java.util.regex.Matcher;
46 * Purge from repository all snapshots older than the specified days in the repository configuration.
48 * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
50 public class DaysOldRepositoryPurge
51 extends AbstractRepositoryPurge
53 private SimpleDateFormat timestampParser;
55 private int daysOlder;
57 private int retentionCount;
59 public DaysOldRepositoryPurge( ManagedRepositoryContent repository, ArtifactDAO artifactDao, int daysOlder,
60 int retentionCount, Map<String, RepositoryContentIndex> indices )
62 super( repository, artifactDao, indices );
63 this.daysOlder = daysOlder;
64 this.retentionCount = retentionCount;
65 timestampParser = new SimpleDateFormat( "yyyyMMdd.HHmmss" );
66 timestampParser.setTimeZone( DateUtils.UTC_TIME_ZONE );
69 public void process( String path )
70 throws RepositoryPurgeException
74 File artifactFile = new File( repository.getRepoRoot(), path );
76 if ( !artifactFile.exists() )
81 ArtifactReference artifact = repository.toArtifactReference( path );
83 Calendar olderThanThisDate = Calendar.getInstance( DateUtils.UTC_TIME_ZONE );
84 olderThanThisDate.add( Calendar.DATE, -daysOlder );
86 // respect retention count
87 VersionedReference reference = new VersionedReference();
88 reference.setGroupId( artifact.getGroupId() );
89 reference.setArtifactId( artifact.getArtifactId() );
90 reference.setVersion( artifact.getVersion() );
92 List<String> versions = new ArrayList<String>( repository.getVersions( reference ) );
94 Collections.sort( versions, VersionComparator.getInstance() );
96 if ( retentionCount > versions.size() )
98 // Done. nothing to do here. skip it.
102 int countToPurge = versions.size() - retentionCount;
104 for ( String version : versions )
106 if ( countToPurge-- <= 0 )
111 ArtifactReference newArtifactReference =
112 repository.toArtifactReference( artifactFile.getAbsolutePath() );
113 newArtifactReference.setVersion( version );
115 File newArtifactFile = repository.toFile( newArtifactReference );
117 // Is this a generic snapshot "1.0-SNAPSHOT" ?
118 if ( VersionUtil.isGenericSnapshot( newArtifactReference.getVersion() ) )
120 if ( newArtifactFile.lastModified() < olderThanThisDate.getTimeInMillis() )
122 doPurgeAllRelated( newArtifactReference );
125 // Is this a timestamp snapshot "1.0-20070822.123456-42" ?
126 else if ( VersionUtil.isUniqueSnapshot( newArtifactReference.getVersion() ) )
128 Calendar timestampCal = uniqueSnapshotToCalendar( newArtifactReference.getVersion() );
130 if ( timestampCal.getTimeInMillis() < olderThanThisDate.getTimeInMillis() )
132 doPurgeAllRelated( newArtifactReference );
134 else if ( newArtifactFile.lastModified() < olderThanThisDate.getTimeInMillis() )
136 doPurgeAllRelated( newArtifactReference );
141 catch ( LayoutException le )
143 throw new RepositoryPurgeException( le.getMessage(), le );
145 catch ( ContentNotFoundException e )
147 throw new RepositoryPurgeException( e.getMessage(), e );
151 private Calendar uniqueSnapshotToCalendar( String version )
153 // The latestVersion will contain the full version string "1.0-alpha-5-20070821.213044-8"
154 // This needs to be broken down into ${base}-${timestamp}-${build_number}
156 Matcher m = VersionUtil.UNIQUE_SNAPSHOT_PATTERN.matcher( version );
159 Matcher mtimestamp = VersionUtil.TIMESTAMP_PATTERN.matcher( m.group( 2 ) );
160 if ( mtimestamp.matches() )
162 String tsDate = mtimestamp.group( 1 );
163 String tsTime = mtimestamp.group( 2 );
168 versionDate = timestampParser.parse( tsDate + "." + tsTime );
169 Calendar cal = Calendar.getInstance( DateUtils.UTC_TIME_ZONE );
170 cal.setTime( versionDate );
174 catch ( ParseException e )
184 private void doPurgeAllRelated( ArtifactReference reference )
185 throws LayoutException
189 Set<ArtifactReference> related = repository.getRelatedArtifacts( reference );
192 catch ( ContentNotFoundException e )
194 // Nothing to do here.