]> source.dussan.org Git - archiva.git/commitdiff
[MRM-556]
authorMaria Odea B. Ching <oching@apache.org>
Tue, 6 Nov 2007 06:16:12 +0000 (06:16 +0000)
committerMaria Odea B. Ching <oching@apache.org>
Tue, 6 Nov 2007 06:16:12 +0000 (06:16 +0000)
- 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

archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/maven/archiva/consumers/core/repository/DaysOldRepositoryPurge.java
archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/maven/archiva/consumers/core/repository/RepositoryPurgeConsumer.java
archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/maven/archiva/consumers/core/repository/DaysOldRepositoryPurgeTest.java
archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/maven/archiva/consumers/core/repository/stubs/LuceneRepositoryContentIndexStub.java
archiva-web/archiva-webapp/src/main/resources/org/apache/maven/archiva/web/action/admin/repositories/EditManagedRepositoryAction-validation.xml

index 96d765d1aa948732cdba3a3e8f7d164757faa1ee..e08a282dcbe2bdee5c313761326716a1ec70fa30 100644 (file)
@@ -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<String, RepositoryContentIndex> indices )
+                                   int daysOlder, int retentionCount, Map<String, RepositoryContentIndex> 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<String> versions = new ArrayList<String>( 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<String> versions, File artifactFile )
+        throws LayoutException
+    {
+        int countToPurge = versions.size() - retentionCount;
+
+        while( versions.iterator().hasNext() )
+        {
+            if ( countToPurge-- <= 0 )
+            {
+                break;
+            }
+            doPurgeAllRelated( artifactFile );
+        }
+    }
 }
index 417b809b7faaa7a4d6002ad22043aa71e9b80d32..aa5242f2bba09f9cc860aa7056acc210459519a9 100644 (file)
@@ -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
             {
index dda469fc459e459672a87d32b9659fd58e9b42c2..90c4b56f71c8b57f47c76cdbc0b8b20f7ba0d684 100644 (file)
@@ -35,18 +35,18 @@ public class DaysOldRepositoryPurgeTest
     extends AbstractRepositoryPurgeTest
 {
 
+    private Map<String, RepositoryContentIndex> map;
+    
     protected void setUp()
         throws Exception
     {
         super.setUp();
 
-        Map<String, RepositoryContentIndex> map = new HashMap<String, RepositoryContentIndex>();
+        map = new HashMap<String, RepositoryContentIndex>();
         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();
index e9966def2bd1b9865fefbe1f5b1f03719f37254a..ad9c69de0de45dadecbfb2f919021a20ae4b6363 100644 (file)
@@ -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()
index d82bb772b98c52ce66c3166dc28c67fa837c93da..1c3997ce5560567ce6cd8c3264b38c7b3d21c64d 100644 (file)
@@ -46,7 +46,7 @@
   </field>
    <field name="repository.daysOlder">
        <field-validator type="int">
-       <param name="min">1</param>
+       <param name="min">0</param>
         <param name="max">1000</param>
                <message>Repository Purge By Days Older Than needs to be between ${min} and ${max}.</message>
        </field-validator>