@Test public void testByLastModified() throws Exception { org.apache.archiva.repository.ManagedRepository repoConfiguration = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ); ArtifactCleanupFeature atf = repoConfiguration.getFeature( ArtifactCleanupFeature.class ).get(); sessionControl.reset(); sessionFactoryControl.reset(); EasyMock.expect( sessionFactory.createSession( ) ).andStubReturn( repositorySession ); EasyMock.expect( repositorySession.getRepository()).andStubReturn( metadataRepository ); repositorySession.save(); EasyMock.expectLastCall().anyTimes(); sessionFactoryControl.replay(); sessionControl.replay(); repoPurge = new DaysOldRepositoryPurge( getRepository(), atf.getRetentionPeriod().getDays(), atf.getRetentionCount(), repositorySession, Collections.singletonList( listener ) ); String repoRoot = prepareTestRepos(); String projectNs = "org.apache.maven.plugins"; String projectPath = projectNs.replaceAll("\\.","/"); String projectName = "maven-install-plugin"; String projectVersion = "2.2-SNAPSHOT"; String projectRoot = repoRoot + "/" + projectPath+"/"+projectName; Path repo = getTestRepoRootPath(); Path vDir = repo.resolve(projectPath).resolve(projectName).resolve(projectVersion); Set<String> deletedVersions = new HashSet<>(); deletedVersions.add("2.2-SNAPSHOT"); deletedVersions.add("2.2-20061118.060401-2"); setLastModified( projectRoot + "/" + projectVersion + "/", OLD_TIMESTAMP ); String[] exts = {".md5",".sha1",""}; for (int i=0; i<exts.length; i++) { listener.deleteArtifact(metadataRepository, getRepository().getId(), "org.apache.maven.plugins", "maven-install-plugin", "2.2-SNAPSHOT", "maven-install-plugin-2.2-SNAPSHOT.jar"+exts[i]); listener.deleteArtifact(metadataRepository, getRepository().getId(), "org.apache.maven.plugins", "maven-install-plugin", "2.2-SNAPSHOT", "maven-install-plugin-2.2-SNAPSHOT.pom"+exts[i]); listener.deleteArtifact(metadataRepository, getRepository().getId(), "org.apache.maven.plugins", "maven-install-plugin", "2.2-SNAPSHOT", "maven-install-plugin-2.2-20061118.060401-2.jar"+exts[i]); listener.deleteArtifact(metadataRepository, getRepository().getId(), "org.apache.maven.plugins", "maven-install-plugin", "2.2-SNAPSHOT", "maven-install-plugin-2.2-20061118.060401-2.pom"+exts[i]); } listenerControl.replay(); List<ArtifactMetadata> ml = getArtifactMetadataFromDir(TEST_REPO_ID , projectName, repo.getParent(), vDir ); when(metadataRepository.getArtifacts( repositorySession, TEST_REPO_ID, projectNs, projectName, projectVersion )).thenReturn(ml); repoPurge.process( PATH_TO_BY_DAYS_OLD_ARTIFACT ); listenerControl.verify(); verify(metadataRepository, never()).removeProjectVersion(eq(repositorySession) , eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion) ); ArgumentCaptor<ArtifactMetadata> metadataArg = ArgumentCaptor.forClass(ArtifactMetadata.class); verify(metadataRepository, times(2)).removeTimestampedArtifact(eq(repositorySession) , metadataArg.capture(), eq(projectVersion) ); List<ArtifactMetadata> metaL = metadataArg.getAllValues(); for (ArtifactMetadata meta : metaL) { assertTrue(meta.getId().startsWith(projectName)); assertTrue(deletedVersions.contains(meta.getVersion())); } assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.jar" ); assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.jar.md5" ); assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.jar.sha1" ); assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.pom" ); assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.pom.md5" ); assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.pom.sha1" ); assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.jar" ); assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.jar.md5" ); assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.jar.sha1" ); assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.pom" ); assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.pom.md5" ); assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.pom.sha1" ); assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.jar" ); assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.jar.md5" ); assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.jar.sha1" ); assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.pom" ); assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.pom.md5" ); assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.pom.sha1" ); assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar" ); assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar.md5" ); assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar.sha1" ); assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.pom" ); assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.pom.md5" ); assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.pom.sha1" ); }
@Override public void process( String path ) throws RepositoryPurgeException { try { ContentItem item = repository.toItem( path ); Artifact artifactItem = repository.getLayout( BaseRepositoryContentLayout.class ).adaptItem( Artifact.class, item ); if ( !artifactItem.exists( ) ) { return; } Calendar olderThanThisDate = Calendar.getInstance( TimeZone.getTimeZone( "UTC" ) ); olderThanThisDate.add( Calendar.DATE, -retentionPeriod ); ArchivaItemSelector selector = ArchivaItemSelector.builder( ) .withNamespace( artifactItem.getVersion( ).getProject( ).getNamespace( ).getId( ) ) .withProjectId( artifactItem.getVersion( ).getProject( ).getId( ) ) .withVersion( artifactItem.getVersion( ).getId( ) ) .withClassifier( "*" ) .includeRelatedArtifacts( ) .build( ); List<String> artifactVersions; try ( Stream<? extends Artifact> stream = repository.getLayout( BaseRepositoryContentLayout.class ).newArtifactStream( selector ) ) { artifactVersions = stream.map( a -> a.getArtifactVersion( ) ) .filter( StringUtils::isNotEmpty ) .distinct( ) .collect( Collectors.toList( ) ); } Collections.sort( artifactVersions, VersionComparator.getInstance( ) ); if ( retentionCount > artifactVersions.size( ) ) { return; } int countToPurge = artifactVersions.size( ) - retentionCount; ArchivaItemSelector.Builder artifactSelectorBuilder = ArchivaItemSelector.builder( ) .withNamespace( artifactItem.getVersion( ).getProject( ).getNamespace( ).getId( ) ) .withProjectId( artifactItem.getVersion( ).getProject( ).getId( ) ) .withVersion( artifactItem.getVersion( ).getId( ) ) .withArtifactId( artifactItem.getId( ) ) .withClassifier( "*" ) .includeRelatedArtifacts( ); Set<Artifact> artifactsToDelete = new HashSet<>( ); for ( String version : artifactVersions ) { if ( countToPurge-- <= 0 ) { break; } ArchivaItemSelector artifactSelector = artifactSelectorBuilder.withArtifactVersion( version ).build( ); try { if ( VersionUtil.isGenericSnapshot( version ) ) { List<? extends Artifact> artifactList = repository.getLayout( BaseRepositoryContentLayout.class ).getArtifacts( artifactSelector ); if ( artifactList.size( ) > 0 && artifactList.get( 0 ).getAsset( ).getModificationTime( ).toEpochMilli( ) < olderThanThisDate.getTimeInMillis( ) ) { artifactsToDelete.addAll( artifactList ); } } else if ( VersionUtil.isUniqueSnapshot( version ) ) { Calendar timestampCal = uniqueSnapshotToCalendar( version ); if ( timestampCal.getTimeInMillis( ) < olderThanThisDate.getTimeInMillis( ) ) { artifactsToDelete.addAll( repository.getLayout( BaseRepositoryContentLayout.class ).getArtifacts( artifactSelector ) ); } } } catch ( IllegalArgumentException e ) { log.error( "Bad selector for artifact: {}", e.getMessage( ), e ); } } purge( artifactsToDelete ); } catch ( LayoutException e ) { log.debug( "Not processing file that is not an artifact: {}", e.getMessage( ) ); } catch ( ContentAccessException e ) { e.printStackTrace( ); } }
DaysOldRepositoryPurge extends AbstractRepositoryPurge { @Override public void process( String path ) throws RepositoryPurgeException { try { ContentItem item = repository.toItem( path ); Artifact artifactItem = repository.getLayout( BaseRepositoryContentLayout.class ).adaptItem( Artifact.class, item ); if ( !artifactItem.exists( ) ) { return; } Calendar olderThanThisDate = Calendar.getInstance( TimeZone.getTimeZone( "UTC" ) ); olderThanThisDate.add( Calendar.DATE, -retentionPeriod ); ArchivaItemSelector selector = ArchivaItemSelector.builder( ) .withNamespace( artifactItem.getVersion( ).getProject( ).getNamespace( ).getId( ) ) .withProjectId( artifactItem.getVersion( ).getProject( ).getId( ) ) .withVersion( artifactItem.getVersion( ).getId( ) ) .withClassifier( "*" ) .includeRelatedArtifacts( ) .build( ); List<String> artifactVersions; try ( Stream<? extends Artifact> stream = repository.getLayout( BaseRepositoryContentLayout.class ).newArtifactStream( selector ) ) { artifactVersions = stream.map( a -> a.getArtifactVersion( ) ) .filter( StringUtils::isNotEmpty ) .distinct( ) .collect( Collectors.toList( ) ); } Collections.sort( artifactVersions, VersionComparator.getInstance( ) ); if ( retentionCount > artifactVersions.size( ) ) { return; } int countToPurge = artifactVersions.size( ) - retentionCount; ArchivaItemSelector.Builder artifactSelectorBuilder = ArchivaItemSelector.builder( ) .withNamespace( artifactItem.getVersion( ).getProject( ).getNamespace( ).getId( ) ) .withProjectId( artifactItem.getVersion( ).getProject( ).getId( ) ) .withVersion( artifactItem.getVersion( ).getId( ) ) .withArtifactId( artifactItem.getId( ) ) .withClassifier( "*" ) .includeRelatedArtifacts( ); Set<Artifact> artifactsToDelete = new HashSet<>( ); for ( String version : artifactVersions ) { if ( countToPurge-- <= 0 ) { break; } ArchivaItemSelector artifactSelector = artifactSelectorBuilder.withArtifactVersion( version ).build( ); try { if ( VersionUtil.isGenericSnapshot( version ) ) { List<? extends Artifact> artifactList = repository.getLayout( BaseRepositoryContentLayout.class ).getArtifacts( artifactSelector ); if ( artifactList.size( ) > 0 && artifactList.get( 0 ).getAsset( ).getModificationTime( ).toEpochMilli( ) < olderThanThisDate.getTimeInMillis( ) ) { artifactsToDelete.addAll( artifactList ); } } else if ( VersionUtil.isUniqueSnapshot( version ) ) { Calendar timestampCal = uniqueSnapshotToCalendar( version ); if ( timestampCal.getTimeInMillis( ) < olderThanThisDate.getTimeInMillis( ) ) { artifactsToDelete.addAll( repository.getLayout( BaseRepositoryContentLayout.class ).getArtifacts( artifactSelector ) ); } } } catch ( IllegalArgumentException e ) { log.error( "Bad selector for artifact: {}", e.getMessage( ), e ); } } purge( artifactsToDelete ); } catch ( LayoutException e ) { log.debug( "Not processing file that is not an artifact: {}", e.getMessage( ) ); } catch ( ContentAccessException e ) { e.printStackTrace( ); } } }
DaysOldRepositoryPurge extends AbstractRepositoryPurge { @Override public void process( String path ) throws RepositoryPurgeException { try { ContentItem item = repository.toItem( path ); Artifact artifactItem = repository.getLayout( BaseRepositoryContentLayout.class ).adaptItem( Artifact.class, item ); if ( !artifactItem.exists( ) ) { return; } Calendar olderThanThisDate = Calendar.getInstance( TimeZone.getTimeZone( "UTC" ) ); olderThanThisDate.add( Calendar.DATE, -retentionPeriod ); ArchivaItemSelector selector = ArchivaItemSelector.builder( ) .withNamespace( artifactItem.getVersion( ).getProject( ).getNamespace( ).getId( ) ) .withProjectId( artifactItem.getVersion( ).getProject( ).getId( ) ) .withVersion( artifactItem.getVersion( ).getId( ) ) .withClassifier( "*" ) .includeRelatedArtifacts( ) .build( ); List<String> artifactVersions; try ( Stream<? extends Artifact> stream = repository.getLayout( BaseRepositoryContentLayout.class ).newArtifactStream( selector ) ) { artifactVersions = stream.map( a -> a.getArtifactVersion( ) ) .filter( StringUtils::isNotEmpty ) .distinct( ) .collect( Collectors.toList( ) ); } Collections.sort( artifactVersions, VersionComparator.getInstance( ) ); if ( retentionCount > artifactVersions.size( ) ) { return; } int countToPurge = artifactVersions.size( ) - retentionCount; ArchivaItemSelector.Builder artifactSelectorBuilder = ArchivaItemSelector.builder( ) .withNamespace( artifactItem.getVersion( ).getProject( ).getNamespace( ).getId( ) ) .withProjectId( artifactItem.getVersion( ).getProject( ).getId( ) ) .withVersion( artifactItem.getVersion( ).getId( ) ) .withArtifactId( artifactItem.getId( ) ) .withClassifier( "*" ) .includeRelatedArtifacts( ); Set<Artifact> artifactsToDelete = new HashSet<>( ); for ( String version : artifactVersions ) { if ( countToPurge-- <= 0 ) { break; } ArchivaItemSelector artifactSelector = artifactSelectorBuilder.withArtifactVersion( version ).build( ); try { if ( VersionUtil.isGenericSnapshot( version ) ) { List<? extends Artifact> artifactList = repository.getLayout( BaseRepositoryContentLayout.class ).getArtifacts( artifactSelector ); if ( artifactList.size( ) > 0 && artifactList.get( 0 ).getAsset( ).getModificationTime( ).toEpochMilli( ) < olderThanThisDate.getTimeInMillis( ) ) { artifactsToDelete.addAll( artifactList ); } } else if ( VersionUtil.isUniqueSnapshot( version ) ) { Calendar timestampCal = uniqueSnapshotToCalendar( version ); if ( timestampCal.getTimeInMillis( ) < olderThanThisDate.getTimeInMillis( ) ) { artifactsToDelete.addAll( repository.getLayout( BaseRepositoryContentLayout.class ).getArtifacts( artifactSelector ) ); } } } catch ( IllegalArgumentException e ) { log.error( "Bad selector for artifact: {}", e.getMessage( ), e ); } } purge( artifactsToDelete ); } catch ( LayoutException e ) { log.debug( "Not processing file that is not an artifact: {}", e.getMessage( ) ); } catch ( ContentAccessException e ) { e.printStackTrace( ); } } DaysOldRepositoryPurge( ManagedRepositoryContent repository, int retentionPeriod, int retentionCount,
                                   RepositorySession repositorySession, List<RepositoryListener> listeners ); }
