1 | |
package org.apache.maven.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.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.model.ArtifactReference; |
26 | |
import org.apache.maven.archiva.model.VersionedReference; |
27 | |
import org.apache.maven.archiva.repository.ContentNotFoundException; |
28 | |
import org.apache.maven.archiva.repository.ManagedRepositoryContent; |
29 | |
import org.apache.maven.archiva.repository.events.RepositoryListener; |
30 | |
import org.apache.maven.archiva.repository.layout.LayoutException; |
31 | |
|
32 | |
import java.io.File; |
33 | |
import java.text.ParseException; |
34 | |
import java.text.SimpleDateFormat; |
35 | |
import java.util.ArrayList; |
36 | |
import java.util.Calendar; |
37 | |
import java.util.Collections; |
38 | |
import java.util.Date; |
39 | |
import java.util.List; |
40 | |
import java.util.Set; |
41 | |
import java.util.regex.Matcher; |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
public class DaysOldRepositoryPurge |
48 | |
extends AbstractRepositoryPurge |
49 | |
{ |
50 | |
private SimpleDateFormat timestampParser; |
51 | |
|
52 | |
private int daysOlder; |
53 | |
|
54 | |
private int retentionCount; |
55 | |
|
56 | |
public DaysOldRepositoryPurge( ManagedRepositoryContent repository, int daysOlder, |
57 | |
int retentionCount, List<RepositoryListener> listeners ) |
58 | |
{ |
59 | 0 | super( repository, listeners ); |
60 | 0 | this.daysOlder = daysOlder; |
61 | 0 | this.retentionCount = retentionCount; |
62 | 0 | timestampParser = new SimpleDateFormat( "yyyyMMdd.HHmmss" ); |
63 | 0 | timestampParser.setTimeZone( DateUtils.UTC_TIME_ZONE ); |
64 | 0 | } |
65 | |
|
66 | |
public void process( String path ) |
67 | |
throws RepositoryPurgeException |
68 | |
{ |
69 | |
try |
70 | |
{ |
71 | 0 | File artifactFile = new File( repository.getRepoRoot(), path ); |
72 | |
|
73 | 0 | if ( !artifactFile.exists() ) |
74 | |
{ |
75 | 0 | return; |
76 | |
} |
77 | |
|
78 | 0 | ArtifactReference artifact = repository.toArtifactReference( path ); |
79 | |
|
80 | 0 | Calendar olderThanThisDate = Calendar.getInstance( DateUtils.UTC_TIME_ZONE ); |
81 | 0 | olderThanThisDate.add( Calendar.DATE, -daysOlder ); |
82 | |
|
83 | |
|
84 | 0 | VersionedReference reference = new VersionedReference(); |
85 | 0 | reference.setGroupId( artifact.getGroupId() ); |
86 | 0 | reference.setArtifactId( artifact.getArtifactId() ); |
87 | 0 | reference.setVersion( artifact.getVersion() ); |
88 | |
|
89 | 0 | List<String> versions = new ArrayList<String>( repository.getVersions( reference ) ); |
90 | |
|
91 | 0 | Collections.sort( versions, VersionComparator.getInstance() ); |
92 | |
|
93 | 0 | if ( retentionCount > versions.size() ) |
94 | |
{ |
95 | |
|
96 | 0 | return; |
97 | |
} |
98 | |
|
99 | 0 | int countToPurge = versions.size() - retentionCount; |
100 | |
|
101 | 0 | for ( String version : versions ) |
102 | |
{ |
103 | 0 | if ( countToPurge-- <= 0 ) |
104 | |
{ |
105 | 0 | break; |
106 | |
} |
107 | |
|
108 | 0 | ArtifactReference newArtifactReference = |
109 | |
repository.toArtifactReference( artifactFile.getAbsolutePath() ); |
110 | 0 | newArtifactReference.setVersion( version ); |
111 | |
|
112 | 0 | File newArtifactFile = repository.toFile( newArtifactReference ); |
113 | |
|
114 | |
|
115 | 0 | if ( VersionUtil.isGenericSnapshot( newArtifactReference.getVersion() ) ) |
116 | |
{ |
117 | 0 | if ( newArtifactFile.lastModified() < olderThanThisDate.getTimeInMillis() ) |
118 | |
{ |
119 | 0 | doPurgeAllRelated( newArtifactReference ); |
120 | |
} |
121 | |
} |
122 | |
|
123 | 0 | else if ( VersionUtil.isUniqueSnapshot( newArtifactReference.getVersion() ) ) |
124 | |
{ |
125 | 0 | Calendar timestampCal = uniqueSnapshotToCalendar( newArtifactReference.getVersion() ); |
126 | |
|
127 | 0 | if ( timestampCal.getTimeInMillis() < olderThanThisDate.getTimeInMillis() ) |
128 | |
{ |
129 | 0 | doPurgeAllRelated( newArtifactReference ); |
130 | |
} |
131 | |
} |
132 | 0 | } |
133 | |
} |
134 | 0 | catch ( ContentNotFoundException e ) |
135 | |
{ |
136 | 0 | throw new RepositoryPurgeException( e.getMessage(), e ); |
137 | |
} |
138 | 0 | catch ( LayoutException e ) |
139 | |
{ |
140 | 0 | log.debug( "Not processing file that is not an artifact: " + e.getMessage() ); |
141 | 0 | } |
142 | 0 | } |
143 | |
|
144 | |
private Calendar uniqueSnapshotToCalendar( String version ) |
145 | |
{ |
146 | |
|
147 | |
|
148 | |
|
149 | 0 | Matcher m = VersionUtil.UNIQUE_SNAPSHOT_PATTERN.matcher( version ); |
150 | 0 | if ( m.matches() ) |
151 | |
{ |
152 | 0 | Matcher mtimestamp = VersionUtil.TIMESTAMP_PATTERN.matcher( m.group( 2 ) ); |
153 | 0 | if ( mtimestamp.matches() ) |
154 | |
{ |
155 | 0 | String tsDate = mtimestamp.group( 1 ); |
156 | 0 | String tsTime = mtimestamp.group( 2 ); |
157 | |
|
158 | |
Date versionDate; |
159 | |
try |
160 | |
{ |
161 | 0 | versionDate = timestampParser.parse( tsDate + "." + tsTime ); |
162 | 0 | Calendar cal = Calendar.getInstance( DateUtils.UTC_TIME_ZONE ); |
163 | 0 | cal.setTime( versionDate ); |
164 | |
|
165 | 0 | return cal; |
166 | |
} |
167 | 0 | catch ( ParseException e ) |
168 | |
{ |
169 | |
|
170 | 0 | return null; |
171 | |
} |
172 | |
} |
173 | |
} |
174 | 0 | return null; |
175 | |
} |
176 | |
|
177 | |
private void doPurgeAllRelated( ArtifactReference reference ) |
178 | |
{ |
179 | |
try |
180 | |
{ |
181 | 0 | Set<ArtifactReference> related = repository.getRelatedArtifacts( reference ); |
182 | 0 | purge( related ); |
183 | |
} |
184 | 0 | catch ( ContentNotFoundException e ) |
185 | |
{ |
186 | |
|
187 | 0 | log.debug( e.getMessage(), e ); |
188 | 0 | } |
189 | 0 | } |
190 | |
} |