@Test public void testOrderOfDeletion() throws Exception { String repoRoot = prepareTestRepos(); String projectNs = "org.apache.maven.plugins"; String projectPath = projectNs.replaceAll("\\.","/"); String projectName = "maven-assembly-plugin"; String projectVersion = "1.1.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("1.1.2-20070427.065136-1"); String versionRoot = projectRoot + "/" + projectVersion; listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.maven.plugins", "maven-assembly-plugin", "1.1.2-SNAPSHOT", "maven-assembly-plugin-1.1.2-20070427.065136-1.jar.md5" ); listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.maven.plugins", "maven-assembly-plugin", "1.1.2-SNAPSHOT", "maven-assembly-plugin-1.1.2-20070427.065136-1.jar.sha1" ); listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.maven.plugins", "maven-assembly-plugin", "1.1.2-SNAPSHOT", "maven-assembly-plugin-1.1.2-20070427.065136-1.jar" ); listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.maven.plugins", "maven-assembly-plugin", "1.1.2-SNAPSHOT", "maven-assembly-plugin-1.1.2-20070427.065136-1.pom.md5" ); listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.maven.plugins", "maven-assembly-plugin", "1.1.2-SNAPSHOT", "maven-assembly-plugin-1.1.2-20070427.065136-1.pom.sha1" ); listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.maven.plugins", "maven-assembly-plugin", "1.1.2-SNAPSHOT", "maven-assembly-plugin-1.1.2-20070427.065136-1.pom" ); 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_TEST_ORDER_OF_DELETION ); 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(deletedVersions.size())).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( versionRoot + "/maven-assembly-plugin-1.1.2-20070427.065136-1.jar" ); assertDeleted( versionRoot + "/maven-assembly-plugin-1.1.2-20070427.065136-1.jar.sha1" ); assertDeleted( versionRoot + "/maven-assembly-plugin-1.1.2-20070427.065136-1.jar.md5" ); assertDeleted( versionRoot + "/maven-assembly-plugin-1.1.2-20070427.065136-1.pom" ); assertDeleted( versionRoot + "/maven-assembly-plugin-1.1.2-20070427.065136-1.pom.sha1" ); assertDeleted( versionRoot + "/maven-assembly-plugin-1.1.2-20070427.065136-1.pom.md5" ); assertExists( versionRoot + "/maven-assembly-plugin-1.1.2-20070506.163513-2.jar" ); assertExists( versionRoot + "/maven-assembly-plugin-1.1.2-20070506.163513-2.jar.sha1" ); assertExists( versionRoot + "/maven-assembly-plugin-1.1.2-20070506.163513-2.jar.md5" ); assertExists( versionRoot + "/maven-assembly-plugin-1.1.2-20070506.163513-2.pom" ); assertExists( versionRoot + "/maven-assembly-plugin-1.1.2-20070506.163513-2.pom.sha1" ); assertExists( versionRoot + "/maven-assembly-plugin-1.1.2-20070506.163513-2.pom.md5" ); assertExists( versionRoot + "/maven-assembly-plugin-1.1.2-20070615.105019-3.jar" ); assertExists( versionRoot + "/maven-assembly-plugin-1.1.2-20070615.105019-3.jar.sha1" ); assertExists( versionRoot + "/maven-assembly-plugin-1.1.2-20070615.105019-3.jar.md5" ); assertExists( versionRoot + "/maven-assembly-plugin-1.1.2-20070615.105019-3.pom" ); assertExists( versionRoot + "/maven-assembly-plugin-1.1.2-20070615.105019-3.pom.sha1" ); assertExists( versionRoot + "/maven-assembly-plugin-1.1.2-20070615.105019-3.pom.md5" ); }
@Override public void process( String path ) throws RepositoryPurgeException { try { ContentItem item = repository.toItem( path ); BaseRepositoryContentLayout layout = repository.getLayout( BaseRepositoryContentLayout.class ); Artifact artifact = layout.adaptItem( Artifact.class, item ); if ( !artifact.exists( ) ) { return; } if ( VersionUtil.isSnapshot( artifact.getVersion( ).getId( ) ) ) { ArchivaItemSelector selector = ArchivaItemSelector.builder( ) .withNamespace( artifact.getVersion( ).getProject( ).getNamespace( ).getId( ) ) .withProjectId( artifact.getVersion( ).getProject( ).getId( ) ) .withArtifactId( artifact.getId( ) ) .withVersion( artifact.getVersion( ).getId( ) ) .withClassifier( "*" ) .includeRelatedArtifacts( ) .build( ); List<String> versions; try ( Stream<? extends Artifact> stream = repository.getLayout( BaseRepositoryContentLayout.class ).newArtifactStream( selector ) ) { versions = stream.map( a -> a.getArtifactVersion( ) ) .filter( StringUtils::isNotEmpty ) .distinct( ) .collect( Collectors.toList( ) ); } Collections.sort( versions, VersionComparator.getInstance( ) ); if ( retentionCount > versions.size( ) ) { log.trace( "No deletion, because retention count is higher than actual number of artifacts." ); return; } ArchivaItemSelector.Builder selectorBuilder = ArchivaItemSelector.builder( ) .withNamespace( artifact.getVersion( ).getProject( ).getNamespace( ).getId( ) ) .withProjectId( artifact.getVersion( ).getProject( ).getId( ) ) .withArtifactId( artifact.getId( ) ) .withClassifier( "*" ) .includeRelatedArtifacts( ) .withVersion( artifact.getVersion( ).getId( ) ); int countToPurge = versions.size( ) - retentionCount; Set<Artifact> artifactsToDelete = new HashSet<>( ); for ( String version : versions ) { if ( countToPurge-- <= 0 ) { break; } List<? extends Artifact> delArtifacts = repository.getLayout( BaseRepositoryContentLayout.class ).getArtifacts( selectorBuilder.withArtifactVersion( version ).build( ) ); if ( delArtifacts != null && delArtifacts.size( ) > 0 ) { artifactsToDelete.addAll( delArtifacts ); } } purge( artifactsToDelete ); } } catch ( LayoutException le ) { throw new RepositoryPurgeException( le.getMessage( ), le ); } catch ( ContentAccessException e ) { log.error( "Error while accessing the repository data: {}", e.getMessage( ), e ); throw new RepositoryPurgeException( e.getMessage( ), e ); } }
RetentionCountRepositoryPurge extends AbstractRepositoryPurge { @Override public void process( String path ) throws RepositoryPurgeException { try { ContentItem item = repository.toItem( path ); BaseRepositoryContentLayout layout = repository.getLayout( BaseRepositoryContentLayout.class ); Artifact artifact = layout.adaptItem( Artifact.class, item ); if ( !artifact.exists( ) ) { return; } if ( VersionUtil.isSnapshot( artifact.getVersion( ).getId( ) ) ) { ArchivaItemSelector selector = ArchivaItemSelector.builder( ) .withNamespace( artifact.getVersion( ).getProject( ).getNamespace( ).getId( ) ) .withProjectId( artifact.getVersion( ).getProject( ).getId( ) ) .withArtifactId( artifact.getId( ) ) .withVersion( artifact.getVersion( ).getId( ) ) .withClassifier( "*" ) .includeRelatedArtifacts( ) .build( ); List<String> versions; try ( Stream<? extends Artifact> stream = repository.getLayout( BaseRepositoryContentLayout.class ).newArtifactStream( selector ) ) { versions = stream.map( a -> a.getArtifactVersion( ) ) .filter( StringUtils::isNotEmpty ) .distinct( ) .collect( Collectors.toList( ) ); } Collections.sort( versions, VersionComparator.getInstance( ) ); if ( retentionCount > versions.size( ) ) { log.trace( "No deletion, because retention count is higher than actual number of artifacts." ); return; } ArchivaItemSelector.Builder selectorBuilder = ArchivaItemSelector.builder( ) .withNamespace( artifact.getVersion( ).getProject( ).getNamespace( ).getId( ) ) .withProjectId( artifact.getVersion( ).getProject( ).getId( ) ) .withArtifactId( artifact.getId( ) ) .withClassifier( "*" ) .includeRelatedArtifacts( ) .withVersion( artifact.getVersion( ).getId( ) ); int countToPurge = versions.size( ) - retentionCount; Set<Artifact> artifactsToDelete = new HashSet<>( ); for ( String version : versions ) { if ( countToPurge-- <= 0 ) { break; } List<? extends Artifact> delArtifacts = repository.getLayout( BaseRepositoryContentLayout.class ).getArtifacts( selectorBuilder.withArtifactVersion( version ).build( ) ); if ( delArtifacts != null && delArtifacts.size( ) > 0 ) { artifactsToDelete.addAll( delArtifacts ); } } purge( artifactsToDelete ); } } catch ( LayoutException le ) { throw new RepositoryPurgeException( le.getMessage( ), le ); } catch ( ContentAccessException e ) { log.error( "Error while accessing the repository data: {}", e.getMessage( ), e ); throw new RepositoryPurgeException( e.getMessage( ), e ); } } }
RetentionCountRepositoryPurge extends AbstractRepositoryPurge { @Override public void process( String path ) throws RepositoryPurgeException { try { ContentItem item = repository.toItem( path ); BaseRepositoryContentLayout layout = repository.getLayout( BaseRepositoryContentLayout.class ); Artifact artifact = layout.adaptItem( Artifact.class, item ); if ( !artifact.exists( ) ) { return; } if ( VersionUtil.isSnapshot( artifact.getVersion( ).getId( ) ) ) { ArchivaItemSelector selector = ArchivaItemSelector.builder( ) .withNamespace( artifact.getVersion( ).getProject( ).getNamespace( ).getId( ) ) .withProjectId( artifact.getVersion( ).getProject( ).getId( ) ) .withArtifactId( artifact.getId( ) ) .withVersion( artifact.getVersion( ).getId( ) ) .withClassifier( "*" ) .includeRelatedArtifacts( ) .build( ); List<String> versions; try ( Stream<? extends Artifact> stream = repository.getLayout( BaseRepositoryContentLayout.class ).newArtifactStream( selector ) ) { versions = stream.map( a -> a.getArtifactVersion( ) ) .filter( StringUtils::isNotEmpty ) .distinct( ) .collect( Collectors.toList( ) ); } Collections.sort( versions, VersionComparator.getInstance( ) ); if ( retentionCount > versions.size( ) ) { log.trace( "No deletion, because retention count is higher than actual number of artifacts." ); return; } ArchivaItemSelector.Builder selectorBuilder = ArchivaItemSelector.builder( ) .withNamespace( artifact.getVersion( ).getProject( ).getNamespace( ).getId( ) ) .withProjectId( artifact.getVersion( ).getProject( ).getId( ) ) .withArtifactId( artifact.getId( ) ) .withClassifier( "*" ) .includeRelatedArtifacts( ) .withVersion( artifact.getVersion( ).getId( ) ); int countToPurge = versions.size( ) - retentionCount; Set<Artifact> artifactsToDelete = new HashSet<>( ); for ( String version : versions ) { if ( countToPurge-- <= 0 ) { break; } List<? extends Artifact> delArtifacts = repository.getLayout( BaseRepositoryContentLayout.class ).getArtifacts( selectorBuilder.withArtifactVersion( version ).build( ) ); if ( delArtifacts != null && delArtifacts.size( ) > 0 ) { artifactsToDelete.addAll( delArtifacts ); } } purge( artifactsToDelete ); } } catch ( LayoutException le ) { throw new RepositoryPurgeException( le.getMessage( ), le ); } catch ( ContentAccessException e ) { log.error( "Error while accessing the repository data: {}", e.getMessage( ), e ); throw new RepositoryPurgeException( e.getMessage( ), e ); } } RetentionCountRepositoryPurge( ManagedRepositoryContent repository, int retentionCount,
                                          RepositorySession repositorySession, List<RepositoryListener> listeners ); }
