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 java.io.File; |
23 | |
import java.io.IOException; |
24 | |
import java.util.ArrayList; |
25 | |
import java.util.Collections; |
26 | |
import java.util.List; |
27 | |
|
28 | |
import org.apache.maven.archiva.common.utils.VersionComparator; |
29 | |
import org.apache.maven.archiva.common.utils.VersionUtil; |
30 | |
import org.apache.maven.archiva.configuration.ArchivaConfiguration; |
31 | |
import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration; |
32 | |
import org.apache.maven.archiva.model.ArchivaArtifact; |
33 | |
import org.apache.maven.archiva.model.ArtifactReference; |
34 | |
import org.apache.maven.archiva.model.ProjectReference; |
35 | |
import org.apache.maven.archiva.model.VersionedReference; |
36 | |
import org.apache.maven.archiva.repository.ContentNotFoundException; |
37 | |
import org.apache.maven.archiva.repository.ManagedRepositoryContent; |
38 | |
import org.apache.maven.archiva.repository.RepositoryContentFactory; |
39 | |
import org.apache.maven.archiva.repository.RepositoryException; |
40 | |
import org.apache.maven.archiva.repository.RepositoryNotFoundException; |
41 | |
import org.apache.maven.archiva.repository.events.RepositoryListener; |
42 | |
import org.apache.maven.archiva.repository.layout.LayoutException; |
43 | |
import org.apache.maven.archiva.repository.metadata.MetadataTools; |
44 | |
import org.apache.maven.archiva.repository.metadata.RepositoryMetadataException; |
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
public class CleanupReleasedSnapshotsRepositoryPurge |
71 | |
extends AbstractRepositoryPurge |
72 | |
{ |
73 | |
private MetadataTools metadataTools; |
74 | |
|
75 | |
private ArchivaConfiguration archivaConfig; |
76 | |
|
77 | |
private RepositoryContentFactory repoContentFactory; |
78 | |
|
79 | |
public CleanupReleasedSnapshotsRepositoryPurge( ManagedRepositoryContent repository, MetadataTools metadataTools, |
80 | |
ArchivaConfiguration archivaConfig, |
81 | |
RepositoryContentFactory repoContentFactory, |
82 | |
List<RepositoryListener> listeners ) |
83 | |
{ |
84 | 0 | super( repository, listeners ); |
85 | 0 | this.metadataTools = metadataTools; |
86 | 0 | this.archivaConfig = archivaConfig; |
87 | 0 | this.repoContentFactory = repoContentFactory; |
88 | 0 | } |
89 | |
|
90 | |
public void process( String path ) |
91 | |
throws RepositoryPurgeException |
92 | |
{ |
93 | |
try |
94 | |
{ |
95 | 0 | File artifactFile = new File( repository.getRepoRoot(), path ); |
96 | |
|
97 | 0 | if ( !artifactFile.exists() ) |
98 | |
{ |
99 | |
|
100 | 0 | return; |
101 | |
} |
102 | |
|
103 | 0 | ArtifactReference artifactRef = repository.toArtifactReference( path ); |
104 | |
|
105 | 0 | if ( !VersionUtil.isSnapshot( artifactRef.getVersion() ) ) |
106 | |
{ |
107 | |
|
108 | 0 | return; |
109 | |
} |
110 | |
|
111 | 0 | ProjectReference reference = new ProjectReference(); |
112 | 0 | reference.setGroupId( artifactRef.getGroupId() ); |
113 | 0 | reference.setArtifactId( artifactRef.getArtifactId() ); |
114 | |
|
115 | |
|
116 | 0 | List<String> allVersions = new ArrayList<String>( repository.getVersions( reference ) ); |
117 | |
|
118 | 0 | List<ManagedRepositoryConfiguration> repos = archivaConfig.getConfiguration().getManagedRepositories(); |
119 | 0 | for( ManagedRepositoryConfiguration repo : repos ) |
120 | |
{ |
121 | 0 | if( repo.isReleases() && !repo.getId().equals( repository.getId() ) ) |
122 | |
{ |
123 | |
try |
124 | |
{ |
125 | 0 | ManagedRepositoryContent repoContent = repoContentFactory.getManagedRepositoryContent( repo.getId() ); |
126 | 0 | allVersions.addAll( repoContent.getVersions( reference ) ); |
127 | |
} |
128 | 0 | catch( RepositoryNotFoundException e ) |
129 | |
{ |
130 | |
|
131 | |
} |
132 | 0 | catch( RepositoryException e ) |
133 | |
{ |
134 | |
|
135 | 0 | } |
136 | |
} |
137 | |
} |
138 | |
|
139 | |
|
140 | 0 | List<String> releasedVersions = new ArrayList<String>(); |
141 | 0 | List<String> snapshotVersions = new ArrayList<String>(); |
142 | |
|
143 | 0 | for ( String version : allVersions ) |
144 | |
{ |
145 | 0 | if ( VersionUtil.isSnapshot( version ) ) |
146 | |
{ |
147 | 0 | snapshotVersions.add( version ); |
148 | |
} |
149 | |
else |
150 | |
{ |
151 | 0 | releasedVersions.add( version ); |
152 | |
} |
153 | |
} |
154 | |
|
155 | 0 | Collections.sort( allVersions, VersionComparator.getInstance() ); |
156 | 0 | Collections.sort( releasedVersions, VersionComparator.getInstance() ); |
157 | 0 | Collections.sort( snapshotVersions, VersionComparator.getInstance() ); |
158 | |
|
159 | |
|
160 | 0 | boolean needsMetadataUpdate = false; |
161 | |
|
162 | 0 | VersionedReference versionRef = new VersionedReference(); |
163 | 0 | versionRef.setGroupId( artifactRef.getGroupId() ); |
164 | 0 | versionRef.setArtifactId( artifactRef.getArtifactId() ); |
165 | |
|
166 | 0 | ArchivaArtifact artifact = |
167 | |
new ArchivaArtifact( artifactRef.getGroupId(), artifactRef.getArtifactId(), artifactRef.getVersion(), |
168 | |
artifactRef.getClassifier(), artifactRef.getType(), repository.getId() ); |
169 | |
|
170 | 0 | for ( String version : snapshotVersions ) |
171 | |
{ |
172 | 0 | if( releasedVersions.contains( VersionUtil.getReleaseVersion( version ) ) ) |
173 | |
{ |
174 | 0 | versionRef.setVersion( version ); |
175 | 0 | repository.deleteVersion( versionRef ); |
176 | |
|
177 | |
|
178 | 0 | for ( RepositoryListener listener : listeners ) |
179 | |
{ |
180 | 0 | listener.deleteArtifact( repository, artifact ); |
181 | |
} |
182 | |
|
183 | 0 | needsMetadataUpdate = true; |
184 | |
} |
185 | |
} |
186 | |
|
187 | 0 | if ( needsMetadataUpdate ) |
188 | |
{ |
189 | 0 | updateMetadata( artifactRef ); |
190 | |
} |
191 | |
} |
192 | 0 | catch ( LayoutException e ) |
193 | |
{ |
194 | 0 | log.debug( "Not processing file that is not an artifact: " + e.getMessage() ); |
195 | |
} |
196 | 0 | catch ( ContentNotFoundException e ) |
197 | |
{ |
198 | 0 | throw new RepositoryPurgeException( e.getMessage(), e ); |
199 | 0 | } |
200 | 0 | } |
201 | |
|
202 | |
private void updateMetadata( ArtifactReference artifact ) |
203 | |
{ |
204 | 0 | VersionedReference versionRef = new VersionedReference(); |
205 | 0 | versionRef.setGroupId( artifact.getGroupId() ); |
206 | 0 | versionRef.setArtifactId( artifact.getArtifactId() ); |
207 | 0 | versionRef.setVersion( artifact.getVersion() ); |
208 | |
|
209 | 0 | ProjectReference projectRef = new ProjectReference(); |
210 | 0 | projectRef.setGroupId( artifact.getGroupId() ); |
211 | 0 | projectRef.setArtifactId( artifact.getArtifactId() ); |
212 | |
|
213 | |
try |
214 | |
{ |
215 | 0 | metadataTools.updateMetadata( repository, versionRef ); |
216 | |
} |
217 | 0 | catch ( ContentNotFoundException e ) |
218 | |
{ |
219 | |
|
220 | |
} |
221 | 0 | catch ( RepositoryMetadataException e ) |
222 | |
{ |
223 | |
|
224 | |
} |
225 | 0 | catch ( IOException e ) |
226 | |
{ |
227 | |
|
228 | |
} |
229 | 0 | catch ( LayoutException e ) |
230 | |
{ |
231 | |
|
232 | 0 | } |
233 | |
|
234 | |
try |
235 | |
{ |
236 | 0 | metadataTools.updateMetadata( repository, projectRef ); |
237 | |
} |
238 | 0 | catch ( ContentNotFoundException e ) |
239 | |
{ |
240 | |
|
241 | |
} |
242 | 0 | catch ( RepositoryMetadataException e ) |
243 | |
{ |
244 | |
|
245 | |
} |
246 | 0 | catch ( IOException e ) |
247 | |
{ |
248 | |
|
249 | |
} |
250 | 0 | catch ( LayoutException e ) |
251 | |
{ |
252 | |
|
253 | 0 | } |
254 | 0 | } |
255 | |
} |