aboutsummaryrefslogtreecommitdiffstats
path: root/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers
diff options
context:
space:
mode:
authorMartin Stockhammer <martin_s@apache.org>2020-06-11 14:33:00 +0200
committerMartin Stockhammer <martin_s@apache.org>2020-06-11 14:33:00 +0200
commit46fd585f40f0264b8889154e1924eb8df991b0f3 (patch)
tree09408c7375f7e57d0760ef48b0ddae7be768e4ca /archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers
parentb942314aa27330a179acf17300fea6849bf89d4d (diff)
downloadarchiva-46fd585f40f0264b8889154e1924eb8df991b0f3.tar.gz
archiva-46fd585f40f0264b8889154e1924eb8df991b0f3.zip
Improving repository API
Diffstat (limited to 'archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers')
-rw-r--r--archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/archiva/consumers/core/repository/DaysOldRepositoryPurge.java140
-rw-r--r--archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/archiva/consumers/core/repository/RetentionCountRepositoryPurge.java104
2 files changed, 121 insertions, 123 deletions
diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/archiva/consumers/core/repository/DaysOldRepositoryPurge.java b/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/archiva/consumers/core/repository/DaysOldRepositoryPurge.java
index fb42390bb..282f45c41 100644
--- a/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/archiva/consumers/core/repository/DaysOldRepositoryPurge.java
+++ b/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/archiva/consumers/core/repository/DaysOldRepositoryPurge.java
@@ -23,9 +23,9 @@ import org.apache.archiva.common.utils.VersionComparator;
import org.apache.archiva.common.utils.VersionUtil;
import org.apache.archiva.metadata.audit.RepositoryListener;
import org.apache.archiva.metadata.repository.RepositorySession;
-import org.apache.archiva.repository.ManagedRepositoryContent;
-import org.apache.archiva.repository.LayoutException;
import org.apache.archiva.repository.BaseRepositoryContentLayout;
+import org.apache.archiva.repository.LayoutException;
+import org.apache.archiva.repository.ManagedRepositoryContent;
import org.apache.archiva.repository.content.Artifact;
import org.apache.archiva.repository.content.ContentItem;
import org.apache.archiva.repository.content.base.ArchivaItemSelector;
@@ -74,94 +74,94 @@ public class DaysOldRepositoryPurge
{
ContentItem item = repository.toItem( path );
- if ( item instanceof Artifact )
+ Artifact artifactItem = repository.getLayout( BaseRepositoryContentLayout.class ).adaptItem( Artifact.class, item );
+
+ if ( !artifactItem.exists( ) )
{
- Artifact artifactItem = (Artifact) item;
+ return;
+ }
- if ( !artifactItem.exists( ) )
- {
- return;
- }
+ // ArtifactReference artifact = repository.toArtifactReference( path );
- // ArtifactReference artifact = repository.toArtifactReference( path );
-
- 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( ) );
- }
+ Calendar olderThanThisDate = Calendar.getInstance( TimeZone.getTimeZone( "UTC" ) );
+ olderThanThisDate.add( Calendar.DATE, -retentionPeriod );
- Collections.sort( artifactVersions, VersionComparator.getInstance( ) );
+ ArchivaItemSelector selector = ArchivaItemSelector.builder( )
+ .withNamespace( artifactItem.getVersion( ).getProject( ).getNamespace( ).getId( ) )
+ .withProjectId( artifactItem.getVersion( ).getProject( ).getId( ) )
+ .withVersion( artifactItem.getVersion( ).getId( ) )
+ .withClassifier( "*" )
+ .includeRelatedArtifacts( )
+ .build( );
- if ( retentionCount > artifactVersions.size( ) )
- {
- // Done. nothing to do here. skip it.
- return;
- }
+ 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( ) );
- int countToPurge = artifactVersions.size( ) - retentionCount;
+ if ( retentionCount > artifactVersions.size( ) )
+ {
+ // Done. nothing to do here. skip it.
+ 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 )
+ 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 )
{
- if ( countToPurge-- <= 0 )
- {
- break;
- }
+ break;
+ }
- ArchivaItemSelector artifactSelector = artifactSelectorBuilder.withArtifactVersion( version ).build( );
- try
- {
+ ArchivaItemSelector artifactSelector = artifactSelectorBuilder.withArtifactVersion( version ).build( );
+ try
+ {
- // Is this a generic snapshot "1.0-SNAPSHOT" ?
- if ( VersionUtil.isGenericSnapshot( version ) )
+ // Is this a generic snapshot "1.0-SNAPSHOT" ?
+ 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( ) )
{
- 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 );
- }
+ artifactsToDelete.addAll( artifactList );
}
- // Is this a timestamp snapshot "1.0-20070822.123456-42" ?
- else if ( VersionUtil.isUniqueSnapshot( version ) )
- {
- Calendar timestampCal = uniqueSnapshotToCalendar( version );
+ }
+ // Is this a timestamp snapshot "1.0-20070822.123456-42" ?
+ else if ( VersionUtil.isUniqueSnapshot( version ) )
+ {
+ Calendar timestampCal = uniqueSnapshotToCalendar( version );
- if ( timestampCal.getTimeInMillis( ) < olderThanThisDate.getTimeInMillis( ) )
- {
- artifactsToDelete.addAll( repository.getLayout( BaseRepositoryContentLayout.class ).getArtifacts( artifactSelector ) );
- }
+ 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 );
- // continue
}
}
- purge( artifactsToDelete );
+ catch ( IllegalArgumentException e )
+ {
+ log.error( "Bad selector for artifact: {}", e.getMessage( ), e );
+ // continue
+ }
}
+ purge( artifactsToDelete );
}
catch ( LayoutException e )
{
diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/archiva/consumers/core/repository/RetentionCountRepositoryPurge.java b/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/archiva/consumers/core/repository/RetentionCountRepositoryPurge.java
index ec3a242f4..74df6a679 100644
--- a/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/archiva/consumers/core/repository/RetentionCountRepositoryPurge.java
+++ b/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/archiva/consumers/core/repository/RetentionCountRepositoryPurge.java
@@ -21,12 +21,12 @@ package org.apache.archiva.consumers.core.repository;
import org.apache.archiva.common.utils.VersionComparator;
import org.apache.archiva.common.utils.VersionUtil;
+import org.apache.archiva.metadata.audit.RepositoryListener;
import org.apache.archiva.metadata.repository.RepositorySession;
import org.apache.archiva.model.ArtifactReference;
-import org.apache.archiva.repository.ManagedRepositoryContent;
-import org.apache.archiva.repository.LayoutException;
import org.apache.archiva.repository.BaseRepositoryContentLayout;
-import org.apache.archiva.metadata.audit.RepositoryListener;
+import org.apache.archiva.repository.LayoutException;
+import org.apache.archiva.repository.ManagedRepositoryContent;
import org.apache.archiva.repository.content.Artifact;
import org.apache.archiva.repository.content.ContentItem;
import org.apache.archiva.repository.content.base.ArchivaItemSelector;
@@ -61,67 +61,65 @@ public class RetentionCountRepositoryPurge
try
{
ContentItem item = repository.toItem( path );
- if (item instanceof Artifact )
+ BaseRepositoryContentLayout layout = repository.getLayout( BaseRepositoryContentLayout.class );
+ Artifact artifact = layout.adaptItem( Artifact.class, item );
+ if ( !artifact.exists( ) )
{
- Artifact artifact = (Artifact) item;
- if (!artifact.exists()) {
- return;
- }
+ 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( );
+ 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( ) );
- }
+ 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( ) );
- Collections.sort( versions, VersionComparator.getInstance( ) );
+ if ( retentionCount > versions.size( ) )
+ {
+ log.trace( "No deletion, because retention count is higher than actual number of artifacts." );
+ // Done. nothing to do here. skip it.
+ return;
+ }
- if ( retentionCount > versions.size( ) )
+ 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 )
{
- log.trace( "No deletion, because retention count is higher than actual number of artifacts." );
- // Done. nothing to do here. skip it.
- return;
+ break;
}
-
- 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 )
+ List<? extends Artifact> delArtifacts = repository.getLayout( BaseRepositoryContentLayout.class ).getArtifacts( selectorBuilder.withArtifactVersion( version ).build( ) );
+ if ( delArtifacts != null && delArtifacts.size( ) > 0 )
{
- 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 );
- }
+ artifactsToDelete.addAll( delArtifacts );
}
- purge( artifactsToDelete );
}
- } else {
- throw new RepositoryPurgeException( "Bad artifact path " + path );
+ purge( artifactsToDelete );
}
}
catch ( LayoutException le )