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.metadata.repository.RepositorySession;
23 import org.apache.archiva.repository.events.RepositoryListener;
24 import org.apache.archiva.common.utils.VersionComparator;
25 import org.apache.archiva.common.utils.VersionUtil;
26 import org.apache.archiva.model.ArtifactReference;
27 import org.apache.archiva.model.VersionedReference;
28 import org.apache.archiva.repository.ContentNotFoundException;
29 import org.apache.archiva.repository.ManagedRepositoryContent;
30 import org.apache.archiva.repository.layout.LayoutException;
33 import java.util.ArrayList;
34 import java.util.Collections;
35 import java.util.List;
39 * Purge the repository by retention count. Retain only the specified number of snapshots.
41 public class RetentionCountRepositoryPurge
42 extends AbstractRepositoryPurge
44 private int retentionCount;
46 public RetentionCountRepositoryPurge( ManagedRepositoryContent repository, int retentionCount,
47 RepositorySession repositorySession, List<RepositoryListener> listeners )
49 super( repository, repositorySession, listeners );
50 this.retentionCount = retentionCount;
54 public void process( String path )
55 throws RepositoryPurgeException
59 File artifactFile = new File( repository.getRepoRoot(), path );
61 if ( !artifactFile.exists() )
66 ArtifactReference artifact = repository.toArtifactReference( path );
68 if ( VersionUtil.isSnapshot( artifact.getVersion() ) )
70 VersionedReference reference = new VersionedReference();
71 reference.setGroupId( artifact.getGroupId() );
72 reference.setArtifactId( artifact.getArtifactId() );
73 reference.setVersion( artifact.getVersion() );
75 List<String> versions = new ArrayList<>( repository.getVersions( reference ) );
77 Collections.sort( versions, VersionComparator.getInstance() );
79 if ( retentionCount > versions.size() )
81 // Done. nothing to do here. skip it.
85 int countToPurge = versions.size() - retentionCount;
87 for ( String version : versions )
89 if ( countToPurge-- <= 0 )
94 doPurgeAllRelated( artifact, version );
98 catch ( LayoutException le )
100 throw new RepositoryPurgeException( le.getMessage(), le );
102 catch ( ContentNotFoundException e )
104 // Nothing to do here.
105 // TODO: Log this condition?
109 private void doPurgeAllRelated( ArtifactReference reference, String version )
110 throws LayoutException
112 ArtifactReference artifact = new ArtifactReference();
113 artifact.setGroupId( reference.getGroupId() );
114 artifact.setArtifactId( reference.getArtifactId() );
115 artifact.setVersion( version );
116 artifact.setClassifier( reference.getClassifier() );
117 artifact.setType( reference.getType() );
121 Set<ArtifactReference> related = repository.getRelatedArtifacts( artifact );
124 catch ( ContentNotFoundException e )
126 // Nothing to do here.