From 293bd8e2f2a05b9c5475bb471996a002caf597b0 Mon Sep 17 00:00:00 2001 From: "Maria Odea B. Ching" Date: Tue, 6 Nov 2007 06:16:12 +0000 Subject: [PATCH] [MRM-556] - respect set retention count when purging by number of days old (used same logic for purging by retention count) - updated edit managed repository validation for the number of days old minimum range - adjusted DaysOldRepositoryPurgeTest for the changes git-svn-id: https://svn.apache.org/repos/asf/maven/archiva/trunk@592297 13f79535-47bb-0310-9956-ffa450edef68 --- .../repository/DaysOldRepositoryPurge.java | 71 +++++++++++++++++-- .../repository/RepositoryPurgeConsumer.java | 2 +- .../DaysOldRepositoryPurgeTest.java | 18 +++-- .../LuceneRepositoryContentIndexStub.java | 5 +- ...EditManagedRepositoryAction-validation.xml | 2 +- 5 files changed, 83 insertions(+), 15 deletions(-) diff --git a/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/maven/archiva/consumers/core/repository/DaysOldRepositoryPurge.java b/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/maven/archiva/consumers/core/repository/DaysOldRepositoryPurge.java index 96d765d1a..e08a282dc 100644 --- a/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/maven/archiva/consumers/core/repository/DaysOldRepositoryPurge.java +++ b/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/maven/archiva/consumers/core/repository/DaysOldRepositoryPurge.java @@ -20,10 +20,12 @@ package org.apache.maven.archiva.consumers.core.repository; */ import org.apache.commons.lang.time.DateUtils; +import org.apache.maven.archiva.common.utils.VersionComparator; import org.apache.maven.archiva.common.utils.VersionUtil; import org.apache.maven.archiva.database.ArtifactDAO; import org.apache.maven.archiva.indexer.RepositoryContentIndex; import org.apache.maven.archiva.model.ArtifactReference; +import org.apache.maven.archiva.model.VersionedReference; import org.apache.maven.archiva.repository.ContentNotFoundException; import org.apache.maven.archiva.repository.ManagedRepositoryContent; import org.apache.maven.archiva.repository.layout.LayoutException; @@ -31,8 +33,11 @@ import org.apache.maven.archiva.repository.layout.LayoutException; import java.io.File; import java.text.ParseException; import java.text.SimpleDateFormat; +import java.util.ArrayList; import java.util.Calendar; +import java.util.Collections; import java.util.Date; +import java.util.List; import java.util.Map; import java.util.Set; import java.util.regex.Matcher; @@ -49,11 +54,14 @@ public class DaysOldRepositoryPurge private int daysOlder; + private int retentionCount; + public DaysOldRepositoryPurge( ManagedRepositoryContent repository, ArtifactDAO artifactDao, - int daysOlder, Map indices ) + int daysOlder, int retentionCount, Map indices ) { super( repository, artifactDao, indices ); this.daysOlder = daysOlder; + this.retentionCount = retentionCount; timestampParser = new SimpleDateFormat( "yyyyMMdd.HHmmss" ); timestampParser.setTimeZone( DateUtils.UTC_TIME_ZONE ); } @@ -75,12 +83,29 @@ public class DaysOldRepositoryPurge Calendar olderThanThisDate = Calendar.getInstance( DateUtils.UTC_TIME_ZONE ); olderThanThisDate.add( Calendar.DATE, -daysOlder ); + // respect retention count + VersionedReference reference = new VersionedReference(); + reference.setGroupId( artifact.getGroupId() ); + reference.setArtifactId( artifact.getArtifactId() ); + reference.setVersion( artifact.getVersion() ); + + List versions = new ArrayList( repository.getVersions( reference ) ); + + Collections.sort( versions, VersionComparator.getInstance() ); + + // Is this a generic snapshot "1.0-SNAPSHOT" ? if ( VersionUtil.isGenericSnapshot( artifact.getVersion() ) ) { if ( artifactFile.lastModified() < olderThanThisDate.getTimeInMillis() ) - { - doPurgeAllRelated( artifactFile ); + { + if ( retentionCount > versions.size() ) + { + // Done. nothing to do here. skip it. + return; + } + + purgeArtifact( versions, artifactFile ); } } // Is this a timestamp snapshot "1.0-20070822.123456-42" ? @@ -89,12 +114,25 @@ public class DaysOldRepositoryPurge Calendar timestampCal = uniqueSnapshotToCalendar( artifact.getVersion() ); if ( timestampCal.getTimeInMillis() < olderThanThisDate.getTimeInMillis() ) - { - doPurgeAllRelated( artifactFile ); + { + if ( retentionCount > versions.size() ) + { + // Done. nothing to do here. skip it. + return; + } + + purgeArtifact( versions, artifactFile ); } else if ( artifactFile.lastModified() < olderThanThisDate.getTimeInMillis() ) - { - doPurgeAllRelated( artifactFile ); + { + + if ( retentionCount > versions.size() ) + { + // Done. nothing to do here. skip it. + return; + } + + purgeArtifact( versions, artifactFile ); } } } @@ -102,6 +140,10 @@ public class DaysOldRepositoryPurge { throw new RepositoryPurgeException( le.getMessage() ); } + catch ( ContentNotFoundException e ) + { + throw new RepositoryPurgeException( e.getMessage() ); + } } private Calendar uniqueSnapshotToCalendar( String version ) @@ -152,4 +194,19 @@ public class DaysOldRepositoryPurge // TODO: Log this? } } + + private void purgeArtifact( List versions, File artifactFile ) + throws LayoutException + { + int countToPurge = versions.size() - retentionCount; + + while( versions.iterator().hasNext() ) + { + if ( countToPurge-- <= 0 ) + { + break; + } + doPurgeAllRelated( artifactFile ); + } + } } diff --git a/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/maven/archiva/consumers/core/repository/RepositoryPurgeConsumer.java b/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/maven/archiva/consumers/core/repository/RepositoryPurgeConsumer.java index 417b809b7..aa5242f2b 100644 --- a/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/maven/archiva/consumers/core/repository/RepositoryPurgeConsumer.java +++ b/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/maven/archiva/consumers/core/repository/RepositoryPurgeConsumer.java @@ -149,7 +149,7 @@ public class RepositoryPurgeConsumer if ( repository.getDaysOlder() != 0 ) { repoPurge = new DaysOldRepositoryPurge( repositoryContent, dao.getArtifactDAO(), repository - .getDaysOlder(), indices ); + .getDaysOlder(), repository.getRetentionCount(), indices ); } else { diff --git a/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/maven/archiva/consumers/core/repository/DaysOldRepositoryPurgeTest.java b/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/maven/archiva/consumers/core/repository/DaysOldRepositoryPurgeTest.java index dda469fc4..90c4b56f7 100644 --- a/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/maven/archiva/consumers/core/repository/DaysOldRepositoryPurgeTest.java +++ b/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/maven/archiva/consumers/core/repository/DaysOldRepositoryPurgeTest.java @@ -35,18 +35,18 @@ public class DaysOldRepositoryPurgeTest extends AbstractRepositoryPurgeTest { + private Map map; + protected void setUp() throws Exception { super.setUp(); - Map map = new HashMap(); + map = new HashMap(); map.put( "filecontent", new LuceneRepositoryContentIndexStub() ); map.put( "hashcodes", new LuceneRepositoryContentIndexStub() ); - map.put( "bytecode", new LuceneRepositoryContentIndexStub() ); + map.put( "bytecode", new LuceneRepositoryContentIndexStub() ); - repoPurge = - new DaysOldRepositoryPurge( getRepository(), dao, getRepoConfiguration().getDaysOlder(), map ); } private void setLastModified( String dirPath ) @@ -61,7 +61,11 @@ public class DaysOldRepositoryPurgeTest public void testByLastModified() throws Exception - { + { + repoPurge = + new DaysOldRepositoryPurge( getRepository(), dao, getRepoConfiguration().getDaysOlder(), + 1, map ); + populateDbForTestByLastModified(); String repoRoot = prepareTestRepo(); @@ -83,6 +87,10 @@ public class DaysOldRepositoryPurgeTest public void testMetadataDrivenSnapshots() throws Exception { + repoPurge = + new DaysOldRepositoryPurge( getRepository(), dao, getRepoConfiguration().getDaysOlder(), + getRepoConfiguration().getRetentionCount(), map ); + populateDbForTestMetadataDrivenSnapshots(); String repoRoot = prepareTestRepo(); diff --git a/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/maven/archiva/consumers/core/repository/stubs/LuceneRepositoryContentIndexStub.java b/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/maven/archiva/consumers/core/repository/stubs/LuceneRepositoryContentIndexStub.java index e9966def2..ad9c69de0 100644 --- a/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/maven/archiva/consumers/core/repository/stubs/LuceneRepositoryContentIndexStub.java +++ b/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/maven/archiva/consumers/core/repository/stubs/LuceneRepositoryContentIndexStub.java @@ -46,7 +46,10 @@ public class LuceneRepositoryContentIndexStub throws RepositoryIndexException { // TODO Auto-generated method stub - Assert.assertEquals( 2, records.size() ); + if( records.size() != 0 ) + { + Assert.assertEquals( 2, records.size() ); + } } public boolean exists() diff --git a/archiva-web/archiva-webapp/src/main/resources/org/apache/maven/archiva/web/action/admin/repositories/EditManagedRepositoryAction-validation.xml b/archiva-web/archiva-webapp/src/main/resources/org/apache/maven/archiva/web/action/admin/repositories/EditManagedRepositoryAction-validation.xml index d82bb772b..1c3997ce5 100644 --- a/archiva-web/archiva-webapp/src/main/resources/org/apache/maven/archiva/web/action/admin/repositories/EditManagedRepositoryAction-validation.xml +++ b/archiva-web/archiva-webapp/src/main/resources/org/apache/maven/archiva/web/action/admin/repositories/EditManagedRepositoryAction-validation.xml @@ -46,7 +46,7 @@ - 1 + 0 1000 Repository Purge By Days Older Than needs to be between ${min} and ${max}. -- 2.39.5