DaysOldRepositoryPurge extends AbstractRepositoryPurge { @Override public void process( String path ) throws RepositoryPurgeException { try { ContentItem item = repository.toItem( path ); Artifact artifactItem = repository.getLayout( BaseRepositoryContentLayout.class ).adaptItem( Artifact.class, item ); if ( !artifactItem.exists( ) ) { return; } Calendar olderThanThisDate = Calendar.getInstance( TimeZone.getTimeZone( "UTC" ) ); olderThanThisDate.add( Calendar.DATE, -retentionPeriod ); ArchivaItemSelector selector = ArchivaItemSelector.builder( ) .withNamespace( artifactItem.getVersion( ).getProject( ).getNamespace( ).getId( ) ) .withProjectId( artifactItem.getVersion( ).getProject( ).getId( ) ) .withVersion( artifactItem.getVersion( ).getId( ) ) .withClassifier( "*" ) .includeRelatedArtifacts( ) .build( ); List<String> artifactVersions; try ( Stream<? extends Artifact> stream = repository.getLayout( BaseRepositoryContentLayout.class ).newArtifactStream( selector ) ) { artifactVersions = stream.map( a -> a.getArtifactVersion( ) ) .filter( StringUtils::isNotEmpty ) .distinct( ) .collect( Collectors.toList( ) ); } Collections.sort( artifactVersions, VersionComparator.getInstance( ) ); if ( retentionCount > artifactVersions.size( ) ) { return; } int countToPurge = artifactVersions.size( ) - retentionCount; ArchivaItemSelector.Builder artifactSelectorBuilder = ArchivaItemSelector.builder( ) .withNamespace( artifactItem.getVersion( ).getProject( ).getNamespace( ).getId( ) ) .withProjectId( artifactItem.getVersion( ).getProject( ).getId( ) ) .withVersion( artifactItem.getVersion( ).getId( ) ) .withArtifactId( artifactItem.getId( ) ) .withClassifier( "*" ) .includeRelatedArtifacts( ); Set<Artifact> artifactsToDelete = new HashSet<>( ); for ( String version : artifactVersions ) { if ( countToPurge-- <= 0 ) { break; } ArchivaItemSelector artifactSelector = artifactSelectorBuilder.withArtifactVersion( version ).build( ); try { if ( VersionUtil.isGenericSnapshot( version ) ) { List<? extends Artifact> artifactList = repository.getLayout( BaseRepositoryContentLayout.class ).getArtifacts( artifactSelector ); if ( artifactList.size( ) > 0 && artifactList.get( 0 ).getAsset( ).getModificationTime( ).toEpochMilli( ) < olderThanThisDate.getTimeInMillis( ) ) { artifactsToDelete.addAll( artifactList ); } } else if ( VersionUtil.isUniqueSnapshot( version ) ) { Calendar timestampCal = uniqueSnapshotToCalendar( version ); if ( timestampCal.getTimeInMillis( ) < olderThanThisDate.getTimeInMillis( ) ) { artifactsToDelete.addAll( repository.getLayout( BaseRepositoryContentLayout.class ).getArtifacts( artifactSelector ) ); } } } catch ( IllegalArgumentException e ) { log.error( "Bad selector for artifact: {}", e.getMessage( ), e ); } } purge( artifactsToDelete ); } catch ( LayoutException e ) { log.debug( "Not processing file that is not an artifact: {}", e.getMessage( ) ); } catch ( ContentAccessException e ) { e.printStackTrace( ); } } DaysOldRepositoryPurge( ManagedRepositoryContent repository, int retentionPeriod, int retentionCount,
                                   RepositorySession repositorySession, List<RepositoryListener> listeners ); @Override void process( String path ); }
