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.LayoutException;
29 import org.apache.archiva.repository.ManagedRepositoryContent;
30 import org.apache.archiva.repository.events.RepositoryListener;
32 import java.nio.file.Files;
33 import java.nio.file.Path;
34 import java.nio.file.Paths;
35 import java.util.ArrayList;
36 import java.util.Collections;
37 import java.util.HashSet;
38 import java.util.List;
42 * Purge the repository by retention count. Retain only the specified number of snapshots.
44 public class RetentionCountRepositoryPurge
45 extends AbstractRepositoryPurge
47 private int retentionCount;
49 public RetentionCountRepositoryPurge( ManagedRepositoryContent repository, int retentionCount,
50 RepositorySession repositorySession, List<RepositoryListener> listeners )
52 super( repository, repositorySession, listeners );
53 this.retentionCount = retentionCount;
57 public void process( String path )
58 throws RepositoryPurgeException
62 Path artifactFile = Paths.get( repository.getRepoRoot( ), path );
64 if ( !Files.exists(artifactFile) )
69 ArtifactReference artifact = repository.toArtifactReference( path );
71 if ( VersionUtil.isSnapshot( artifact.getVersion( ) ) )
73 VersionedReference reference = new VersionedReference( );
74 reference.setGroupId( artifact.getGroupId( ) );
75 reference.setArtifactId( artifact.getArtifactId( ) );
76 reference.setVersion( artifact.getVersion( ) );
78 List<String> versions = new ArrayList<>( repository.getVersions( reference ) );
80 Collections.sort( versions, VersionComparator.getInstance( ) );
82 if ( retentionCount > versions.size( ) )
84 log.trace( "No deletion, because retention count is higher than actual number of artifacts." );
85 // Done. nothing to do here. skip it.
89 int countToPurge = versions.size( ) - retentionCount;
90 Set<ArtifactReference> artifactsToDelete = new HashSet<>( );
91 for ( String version : versions )
93 if ( countToPurge-- <= 0 )
97 artifactsToDelete.addAll( repository.getRelatedArtifacts( getNewArtifactReference( artifact, version ) ) );
99 purge( artifactsToDelete );
102 catch ( LayoutException le )
104 throw new RepositoryPurgeException( le.getMessage( ), le );
106 catch ( ContentNotFoundException e )
108 log.error( "Repostory artifact not found {}", path );
113 * Returns a new artifact reference with different version
115 private ArtifactReference getNewArtifactReference( ArtifactReference reference, String version )
116 throws LayoutException
118 ArtifactReference artifact = new ArtifactReference( );
119 artifact.setGroupId( reference.getGroupId( ) );
120 artifact.setArtifactId( reference.getArtifactId( ) );
121 artifact.setVersion( version );
122 artifact.setClassifier( reference.getClassifier( ) );
123 artifact.setType( reference.getType( ) );