1 package org.apache.maven.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.repository.events.RepositoryListener;
23 import org.apache.maven.archiva.common.utils.VersionComparator;
24 import org.apache.maven.archiva.common.utils.VersionUtil;
25 import org.apache.maven.archiva.model.ArtifactReference;
26 import org.apache.maven.archiva.model.VersionedReference;
27 import org.apache.maven.archiva.repository.ContentNotFoundException;
28 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
29 import org.apache.maven.archiva.repository.layout.LayoutException;
32 import java.util.ArrayList;
33 import java.util.Collections;
34 import java.util.List;
38 * 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,
47 int retentionCount, List<RepositoryListener> listeners )
49 super( repository, listeners );
50 this.retentionCount = retentionCount;
53 public void process( String path )
54 throws RepositoryPurgeException
58 File artifactFile = new File( repository.getRepoRoot(), path );
60 if ( !artifactFile.exists() )
65 ArtifactReference artifact = repository.toArtifactReference( path );
67 if ( VersionUtil.isSnapshot( artifact.getVersion() ) )
69 VersionedReference reference = new VersionedReference();
70 reference.setGroupId( artifact.getGroupId() );
71 reference.setArtifactId( artifact.getArtifactId() );
72 reference.setVersion( artifact.getVersion() );
74 List<String> versions = new ArrayList<String>( repository.getVersions( reference ) );
76 Collections.sort( versions, VersionComparator.getInstance() );
78 if ( retentionCount > versions.size() )
80 // Done. nothing to do here. skip it.
84 int countToPurge = versions.size() - retentionCount;
86 for ( String version : versions )
88 if ( countToPurge-- <= 0 )
93 doPurgeAllRelated( artifact, version );
97 catch ( LayoutException le )
99 throw new RepositoryPurgeException( le.getMessage(), le );
101 catch ( ContentNotFoundException e )
103 // Nothing to do here.
104 // TODO: Log this condition?
108 private void doPurgeAllRelated( ArtifactReference reference, String version )
109 throws LayoutException
111 ArtifactReference artifact = new ArtifactReference();
112 artifact.setGroupId( reference.getGroupId() );
113 artifact.setArtifactId( reference.getArtifactId() );
114 artifact.setVersion( version );
115 artifact.setClassifier( reference.getClassifier() );
116 artifact.setType( reference.getType() );
120 Set<ArtifactReference> related = repository.getRelatedArtifacts( artifact );
123 catch ( ContentNotFoundException e )
125 // Nothing to do here.