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.ArtifactReference;
31 import org.apache.archiva.model.ProjectReference;
32 import org.apache.archiva.model.VersionedReference;
33 import org.apache.archiva.repository.ContentNotFoundException;
34 import org.apache.archiva.repository.ManagedRepositoryContent;
35 import org.apache.archiva.repository.RepositoryContentFactory;
36 import org.apache.archiva.repository.RepositoryException;
37 import org.apache.archiva.repository.RepositoryNotFoundException;
38 import org.apache.archiva.repository.layout.LayoutException;
39 import org.apache.archiva.repository.metadata.MetadataTools;
40 import org.apache.archiva.repository.metadata.RepositoryMetadataException;
43 import java.io.IOException;
44 import java.util.ArrayList;
45 import java.util.Collections;
46 import java.util.List;
50 * This will look in a single managed repository, and purge any snapshots that are present
51 * that have a corresponding released version on the same repository.
55 * So, if you have the following (presented in the m2/default layout form) ...
57 * /com/foo/foo-tool/1.0-SNAPSHOT/foo-tool-1.0-SNAPSHOT.jar
58 * /com/foo/foo-tool/1.1-SNAPSHOT/foo-tool-1.1-SNAPSHOT.jar
59 * /com/foo/foo-tool/1.2.1-SNAPSHOT/foo-tool-1.2.1-SNAPSHOT.jar
60 * /com/foo/foo-tool/1.2.1/foo-tool-1.2.1.jar
61 * /com/foo/foo-tool/2.0-SNAPSHOT/foo-tool-2.0-SNAPSHOT.jar
62 * /com/foo/foo-tool/2.0/foo-tool-2.0.jar
63 * /com/foo/foo-tool/2.1-SNAPSHOT/foo-tool-2.1-SNAPSHOT.jar
65 * then the current highest ranked released (non-snapshot) version is 2.0, which means
66 * the snapshots from 1.0-SNAPSHOT, 1.1-SNAPSHOT, 1.2.1-SNAPSHOT, and 2.0-SNAPSHOT can
67 * be purged. Leaving 2.1-SNAPSHOT in alone.
72 public class CleanupReleasedSnapshotsRepositoryPurge
73 extends AbstractRepositoryPurge
75 private MetadataTools metadataTools;
77 private ManagedRepositoryAdmin managedRepositoryAdmin;
79 private RepositoryContentFactory repoContentFactory;
81 public CleanupReleasedSnapshotsRepositoryPurge( ManagedRepositoryContent repository, MetadataTools metadataTools,
82 ManagedRepositoryAdmin managedRepositoryAdmin,
83 RepositoryContentFactory repoContentFactory,
84 RepositorySession repositorySession,
85 List<RepositoryListener> listeners )
87 super( repository, repositorySession, listeners );
88 this.metadataTools = metadataTools;
89 this.managedRepositoryAdmin = managedRepositoryAdmin;
90 this.repoContentFactory = repoContentFactory;
93 public void process( String path )
94 throws RepositoryPurgeException
98 File artifactFile = new File( repository.getRepoRoot(), path );
100 if ( !artifactFile.exists() )
102 // Nothing to do here, file doesn't exist, skip it.
106 ArtifactReference artifactRef = repository.toArtifactReference( path );
108 if ( !VersionUtil.isSnapshot( artifactRef.getVersion() ) )
110 // Nothing to do here, not a snapshot, skip it.
114 ProjectReference reference = new ProjectReference();
115 reference.setGroupId( artifactRef.getGroupId() );
116 reference.setArtifactId( artifactRef.getArtifactId() );
118 // Gether the released versions
119 List<String> releasedVersions = new ArrayList<String>();
121 List<ManagedRepository> repos = managedRepositoryAdmin.getManagedRepositories();
122 for ( ManagedRepository repo : repos )
124 if ( repo.isReleases() )
128 ManagedRepositoryContent repoContent =
129 repoContentFactory.getManagedRepositoryContent( repo.getId() );
130 for ( String version : repoContent.getVersions( reference ) )
132 if ( !VersionUtil.isSnapshot( version ) )
134 releasedVersions.add( version );
138 catch ( RepositoryNotFoundException e )
142 catch ( RepositoryException e )
149 Collections.sort( releasedVersions, VersionComparator.getInstance() );
151 // Now clean out any version that is earlier than the highest released version.
152 boolean needsMetadataUpdate = false;
154 VersionedReference versionRef = new VersionedReference();
155 versionRef.setGroupId( artifactRef.getGroupId() );
156 versionRef.setArtifactId( artifactRef.getArtifactId() );
158 MetadataRepository metadataRepository = repositorySession.getRepository();
160 if ( releasedVersions.contains( VersionUtil.getReleaseVersion( artifactRef.getVersion() ) ) )
162 versionRef.setVersion( artifactRef.getVersion() );
163 repository.deleteVersion( versionRef );
165 // FIXME: looks incomplete, might not delete related metadata?
166 for ( RepositoryListener listener : listeners )
168 listener.deleteArtifact( metadataRepository, repository.getId(), artifactRef.getGroupId(),
169 artifactRef.getArtifactId(), artifactRef.getVersion(),
170 artifactFile.getName() );
173 needsMetadataUpdate = true;
176 if ( needsMetadataUpdate )
178 updateMetadata( artifactRef );
180 } catch ( RepositoryAdminException e )
182 throw new RepositoryPurgeException( e.getMessage(), e );
184 catch ( LayoutException e )
186 log.debug( "Not processing file that is not an artifact: {}", e.getMessage() );
188 catch ( ContentNotFoundException e )
190 throw new RepositoryPurgeException( e.getMessage(), e );
194 private void updateMetadata( ArtifactReference artifact )
196 VersionedReference versionRef = new VersionedReference();
197 versionRef.setGroupId( artifact.getGroupId() );
198 versionRef.setArtifactId( artifact.getArtifactId() );
199 versionRef.setVersion( artifact.getVersion() );
201 ProjectReference projectRef = new ProjectReference();
202 projectRef.setGroupId( artifact.getGroupId() );
203 projectRef.setArtifactId( artifact.getArtifactId() );
207 metadataTools.updateMetadata( repository, versionRef );
209 catch ( ContentNotFoundException e )
211 // Ignore. (Just means we have no snapshot versions left to reference).
213 catch ( RepositoryMetadataException e )
217 catch ( IOException e )
221 catch ( LayoutException e )
228 metadataTools.updateMetadata( repository, projectRef );
230 catch ( ContentNotFoundException e )
232 // Ignore. (Just means we have no snapshot versions left to reference).
234 catch ( RepositoryMetadataException e )
238 catch ( IOException e )
242 catch ( LayoutException e )