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.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.
40 * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
42 public class RetentionCountRepositoryPurge
43 extends AbstractRepositoryPurge
45 private int retentionCount;
47 public RetentionCountRepositoryPurge( ManagedRepositoryContent repository, ArtifactDAO artifactDao,
50 super( repository, artifactDao );
51 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<String>( 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() );
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.