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.util.ArrayList; |
24 | |
import java.util.Collections; |
25 | |
import java.util.List; |
26 | |
import java.util.Set; |
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.model.ArtifactReference; |
31 | |
import org.apache.maven.archiva.model.VersionedReference; |
32 | |
import org.apache.maven.archiva.repository.ContentNotFoundException; |
33 | |
import org.apache.maven.archiva.repository.ManagedRepositoryContent; |
34 | |
import org.apache.maven.archiva.repository.events.RepositoryListener; |
35 | |
import org.apache.maven.archiva.repository.layout.LayoutException; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
public class RetentionCountRepositoryPurge |
42 | |
extends AbstractRepositoryPurge |
43 | |
{ |
44 | |
private int retentionCount; |
45 | |
|
46 | |
public RetentionCountRepositoryPurge( ManagedRepositoryContent repository, |
47 | |
int retentionCount, List<RepositoryListener> listeners ) |
48 | |
{ |
49 | 0 | super( repository, listeners ); |
50 | 0 | this.retentionCount = retentionCount; |
51 | 0 | } |
52 | |
|
53 | |
public void process( String path ) |
54 | |
throws RepositoryPurgeException |
55 | |
{ |
56 | |
try |
57 | |
{ |
58 | 0 | File artifactFile = new File( repository.getRepoRoot(), path ); |
59 | |
|
60 | 0 | if ( !artifactFile.exists() ) |
61 | |
{ |
62 | 0 | return; |
63 | |
} |
64 | |
|
65 | 0 | ArtifactReference artifact = repository.toArtifactReference( path ); |
66 | |
|
67 | 0 | if ( VersionUtil.isSnapshot( artifact.getVersion() ) ) |
68 | |
{ |
69 | 0 | VersionedReference reference = new VersionedReference(); |
70 | 0 | reference.setGroupId( artifact.getGroupId() ); |
71 | 0 | reference.setArtifactId( artifact.getArtifactId() ); |
72 | 0 | reference.setVersion( artifact.getVersion() ); |
73 | |
|
74 | 0 | List<String> versions = new ArrayList<String>( repository.getVersions( reference ) ); |
75 | |
|
76 | 0 | Collections.sort( versions, VersionComparator.getInstance() ); |
77 | |
|
78 | 0 | if ( retentionCount > versions.size() ) |
79 | |
{ |
80 | |
|
81 | 0 | return; |
82 | |
} |
83 | |
|
84 | 0 | int countToPurge = versions.size() - retentionCount; |
85 | |
|
86 | 0 | for ( String version : versions ) |
87 | |
{ |
88 | 0 | if ( countToPurge-- <= 0 ) |
89 | |
{ |
90 | 0 | break; |
91 | |
} |
92 | |
|
93 | 0 | doPurgeAllRelated( artifact, version ); |
94 | |
} |
95 | |
} |
96 | |
} |
97 | 0 | catch ( LayoutException le ) |
98 | |
{ |
99 | 0 | throw new RepositoryPurgeException( le.getMessage(), le ); |
100 | |
} |
101 | 0 | catch ( ContentNotFoundException e ) |
102 | |
{ |
103 | |
|
104 | |
|
105 | 0 | } |
106 | 0 | } |
107 | |
|
108 | |
private void doPurgeAllRelated( ArtifactReference reference, String version ) |
109 | |
throws LayoutException |
110 | |
{ |
111 | 0 | ArtifactReference artifact = new ArtifactReference(); |
112 | 0 | artifact.setGroupId( reference.getGroupId() ); |
113 | 0 | artifact.setArtifactId( reference.getArtifactId() ); |
114 | 0 | artifact.setVersion( version ); |
115 | 0 | artifact.setClassifier( reference.getClassifier() ); |
116 | 0 | artifact.setType( reference.getType() ); |
117 | |
|
118 | |
try |
119 | |
{ |
120 | 0 | Set<ArtifactReference> related = repository.getRelatedArtifacts( artifact ); |
121 | 0 | purge( related ); |
122 | |
} |
123 | 0 | catch ( ContentNotFoundException e ) |
124 | |
{ |
125 | |
|
126 | |
|
127 | 0 | } |
128 | 0 | } |
129 | |
} |