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