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