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