[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
This commit is contained in:
Maria Odea B. Ching 2007-11-06 06:16:12 +00:00
parent ed1fe1cb4c
commit 293bd8e2f2
5 changed files with 83 additions and 15 deletions

View File

@ -20,10 +20,12 @@
*/
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 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 void process( String path )
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 @@ else if ( VersionUtil.isUniqueSnapshot( artifact.getVersion() ) )
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 @@ else if ( artifactFile.lastModified() < olderThanThisDate.getTimeInMillis() )
{
throw new RepositoryPurgeException( le.getMessage() );
}
catch ( ContentNotFoundException e )
{
throw new RepositoryPurgeException( e.getMessage() );
}
}
private Calendar uniqueSnapshotToCalendar( String version )
@ -152,4 +194,19 @@ private void doPurgeAllRelated( File artifactFile ) throws LayoutException
// 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 );
}
}
}

View File

@ -149,7 +149,7 @@ public void beginScan( ManagedRepositoryConfiguration repository )
if ( repository.getDaysOlder() != 0 )
{
repoPurge = new DaysOldRepositoryPurge( repositoryContent, dao.getArtifactDAO(), repository
.getDaysOlder(), indices );
.getDaysOlder(), repository.getRetentionCount(), indices );
}
else
{

View 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 @@ private void setLastModified( String dirPath )
public void testByLastModified()
throws Exception
{
{
repoPurge =
new DaysOldRepositoryPurge( getRepository(), dao, getRepoConfiguration().getDaysOlder(),
1, map );
populateDbForTestByLastModified();
String repoRoot = prepareTestRepo();
@ -83,6 +87,10 @@ public void testByLastModified()
public void testMetadataDrivenSnapshots()
throws Exception
{
repoPurge =
new DaysOldRepositoryPurge( getRepository(), dao, getRepoConfiguration().getDaysOlder(),
getRepoConfiguration().getRetentionCount(), map );
populateDbForTestMetadataDrivenSnapshots();
String repoRoot = prepareTestRepo();

View File

@ -46,7 +46,10 @@ public void deleteRecords( Collection records )
throws RepositoryIndexException
{
// TODO Auto-generated method stub
Assert.assertEquals( 2, records.size() );
if( records.size() != 0 )
{
Assert.assertEquals( 2, records.size() );
}
}
public boolean exists()

View 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>