This project has retired. For details please refer to its Attic page.
DaysOldRepositoryPurge xref
View Javadoc
1   package org.apache.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.archiva.common.utils.VersionComparator;
23  import org.apache.archiva.common.utils.VersionUtil;
24  import org.apache.archiva.metadata.repository.RepositorySession;
25  import org.apache.archiva.model.ArtifactReference;
26  import org.apache.archiva.model.VersionedReference;
27  import org.apache.archiva.repository.ContentNotFoundException;
28  import org.apache.archiva.repository.ManagedRepositoryContent;
29  import org.apache.archiva.repository.events.RepositoryListener;
30  import org.apache.archiva.repository.layout.LayoutException;
31  import org.apache.commons.lang.time.DateUtils;
32  
33  import java.io.File;
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.HashSet;
41  import java.util.List;
42  import java.util.Set;
43  import java.util.regex.Matcher;
44  
45  /**
46   * Purge from repository all snapshots older than the specified days in the repository configuration.
47   */
48  public class DaysOldRepositoryPurge
49      extends AbstractRepositoryPurge
50  {
51      private SimpleDateFormat timestampParser;
52  
53      private int daysOlder;
54  
55      private int retentionCount;
56  
57      public DaysOldRepositoryPurge( ManagedRepositoryContent repository, int daysOlder, int retentionCount,
58                                     RepositorySession repositorySession, List<RepositoryListener> listeners )
59      {
60          super( repository, repositorySession, listeners );
61          this.daysOlder = daysOlder;
62          this.retentionCount = retentionCount;
63          timestampParser = new SimpleDateFormat( "yyyyMMdd.HHmmss" );
64          timestampParser.setTimeZone( DateUtils.UTC_TIME_ZONE );
65      }
66  
67      @Override
68      public void process( String path )
69          throws RepositoryPurgeException
70      {
71          try
72          {
73              File artifactFile = new File( repository.getRepoRoot( ), path );
74  
75              if ( !artifactFile.exists( ) )
76              {
77                  return;
78              }
79  
80              ArtifactReference artifact = repository.toArtifactReference( path );
81  
82              Calendar olderThanThisDate = Calendar.getInstance( DateUtils.UTC_TIME_ZONE );
83              olderThanThisDate.add( Calendar.DATE, -daysOlder );
84  
85              // respect retention count
86              VersionedReference reference = new VersionedReference( );
87              reference.setGroupId( artifact.getGroupId( ) );
88              reference.setArtifactId( artifact.getArtifactId( ) );
89              reference.setVersion( artifact.getVersion( ) );
90  
91              List<String> versions = new ArrayList<>( repository.getVersions( reference ) );
92  
93              Collections.sort( versions, VersionComparator.getInstance( ) );
94  
95              if ( retentionCount > versions.size( ) )
96              {
97                  // Done. nothing to do here. skip it.
98                  return;
99              }
100 
101             int countToPurge = versions.size( ) - retentionCount;
102 
103             Set<ArtifactReference> artifactsToDelete = new HashSet<>( );
104             for ( String version : versions )
105             {
106                 if ( countToPurge-- <= 0 )
107                 {
108                     break;
109                 }
110 
111                 ArtifactReference newArtifactReference = repository.toArtifactReference(
112                     artifactFile.getAbsolutePath( ) );
113                 newArtifactReference.setVersion( version );
114 
115                 File newArtifactFile = repository.toFile( newArtifactReference );
116 
117                 // Is this a generic snapshot "1.0-SNAPSHOT" ?
118                 if ( VersionUtil.isGenericSnapshot( newArtifactReference.getVersion( ) ) )
119                 {
120                     if ( newArtifactFile.lastModified( ) < olderThanThisDate.getTimeInMillis( ) )
121                     {
122                         artifactsToDelete.addAll( repository.getRelatedArtifacts( newArtifactReference ) );
123                     }
124                 }
125                 // Is this a timestamp snapshot "1.0-20070822.123456-42" ?
126                 else if ( VersionUtil.isUniqueSnapshot( newArtifactReference.getVersion( ) ) )
127                 {
128                     Calendar timestampCal = uniqueSnapshotToCalendar( newArtifactReference.getVersion( ) );
129 
130                     if ( timestampCal.getTimeInMillis( ) < olderThanThisDate.getTimeInMillis( ) )
131                     {
132                         artifactsToDelete.addAll( repository.getRelatedArtifacts( newArtifactReference ) );
133                     }
134                 }
135             }
136             purge( artifactsToDelete );
137         }
138         catch ( ContentNotFoundException e )
139         {
140             throw new RepositoryPurgeException( e.getMessage( ), e );
141         }
142         catch ( LayoutException e )
143         {
144             log.debug( "Not processing file that is not an artifact: {}", e.getMessage( ) );
145         }
146     }
147 
148     private Calendar uniqueSnapshotToCalendar( String version )
149     {
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}
152 
153         Matcher m = VersionUtil.UNIQUE_SNAPSHOT_PATTERN.matcher( version );
154         if ( m.matches( ) )
155         {
156             Matcher mtimestamp = VersionUtil.TIMESTAMP_PATTERN.matcher( m.group( 2 ) );
157             if ( mtimestamp.matches( ) )
158             {
159                 String tsDate = mtimestamp.group( 1 );
160                 String tsTime = mtimestamp.group( 2 );
161 
162                 Date versionDate;
163                 try
164                 {
165                     versionDate = timestampParser.parse( tsDate + "." + tsTime );
166                     Calendar cal = Calendar.getInstance( DateUtils.UTC_TIME_ZONE );
167                     cal.setTime( versionDate );
168 
169                     return cal;
170                 }
171                 catch ( ParseException e )
172                 {
173                     // Invalid Date/Time
174                     return null;
175                 }
176             }
177         }
178         return null;
179     }
180 
181 }