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.maven.archiva.common.utils.VersionComparator;
23 import org.apache.maven.archiva.common.utils.VersionUtil;
24 import org.apache.maven.archiva.database.ArtifactDAO;
25 import org.apache.maven.archiva.indexer.RepositoryContentIndex;
26 import org.apache.maven.archiva.model.ArtifactReference;
27 import org.apache.maven.archiva.model.VersionedReference;
28 import org.apache.maven.archiva.repository.ContentNotFoundException;
29 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
30 import org.apache.maven.archiva.repository.layout.LayoutException;
33 import java.util.ArrayList;
34 import java.util.Collections;
35 import java.util.List;
40 * Purge the repository by retention count. Retain only the specified number of snapshots.
42 * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
44 public class RetentionCountRepositoryPurge
45 extends AbstractRepositoryPurge
47 private int retentionCount;
49 public RetentionCountRepositoryPurge( ManagedRepositoryContent repository, ArtifactDAO artifactDao,
50 int retentionCount, Map<String, RepositoryContentIndex> indices )
52 super( repository, artifactDao, indices );
53 this.retentionCount = retentionCount;
56 public void process( String path )
57 throws RepositoryPurgeException
61 File artifactFile = new File( repository.getRepoRoot(), path );
63 if ( !artifactFile.exists() )
68 ArtifactReference artifact = repository.toArtifactReference( path );
70 if ( VersionUtil.isSnapshot( artifact.getVersion() ) )
72 VersionedReference reference = new VersionedReference();
73 reference.setGroupId( artifact.getGroupId() );
74 reference.setArtifactId( artifact.getArtifactId() );
75 reference.setVersion( artifact.getVersion() );
77 List<String> versions = new ArrayList<String>( repository.getVersions( reference ) );
79 Collections.sort( versions, VersionComparator.getInstance() );
81 if ( retentionCount > versions.size() )
83 // Done. nothing to do here. skip it.
87 int countToPurge = versions.size() - retentionCount;
89 for ( String version : versions )
91 if ( countToPurge-- <= 0 )
96 doPurgeAllRelated( artifact, version );
100 catch ( LayoutException le )
102 throw new RepositoryPurgeException( le.getMessage(), le );
104 catch ( ContentNotFoundException e )
106 // Nothing to do here.
107 // TODO: Log this condition?
111 private void doPurgeAllRelated( ArtifactReference reference, String version )
112 throws LayoutException
114 ArtifactReference artifact = new ArtifactReference();
115 artifact.setGroupId( reference.getGroupId() );
116 artifact.setArtifactId( reference.getArtifactId() );
117 artifact.setVersion( version );
118 artifact.setClassifier( reference.getClassifier() );
119 artifact.setType( reference.getType() );
123 Set<ArtifactReference> related = repository.getRelatedArtifacts( artifact );
126 catch ( ContentNotFoundException e )
128 // Nothing to do here.