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 org.apache.continuum.purge.ContinuumPurgeConstants;
23 import org.apache.continuum.purge.repository.content.RepositoryManagedContent;
24 import org.apache.maven.archiva.common.utils.VersionComparator;
25 import org.apache.maven.archiva.common.utils.VersionUtil;
26 import org.apache.maven.archiva.model.ArtifactReference;
27 import org.apache.maven.archiva.model.ProjectReference;
28 import org.apache.maven.archiva.model.VersionedReference;
29 import org.apache.maven.archiva.repository.ContentNotFoundException;
30 import org.apache.maven.archiva.repository.layout.LayoutException;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 import java.io.File;
35 import java.util.ArrayList;
36 import java.util.Collections;
37 import java.util.List;
38
39
40
41
42
43
44 public class ReleasedSnapshotsRepositoryPurgeExecutor
45 extends AbstractContinuumPurgeExecutor
46 {
47 private Logger log = LoggerFactory.getLogger( ReleasedSnapshotsRepositoryPurgeExecutor.class );
48
49 private final RepositoryManagedContent repository;
50
51 public ReleasedSnapshotsRepositoryPurgeExecutor( RepositoryManagedContent repository )
52 {
53 this.repository = repository;
54 }
55
56 public void purge( String path )
57 throws ContinuumPurgeExecutorException
58 {
59 try
60 {
61 File artifactFile = new File( repository.getRepoRoot(), path );
62
63 if ( !artifactFile.exists() )
64 {
65
66 return;
67 }
68
69 ArtifactReference artifact = repository.toArtifactReference( path );
70
71 if ( !VersionUtil.isSnapshot( artifact.getVersion() ) )
72 {
73
74 return;
75 }
76
77 ProjectReference reference = new ProjectReference();
78 reference.setGroupId( artifact.getGroupId() );
79 reference.setArtifactId( artifact.getArtifactId() );
80
81
82 List<String> allVersions = new ArrayList<String>( repository.getVersions( reference ) );
83
84
85 List<String> releasedVersions = new ArrayList<String>();
86 List<String> snapshotVersions = new ArrayList<String>();
87
88 for ( String version : allVersions )
89 {
90 if ( VersionUtil.isSnapshot( version ) )
91 {
92 snapshotVersions.add( version );
93 }
94 else
95 {
96 releasedVersions.add( version );
97 }
98 }
99
100 Collections.sort( allVersions, VersionComparator.getInstance() );
101 Collections.sort( releasedVersions, VersionComparator.getInstance() );
102 Collections.sort( snapshotVersions, VersionComparator.getInstance() );
103
104 VersionedReference versionRef = new VersionedReference();
105 versionRef.setGroupId( artifact.getGroupId() );
106 versionRef.setArtifactId( artifact.getArtifactId() );
107
108 for ( String version : snapshotVersions )
109 {
110 if ( releasedVersions.contains( VersionUtil.getReleaseVersion( version ) ) )
111 {
112 versionRef.setVersion( version );
113 repository.deleteVersion( versionRef );
114
115 log.info( ContinuumPurgeConstants.PURGE_PROJECT + " - " + VersionedReference.toKey( versionRef ) );
116
117 removeMetadata( versionRef );
118 }
119 }
120 }
121 catch ( LayoutException e )
122 {
123 throw new ContinuumPurgeExecutorException( e.getMessage(), e );
124 }
125 catch ( ContentNotFoundException e )
126 {
127 throw new ContinuumPurgeExecutorException( e.getMessage(), e );
128 }
129 }
130
131 private void removeMetadata( VersionedReference versionRef )
132 throws ContinuumPurgeExecutorException
133 {
134 String path = repository.toMetadataPath( versionRef );
135 File projectPath = new File( repository.getRepoRoot(), path );
136
137 File projectDir = projectPath.getParentFile();
138
139 purgeSupportFiles( projectDir, "maven-metadata" );
140 }
141 }