RetentionCountRepositoryPurge extends AbstractRepositoryPurge { @Override public void process( String path ) throws RepositoryPurgeException { try { ContentItem item = repository.toItem( path ); BaseRepositoryContentLayout layout = repository.getLayout( BaseRepositoryContentLayout.class ); Artifact artifact = layout.adaptItem( Artifact.class, item ); if ( !artifact.exists( ) ) { return; } if ( VersionUtil.isSnapshot( artifact.getVersion( ).getId( ) ) ) { ArchivaItemSelector selector = ArchivaItemSelector.builder( ) .withNamespace( artifact.getVersion( ).getProject( ).getNamespace( ).getId( ) ) .withProjectId( artifact.getVersion( ).getProject( ).getId( ) ) .withArtifactId( artifact.getId( ) ) .withVersion( artifact.getVersion( ).getId( ) ) .withClassifier( "*" ) .includeRelatedArtifacts( ) .build( ); List<String> versions; try ( Stream<? extends Artifact> stream = repository.getLayout( BaseRepositoryContentLayout.class ).newArtifactStream( selector ) ) { versions = stream.map( a -> a.getArtifactVersion( ) ) .filter( StringUtils::isNotEmpty ) .distinct( ) .collect( Collectors.toList( ) ); } Collections.sort( versions, VersionComparator.getInstance( ) ); if ( retentionCount > versions.size( ) ) { log.trace( "No deletion, because retention count is higher than actual number of artifacts." ); return; } ArchivaItemSelector.Builder selectorBuilder = ArchivaItemSelector.builder( ) .withNamespace( artifact.getVersion( ).getProject( ).getNamespace( ).getId( ) ) .withProjectId( artifact.getVersion( ).getProject( ).getId( ) ) .withArtifactId( artifact.getId( ) ) .withClassifier( "*" ) .includeRelatedArtifacts( ) .withVersion( artifact.getVersion( ).getId( ) ); int countToPurge = versions.size( ) - retentionCount; Set<Artifact> artifactsToDelete = new HashSet<>( ); for ( String version : versions ) { if ( countToPurge-- <= 0 ) { break; } List<? extends Artifact> delArtifacts = repository.getLayout( BaseRepositoryContentLayout.class ).getArtifacts( selectorBuilder.withArtifactVersion( version ).build( ) ); if ( delArtifacts != null && delArtifacts.size( ) > 0 ) { artifactsToDelete.addAll( delArtifacts ); } } purge( artifactsToDelete ); } } catch ( LayoutException le ) { throw new RepositoryPurgeException( le.getMessage( ), le ); } catch ( ContentAccessException e ) { log.error( "Error while accessing the repository data: {}", e.getMessage( ), e ); throw new RepositoryPurgeException( e.getMessage( ), e ); } } RetentionCountRepositoryPurge( ManagedRepositoryContent repository, int retentionCount,
                                          RepositorySession repositorySession, List<RepositoryListener> listeners ); @Override void process( String path ); }
