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.common.utils.VersionComparator;
23 import org.apache.archiva.common.utils.VersionUtil;
24 import org.apache.archiva.metadata.repository.RepositorySession;
25 import org.apache.archiva.model.ArtifactReference;
26 import org.apache.archiva.model.VersionedReference;
27 import org.apache.archiva.repository.ContentNotFoundException;
28 import org.apache.archiva.repository.ManagedRepositoryContent;
29 import org.apache.archiva.repository.events.RepositoryListener;
30 import org.apache.archiva.repository.layout.LayoutException;
33 import java.util.ArrayList;
34 import java.util.Collections;
35 import java.util.HashSet;
36 import java.util.List;
40 * Purge the repository by retention count. Retain only the specified number of snapshots.
42 public class RetentionCountRepositoryPurge
43 extends AbstractRepositoryPurge
45 private int retentionCount;
47 public RetentionCountRepositoryPurge( ManagedRepositoryContent repository, int retentionCount,
48 RepositorySession repositorySession, List<RepositoryListener> listeners )
50 super( repository, repositorySession, listeners );
51 this.retentionCount = retentionCount;
55 public void process( String path )
56 throws RepositoryPurgeException
60 File artifactFile = new File( repository.getRepoRoot(), path );
62 if ( !artifactFile.exists() )
67 ArtifactReference artifact = repository.toArtifactReference( path );
69 if ( VersionUtil.isSnapshot( artifact.getVersion() ) )
71 VersionedReference reference = new VersionedReference();
72 reference.setGroupId( artifact.getGroupId() );
73 reference.setArtifactId( artifact.getArtifactId() );
74 reference.setVersion( artifact.getVersion() );
76 List<String> versions = new ArrayList<>( repository.getVersions( reference ) );
78 Collections.sort( versions, VersionComparator.getInstance() );
80 if ( retentionCount > versions.size() )
82 log.trace("No deletion, because retention count is higher than actual number of artifacts.");
83 // Done. nothing to do here. skip it.
87 int countToPurge = versions.size() - retentionCount;
88 Set<ArtifactReference> artifactsToDelete = new HashSet<>();
89 for ( String version : versions )
91 if ( countToPurge-- <= 0 )
95 artifactsToDelete.addAll(repository.getRelatedArtifacts( getNewArtifactReference( artifact, version) ));
97 purge(artifactsToDelete);
100 catch ( LayoutException le )
102 throw new RepositoryPurgeException( le.getMessage(), le );
104 catch ( ContentNotFoundException e )
106 log.error("Repostory artifact not found {}", path);
111 * Returns a new artifact reference with different version
113 private ArtifactReference getNewArtifactReference( ArtifactReference reference, String version )
114 throws LayoutException
116 ArtifactReference artifact = new ArtifactReference();
117 artifact.setGroupId( reference.getGroupId() );
118 artifact.setArtifactId( reference.getArtifactId() );
119 artifact.setVersion( version );
120 artifact.setClassifier( reference.getClassifier() );
121 artifact.setType( reference.getType() );