001package org.apache.archiva.consumers.core.repository; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import org.apache.archiva.common.utils.VersionComparator; 023import org.apache.archiva.common.utils.VersionUtil; 024import org.apache.archiva.metadata.repository.RepositorySession; 025import org.apache.archiva.model.ArtifactReference; 026import org.apache.archiva.model.VersionedReference; 027import org.apache.archiva.repository.ContentNotFoundException; 028import org.apache.archiva.repository.ManagedRepositoryContent; 029import org.apache.archiva.repository.events.RepositoryListener; 030import org.apache.archiva.repository.layout.LayoutException; 031 032import java.io.File; 033import java.util.ArrayList; 034import java.util.Collections; 035import java.util.HashSet; 036import java.util.List; 037import java.util.Set; 038 039/** 040 * Purge the repository by retention count. Retain only the specified number of snapshots. 041 */ 042public class RetentionCountRepositoryPurge 043 extends AbstractRepositoryPurge 044{ 045 private int retentionCount; 046 047 public RetentionCountRepositoryPurge( ManagedRepositoryContent repository, int retentionCount, 048 RepositorySession repositorySession, List<RepositoryListener> listeners ) 049 { 050 super( repository, repositorySession, listeners ); 051 this.retentionCount = retentionCount; 052 } 053 054 @Override 055 public void process( String path ) 056 throws RepositoryPurgeException 057 { 058 try 059 { 060 File artifactFile = new File( repository.getRepoRoot( ), path ); 061 062 if ( !artifactFile.exists( ) ) 063 { 064 return; 065 } 066 067 ArtifactReference artifact = repository.toArtifactReference( path ); 068 069 if ( VersionUtil.isSnapshot( artifact.getVersion( ) ) ) 070 { 071 VersionedReference reference = new VersionedReference( ); 072 reference.setGroupId( artifact.getGroupId( ) ); 073 reference.setArtifactId( artifact.getArtifactId( ) ); 074 reference.setVersion( artifact.getVersion( ) ); 075 076 List<String> versions = new ArrayList<>( repository.getVersions( reference ) ); 077 078 Collections.sort( versions, VersionComparator.getInstance( ) ); 079 080 if ( retentionCount > versions.size( ) ) 081 { 082 log.trace( "No deletion, because retention count is higher than actual number of artifacts." ); 083 // Done. nothing to do here. skip it. 084 return; 085 } 086 087 int countToPurge = versions.size( ) - retentionCount; 088 Set<ArtifactReference> artifactsToDelete = new HashSet<>( ); 089 for ( String version : versions ) 090 { 091 if ( countToPurge-- <= 0 ) 092 { 093 break; 094 } 095 artifactsToDelete.addAll( repository.getRelatedArtifacts( getNewArtifactReference( artifact, version ) ) ); 096 } 097 purge( artifactsToDelete ); 098 } 099 } 100 catch ( LayoutException le ) 101 { 102 throw new RepositoryPurgeException( le.getMessage( ), le ); 103 } 104 catch ( ContentNotFoundException e ) 105 { 106 log.error( "Repostory artifact not found {}", path ); 107 } 108 } 109 110 /* 111 * Returns a new artifact reference with different version 112 */ 113 private ArtifactReference getNewArtifactReference( ArtifactReference reference, String version ) 114 throws LayoutException 115 { 116 ArtifactReference artifact = new ArtifactReference( ); 117 artifact.setGroupId( reference.getGroupId( ) ); 118 artifact.setArtifactId( reference.getArtifactId( ) ); 119 artifact.setVersion( version ); 120 artifact.setClassifier( reference.getClassifier( ) ); 121 artifact.setType( reference.getType( ) ); 122 return artifact; 123 124 } 125}