RetentionCountRepositoryPurge extends AbstractRepositoryPurge { @Override public void process( String path ) throws RepositoryPurgeException { try { ContentItem item = repository.toItem( path ); BaseRepositoryContentLayout layout = repository.getLayout( BaseRepositoryContentLayout.class ); Artifact artifact = layout.adaptItem( Artifact.class, item ); if ( !artifact.exists( ) ) { return; } if ( VersionUtil.isSnapshot( artifact.getVersion( ).getId( ) ) ) { ArchivaItemSelector selector = ArchivaItemSelector.builder( ) .withNamespace( artifact.getVersion( ).getProject( ).getNamespace( ).getId( ) ) .withProjectId( artifact.getVersion( ).getProject( ).getId( ) ) .withArtifactId( artifact.getId( ) ) .withVersion( artifact.getVersion( ).getId( ) ) .withClassifier( "*" ) .includeRelatedArtifacts( ) .build( ); List<String> versions; try ( Stream<? extends Artifact> stream = repository.getLayout( BaseRepositoryContentLayout.class ).newArtifactStream( selector ) ) { versions = stream.map( a -> a.getArtifactVersion( ) ) .filter( StringUtils::isNotEmpty ) .distinct( ) .collect( Collectors.toList( ) ); } Collections.sort( versions, VersionComparator.getInstance( ) ); if ( retentionCount > versions.size( ) ) { log.trace( "No deletion, because retention count is higher than actual number of artifacts." ); return; } ArchivaItemSelector.Builder selectorBuilder = ArchivaItemSelector.builder( ) .withNamespace( artifact.getVersion( ).getProject( ).getNamespace( ).getId( ) ) .withProjectId( artifact.getVersion( ).getProject( ).getId( ) ) .withArtifactId( artifact.getId( ) ) .withClassifier( "*" ) .includeRelatedArtifacts( ) .withVersion( artifact.getVersion( ).getId( ) ); int countToPurge = versions.size( ) - retentionCount; Set<Artifact> artifactsToDelete = new HashSet<>( ); for ( String version : versions ) { if ( countToPurge-- <= 0 ) { break; } List<? extends Artifact> delArtifacts = repository.getLayout( BaseRepositoryContentLayout.class ).getArtifacts( selectorBuilder.withArtifactVersion( version ).build( ) ); if ( delArtifacts != null && delArtifacts.size( ) > 0 ) { artifactsToDelete.addAll( delArtifacts ); } } purge( artifactsToDelete ); } } catch ( LayoutException le ) { throw new RepositoryPurgeException( le.getMessage( ), le ); } catch ( ContentAccessException e ) { log.error( "Error while accessing the repository data: {}", e.getMessage( ), e ); throw new RepositoryPurgeException( e.getMessage( ), e ); } } RetentionCountRepositoryPurge( ManagedRepositoryContent repository, int retentionCount,
                                          RepositorySession repositorySession, List<RepositoryListener> listeners ); @Override void process( String path );  }