DaysOldRepositoryPurge extends AbstractRepositoryPurge { @Override public void process( String path ) throws RepositoryPurgeException { try { ContentItem item = repository.toItem( path ); Artifact artifactItem = repository.getLayout( BaseRepositoryContentLayout.class ).adaptItem( Artifact.class, item ); if ( !artifactItem.exists( ) ) { return; } Calendar olderThanThisDate = Calendar.getInstance( TimeZone.getTimeZone( "UTC" ) ); olderThanThisDate.add( Calendar.DATE, -retentionPeriod ); ArchivaItemSelector selector = ArchivaItemSelector.builder( ) .withNamespace( artifactItem.getVersion( ).getProject( ).getNamespace( ).getId( ) ) .withProjectId( artifactItem.getVersion( ).getProject( ).getId( ) ) .withVersion( artifactItem.getVersion( ).getId( ) ) .withClassifier( "*" ) .includeRelatedArtifacts( ) .build( ); List<String> artifactVersions; try ( Stream<? extends Artifact> stream = repository.getLayout( BaseRepositoryContentLayout.class ).newArtifactStream( selector ) ) { artifactVersions = stream.map( a -> a.getArtifactVersion( ) ) .filter( StringUtils::isNotEmpty ) .distinct( ) .collect( Collectors.toList( ) ); } Collections.sort( artifactVersions, VersionComparator.getInstance( ) ); if ( retentionCount > artifactVersions.size( ) ) { return; } int countToPurge = artifactVersions.size( ) - retentionCount; ArchivaItemSelector.Builder artifactSelectorBuilder = ArchivaItemSelector.builder( ) .withNamespace( artifactItem.getVersion( ).getProject( ).getNamespace( ).getId( ) ) .withProjectId( artifactItem.getVersion( ).getProject( ).getId( ) ) .withVersion( artifactItem.getVersion( ).getId( ) ) .withArtifactId( artifactItem.getId( ) ) .withClassifier( "*" ) .includeRelatedArtifacts( ); Set<Artifact> artifactsToDelete = new HashSet<>( ); for ( String version : artifactVersions ) { if ( countToPurge-- <= 0 ) { break; } ArchivaItemSelector artifactSelector = artifactSelectorBuilder.withArtifactVersion( version ).build( ); try { if ( VersionUtil.isGenericSnapshot( version ) ) { List<? extends Artifact> artifactList = repository.getLayout( BaseRepositoryContentLayout.class ).getArtifacts( artifactSelector ); if ( artifactList.size( ) > 0 && artifactList.get( 0 ).getAsset( ).getModificationTime( ).toEpochMilli( ) < olderThanThisDate.getTimeInMillis( ) ) { artifactsToDelete.addAll( artifactList ); } } else if ( VersionUtil.isUniqueSnapshot( version ) ) { Calendar timestampCal = uniqueSnapshotToCalendar( version ); if ( timestampCal.getTimeInMillis( ) < olderThanThisDate.getTimeInMillis( ) ) { artifactsToDelete.addAll( repository.getLayout( BaseRepositoryContentLayout.class ).getArtifacts( artifactSelector ) ); } } } catch ( IllegalArgumentException e ) { log.error( "Bad selector for artifact: {}", e.getMessage( ), e ); } } purge( artifactsToDelete ); } catch ( LayoutException e ) { log.debug( "Not processing file that is not an artifact: {}", e.getMessage( ) ); } catch ( ContentAccessException e ) { e.printStackTrace( ); } } DaysOldRepositoryPurge( ManagedRepositoryContent repository, int retentionPeriod, int retentionCount,
                                   RepositorySession repositorySession, List<RepositoryListener> listeners ); @Override void process( String path );  }