1 package org.apache.archiva.consumers.core.repository;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.archiva.admin.model.RepositoryAdminException;
23 import org.apache.archiva.admin.model.beans.ManagedRepository;
24 import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
25 import org.apache.archiva.metadata.repository.MetadataRepository;
26 import org.apache.archiva.metadata.repository.RepositorySession;
27 import org.apache.archiva.repository.events.RepositoryListener;
28 import org.apache.archiva.common.utils.VersionComparator;
29 import org.apache.archiva.common.utils.VersionUtil;
30 import org.apache.archiva.model.ArchivaArtifact;
31 import org.apache.archiva.model.ArtifactReference;
32 import org.apache.archiva.model.ProjectReference;
33 import org.apache.archiva.model.VersionedReference;
34 import org.apache.archiva.repository.ContentNotFoundException;
35 import org.apache.archiva.repository.ManagedRepositoryContent;
36 import org.apache.archiva.repository.RepositoryContentFactory;
37 import org.apache.archiva.repository.RepositoryException;
38 import org.apache.archiva.repository.RepositoryNotFoundException;
39 import org.apache.archiva.repository.layout.LayoutException;
40 import org.apache.archiva.repository.metadata.MetadataTools;
41 import org.apache.archiva.repository.metadata.RepositoryMetadataException;
44 import java.io.IOException;
45 import java.util.ArrayList;
46 import java.util.Collections;
47 import java.util.List;
51 * This will look in a single managed repository, and purge any snapshots that are present
52 * that have a corresponding released version on the same repository.
56 * So, if you have the following (presented in the m2/default layout form) ...
58 * /com/foo/foo-tool/1.0-SNAPSHOT/foo-tool-1.0-SNAPSHOT.jar
59 * /com/foo/foo-tool/1.1-SNAPSHOT/foo-tool-1.1-SNAPSHOT.jar
60 * /com/foo/foo-tool/1.2.1-SNAPSHOT/foo-tool-1.2.1-SNAPSHOT.jar
61 * /com/foo/foo-tool/1.2.1/foo-tool-1.2.1.jar
62 * /com/foo/foo-tool/2.0-SNAPSHOT/foo-tool-2.0-SNAPSHOT.jar
63 * /com/foo/foo-tool/2.0/foo-tool-2.0.jar
64 * /com/foo/foo-tool/2.1-SNAPSHOT/foo-tool-2.1-SNAPSHOT.jar
66 * then the current highest ranked released (non-snapshot) version is 2.0, which means
67 * the snapshots from 1.0-SNAPSHOT, 1.1-SNAPSHOT, 1.2.1-SNAPSHOT, and 2.0-SNAPSHOT can
68 * be purged. Leaving 2.1-SNAPSHOT in alone.
73 public class CleanupReleasedSnapshotsRepositoryPurge
74 extends AbstractRepositoryPurge
76 private MetadataTools metadataTools;
78 private ManagedRepositoryAdmin managedRepositoryAdmin;
80 private RepositoryContentFactory repoContentFactory;
82 public CleanupReleasedSnapshotsRepositoryPurge( ManagedRepositoryContent repository, MetadataTools metadataTools,
83 ManagedRepositoryAdmin managedRepositoryAdmin,
84 RepositoryContentFactory repoContentFactory,
85 RepositorySession repositorySession,
86 List<RepositoryListener> listeners )
88 super( repository, repositorySession, listeners );
89 this.metadataTools = metadataTools;
90 this.managedRepositoryAdmin = managedRepositoryAdmin;
91 this.repoContentFactory = repoContentFactory;
94 public void process( String path )
95 throws RepositoryPurgeException
99 File artifactFile = new File( repository.getRepoRoot(), path );
101 if ( !artifactFile.exists() )
103 // Nothing to do here, file doesn't exist, skip it.
107 ArtifactReference artifactRef = repository.toArtifactReference( path );
109 if ( !VersionUtil.isSnapshot( artifactRef.getVersion() ) )
111 // Nothing to do here, not a snapshot, skip it.
115 ProjectReference reference = new ProjectReference();
116 reference.setGroupId( artifactRef.getGroupId() );
117 reference.setArtifactId( artifactRef.getArtifactId() );
119 // Gather up all of the versions.
120 List<String> allVersions = new ArrayList<String>( repository.getVersions( reference ) );
122 List<ManagedRepository> repos = managedRepositoryAdmin.getManagedRepositories();
123 for ( ManagedRepository repo : repos )
125 if ( repo.isReleases() && !repo.getId().equals( repository.getId() ) )
129 ManagedRepositoryContent repoContent =
130 repoContentFactory.getManagedRepositoryContent( repo.getId() );
131 allVersions.addAll( repoContent.getVersions( reference ) );
133 catch ( RepositoryNotFoundException e )
137 catch ( RepositoryException e )
144 // Split the versions into released and snapshots.
145 List<String> releasedVersions = new ArrayList<String>();
146 List<String> snapshotVersions = new ArrayList<String>();
148 for ( String version : allVersions )
150 if ( VersionUtil.isSnapshot( version ) )
152 snapshotVersions.add( version );
156 releasedVersions.add( version );
160 Collections.sort( allVersions, VersionComparator.getInstance() );
161 Collections.sort( releasedVersions, VersionComparator.getInstance() );
162 Collections.sort( snapshotVersions, VersionComparator.getInstance() );
164 // Now clean out any version that is earlier than the highest released version.
165 boolean needsMetadataUpdate = false;
167 VersionedReference versionRef = new VersionedReference();
168 versionRef.setGroupId( artifactRef.getGroupId() );
169 versionRef.setArtifactId( artifactRef.getArtifactId() );
171 ArchivaArtifact artifact =
172 new ArchivaArtifact( artifactRef.getGroupId(), artifactRef.getArtifactId(), artifactRef.getVersion(),
173 artifactRef.getClassifier(), artifactRef.getType(), repository.getId() );
175 MetadataRepository metadataRepository = repositorySession.getRepository();
176 for ( String version : snapshotVersions )
178 if ( releasedVersions.contains( VersionUtil.getReleaseVersion( version ) ) )
180 versionRef.setVersion( version );
181 repository.deleteVersion( versionRef );
183 // FIXME: looks incomplete, might not delete related metadata?
184 for ( RepositoryListener listener : listeners )
186 listener.deleteArtifact( metadataRepository, repository.getId(), artifact.getGroupId(),
187 artifact.getArtifactId(), artifact.getVersion(),
188 artifactFile.getName() );
191 needsMetadataUpdate = true;
195 if ( needsMetadataUpdate )
197 updateMetadata( artifactRef );
199 } catch ( RepositoryAdminException e )
201 throw new RepositoryPurgeException( e.getMessage(), e );
203 catch ( LayoutException e )
205 log.debug( "Not processing file that is not an artifact: {}", e.getMessage() );
207 catch ( ContentNotFoundException e )
209 throw new RepositoryPurgeException( e.getMessage(), e );
213 private void updateMetadata( ArtifactReference artifact )
215 VersionedReference versionRef = new VersionedReference();
216 versionRef.setGroupId( artifact.getGroupId() );
217 versionRef.setArtifactId( artifact.getArtifactId() );
218 versionRef.setVersion( artifact.getVersion() );
220 ProjectReference projectRef = new ProjectReference();
221 projectRef.setGroupId( artifact.getGroupId() );
222 projectRef.setArtifactId( artifact.getArtifactId() );
226 metadataTools.updateMetadata( repository, versionRef );
228 catch ( ContentNotFoundException e )
230 // Ignore. (Just means we have no snapshot versions left to reference).
232 catch ( RepositoryMetadataException e )
236 catch ( IOException e )
240 catch ( LayoutException e )
247 metadataTools.updateMetadata( repository, projectRef );
249 catch ( ContentNotFoundException e )
251 // Ignore. (Just means we have no snapshot versions left to reference).
253 catch ( RepositoryMetadataException e )
257 catch ( IOException e )
261 catch ( LayoutException e )