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.VersionUtil;
24 import org.apache.maven.archiva.database.ArtifactDAO;
25 import org.apache.maven.archiva.model.ArtifactReference;
26 import org.apache.maven.archiva.repository.ContentNotFoundException;
27 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
28 import org.apache.maven.archiva.repository.layout.LayoutException;
31 import java.text.ParseException;
32 import java.text.SimpleDateFormat;
33 import java.util.Calendar;
34 import java.util.Date;
36 import java.util.regex.Matcher;
39 * Purge from repository all snapshots older than the specified days in the repository configuration.
41 * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
43 public class DaysOldRepositoryPurge
44 extends AbstractRepositoryPurge
46 private SimpleDateFormat timestampParser;
48 private int daysOlder;
50 public DaysOldRepositoryPurge( ManagedRepositoryContent repository, ArtifactDAO artifactDao,
53 super( repository, artifactDao );
54 this.daysOlder = daysOlder;
55 timestampParser = new SimpleDateFormat( "yyyyMMdd.HHmmss" );
56 timestampParser.setTimeZone( DateUtils.UTC_TIME_ZONE );
59 public void process( String path )
60 throws RepositoryPurgeException
64 File artifactFile = new File( repository.getRepoRoot(), path );
66 if ( !artifactFile.exists() )
71 ArtifactReference artifact = repository.toArtifactReference( path );
73 Calendar olderThanThisDate = Calendar.getInstance( DateUtils.UTC_TIME_ZONE );
74 olderThanThisDate.add( Calendar.DATE, -daysOlder );
76 // Is this a generic snapshot "1.0-SNAPSHOT" ?
77 if ( VersionUtil.isGenericSnapshot( artifact.getVersion() ) )
79 if ( artifactFile.lastModified() < olderThanThisDate.getTimeInMillis() )
81 doPurgeAllRelated( artifactFile );
84 // Is this a timestamp snapshot "1.0-20070822.123456-42" ?
85 else if ( VersionUtil.isUniqueSnapshot( artifact.getVersion() ) )
87 Calendar timestampCal = uniqueSnapshotToCalendar( artifact.getVersion() );
89 if ( timestampCal.getTimeInMillis() < olderThanThisDate.getTimeInMillis() )
91 doPurgeAllRelated( artifactFile );
93 else if ( artifactFile.lastModified() < olderThanThisDate.getTimeInMillis() )
95 doPurgeAllRelated( artifactFile );
99 catch ( LayoutException le )
101 throw new RepositoryPurgeException( le.getMessage() );
105 private Calendar uniqueSnapshotToCalendar( String version )
107 // The latestVersion will contain the full version string "1.0-alpha-5-20070821.213044-8"
108 // This needs to be broken down into ${base}-${timestamp}-${build_number}
110 Matcher m = VersionUtil.UNIQUE_SNAPSHOT_PATTERN.matcher( version );
113 Matcher mtimestamp = VersionUtil.TIMESTAMP_PATTERN.matcher( m.group( 2 ) );
114 if ( mtimestamp.matches() )
116 String tsDate = mtimestamp.group( 1 );
117 String tsTime = mtimestamp.group( 2 );
122 versionDate = timestampParser.parse( tsDate + "." + tsTime );
123 Calendar cal = Calendar.getInstance( DateUtils.UTC_TIME_ZONE );
124 cal.setTime( versionDate );
128 catch ( ParseException e )
138 private void doPurgeAllRelated( File artifactFile ) throws LayoutException
140 ArtifactReference reference = repository.toArtifactReference( artifactFile.getAbsolutePath() );
144 Set<ArtifactReference> related = repository.getRelatedArtifacts( reference );
147 catch ( ContentNotFoundException e )
149 // Nothing to do here.