This project has retired. For details please refer to its
Attic page.
DaysOldRepositoryPurge xref
1 package org.apache.archiva.consumers.core.repository;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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
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
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
118 if ( VersionUtil.isGenericSnapshot( newArtifactReference.getVersion( ) ) )
119 {
120 if ( newArtifactFile.lastModified( ) < olderThanThisDate.getTimeInMillis( ) )
121 {
122 artifactsToDelete.addAll( repository.getRelatedArtifacts( newArtifactReference ) );
123 }
124 }
125
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
151
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
174 return null;
175 }
176 }
177 }
178 return null;
179 }
180
181 }