1 package org.apache.continuum.purge.repository.content;
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.io.FileUtils;
23 import org.apache.commons.lang.StringUtils;
24 import org.apache.continuum.model.repository.LocalRepository;
25 import org.apache.continuum.purge.repository.utils.FileTypes;
26 import org.apache.maven.archiva.common.utils.PathUtil;
27 import org.apache.maven.archiva.common.utils.VersionUtil;
28 import org.apache.maven.archiva.model.ArtifactReference;
29 import org.apache.maven.archiva.model.ProjectReference;
30 import org.apache.maven.archiva.model.VersionedReference;
31 import org.apache.maven.archiva.repository.ContentNotFoundException;
32 import org.apache.maven.archiva.repository.content.ArtifactExtensionMapping;
33 import org.apache.maven.archiva.repository.content.DefaultPathParser;
34 import org.apache.maven.archiva.repository.content.PathParser;
35 import org.apache.maven.archiva.repository.layout.LayoutException;
36
37 import java.io.File;
38 import java.io.IOException;
39 import java.util.HashSet;
40 import java.util.Set;
41
42
43
44
45
46
47
48
49 public class ManagedDefaultRepositoryContent
50 implements RepositoryManagedContent
51 {
52 private static final String MAVEN_METADATA = "maven-metadata.xml";
53
54 private static final char PATH_SEPARATOR = '/';
55
56 private static final char GROUP_SEPARATOR = '.';
57
58 private static final char ARTIFACT_SEPARATOR = '-';
59
60 private final PathParser defaultPathParser = new DefaultPathParser();
61
62
63
64
65 private FileTypes filetypes;
66
67 private LocalRepository repository;
68
69 public void deleteVersion( VersionedReference reference )
70 throws ContentNotFoundException
71 {
72 String path = toMetadataPath( reference );
73 File projectPath = new File( getRepoRoot(), path );
74
75 File projectDir = projectPath.getParentFile();
76 if ( projectDir.exists() && projectDir.isDirectory() )
77 {
78 try
79 {
80 FileUtils.deleteDirectory( projectDir );
81 }
82 catch ( IOException e )
83 {
84
85 }
86 }
87 }
88
89 public int getId()
90 {
91 return repository.getId();
92 }
93
94 public Set<ArtifactReference> getRelatedArtifacts( ArtifactReference reference )
95 throws ContentNotFoundException, LayoutException
96 {
97 File artifactFile = toFile( reference );
98 File repoDir = artifactFile.getParentFile();
99
100 if ( !repoDir.exists() )
101 {
102 throw new ContentNotFoundException(
103 "Unable to get related artifacts using a non-existant directory: " + repoDir.getAbsolutePath() );
104 }
105
106 if ( !repoDir.isDirectory() )
107 {
108 throw new ContentNotFoundException(
109 "Unable to get related artifacts using a non-directory: " + repoDir.getAbsolutePath() );
110 }
111
112 Set<ArtifactReference> foundArtifacts = new HashSet<ArtifactReference>();
113
114
115 for ( File repoFile : repoDir.listFiles() )
116 {
117 if ( repoFile.isDirectory() )
118 {
119
120 continue;
121 }
122
123 String relativePath = PathUtil.getRelative( repository.getLocation(), repoFile );
124
125 if ( filetypes.matchesArtifactPattern( relativePath ) )
126 {
127 ArtifactReference artifact = toArtifactReference( relativePath );
128
129
130 if ( artifact.getGroupId().equals( reference.getGroupId() ) &&
131 artifact.getArtifactId().equals( reference.getArtifactId() ) &&
132 artifact.getVersion().equals( reference.getVersion() ) )
133 {
134 foundArtifacts.add( artifact );
135 }
136 }
137 }
138
139 return foundArtifacts;
140 }
141
142 public String getRepoRoot()
143 {
144 return repository.getLocation();
145 }
146
147 public LocalRepository getRepository()
148 {
149 return repository;
150 }
151
152
153
154
155
156
157
158
159
160 public Set<String> getVersions( ProjectReference reference )
161 throws ContentNotFoundException, LayoutException
162 {
163 String path = toMetadataPath( reference );
164
165 int idx = path.lastIndexOf( '/' );
166 if ( idx > 0 )
167 {
168 path = path.substring( 0, idx );
169 }
170
171 File repoDir = new File( repository.getLocation(), path );
172
173 if ( !repoDir.exists() )
174 {
175 throw new ContentNotFoundException(
176 "Unable to get Versions on a non-existant directory: " + repoDir.getAbsolutePath() );
177 }
178
179 if ( !repoDir.isDirectory() )
180 {
181 throw new ContentNotFoundException(
182 "Unable to get Versions on a non-directory: " + repoDir.getAbsolutePath() );
183 }
184
185 Set<String> foundVersions = new HashSet<String>();
186 VersionedReference versionRef = new VersionedReference();
187 versionRef.setGroupId( reference.getGroupId() );
188 versionRef.setArtifactId( reference.getArtifactId() );
189
190 for ( File repoFile : repoDir.listFiles() )
191 {
192 if ( !repoFile.isDirectory() )
193 {
194
195 continue;
196 }
197
198
199 String version = repoFile.getName();
200 versionRef.setVersion( version );
201
202 if ( hasArtifact( versionRef ) )
203 {
204
205 foundVersions.add( version );
206 }
207 }
208
209 return foundVersions;
210 }
211
212 public Set<String> getVersions( VersionedReference reference )
213 throws ContentNotFoundException, LayoutException
214 {
215 String path = toMetadataPath( reference );
216
217 int idx = path.lastIndexOf( '/' );
218 if ( idx > 0 )
219 {
220 path = path.substring( 0, idx );
221 }
222
223 File repoDir = new File( repository.getLocation(), path );
224
225 if ( !repoDir.exists() )
226 {
227 throw new ContentNotFoundException(
228 "Unable to get versions on a non-existant directory: " + repoDir.getAbsolutePath() );
229 }
230
231 if ( !repoDir.isDirectory() )
232 {
233 throw new ContentNotFoundException(
234 "Unable to get versions on a non-directory: " + repoDir.getAbsolutePath() );
235 }
236
237 Set<String> foundVersions = new HashSet<String>();
238
239
240 for ( File repoFile : repoDir.listFiles() )
241 {
242 if ( repoFile.isDirectory() )
243 {
244
245 continue;
246 }
247
248 String relativePath = PathUtil.getRelative( repository.getLocation(), repoFile );
249
250 if ( filetypes.matchesDefaultExclusions( relativePath ) )
251 {
252
253 continue;
254 }
255
256 if ( filetypes.matchesArtifactPattern( relativePath ) )
257 {
258 ArtifactReference artifact = toArtifactReference( relativePath );
259
260 foundVersions.add( artifact.getVersion() );
261 }
262 }
263
264 return foundVersions;
265 }
266
267
268 public String toMetadataPath( ProjectReference reference )
269 {
270 StringBuffer path = new StringBuffer();
271
272 path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
273 path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
274 path.append( MAVEN_METADATA );
275
276 return path.toString();
277 }
278
279 public String toMetadataPath( VersionedReference reference )
280 {
281 StringBuffer path = new StringBuffer();
282
283 path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
284 path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
285 if ( reference.getVersion() != null )
286 {
287
288 path.append( VersionUtil.getBaseVersion( reference.getVersion() ) ).append( PATH_SEPARATOR );
289 }
290 path.append( MAVEN_METADATA );
291
292 return path.toString();
293 }
294
295 public String toPath( ArtifactReference reference )
296 {
297 if ( reference == null )
298 {
299 throw new IllegalArgumentException( "Artifact reference cannot be null" );
300 }
301
302 String baseVersion = VersionUtil.getBaseVersion( reference.getVersion() );
303 return toPath( reference.getGroupId(), reference.getArtifactId(), baseVersion, reference.getVersion(),
304 reference.getClassifier(), reference.getType() );
305 }
306
307 public void setRepository( LocalRepository repository )
308 {
309 this.repository = repository;
310 }
311
312
313
314
315
316
317
318 public ArtifactReference toArtifactReference( String path )
319 throws LayoutException
320 {
321 if ( ( path != null ) && path.startsWith( repository.getLocation() ) )
322 {
323 return defaultPathParser.toArtifactReference( path.substring( repository.getLocation().length() ) );
324 }
325
326 return defaultPathParser.toArtifactReference( path );
327 }
328
329 public File toFile( ArtifactReference reference )
330 {
331 return new File( repository.getLocation(), toPath( reference ) );
332 }
333
334
335
336
337
338
339
340
341
342
343 private ArtifactReference getFirstArtifact( VersionedReference reference )
344 throws LayoutException, IOException
345 {
346 String path = toMetadataPath( reference );
347
348 int idx = path.lastIndexOf( '/' );
349 if ( idx > 0 )
350 {
351 path = path.substring( 0, idx );
352 }
353
354 File repoDir = new File( repository.getLocation(), path );
355
356 if ( !repoDir.exists() )
357 {
358 throw new IOException( "Unable to gather the list of snapshot versions on a non-existant directory: " +
359 repoDir.getAbsolutePath() );
360 }
361
362 if ( !repoDir.isDirectory() )
363 {
364 throw new IOException(
365 "Unable to gather the list of snapshot versions on a non-directory: " + repoDir.getAbsolutePath() );
366 }
367
368 for ( File repoFile : repoDir.listFiles() )
369 {
370 if ( repoFile.isDirectory() )
371 {
372
373 continue;
374 }
375
376 String relativePath = PathUtil.getRelative( repository.getLocation(), repoFile );
377
378 if ( filetypes.matchesArtifactPattern( relativePath ) )
379 {
380 return toArtifactReference( relativePath );
381 }
382 }
383
384
385 return null;
386 }
387
388 private boolean hasArtifact( VersionedReference reference )
389 throws LayoutException
390 {
391 try
392 {
393 return ( getFirstArtifact( reference ) != null );
394 }
395 catch ( IOException e )
396 {
397 return false;
398 }
399 }
400
401 private String formatAsDirectory( String directory )
402 {
403 return directory.replace( GROUP_SEPARATOR, PATH_SEPARATOR );
404 }
405
406 private String toPath( String groupId, String artifactId, String baseVersion, String version, String classifier,
407 String type )
408 {
409 StringBuffer path = new StringBuffer();
410
411 path.append( formatAsDirectory( groupId ) ).append( PATH_SEPARATOR );
412 path.append( artifactId ).append( PATH_SEPARATOR );
413
414 if ( baseVersion != null )
415 {
416 path.append( baseVersion ).append( PATH_SEPARATOR );
417 if ( ( version != null ) && ( type != null ) )
418 {
419 path.append( artifactId ).append( ARTIFACT_SEPARATOR ).append( version );
420
421 if ( StringUtils.isNotBlank( classifier ) )
422 {
423 path.append( ARTIFACT_SEPARATOR ).append( classifier );
424 }
425
426 path.append( GROUP_SEPARATOR ).append( ArtifactExtensionMapping.getExtension( type ) );
427 }
428 }
429
430 return path.toString();
431 }
432 }