From a4e0310100a3ad3d82e90534f83ff3401a9c2a05 Mon Sep 17 00:00:00 2001 From: Brett Porter Date: Fri, 12 Sep 2008 07:13:04 +0000 Subject: [PATCH] [MRM-948] remove direct use of database and indexer from the core consumers git-svn-id: https://svn.apache.org/repos/asf/archiva/branches@694625 13f79535-47bb-0310-9956-ffa450edef68 --- .../archiva-core-consumers/pom.xml | 8 - .../repository/AbstractRepositoryPurge.java | 108 ++--------- ...eanupReleasedSnapshotsRepositoryPurge.java | 50 +++--- .../repository/DaysOldRepositoryPurge.java | 28 ++- .../repository/RepositoryPurgeConsumer.java | 52 ++---- .../RetentionCountRepositoryPurge.java | 22 ++- .../AbstractRepositoryPurgeTest.java | 125 ++----------- ...pReleasedSnapshotsRepositoryPurgeTest.java | 87 ++++----- .../DaysOldRepositoryPurgeTest.java | 94 +++++----- .../RepositoryPurgeConsumerTest.java | 56 +----- .../RetentionCountRepositoryPurgeTest.java | 82 ++++----- ...ceneRepositoryContentIndexFactoryStub.java | 60 ------- .../LuceneRepositoryContentIndexStub.java | 147 --------------- .../RepositoryPurgeConsumerTest.xml | 119 ------------- .../RepositoryContentIndexEventListener.java | 70 ++++++++ ...positoryContentIndexEventListenerTest.java | 167 ++++++++++++++++++ ...epositoryContentIndexEventListenerTest.xml | 22 +++ .../repository/events/RepositoryListener.java | 37 ++++ .../archiva-modules/archiva-database/pom.xml | 4 - .../RepositoryDatabaseEventListener.java | 55 ++++++ .../RepositoryDatabaseEventListenerTest.java | 90 ++++++++++ .../archiva-web/archiva-rss/pom.xml | 1 + .../archiva-web/archiva-webapp/pom.xml | 5 - .../branch-working-notes.txt | 28 +++ 24 files changed, 687 insertions(+), 830 deletions(-) delete mode 100644 archiva-database-decoupling/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/maven/archiva/consumers/core/repository/stubs/LuceneRepositoryContentIndexFactoryStub.java delete mode 100644 archiva-database-decoupling/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/maven/archiva/consumers/core/repository/stubs/LuceneRepositoryContentIndexStub.java create mode 100644 archiva-database-decoupling/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/RepositoryContentIndexEventListener.java create mode 100644 archiva-database-decoupling/archiva-modules/archiva-base/archiva-indexer/src/test/java/org/apache/maven/archiva/indexer/RepositoryContentIndexEventListenerTest.java create mode 100644 archiva-database-decoupling/archiva-modules/archiva-base/archiva-indexer/src/test/resources/org/apache/maven/archiva/indexer/RepositoryContentIndexEventListenerTest.xml create mode 100644 archiva-database-decoupling/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/maven/archiva/repository/events/RepositoryListener.java create mode 100644 archiva-database-decoupling/archiva-modules/archiva-database/src/main/java/org/apache/maven/archiva/database/RepositoryDatabaseEventListener.java create mode 100644 archiva-database-decoupling/archiva-modules/archiva-database/src/test/java/org/apache/maven/archiva/database/RepositoryDatabaseEventListenerTest.java create mode 100644 archiva-database-decoupling/branch-working-notes.txt diff --git a/archiva-database-decoupling/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/pom.xml b/archiva-database-decoupling/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/pom.xml index 8b85df5a0..538c0cd4d 100644 --- a/archiva-database-decoupling/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/pom.xml +++ b/archiva-database-decoupling/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/pom.xml @@ -28,14 +28,6 @@ Archiva Consumers :: Core Consumers - - org.apache.archiva - archiva-database - - - org.apache.archiva - archiva-indexer - org.apache.archiva archiva-configuration diff --git a/archiva-database-decoupling/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/maven/archiva/consumers/core/repository/AbstractRepositoryPurge.java b/archiva-database-decoupling/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/maven/archiva/consumers/core/repository/AbstractRepositoryPurge.java index f5aaf21ad..598375f2e 100644 --- a/archiva-database-decoupling/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/maven/archiva/consumers/core/repository/AbstractRepositoryPurge.java +++ b/archiva-database-decoupling/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/maven/archiva/consumers/core/repository/AbstractRepositoryPurge.java @@ -19,26 +19,16 @@ package org.apache.maven.archiva.consumers.core.repository; * under the License. */ -import org.apache.maven.archiva.database.ArchivaDatabaseException; -import org.apache.maven.archiva.database.ArtifactDAO; -import org.apache.maven.archiva.indexer.RepositoryContentIndex; -import org.apache.maven.archiva.indexer.RepositoryIndexException; -import org.apache.maven.archiva.indexer.bytecode.BytecodeRecord; -import org.apache.maven.archiva.indexer.filecontent.FileContentRecord; -import org.apache.maven.archiva.indexer.hashcodes.HashcodesRecord; -import org.apache.maven.archiva.indexer.lucene.LuceneRepositoryContentRecord; -import org.apache.maven.archiva.model.ArchivaArtifact; -import org.apache.maven.archiva.model.ArtifactReference; -import org.apache.maven.archiva.repository.ManagedRepositoryContent; -import org.apache.maven.archiva.repository.layout.LayoutException; - import java.io.File; import java.io.FilenameFilter; -import java.util.ArrayList; import java.util.List; -import java.util.Map; import java.util.Set; +import org.apache.maven.archiva.model.ArchivaArtifact; +import org.apache.maven.archiva.model.ArtifactReference; +import org.apache.maven.archiva.repository.ManagedRepositoryContent; +import org.apache.maven.archiva.repository.events.RepositoryListener; + /** * Base class for all repository purge tasks. * @@ -47,18 +37,14 @@ import java.util.Set; public abstract class AbstractRepositoryPurge implements RepositoryPurge { - protected ManagedRepositoryContent repository; - - protected ArtifactDAO artifactDao; - - private Map indices; + protected final ManagedRepositoryContent repository; + + protected final List listeners; - public AbstractRepositoryPurge( ManagedRepositoryContent repository, ArtifactDAO artifactDao, - Map indices ) + public AbstractRepositoryPurge( ManagedRepositoryContent repository, List listeners ) { this.repository = repository; - this.artifactDao = artifactDao; - this.indices = indices; + this.listeners = listeners; } /** @@ -98,10 +84,6 @@ public abstract class AbstractRepositoryPurge { if( references != null && !references.isEmpty() ) { - List fileContentRecords = new ArrayList(); - List hashcodeRecords = new ArrayList(); - List bytecodeRecords = new ArrayList(); - for ( ArtifactReference reference : references ) { File artifactFile = repository.toFile( reference ); @@ -110,46 +92,14 @@ public abstract class AbstractRepositoryPurge new ArchivaArtifact( reference.getGroupId(), reference.getArtifactId(), reference.getVersion(), reference.getClassifier(), reference.getType() ); - FileContentRecord fileContentRecord = new FileContentRecord(); - fileContentRecord.setFilename( repository.toPath( artifact ) ); - fileContentRecords.add( fileContentRecord ); - - HashcodesRecord hashcodesRecord = new HashcodesRecord(); - hashcodesRecord.setArtifact( artifact ); - hashcodeRecords.add( hashcodesRecord ); - - BytecodeRecord bytecodeRecord = new BytecodeRecord(); - bytecodeRecord.setArtifact( artifact ); - bytecodeRecords.add( bytecodeRecord ); - + for ( RepositoryListener listener : listeners ) + { + listener.deleteArtifact( repository, artifact ); + } + // TODO: this needs to be logged artifactFile.delete(); purgeSupportFiles( artifactFile ); - - // intended to be swallowed - // continue updating the database for all artifacts - try - { - String artifactPath = toRelativePath( artifactFile ); - updateDatabase( artifactPath ); - } - catch ( ArchivaDatabaseException ae ) - { - // TODO: determine logging to be used - } - catch ( LayoutException le ) - { - // Ignore - } - } - - try - { - updateIndices( fileContentRecords, hashcodeRecords, bytecodeRecords ); - } - catch ( RepositoryIndexException e ) - { - // Ignore } } } @@ -186,32 +136,4 @@ public abstract class AbstractRepositoryPurge } } } - - private void updateDatabase( String path ) - throws ArchivaDatabaseException, LayoutException - { - ArtifactReference artifact = repository.toArtifactReference( path ); - ArchivaArtifact queriedArtifact = - artifactDao.getArtifact( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), - artifact.getClassifier(), artifact.getType() ); - - artifactDao.deleteArtifact( queriedArtifact ); - - // TODO [MRM-37]: re-run the database consumers to clean up - } - - private void updateIndices( List fileContentRecords, - List hashcodeRecords, - List bytecodeRecords ) - throws RepositoryIndexException - { - RepositoryContentIndex index = indices.get( "filecontent" ); - index.deleteRecords( fileContentRecords ); - - index = indices.get( "hashcodes" ); - index.deleteRecords( hashcodeRecords ); - - index = indices.get( "bytecode" ); - index.deleteRecords( bytecodeRecords ); - } } diff --git a/archiva-database-decoupling/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/maven/archiva/consumers/core/repository/CleanupReleasedSnapshotsRepositoryPurge.java b/archiva-database-decoupling/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/maven/archiva/consumers/core/repository/CleanupReleasedSnapshotsRepositoryPurge.java index b9c1795a7..3bfba763b 100644 --- a/archiva-database-decoupling/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/maven/archiva/consumers/core/repository/CleanupReleasedSnapshotsRepositoryPurge.java +++ b/archiva-database-decoupling/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/maven/archiva/consumers/core/repository/CleanupReleasedSnapshotsRepositoryPurge.java @@ -19,12 +19,17 @@ package org.apache.maven.archiva.consumers.core.repository; * under the License. */ +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + import org.apache.maven.archiva.common.utils.VersionComparator; import org.apache.maven.archiva.common.utils.VersionUtil; import org.apache.maven.archiva.configuration.ArchivaConfiguration; import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration; -import org.apache.maven.archiva.database.ArtifactDAO; -import org.apache.maven.archiva.indexer.RepositoryContentIndex; +import org.apache.maven.archiva.model.ArchivaArtifact; import org.apache.maven.archiva.model.ArtifactReference; import org.apache.maven.archiva.model.ProjectReference; import org.apache.maven.archiva.model.VersionedReference; @@ -33,17 +38,11 @@ import org.apache.maven.archiva.repository.ManagedRepositoryContent; import org.apache.maven.archiva.repository.RepositoryContentFactory; import org.apache.maven.archiva.repository.RepositoryException; import org.apache.maven.archiva.repository.RepositoryNotFoundException; +import org.apache.maven.archiva.repository.events.RepositoryListener; import org.apache.maven.archiva.repository.layout.LayoutException; import org.apache.maven.archiva.repository.metadata.MetadataTools; import org.apache.maven.archiva.repository.metadata.RepositoryMetadataException; -import java.io.File; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Map; - /** *

* This will look in a single managed repository, and purge any snapshots that are present @@ -78,11 +77,12 @@ public class CleanupReleasedSnapshotsRepositoryPurge private RepositoryContentFactory repoContentFactory; - public CleanupReleasedSnapshotsRepositoryPurge( ManagedRepositoryContent repository, ArtifactDAO artifactDao, - MetadataTools metadataTools, Map indices, - ArchivaConfiguration archivaConfig, RepositoryContentFactory repoContentFactory ) + public CleanupReleasedSnapshotsRepositoryPurge( ManagedRepositoryContent repository, MetadataTools metadataTools, + ArchivaConfiguration archivaConfig, + RepositoryContentFactory repoContentFactory, + List listeners ) { - super( repository, artifactDao, indices ); + super( repository, listeners ); this.metadataTools = metadataTools; this.archivaConfig = archivaConfig; this.repoContentFactory = repoContentFactory; @@ -101,17 +101,17 @@ public class CleanupReleasedSnapshotsRepositoryPurge return; } - ArtifactReference artifact = repository.toArtifactReference( path ); + ArtifactReference artifactRef = repository.toArtifactReference( path ); - if ( !VersionUtil.isSnapshot( artifact.getVersion() ) ) + if ( !VersionUtil.isSnapshot( artifactRef.getVersion() ) ) { // Nothing to do here, not a snapshot, skip it. return; } ProjectReference reference = new ProjectReference(); - reference.setGroupId( artifact.getGroupId() ); - reference.setArtifactId( artifact.getArtifactId() ); + reference.setGroupId( artifactRef.getGroupId() ); + reference.setArtifactId( artifactRef.getArtifactId() ); // Gather up all of the versions. List allVersions = new ArrayList( repository.getVersions( reference ) ); @@ -161,8 +161,12 @@ public class CleanupReleasedSnapshotsRepositoryPurge boolean needsMetadataUpdate = false; VersionedReference versionRef = new VersionedReference(); - versionRef.setGroupId( artifact.getGroupId() ); - versionRef.setArtifactId( artifact.getArtifactId() ); + versionRef.setGroupId( artifactRef.getGroupId() ); + versionRef.setArtifactId( artifactRef.getArtifactId() ); + + ArchivaArtifact artifact = + new ArchivaArtifact( artifactRef.getGroupId(), artifactRef.getArtifactId(), artifactRef.getVersion(), + artifactRef.getClassifier(), artifactRef.getType() ); for ( String version : snapshotVersions ) { @@ -170,13 +174,19 @@ public class CleanupReleasedSnapshotsRepositoryPurge { versionRef.setVersion( version ); repository.deleteVersion( versionRef ); + + for ( RepositoryListener listener : listeners ) + { + listener.deleteArtifact( repository, artifact ); + } + needsMetadataUpdate = true; } } if ( needsMetadataUpdate ) { - updateMetadata( artifact ); + updateMetadata( artifactRef ); } } catch ( LayoutException e ) diff --git a/archiva-database-decoupling/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/maven/archiva/consumers/core/repository/DaysOldRepositoryPurge.java b/archiva-database-decoupling/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/maven/archiva/consumers/core/repository/DaysOldRepositoryPurge.java index a844128c3..415fa6082 100644 --- a/archiva-database-decoupling/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/maven/archiva/consumers/core/repository/DaysOldRepositoryPurge.java +++ b/archiva-database-decoupling/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/maven/archiva/consumers/core/repository/DaysOldRepositoryPurge.java @@ -19,17 +19,6 @@ package org.apache.maven.archiva.consumers.core.repository; * under the License. */ -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; - import java.io.File; import java.text.ParseException; import java.text.SimpleDateFormat; @@ -38,10 +27,19 @@ 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; +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.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.events.RepositoryListener; +import org.apache.maven.archiva.repository.layout.LayoutException; + /** * Purge from repository all snapshots older than the specified days in the repository configuration. * @@ -56,10 +54,10 @@ public class DaysOldRepositoryPurge private int retentionCount; - public DaysOldRepositoryPurge( ManagedRepositoryContent repository, ArtifactDAO artifactDao, int daysOlder, - int retentionCount, Map indices ) + public DaysOldRepositoryPurge( ManagedRepositoryContent repository, int daysOlder, + int retentionCount, List listeners ) { - super( repository, artifactDao, indices ); + super( repository, listeners ); this.daysOlder = daysOlder; this.retentionCount = retentionCount; timestampParser = new SimpleDateFormat( "yyyyMMdd.HHmmss" ); diff --git a/archiva-database-decoupling/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/maven/archiva/consumers/core/repository/RepositoryPurgeConsumer.java b/archiva-database-decoupling/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/maven/archiva/consumers/core/repository/RepositoryPurgeConsumer.java index ad13411bb..d388814c9 100644 --- a/archiva-database-decoupling/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/maven/archiva/consumers/core/repository/RepositoryPurgeConsumer.java +++ b/archiva-database-decoupling/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/maven/archiva/consumers/core/repository/RepositoryPurgeConsumer.java @@ -19,6 +19,10 @@ package org.apache.maven.archiva.consumers.core.repository; * under the License. */ +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + import org.apache.maven.archiva.configuration.ArchivaConfiguration; import org.apache.maven.archiva.configuration.ConfigurationNames; import org.apache.maven.archiva.configuration.FileTypes; @@ -26,24 +30,18 @@ import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration; import org.apache.maven.archiva.consumers.AbstractMonitoredConsumer; import org.apache.maven.archiva.consumers.ConsumerException; import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer; -import org.apache.maven.archiva.database.ArchivaDAO; -import org.apache.maven.archiva.indexer.RepositoryContentIndex; -import org.apache.maven.archiva.indexer.RepositoryContentIndexFactory; import org.apache.maven.archiva.repository.ManagedRepositoryContent; import org.apache.maven.archiva.repository.RepositoryContentFactory; import org.apache.maven.archiva.repository.RepositoryException; import org.apache.maven.archiva.repository.RepositoryNotFoundException; +import org.apache.maven.archiva.repository.events.RepositoryListener; import org.apache.maven.archiva.repository.metadata.MetadataTools; import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable; import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException; import org.codehaus.plexus.registry.Registry; import org.codehaus.plexus.registry.RegistryListener; -import java.util.ArrayList; -import java.util.Date; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import edu.emory.mathcs.backport.java.util.Collections; /** * Consumer for removing old snapshots in the repository based on the criteria @@ -75,11 +73,6 @@ public class RepositoryPurgeConsumer */ private ArchivaConfiguration configuration; - /** - * @plexus.requirement role-hint="jdo" - */ - private ArchivaDAO dao; - /** * @plexus.requirement */ @@ -97,19 +90,15 @@ public class RepositoryPurgeConsumer private List includes = new ArrayList(); - private List propertyNameTriggers = new ArrayList(); - private RepositoryPurge repoPurge; private RepositoryPurge cleanUp; private boolean deleteReleasedSnapshots; - - /** - * @plexus.requirement role-hint="lucene" - */ - private RepositoryContentIndexFactory indexFactory; + /** @plexus.requirement role="org.apache.maven.archiva.repository.events.RepositoryListener" */ + private List listeners = Collections.emptyList(); + public String getId() { return this.id; @@ -140,27 +129,23 @@ public class RepositoryPurgeConsumer { try { - Map indices = new HashMap(); - indices.put( "bytecode", indexFactory.createBytecodeIndex( repository ) ); - indices.put( "hashcodes", indexFactory.createHashcodeIndex( repository ) ); - indices.put( "filecontent", indexFactory.createFileContentIndex( repository ) ); - ManagedRepositoryContent repositoryContent = repositoryFactory.getManagedRepositoryContent( repository .getId() ); if ( repository.getDaysOlder() != 0 ) { - repoPurge = new DaysOldRepositoryPurge( repositoryContent, dao.getArtifactDAO(), repository - .getDaysOlder(), repository.getRetentionCount(), indices ); + repoPurge = new DaysOldRepositoryPurge( repositoryContent, repository.getDaysOlder(), + repository.getRetentionCount(), listeners ); } else { - repoPurge = new RetentionCountRepositoryPurge( repositoryContent, dao.getArtifactDAO(), repository - .getRetentionCount(), indices ); + repoPurge = new RetentionCountRepositoryPurge( repositoryContent, repository.getRetentionCount(), + listeners ); } - cleanUp = new CleanupReleasedSnapshotsRepositoryPurge( repositoryContent, dao.getArtifactDAO(), - metadataTools, indices, configuration, repositoryFactory ); + cleanUp = + new CleanupReleasedSnapshotsRepositoryPurge( repositoryContent, metadataTools, configuration, + repositoryFactory, listeners ); deleteReleasedSnapshots = repository.isDeleteReleasedSnapshots(); } @@ -230,9 +215,4 @@ public class RepositoryPurgeConsumer // we need to check all files for deletion, especially if not modified return true; } - - public void setRepositoryContentIndexFactory( RepositoryContentIndexFactory indexFactory ) - { - this.indexFactory = indexFactory; - } } diff --git a/archiva-database-decoupling/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/maven/archiva/consumers/core/repository/RetentionCountRepositoryPurge.java b/archiva-database-decoupling/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/maven/archiva/consumers/core/repository/RetentionCountRepositoryPurge.java index 20c728f00..2580c3d96 100644 --- a/archiva-database-decoupling/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/maven/archiva/consumers/core/repository/RetentionCountRepositoryPurge.java +++ b/archiva-database-decoupling/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/maven/archiva/consumers/core/repository/RetentionCountRepositoryPurge.java @@ -19,23 +19,21 @@ package org.apache.maven.archiva.consumers.core.repository; * under the License. */ +import java.io.File; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Set; + 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.events.RepositoryListener; import org.apache.maven.archiva.repository.layout.LayoutException; -import java.io.File; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Map; -import java.util.Set; - /** * Purge the repository by retention count. Retain only the specified number of snapshots. * @@ -46,10 +44,10 @@ public class RetentionCountRepositoryPurge { private int retentionCount; - public RetentionCountRepositoryPurge( ManagedRepositoryContent repository, ArtifactDAO artifactDao, - int retentionCount, Map indices ) + public RetentionCountRepositoryPurge( ManagedRepositoryContent repository, + int retentionCount, List listeners ) { - super( repository, artifactDao, indices ); + super( repository, listeners ); this.retentionCount = retentionCount; } diff --git a/archiva-database-decoupling/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/maven/archiva/consumers/core/repository/AbstractRepositoryPurgeTest.java b/archiva-database-decoupling/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/maven/archiva/consumers/core/repository/AbstractRepositoryPurgeTest.java index 5d0869942..7335d4694 100644 --- a/archiva-database-decoupling/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/maven/archiva/consumers/core/repository/AbstractRepositoryPurgeTest.java +++ b/archiva-database-decoupling/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/maven/archiva/consumers/core/repository/AbstractRepositoryPurgeTest.java @@ -19,28 +19,16 @@ package org.apache.maven.archiva.consumers.core.repository; * under the License. */ +import java.io.File; +import java.io.IOException; + import org.apache.commons.io.FileUtils; import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration; -import org.apache.maven.archiva.database.ArchivaDatabaseException; -import org.apache.maven.archiva.database.ArtifactDAO; import org.apache.maven.archiva.model.ArchivaArtifact; import org.apache.maven.archiva.repository.ManagedRepositoryContent; -import org.codehaus.plexus.jdo.DefaultConfigurableJdoFactory; -import org.codehaus.plexus.jdo.JdoFactory; +import org.apache.maven.archiva.repository.events.RepositoryListener; import org.codehaus.plexus.spring.PlexusInSpringTestCase; -import org.jpox.SchemaTool; - -import java.io.File; -import java.io.IOException; -import java.net.URL; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; -import java.util.Properties; -import java.util.Map.Entry; - -import javax.jdo.PersistenceManager; -import javax.jdo.PersistenceManagerFactory; +import org.easymock.MockControl; /** * @author Maria Odea Ching @@ -74,74 +62,21 @@ public abstract class AbstractRepositoryPurgeTest private ManagedRepositoryContent repo; - protected ArtifactDAO dao; - protected RepositoryPurge repoPurge; + protected MockControl listenerControl; + + protected RepositoryListener listener; + + @Override protected void setUp() throws Exception { super.setUp(); + + listenerControl = MockControl.createControl( RepositoryListener.class ); - DefaultConfigurableJdoFactory jdoFactory = (DefaultConfigurableJdoFactory) lookup( JdoFactory.ROLE, "archiva" ); - assertEquals( DefaultConfigurableJdoFactory.class.getName(), jdoFactory.getClass().getName() ); - - jdoFactory.setPersistenceManagerFactoryClass( "org.jpox.PersistenceManagerFactoryImpl" ); - - jdoFactory.setDriverName( System.getProperty( "jdo.test.driver", "org.hsqldb.jdbcDriver" ) ); - jdoFactory.setUrl( System.getProperty( "jdo.test.url", "jdbc:hsqldb:mem:testdb" ) ); - - jdoFactory.setUserName( System.getProperty( "jdo.test.user", "sa" ) ); - - jdoFactory.setPassword( System.getProperty( "jdo.test.pass", "" ) ); - - jdoFactory.setProperty( "org.jpox.transactionIsolation", "READ_COMMITTED" ); - - jdoFactory.setProperty( "org.jpox.poid.transactionIsolation", "READ_COMMITTED" ); - - jdoFactory.setProperty( "org.jpox.autoCreateSchema", "true" ); - - jdoFactory.setProperty( "javax.jdo.option.RetainValues", "true" ); - - jdoFactory.setProperty( "javax.jdo.option.RestoreValues", "true" ); - - // jdoFactory.setProperty( "org.jpox.autoCreateColumns", "true" ); - - jdoFactory.setProperty( "org.jpox.validateTables", "true" ); - - jdoFactory.setProperty( "org.jpox.validateColumns", "true" ); - - jdoFactory.setProperty( "org.jpox.validateConstraints", "true" ); - - Properties properties = jdoFactory.getProperties(); - - for ( Entry entry : properties.entrySet() ) - { - System.setProperty( (String) entry.getKey(), (String) entry.getValue() ); - } - - URL jdoFileUrls[] = new URL[] { getClass().getResource( "/org/apache/maven/archiva/model/package.jdo" ) }; - - if ( ( jdoFileUrls == null ) || ( jdoFileUrls[0] == null ) ) - { - fail( "Unable to process test " + getName() + " - missing package.jdo." ); - } - - File propsFile = null; // intentional - boolean verbose = true; - - SchemaTool.deleteSchemaTables( jdoFileUrls, new URL[] {}, propsFile, verbose ); - SchemaTool.createSchemaTables( jdoFileUrls, new URL[] {}, propsFile, verbose, null ); - - PersistenceManagerFactory pmf = jdoFactory.getPersistenceManagerFactory(); - - assertNotNull( pmf ); - - PersistenceManager pm = pmf.getPersistenceManager(); - - pm.close(); - - dao = (ArtifactDAO) lookup( ArtifactDAO.class.getName(), "jdo" ); + listener = (RepositoryListener) listenerControl.getMock(); } @Override @@ -180,28 +115,6 @@ public abstract class AbstractRepositoryPurgeTest return repo; } - protected void populateDb( String groupId, String artifactId, List versions ) - throws ArchivaDatabaseException - { - for ( String version : versions ) - { - ArchivaArtifact artifact = dao.createArtifact( groupId, artifactId, version, "", "jar" ); - assertNotNull( artifact ); - artifact.getModel().setLastModified( new Date() ); - artifact.getModel().setOrigin( "test" ); - ArchivaArtifact savedArtifact = dao.saveArtifact( artifact ); - assertNotNull( savedArtifact ); - - //POM - artifact = dao.createArtifact( groupId, artifactId, version, "", "pom" ); - assertNotNull( artifact ); - artifact.getModel().setLastModified( new Date() ); - artifact.getModel().setOrigin( "test" ); - savedArtifact = dao.saveArtifact( artifact ); - assertNotNull( savedArtifact ); - } - } - protected void assertDeleted( String path ) { assertFalse( "File should have been deleted: " + path, new File( path ).exists() ); @@ -230,15 +143,9 @@ public abstract class AbstractRepositoryPurgeTest return testDir.getAbsolutePath(); } - - protected void populateDbForTestOrderOfDeletion() - throws Exception + + protected ArchivaArtifact createArtifact( String groupId, String artifactId, String version, String type ) { - List versions = new ArrayList(); - versions.add( "1.1.2-20070427.065136-1" ); - versions.add( "1.1.2-20070506.163513-2" ); - versions.add( "1.1.2-20070615.105019-3" ); - - populateDb( "org.apache.maven.plugins", "maven-assembly-plugin", versions ); + return new ArchivaArtifact( groupId, artifactId, version, null, type ); } } diff --git a/archiva-database-decoupling/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/maven/archiva/consumers/core/repository/CleanupReleasedSnapshotsRepositoryPurgeTest.java b/archiva-database-decoupling/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/maven/archiva/consumers/core/repository/CleanupReleasedSnapshotsRepositoryPurgeTest.java index b133c54d3..50956d265 100644 --- a/archiva-database-decoupling/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/maven/archiva/consumers/core/repository/CleanupReleasedSnapshotsRepositoryPurgeTest.java +++ b/archiva-database-decoupling/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/maven/archiva/consumers/core/repository/CleanupReleasedSnapshotsRepositoryPurgeTest.java @@ -19,21 +19,18 @@ package org.apache.maven.archiva.consumers.core.repository; * under the License. */ +import java.io.File; +import java.util.Collections; + import org.apache.commons.io.FileUtils; import org.apache.maven.archiva.configuration.ArchivaConfiguration; import org.apache.maven.archiva.configuration.Configuration; -import org.apache.maven.archiva.consumers.core.repository.stubs.LuceneRepositoryContentIndexStub; -import org.apache.maven.archiva.database.ArchivaDatabaseException; -import org.apache.maven.archiva.indexer.RepositoryContentIndex; import org.apache.maven.archiva.repository.RepositoryContentFactory; +import org.apache.maven.archiva.repository.events.RepositoryListener; import org.apache.maven.archiva.repository.metadata.MetadataTools; import org.custommonkey.xmlunit.XMLAssert; +import org.easymock.MockControl; -import java.io.File; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; /** * @author Maria Odea Ching @@ -42,6 +39,8 @@ public class CleanupReleasedSnapshotsRepositoryPurgeTest extends AbstractRepositoryPurgeTest { private ArchivaConfiguration archivaConfiguration; + + private MockControl listenerControl; public static final String PATH_TO_RELEASED_SNAPSHOT_IN_DIFF_REPO = "org/apache/archiva/released-artifact-in-diff-repo/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar"; @@ -49,40 +48,45 @@ public class CleanupReleasedSnapshotsRepositoryPurgeTest public static final String PATH_TO_HIGHER_SNAPSHOT_EXISTS_IN_SAME_REPO = "org/apache/maven/plugins/maven-source-plugin/2.0.3-SNAPSHOT/maven-source-plugin-2.0.3-SNAPSHOT.jar"; public static final String PATH_TO_RELEASED_SNAPSHOT_IN_SAME_REPO = "org/apache/maven/plugins/maven-plugin-plugin/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar"; + + private RepositoryListener listener; protected void setUp() throws Exception { - super.setUp(); - - Map map = new HashMap(); - map.put( "filecontent", new LuceneRepositoryContentIndexStub() ); - map.put( "hashcodes", new LuceneRepositoryContentIndexStub() ); - map.put( "bytecode", new LuceneRepositoryContentIndexStub() ); + super.setUp(); MetadataTools metadataTools = (MetadataTools) lookup( MetadataTools.class ); RepositoryContentFactory factory = (RepositoryContentFactory) lookup( RepositoryContentFactory.class, "cleanup-released-snapshots"); archivaConfiguration = (ArchivaConfiguration) lookup( ArchivaConfiguration.class, "cleanup-released-snapshots" ); - + + listenerControl = MockControl.createControl( RepositoryListener.class ); + + listener = (RepositoryListener) listenerControl.getMock(); repoPurge = - new CleanupReleasedSnapshotsRepositoryPurge( getRepository(), dao, metadataTools, map, archivaConfiguration, factory ); + new CleanupReleasedSnapshotsRepositoryPurge( getRepository(), metadataTools, archivaConfiguration, factory, + Collections.singletonList( listener ) ); } public void testReleasedSnapshotsExistsInSameRepo() throws Exception { - Configuration config = archivaConfiguration.getConfiguration(); config.removeManagedRepository( config.findManagedRepositoryById( TEST_REPO_ID ) ); config.addManagedRepository( getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ) ); - populateReleasedSnapshotsTest(); - String repoRoot = prepareTestRepos(); + // test listeners for the correct artifacts + listener.deleteArtifact( getRepository(), createArtifact( "org.apache.maven.plugins", "maven-plugin-plugin", + "2.3-SNAPSHOT", "maven-plugin" ) ); + listenerControl.replay(); + repoPurge.process( CleanupReleasedSnapshotsRepositoryPurgeTest.PATH_TO_RELEASED_SNAPSHOT_IN_SAME_REPO ); + + listenerControl.verify(); String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-plugin-plugin"; @@ -130,12 +134,18 @@ public class CleanupReleasedSnapshotsRepositoryPurgeTest config.addManagedRepository( getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ) ); config.addManagedRepository( getRepoConfiguration( RELEASES_TEST_REPO_ID, RELEASES_TEST_REPO_NAME ) ); - populateReleasedSnapshotsTestInDiffRepo(); - String repoRoot = prepareTestRepos(); + // test listeners for the correct artifacts + listener.deleteArtifact( getRepository(), createArtifact( "org.apache.archiva", + "released-artifact-in-diff-repo", "1.0-SNAPSHOT", + "jar" ) ); + listenerControl.replay(); + repoPurge.process( PATH_TO_RELEASED_SNAPSHOT_IN_DIFF_REPO ); + listenerControl.verify(); + String projectRoot = repoRoot + "/org/apache/archiva/released-artifact-in-diff-repo"; // check if the snapshot was removed @@ -168,11 +178,14 @@ public class CleanupReleasedSnapshotsRepositoryPurgeTest config.removeManagedRepository( config.findManagedRepositoryById( TEST_REPO_ID ) ); config.addManagedRepository( getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ) ); - populateHigherSnapshotExistsTest(); - String repoRoot = prepareTestRepos(); + // test listeners for the correct artifacts - no deletions + listenerControl.replay(); + repoPurge.process( CleanupReleasedSnapshotsRepositoryPurgeTest.PATH_TO_HIGHER_SNAPSHOT_EXISTS_IN_SAME_REPO ); + + listenerControl.verify(); String projectRoot = repoRoot + "/org/apache/maven/plugins/maven-source-plugin"; @@ -207,32 +220,4 @@ public class CleanupReleasedSnapshotsRepositoryPurgeTest "//metadata/versioning/versions/version", metadataXml ); XMLAssert.assertXpathEvaluatesTo( "20070427033345", "//metadata/versioning/lastUpdated", metadataXml ); } - - private void populateReleasedSnapshotsTest() - throws ArchivaDatabaseException - { - List versions = new ArrayList(); - versions.add( "2.3-SNAPSHOT" ); - - populateDb( "org.apache.maven.plugins", "maven-plugin-plugin", versions ); - } - - private void populateHigherSnapshotExistsTest() - throws Exception - { - List versions = new ArrayList(); - versions.add( "2.0.3-SNAPSHOT" ); - - populateDb( "org.apache.maven.plugins", "maven-source-plugin", versions ); - } - - private void populateReleasedSnapshotsTestInDiffRepo() - throws ArchivaDatabaseException - { - List versions = new ArrayList(); - versions.add( "1.0-SNAPSHOT" ); - - populateDb( "org.apache.archiva", "released-artifact-in-diff-repo", versions ); - } - } diff --git a/archiva-database-decoupling/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/maven/archiva/consumers/core/repository/DaysOldRepositoryPurgeTest.java b/archiva-database-decoupling/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/maven/archiva/consumers/core/repository/DaysOldRepositoryPurgeTest.java index 5d51d71aa..48f630e25 100644 --- a/archiva-database-decoupling/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/maven/archiva/consumers/core/repository/DaysOldRepositoryPurgeTest.java +++ b/archiva-database-decoupling/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/maven/archiva/consumers/core/repository/DaysOldRepositoryPurgeTest.java @@ -19,17 +19,14 @@ package org.apache.maven.archiva.consumers.core.repository; * under the License. */ -import org.apache.commons.lang.time.DateUtils; -import org.apache.maven.archiva.consumers.core.repository.stubs.LuceneRepositoryContentIndexStub; -import org.apache.maven.archiva.indexer.RepositoryContentIndex; - import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Calendar; -import java.util.HashMap; +import java.util.Collections; import java.util.List; -import java.util.Map; + +import org.apache.commons.lang.time.DateUtils; /** * @author Maria Odea Ching @@ -37,9 +34,6 @@ import java.util.Map; public class DaysOldRepositoryPurgeTest extends AbstractRepositoryPurgeTest { - - private Map map; - private static final String[] extensions = new String[] { "-5.jar", "-5.pom", "-6.jar", "-6.pom", "-7.jar", "-7.pom" }; @@ -55,12 +49,6 @@ public class DaysOldRepositoryPurgeTest private String sec; - protected void setUp() - throws Exception - { - super.setUp(); - } - private void setLastModified( String dirPath, long lastModified ) { File dir = new File( dirPath ); @@ -74,14 +62,11 @@ public class DaysOldRepositoryPurgeTest public void testByLastModified() throws Exception { - map = new HashMap(); - map.put( "filecontent", new LuceneRepositoryContentIndexStub( 2 ) ); - map.put( "hashcodes", new LuceneRepositoryContentIndexStub( 2 ) ); - map.put( "bytecode", new LuceneRepositoryContentIndexStub( 2 ) ); - repoPurge = - new DaysOldRepositoryPurge( getRepository(), dao, getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ).getDaysOlder(), - getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ).getRetentionCount(), map ); + new DaysOldRepositoryPurge( getRepository(), + getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ).getDaysOlder(), + getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ).getRetentionCount(), + Collections.singletonList( listener ) ); String repoRoot = prepareTestRepos(); @@ -89,9 +74,16 @@ public class DaysOldRepositoryPurgeTest setLastModified( projectRoot + "/2.2-SNAPSHOT/", 1179382029 ); - populateDbForTestByLastModified(); - + // test listeners for the correct artifacts + listener.deleteArtifact( getRepository(), createArtifact( "org.apache.maven.plugins", "maven-install-plugin", + "2.2-SNAPSHOT", "maven-plugin" ) ); + listener.deleteArtifact( getRepository(), createArtifact( "org.apache.maven.plugins", "maven-install-plugin", + "2.2-SNAPSHOT", "pom" ) ); + listenerControl.replay(); + repoPurge.process( PATH_TO_BY_DAYS_OLD_ARTIFACT ); + + listenerControl.verify(); 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" ); @@ -119,14 +111,10 @@ public class DaysOldRepositoryPurgeTest public void testOrderOfDeletion() throws Exception { - map = new HashMap(); - map.put( "filecontent", new LuceneRepositoryContentIndexStub( 2 ) ); - map.put( "hashcodes", new LuceneRepositoryContentIndexStub( 2 ) ); - map.put( "bytecode", new LuceneRepositoryContentIndexStub( 2 ) ); - repoPurge = - new DaysOldRepositoryPurge( getRepository(), dao, getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ).getDaysOlder(), - getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ).getRetentionCount(), map ); + new DaysOldRepositoryPurge( getRepository(), getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ).getDaysOlder(), + getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ).getRetentionCount(), + Collections.singletonList( listener ) ); String repoRoot = prepareTestRepos(); @@ -134,10 +122,17 @@ public class DaysOldRepositoryPurgeTest setLastModified( projectRoot + "/1.1.2-SNAPSHOT/", 1179382029 ); - populateDbForTestOrderOfDeletion(); - + // test listeners for the correct artifacts + listener.deleteArtifact( getRepository(), createArtifact( "org.apache.maven.plugins", "maven-assembly-plugin", + "1.1.2-20070427.065136-1", "maven-plugin" ) ); + listener.deleteArtifact( getRepository(), createArtifact( "org.apache.maven.plugins", "maven-assembly-plugin", + "1.1.2-20070427.065136-1", "pom" ) ); + listenerControl.replay(); + repoPurge.process( PATH_TO_TEST_ORDER_OF_DELETION ); + listenerControl.verify(); + assertDeleted( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070427.065136-1.jar" ); assertDeleted( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070427.065136-1.jar.sha1" ); assertDeleted( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070427.065136-1.jar.md5" ); @@ -164,14 +159,11 @@ public class DaysOldRepositoryPurgeTest public void testMetadataDrivenSnapshots() throws Exception { - map = new HashMap(); - map.put( "filecontent", new LuceneRepositoryContentIndexStub( 2 ) ); - map.put( "hashcodes", new LuceneRepositoryContentIndexStub( 2 ) ); - map.put( "bytecode", new LuceneRepositoryContentIndexStub( 2 ) ); - repoPurge = - new DaysOldRepositoryPurge( getRepository(), dao, getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ).getDaysOlder(), - getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ).getRetentionCount(), map ); + new DaysOldRepositoryPurge( getRepository(), + getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ).getDaysOlder(), + getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ).getRetentionCount(), + Collections.singletonList( listener ) ); String repoRoot = prepareTestRepos(); @@ -221,10 +213,17 @@ public class DaysOldRepositoryPurgeTest versions.add( "1.4.3-" + year + mon + day + "." + hr + min + sec + "-7" ); versions.add( "1.4.3-SNAPSHOT" ); - populateDb( "org.codehaus.plexus", "plexus-utils", versions ); - + // test listeners for the correct artifacts + listener.deleteArtifact( getRepository(), createArtifact( "org.codehaus.plexus", "plexus-utils", + "1.4.3-20070113.163208-4", "jar" ) ); + listener.deleteArtifact( getRepository(), createArtifact( "org.codehaus.plexus", "plexus-utils", + "1.4.3-20070113.163208-4", "pom" ) ); + listenerControl.replay(); + repoPurge.process( PATH_TO_BY_DAYS_OLD_METADATA_DRIVEN_ARTIFACT ); + listenerControl.verify(); + // this should be deleted since the filename version (timestamp) is older than // 100 days even if the last modified date was <100 days ago assertDeleted( versionRoot + "/plexus-utils-1.4.3-20070113.163208-4.jar" ); @@ -260,15 +259,4 @@ public class DaysOldRepositoryPurgeTest super.tearDown(); repoPurge = null; } - - private void populateDbForTestByLastModified() - throws Exception - { - List versions = new ArrayList(); - versions.add( "2.2-20061118.060401-2" ); - versions.add( "2.2-20070513.034619-5" ); - versions.add( "2.2-SNAPSHOT" ); - - populateDb( "org.apache.maven.plugins", "maven-install-plugin", versions ); - } } diff --git a/archiva-database-decoupling/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/maven/archiva/consumers/core/repository/RepositoryPurgeConsumerTest.java b/archiva-database-decoupling/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/maven/archiva/consumers/core/repository/RepositoryPurgeConsumerTest.java index ca8b63f6c..f7e1c5a15 100644 --- a/archiva-database-decoupling/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/maven/archiva/consumers/core/repository/RepositoryPurgeConsumerTest.java +++ b/archiva-database-decoupling/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/maven/archiva/consumers/core/repository/RepositoryPurgeConsumerTest.java @@ -19,6 +19,8 @@ package org.apache.maven.archiva.consumers.core.repository; * under the License. */ +import java.io.File; + import org.apache.commons.io.FileUtils; import org.apache.maven.archiva.common.utils.BaseFile; import org.apache.maven.archiva.configuration.ArchivaConfiguration; @@ -27,15 +29,9 @@ import org.apache.maven.archiva.configuration.FileType; import org.apache.maven.archiva.configuration.FileTypes; import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration; import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer; -import org.apache.maven.archiva.consumers.core.repository.stubs.LuceneRepositoryContentIndexFactoryStub; -import org.apache.maven.archiva.database.ArchivaDatabaseException; import org.apache.maven.archiva.repository.scanner.functors.ConsumerWantsFilePredicate; import org.custommonkey.xmlunit.XMLAssert; -import java.io.File; -import java.util.ArrayList; -import java.util.List; - /** * @author Maria Odea Ching */ @@ -99,13 +95,6 @@ public class RepositoryPurgeConsumerTest (KnownRepositoryContentConsumer) lookup( KnownRepositoryContentConsumer.class, "repo-purge-consumer-by-retention-count" ); - LuceneRepositoryContentIndexFactoryStub indexFactory = new LuceneRepositoryContentIndexFactoryStub(); - indexFactory.setExpectedRecordsSize( 2 ); - - ( (RepositoryPurgeConsumer) repoPurgeConsumer ).setRepositoryContentIndexFactory( indexFactory ); - - populateDbForRetentionCountTest(); - ManagedRepositoryConfiguration repoConfiguration = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ); repoConfiguration.setDaysOlder( 0 ); // force days older off to allow retention count purge to execute. repoConfiguration.setRetentionCount( TEST_RETENTION_COUNT ); @@ -163,17 +152,10 @@ public class RepositoryPurgeConsumerTest public void testConsumerByDaysOld() throws Exception { - populateDbForDaysOldTest(); - KnownRepositoryContentConsumer repoPurgeConsumer = (KnownRepositoryContentConsumer) lookup( KnownRepositoryContentConsumer.class, "repo-purge-consumer-by-days-old" ); - LuceneRepositoryContentIndexFactoryStub indexFactory = new LuceneRepositoryContentIndexFactoryStub(); - indexFactory.setExpectedRecordsSize( 2 ); - - ( (RepositoryPurgeConsumer) repoPurgeConsumer ).setRepositoryContentIndexFactory( indexFactory ); - ManagedRepositoryConfiguration repoConfiguration = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ); repoConfiguration.setDaysOlder( TEST_DAYS_OLDER ); addRepoToConfiguration( "days-old", repoConfiguration ); @@ -222,8 +204,6 @@ public class RepositoryPurgeConsumerTest (KnownRepositoryContentConsumer) lookup( KnownRepositoryContentConsumer.class, "repo-purge-consumer-by-retention-count" ); - populateDbForReleasedSnapshotsTest(); - ManagedRepositoryConfiguration repoConfiguration = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ); repoConfiguration.setDeleteReleasedSnapshots( false ); // Set to NOT delete released snapshots. addRepoToConfiguration( "retention-count", repoConfiguration ); @@ -265,8 +245,6 @@ public class RepositoryPurgeConsumerTest (KnownRepositoryContentConsumer) lookup( KnownRepositoryContentConsumer.class, "repo-purge-consumer-by-days-old" ); - populateDbForReleasedSnapshotsTest(); - ManagedRepositoryConfiguration repoConfiguration = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ); repoConfiguration.setDeleteReleasedSnapshots( true ); addRepoToConfiguration( "days-old", repoConfiguration ); @@ -301,34 +279,4 @@ public class RepositoryPurgeConsumerTest "//metadata/versioning/versions/version", metadataXml ); XMLAssert.assertXpathEvaluatesTo( "20070315032817", "//metadata/versioning/lastUpdated", metadataXml ); } - - public void populateDbForRetentionCountTest() - throws ArchivaDatabaseException - { - List versions = new ArrayList(); - versions.add( "1.0RC1-20070504.153317-1" ); - versions.add( "1.0RC1-20070504.160758-2" ); - versions.add( "1.0RC1-20070505.090015-3" ); - versions.add( "1.0RC1-20070506.090132-4" ); - - populateDb( "org.jruby.plugins", "jruby-rake-plugin", versions ); - } - - private void populateDbForDaysOldTest() - throws ArchivaDatabaseException - { - List versions = new ArrayList(); - versions.add( "2.2-SNAPSHOT" ); - - populateDb( "org.apache.maven.plugins", "maven-install-plugin", versions ); - } - - public void populateDbForReleasedSnapshotsTest() - throws ArchivaDatabaseException - { - List versions = new ArrayList(); - versions.add( "2.3-SNAPSHOT" ); - - populateDb( "org.apache.maven.plugins", "maven-plugin-plugin", versions ); - } } diff --git a/archiva-database-decoupling/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/maven/archiva/consumers/core/repository/RetentionCountRepositoryPurgeTest.java b/archiva-database-decoupling/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/maven/archiva/consumers/core/repository/RetentionCountRepositoryPurgeTest.java index f67720ccf..e38f712d8 100644 --- a/archiva-database-decoupling/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/maven/archiva/consumers/core/repository/RetentionCountRepositoryPurgeTest.java +++ b/archiva-database-decoupling/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/maven/archiva/consumers/core/repository/RetentionCountRepositoryPurgeTest.java @@ -1,5 +1,7 @@ package org.apache.maven.archiva.consumers.core.repository; +import java.util.Collections; + /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file @@ -19,14 +21,6 @@ package org.apache.maven.archiva.consumers.core.repository; * under the License. */ -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import org.apache.maven.archiva.consumers.core.repository.stubs.LuceneRepositoryContentIndexStub; -import org.apache.maven.archiva.indexer.RepositoryContentIndex; - /** * Test RetentionsCountRepositoryPurgeTest * @@ -41,13 +35,11 @@ public class RetentionCountRepositoryPurgeTest { super.setUp(); - Map map = new HashMap(); - map.put( "filecontent", new LuceneRepositoryContentIndexStub( 2 ) ); - map.put( "hashcodes", new LuceneRepositoryContentIndexStub( 2 ) ); - map.put( "bytecode", new LuceneRepositoryContentIndexStub( 2 ) ); - - repoPurge = new RetentionCountRepositoryPurge( getRepository(), dao, - getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ).getRetentionCount(), map ); + repoPurge = + new RetentionCountRepositoryPurge( + getRepository(), + getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ).getRetentionCount(), + Collections.singletonList( listener ) ); } /** @@ -58,12 +50,23 @@ public class RetentionCountRepositoryPurgeTest public void testIfAJarWasFound() throws Exception { - populateIfJarWasFoundDb(); - String repoRoot = prepareTestRepos(); + // test listeners for the correct artifacts + listener.deleteArtifact( getRepository(), createArtifact( "org.jruby.plugins", "jruby-rake-plugin", + "1.0RC1-20070504.153317-1", "jar" ) ); + listener.deleteArtifact( getRepository(), createArtifact( "org.jruby.plugins", "jruby-rake-plugin", + "1.0RC1-20070504.153317-1", "pom" ) ); + listener.deleteArtifact( getRepository(), createArtifact( "org.jruby.plugins", "jruby-rake-plugin", + "1.0RC1-20070504.160758-2", "jar" ) ); + listener.deleteArtifact( getRepository(), createArtifact( "org.jruby.plugins", "jruby-rake-plugin", + "1.0RC1-20070504.160758-2", "pom" ) ); + listenerControl.replay(); + repoPurge.process( PATH_TO_BY_RETENTION_COUNT_ARTIFACT ); + listenerControl.verify(); + String versionRoot = repoRoot + "/org/jruby/plugins/jruby-rake-plugin/1.0RC1-SNAPSHOT"; // assert if removed from repo @@ -105,11 +108,18 @@ public class RetentionCountRepositoryPurgeTest public void testIfAPomWasFound() throws Exception { - populateIfPomWasFoundDb(); - String repoRoot = prepareTestRepos(); + // test listeners for the correct artifacts + listener.deleteArtifact( getRepository(), createArtifact( "org.codehaus.castor", "castor-anttasks", + "1.1.2-20070427.065136-1", "jar" ) ); + listener.deleteArtifact( getRepository(), createArtifact( "org.codehaus.castor", "castor-anttasks", + "1.1.2-20070427.065136-1", "pom" ) ); + listenerControl.replay(); + repoPurge.process( PATH_TO_BY_RETENTION_COUNT_POM ); + + listenerControl.verify(); String versionRoot = repoRoot + "/org/codehaus/castor/castor-anttasks/1.1.2-SNAPSHOT"; @@ -146,12 +156,19 @@ public class RetentionCountRepositoryPurgeTest public void testOrderOfDeletion() throws Exception { - populateDbForTestOrderOfDeletion(); - String repoRoot = prepareTestRepos(); + // test listeners for the correct artifacts + listener.deleteArtifact( getRepository(), createArtifact( "org.apache.maven.plugins", "maven-assembly-plugin", + "1.1.2-20070427.065136-1", "maven-plugin" ) ); + listener.deleteArtifact( getRepository(), createArtifact( "org.apache.maven.plugins", "maven-assembly-plugin", + "1.1.2-20070427.065136-1", "pom" ) ); + listenerControl.replay(); + repoPurge.process( PATH_TO_TEST_ORDER_OF_DELETION ); + listenerControl.verify(); + String versionRoot = repoRoot + "/org/apache/maven/plugins/maven-assembly-plugin/1.1.2-SNAPSHOT"; @@ -177,27 +194,4 @@ public class RetentionCountRepositoryPurgeTest 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" ); } - - public void populateIfJarWasFoundDb() - throws Exception - { - List versions = new ArrayList(); - versions.add( "1.0RC1-20070504.153317-1" ); - versions.add( "1.0RC1-20070504.160758-2" ); - versions.add( "1.0RC1-20070505.090015-3" ); - versions.add( "1.0RC1-20070506.090132-4" ); - - populateDb( "org.jruby.plugins", "jruby-rake-plugin", versions ); - } - - public void populateIfPomWasFoundDb() - throws Exception - { - List versions = new ArrayList(); - versions.add( "1.1.2-20070427.065136-1" ); - versions.add( "1.1.2-20070615.105019-3" ); - versions.add( "1.1.2-20070506.163513-2" ); - - populateDb( "org.codehaus.castor", "castor-anttasks", versions ); - } } diff --git a/archiva-database-decoupling/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/maven/archiva/consumers/core/repository/stubs/LuceneRepositoryContentIndexFactoryStub.java b/archiva-database-decoupling/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/maven/archiva/consumers/core/repository/stubs/LuceneRepositoryContentIndexFactoryStub.java deleted file mode 100644 index f1958abe9..000000000 --- a/archiva-database-decoupling/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/maven/archiva/consumers/core/repository/stubs/LuceneRepositoryContentIndexFactoryStub.java +++ /dev/null @@ -1,60 +0,0 @@ -package org.apache.maven.archiva.consumers.core.repository.stubs; - -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration; -import org.apache.maven.archiva.indexer.RepositoryContentIndex; -import org.apache.maven.archiva.indexer.RepositoryContentIndexFactory; - -/** - * LuceneRepositoryContenIndexFactoryStub - * - * @author Maria Odea Ching - * @version - */ -public class LuceneRepositoryContentIndexFactoryStub - implements RepositoryContentIndexFactory -{ - - private int expectedRecordsSize = 0; - - public RepositoryContentIndex createBytecodeIndex( ManagedRepositoryConfiguration repository ) - { - // TODO Auto-generated method stub - return new LuceneRepositoryContentIndexStub( expectedRecordsSize ); - } - - public RepositoryContentIndex createFileContentIndex( ManagedRepositoryConfiguration repository ) - { - // TODO Auto-generated method stub - return new LuceneRepositoryContentIndexStub( expectedRecordsSize ); - } - - public RepositoryContentIndex createHashcodeIndex( ManagedRepositoryConfiguration repository ) - { - // TODO Auto-generated method stub - return new LuceneRepositoryContentIndexStub( expectedRecordsSize ); - } - - public void setExpectedRecordsSize( int size ) - { - expectedRecordsSize = size; - } -} diff --git a/archiva-database-decoupling/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/maven/archiva/consumers/core/repository/stubs/LuceneRepositoryContentIndexStub.java b/archiva-database-decoupling/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/maven/archiva/consumers/core/repository/stubs/LuceneRepositoryContentIndexStub.java deleted file mode 100644 index bd891e17b..000000000 --- a/archiva-database-decoupling/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/maven/archiva/consumers/core/repository/stubs/LuceneRepositoryContentIndexStub.java +++ /dev/null @@ -1,147 +0,0 @@ -package org.apache.maven.archiva.consumers.core.repository.stubs; - -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import java.io.File; -import java.util.Collection; - -import junit.framework.Assert; - -import org.apache.lucene.analysis.Analyzer; -import org.apache.lucene.queryParser.QueryParser; -import org.apache.lucene.search.Searchable; -import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration; -import org.apache.maven.archiva.indexer.RepositoryContentIndex; -import org.apache.maven.archiva.indexer.RepositoryIndexException; -import org.apache.maven.archiva.indexer.RepositoryIndexSearchException; -import org.apache.maven.archiva.indexer.lucene.LuceneEntryConverter; -import org.apache.maven.archiva.indexer.lucene.LuceneRepositoryContentRecord; - -/** - * @author Maria Odea Ching - * @version - */ -public class LuceneRepositoryContentIndexStub - implements RepositoryContentIndex -{ - private int expectedRecordsSize; - - public LuceneRepositoryContentIndexStub() - { - - } - - public LuceneRepositoryContentIndexStub( int size ) - { - expectedRecordsSize = size; - } - - public void deleteRecords( Collection records ) - throws RepositoryIndexException - { - Assert.assertEquals( expectedRecordsSize, records.size() ); - } - - public boolean exists() - throws RepositoryIndexException - { - // TODO Auto-generated method stub - return false; - } - - public Collection getAllRecordKeys() - throws RepositoryIndexException - { - // TODO Auto-generated method stub - return null; - } - - public Analyzer getAnalyzer() - { - // TODO Auto-generated method stub - return null; - } - - public LuceneEntryConverter getEntryConverter() - { - // TODO Auto-generated method stub - return null; - } - - public String getId() - { - // TODO Auto-generated method stub - return null; - } - - public File getIndexDirectory() - { - // TODO Auto-generated method stub - return null; - } - - public QueryParser getQueryParser() - { - // TODO Auto-generated method stub - return null; - } - - public ManagedRepositoryConfiguration getRepository() - { - // TODO Auto-generated method stub - return null; - } - - public Searchable getSearchable() - throws RepositoryIndexSearchException - { - // TODO Auto-generated method stub - return null; - } - - public void indexRecords( Collection records ) - throws RepositoryIndexException - { - // TODO Auto-generated method stub - - } - - public void modifyRecord( LuceneRepositoryContentRecord record ) - throws RepositoryIndexException - { - // TODO Auto-generated method stub - - } - - public void modifyRecords( Collection records ) - throws RepositoryIndexException - { - // TODO Auto-generated method stub - - } - - public void deleteRecord( LuceneRepositoryContentRecord record ) - throws RepositoryIndexException - { - // TODO Auto-generated method stub - - } - -} diff --git a/archiva-database-decoupling/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/resources/org/apache/maven/archiva/consumers/core/repository/RepositoryPurgeConsumerTest.xml b/archiva-database-decoupling/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/resources/org/apache/maven/archiva/consumers/core/repository/RepositoryPurgeConsumerTest.xml index 52e286e66..63f9b0d4d 100644 --- a/archiva-database-decoupling/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/resources/org/apache/maven/archiva/consumers/core/repository/RepositoryPurgeConsumerTest.xml +++ b/archiva-database-decoupling/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/resources/org/apache/maven/archiva/consumers/core/repository/RepositoryPurgeConsumerTest.xml @@ -31,10 +31,6 @@ org.apache.maven.archiva.configuration.ArchivaConfiguration retention-count - - org.apache.maven.archiva.database.ArchivaDAO - jdo - org.apache.maven.archiva.repository.RepositoryContentFactory retention-count @@ -46,11 +42,6 @@ org.apache.maven.archiva.configuration.FileTypes retention-count - - org.apache.maven.archiva.indexer.RepositoryContentIndexFactory - lucene - indexFactory - repository-purge @@ -123,10 +114,6 @@ org.apache.maven.archiva.configuration.ArchivaConfiguration days-old - - org.apache.maven.archiva.database.ArchivaDAO - jdo - org.apache.maven.archiva.repository.RepositoryContentFactory days-old @@ -138,11 +125,6 @@ org.apache.maven.archiva.configuration.FileTypes days-old - - org.apache.maven.archiva.indexer.RepositoryContentIndexFactory - lucene - indexFactory - repository-purge @@ -203,106 +185,5 @@ - - - - - org.apache.maven.archiva.database.ArchivaDAO - jdo - org.apache.maven.archiva.database.jdo.JdoArchivaDAO - - - org.apache.maven.archiva.database.jdo.JdoAccess - archiva - - - org.apache.maven.archiva.database.ArtifactDAO - jdo - - - org.apache.maven.archiva.database.ProjectModelDAO - jdo - - - org.apache.maven.archiva.database.RepositoryProblemDAO - jdo - - - - - org.apache.maven.archiva.database.ArtifactDAO - jdo - org.apache.maven.archiva.database.jdo.JdoArtifactDAO - - - org.apache.maven.archiva.database.jdo.JdoAccess - archiva - - - - - org.apache.maven.archiva.database.ProjectModelDAO - jdo - org.apache.maven.archiva.database.jdo.JdoProjectModelDAO - - - org.apache.maven.archiva.database.jdo.JdoAccess - archiva - - - - - org.apache.maven.archiva.database.RepositoryProblemDAO - jdo - org.apache.maven.archiva.database.jdo.JdoRepositoryProblemDAO - - - org.apache.maven.archiva.database.jdo.JdoAccess - archiva - - - - - - - - org.apache.maven.archiva.database.jdo.JdoAccess - archiva - org.apache.maven.archiva.database.jdo.JdoAccess - - - org.codehaus.plexus.jdo.JdoFactory - archiva - - - - - - - org.codehaus.plexus.jdo.JdoFactory - archiva - org.codehaus.plexus.jdo.DefaultConfigurableJdoFactory - - org.jpox.PersistenceManagerFactoryImpl - org.hsqldb.jdbcDriver - sa - - jdbc:hsqldb:mem:testdb - - - javax.jdo.PersistenceManagerFactoryClass - org.jpox.PersistenceManagerFactoryImpl - - - - - - - - org.apache.maven.archiva.indexer.RepositoryContentIndexFactory - lucene - org.apache.maven.archiva.consumers.core.repository.stubs.LuceneRepositoryContentIndexFactoryStub - - diff --git a/archiva-database-decoupling/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/RepositoryContentIndexEventListener.java b/archiva-database-decoupling/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/RepositoryContentIndexEventListener.java new file mode 100644 index 000000000..80aaf14d2 --- /dev/null +++ b/archiva-database-decoupling/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/RepositoryContentIndexEventListener.java @@ -0,0 +1,70 @@ +package org.apache.maven.archiva.indexer; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import org.apache.maven.archiva.indexer.bytecode.BytecodeRecord; +import org.apache.maven.archiva.indexer.filecontent.FileContentRecord; +import org.apache.maven.archiva.indexer.hashcodes.HashcodesRecord; +import org.apache.maven.archiva.model.ArchivaArtifact; +import org.apache.maven.archiva.repository.ManagedRepositoryContent; +import org.apache.maven.archiva.repository.events.RepositoryListener; + +/** + * Process repository management events and respond appropriately. + * + * @todo creating index instances every time is inefficient, the plugin needs to have a repository context to operate in + * @plexus.component role="org.apache.maven.archiva.repository.events.RepositoryListener" role-hint="indexer" + */ +public class RepositoryContentIndexEventListener + implements RepositoryListener +{ + /** + * @plexus.requirement role-hint="lucene" + */ + private RepositoryContentIndexFactory indexFactory; + + public void deleteArtifact( ManagedRepositoryContent repository, ArchivaArtifact artifact ) + { + try + { + RepositoryContentIndex index = indexFactory.createFileContentIndex( repository.getRepository() ); + FileContentRecord fileContentRecord = new FileContentRecord(); + fileContentRecord.setRepositoryId( repository.getRepository().getId() ); + fileContentRecord.setFilename( repository.toPath( artifact ) ); + index.deleteRecord( fileContentRecord ); + + index = indexFactory.createHashcodeIndex( repository.getRepository() ); + HashcodesRecord hashcodesRecord = new HashcodesRecord(); + fileContentRecord.setRepositoryId( repository.getRepository().getId() ); + hashcodesRecord.setArtifact( artifact ); + index.deleteRecord( hashcodesRecord ); + + index = indexFactory.createBytecodeIndex( repository.getRepository() ); + BytecodeRecord bytecodeRecord = new BytecodeRecord(); + fileContentRecord.setRepositoryId( repository.getRepository().getId() ); + bytecodeRecord.setArtifact( artifact ); + index.deleteRecord( bytecodeRecord ); + } + catch ( RepositoryIndexException e ) + { + // Ignore + } + } +} diff --git a/archiva-database-decoupling/archiva-modules/archiva-base/archiva-indexer/src/test/java/org/apache/maven/archiva/indexer/RepositoryContentIndexEventListenerTest.java b/archiva-database-decoupling/archiva-modules/archiva-base/archiva-indexer/src/test/java/org/apache/maven/archiva/indexer/RepositoryContentIndexEventListenerTest.java new file mode 100644 index 000000000..a402c46e7 --- /dev/null +++ b/archiva-database-decoupling/archiva-modules/archiva-base/archiva-indexer/src/test/java/org/apache/maven/archiva/indexer/RepositoryContentIndexEventListenerTest.java @@ -0,0 +1,167 @@ +package org.apache.maven.archiva.indexer; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.io.File; +import java.util.List; +import java.util.Map; + +import org.apache.commons.io.FileUtils; +import org.apache.lucene.search.Hits; +import org.apache.lucene.search.MatchAllDocsQuery; +import org.apache.lucene.search.Query; +import org.apache.lucene.search.Searcher; +import org.apache.maven.archiva.configuration.ArchivaConfiguration; +import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration; +import org.apache.maven.archiva.indexer.bytecode.BytecodeRecord; +import org.apache.maven.archiva.indexer.filecontent.FileContentRecord; +import org.apache.maven.archiva.indexer.hashcodes.HashcodesRecord; +import org.apache.maven.archiva.indexer.search.BytecodeIndexPopulator; +import org.apache.maven.archiva.indexer.search.FileContentIndexPopulator; +import org.apache.maven.archiva.indexer.search.HashcodesIndexPopulator; +import org.apache.maven.archiva.model.ArchivaArtifact; +import org.apache.maven.archiva.repository.ManagedRepositoryContent; +import org.apache.maven.archiva.repository.events.RepositoryListener; +import org.codehaus.plexus.spring.PlexusInSpringTestCase; +import org.codehaus.plexus.spring.PlexusToSpringUtils; + +public class RepositoryContentIndexEventListenerTest + extends PlexusInSpringTestCase +{ + private static final String TEST_DEFAULT_REPOSITORY_NAME = "Test Default Repository"; + + private static final String TEST_DEFAULT_REPO_ID = "test-repo"; + + private RepositoryListener listener; + + @Override + protected void setUp() + throws Exception + { + super.setUp(); + + listener = (RepositoryListener) lookup( RepositoryListener.class.getName(), "indexer" ); + } + + public void testWiring() + { + List listeners = + PlexusToSpringUtils.lookupList( PlexusToSpringUtils.buildSpringId( RepositoryListener.class ), + getApplicationContext() ); + + assertEquals( 1, listeners.size() ); + assertEquals( listener, listeners.get( 0 ) ); + } + + public ArchivaArtifact createArtifact( String artifactId, String version ) + { + ArchivaArtifact artifact = + new ArchivaArtifact( "org.apache.maven.archiva.test", artifactId, version, "", "jar" ); + artifact.getModel().setRepositoryId( "testable_repo" ); + return artifact; + } + + public void testDeleteArtifact() + throws Exception + { + RepositoryContentIndexFactory indexFactory = + (RepositoryContentIndexFactory) lookup( RepositoryContentIndexFactory.class.getName(), "lucene" ); + + File repoDir = new File( getBasedir(), "src/test/managed-repository" ); + + assertTrue( "Default Test Repository should exist.", repoDir.exists() && repoDir.isDirectory() ); + + ManagedRepositoryConfiguration repository = + createRepository( TEST_DEFAULT_REPO_ID, TEST_DEFAULT_REPOSITORY_NAME, repoDir ); + + File indexLocation = new File( "target/index-events-" + getName() + "/" ); + + MockConfiguration config = (MockConfiguration) lookup( ArchivaConfiguration.class.getName(), "mock" ); + + ManagedRepositoryConfiguration repoConfig = new ManagedRepositoryConfiguration(); + repoConfig.setId( TEST_DEFAULT_REPO_ID ); + repoConfig.setName( TEST_DEFAULT_REPOSITORY_NAME ); + repoConfig.setLocation( repoDir.getAbsolutePath() ); + repoConfig.setIndexDir( indexLocation.getAbsolutePath() ); + repoConfig.setScanned( true ); + + if ( indexLocation.exists() ) + { + FileUtils.deleteDirectory( indexLocation ); + } + + config.getConfiguration().addManagedRepository( repoConfig ); + + // Create the (empty) indexes. + RepositoryContentIndex indexHashcode = indexFactory.createHashcodeIndex( repository ); + RepositoryContentIndex indexBytecode = indexFactory.createBytecodeIndex( repository ); + RepositoryContentIndex indexContents = indexFactory.createFileContentIndex( repository ); + + // Now populate them. + Map hashcodesMap = new HashcodesIndexPopulator().populate( new File( getBasedir() ) ); + indexHashcode.indexRecords( hashcodesMap.values() ); + assertEquals( "Hashcode Key Count", hashcodesMap.size(), indexHashcode.getAllRecordKeys().size() ); + assertRecordCount( indexHashcode, hashcodesMap.size() ); + + Map bytecodeMap = new BytecodeIndexPopulator().populate( new File( getBasedir() ) ); + indexBytecode.indexRecords( bytecodeMap.values() ); + assertEquals( "Bytecode Key Count", bytecodeMap.size(), indexBytecode.getAllRecordKeys().size() ); + assertRecordCount( indexBytecode, bytecodeMap.size() ); + + Map contentMap = new FileContentIndexPopulator().populate( new File( getBasedir() ) ); + indexContents.indexRecords( contentMap.values() ); + assertEquals( "File Content Key Count", contentMap.size(), indexContents.getAllRecordKeys().size() ); + assertRecordCount( indexContents, contentMap.size() ); + + ManagedRepositoryContent repositoryContent = + (ManagedRepositoryContent) lookup( ManagedRepositoryContent.class.getName(), "default" ); + repositoryContent.setRepository( repository ); + + ArchivaArtifact artifact = + new ArchivaArtifact( "org.apache.maven.archiva", "archiva-common", "1.0", "", "jar" ); + listener.deleteArtifact( repositoryContent, artifact ); + + artifact = + new ArchivaArtifact( "org.apache.maven.archiva.record", "test-pom", "1.0", "", "pom" ); + listener.deleteArtifact( repositoryContent, artifact ); + + assertRecordCount( indexHashcode, hashcodesMap.size() - 1 ); + assertRecordCount( indexBytecode, bytecodeMap.size() - 1 ); + assertRecordCount( indexContents, contentMap.size() - 1 ); + } + + protected ManagedRepositoryConfiguration createRepository( String id, String name, File location ) + { + ManagedRepositoryConfiguration repo = new ManagedRepositoryConfiguration(); + repo.setId( id ); + repo.setName( name ); + repo.setLocation( location.getAbsolutePath() ); + return repo; + } + + private void assertRecordCount( RepositoryContentIndex index, int expectedCount ) + throws Exception + { + Query query = new MatchAllDocsQuery(); + Searcher searcher = (Searcher) index.getSearchable(); + Hits hits = searcher.search( query ); + assertEquals( "Expected Record Count for " + index.getId(), expectedCount, hits.length() ); + } +} diff --git a/archiva-database-decoupling/archiva-modules/archiva-base/archiva-indexer/src/test/resources/org/apache/maven/archiva/indexer/RepositoryContentIndexEventListenerTest.xml b/archiva-database-decoupling/archiva-modules/archiva-base/archiva-indexer/src/test/resources/org/apache/maven/archiva/indexer/RepositoryContentIndexEventListenerTest.xml new file mode 100644 index 000000000..af15697a1 --- /dev/null +++ b/archiva-database-decoupling/archiva-modules/archiva-base/archiva-indexer/src/test/resources/org/apache/maven/archiva/indexer/RepositoryContentIndexEventListenerTest.xml @@ -0,0 +1,22 @@ + + + + org.apache.maven.archiva.configuration.ArchivaConfiguration + mock + org.apache.maven.archiva.indexer.MockConfiguration + + + org.apache.maven.archiva.indexer.RepositoryContentIndexFactory + lucene + org.apache.maven.archiva.indexer.lucene.LuceneRepositoryContentIndexFactory + Factory for Lucene repository content index instances. + + + org.apache.maven.archiva.configuration.ArchivaConfiguration + mock + configuration + + + + + diff --git a/archiva-database-decoupling/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/maven/archiva/repository/events/RepositoryListener.java b/archiva-database-decoupling/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/maven/archiva/repository/events/RepositoryListener.java new file mode 100644 index 000000000..f725071ab --- /dev/null +++ b/archiva-database-decoupling/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/maven/archiva/repository/events/RepositoryListener.java @@ -0,0 +1,37 @@ +package org.apache.maven.archiva.repository.events; + +import org.apache.maven.archiva.model.ArchivaArtifact; +import org.apache.maven.archiva.repository.ManagedRepositoryContent; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/** + * Listen to events on the repository. This class is a stopgap + * refactoring measure until an event bus is in place to handle + * generic events such as these. + */ +public interface RepositoryListener +{ + /** + * Event for the deletion of a given artifact. + * @param artifactPath the path to the artifact that was deleted. + */ + void deleteArtifact( ManagedRepositoryContent repository, ArchivaArtifact artifact ); +} diff --git a/archiva-database-decoupling/archiva-modules/archiva-database/pom.xml b/archiva-database-decoupling/archiva-modules/archiva-database/pom.xml index 5268b9531..fad154168 100755 --- a/archiva-database-decoupling/archiva-modules/archiva-database/pom.xml +++ b/archiva-database-decoupling/archiva-modules/archiva-database/pom.xml @@ -74,10 +74,6 @@ log4j log4j - - org.apache.derby - derby - org.codehaus.plexus.registry diff --git a/archiva-database-decoupling/archiva-modules/archiva-database/src/main/java/org/apache/maven/archiva/database/RepositoryDatabaseEventListener.java b/archiva-database-decoupling/archiva-modules/archiva-database/src/main/java/org/apache/maven/archiva/database/RepositoryDatabaseEventListener.java new file mode 100644 index 000000000..fa865e3ba --- /dev/null +++ b/archiva-database-decoupling/archiva-modules/archiva-database/src/main/java/org/apache/maven/archiva/database/RepositoryDatabaseEventListener.java @@ -0,0 +1,55 @@ +package org.apache.maven.archiva.database; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import org.apache.maven.archiva.model.ArchivaArtifact; +import org.apache.maven.archiva.repository.ManagedRepositoryContent; +import org.apache.maven.archiva.repository.events.RepositoryListener; + +/** + * Process repository management events and respond appropriately. + * + * @plexus.component role="org.apache.maven.archiva.repository.events.RepositoryListener" role-hint="database" + */ +public class RepositoryDatabaseEventListener + implements RepositoryListener +{ + /** + * @plexus.requirement role-hint="jdo" + */ + private ArtifactDAO artifactDAO; + + public void deleteArtifact( ManagedRepositoryContent repository, ArchivaArtifact artifact ) + { + try + { + ArchivaArtifact queriedArtifact = + artifactDAO.getArtifact( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), + artifact.getClassifier(), artifact.getType() ); + artifactDAO.deleteArtifact( queriedArtifact ); + } + catch ( ArchivaDatabaseException e ) + { + // ignored + } + + // TODO [MRM-37]: re-run the database consumers to clean up + } +} diff --git a/archiva-database-decoupling/archiva-modules/archiva-database/src/test/java/org/apache/maven/archiva/database/RepositoryDatabaseEventListenerTest.java b/archiva-database-decoupling/archiva-modules/archiva-database/src/test/java/org/apache/maven/archiva/database/RepositoryDatabaseEventListenerTest.java new file mode 100644 index 000000000..7e76ee6b0 --- /dev/null +++ b/archiva-database-decoupling/archiva-modules/archiva-database/src/test/java/org/apache/maven/archiva/database/RepositoryDatabaseEventListenerTest.java @@ -0,0 +1,90 @@ +package org.apache.maven.archiva.database; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.util.Date; +import java.util.List; + +import org.apache.maven.archiva.model.ArchivaArtifact; +import org.apache.maven.archiva.repository.ManagedRepositoryContent; +import org.apache.maven.archiva.repository.events.RepositoryListener; +import org.codehaus.plexus.spring.PlexusToSpringUtils; + +public class RepositoryDatabaseEventListenerTest + extends AbstractArchivaDatabaseTestCase +{ + private RepositoryListener listener; + + @Override + protected void setUp() + throws Exception + { + super.setUp(); + + listener = (RepositoryListener) lookup( RepositoryListener.class.getName(), "database" ); + } + + public void testWiring() + { + List listeners = + PlexusToSpringUtils.lookupList( PlexusToSpringUtils.buildSpringId( RepositoryListener.class ), + getApplicationContext() ); + + assertEquals( 1, listeners.size() ); + assertEquals( listener, listeners.get( 0 ) ); + } + + public ArchivaArtifact createArtifact( String artifactId, String version, ArtifactDAO artifactDao ) + { + ArchivaArtifact artifact = + artifactDao.createArtifact( "org.apache.maven.archiva.test", artifactId, version, "", "jar" ); + artifact.getModel().setLastModified( new Date() ); + artifact.getModel().setRepositoryId( "testable_repo" ); + return artifact; + } + + public void testDeleteArtifact() + throws Exception + { + ArtifactDAO artifactDao = (ArtifactDAO) lookup( ArtifactDAO.class.getName(), "jdo" ); + + // Setup artifacts in fresh DB. + ArchivaArtifact artifact = createArtifact( "test-artifact", "1.0", artifactDao ); + artifactDao.saveArtifact( artifact ); + + assertEquals( artifact, artifactDao.getArtifact( "org.apache.maven.archiva.test", "test-artifact", "1.0", null, + "jar" ) ); + + artifact = new ArchivaArtifact( "org.apache.maven.archiva.test", "test-artifact", "1.0", null, "jar" ); + ManagedRepositoryContent repository = + (ManagedRepositoryContent) lookup( ManagedRepositoryContent.class.getName(), "default" ); + listener.deleteArtifact( repository, artifact ); + + try + { + artifactDao.getArtifact( "org.apache.maven.archiva.test", "test-artifact", "1.0", null, "jar" ); + fail( "Should not find artifact" ); + } + catch ( ObjectNotFoundException e ) + { + assertTrue( true ); + } + } +} diff --git a/archiva-database-decoupling/archiva-modules/archiva-web/archiva-rss/pom.xml b/archiva-database-decoupling/archiva-modules/archiva-web/archiva-rss/pom.xml index 9aa671e60..be70c4a21 100644 --- a/archiva-database-decoupling/archiva-modules/archiva-web/archiva-rss/pom.xml +++ b/archiva-database-decoupling/archiva-modules/archiva-web/archiva-rss/pom.xml @@ -18,6 +18,7 @@ org.apache.archiva archiva-model + org.apache.archiva archiva-database diff --git a/archiva-database-decoupling/archiva-modules/archiva-web/archiva-webapp/pom.xml b/archiva-database-decoupling/archiva-modules/archiva-web/archiva-webapp/pom.xml index 7e1927a81..3b8079ede 100644 --- a/archiva-database-decoupling/archiva-modules/archiva-web/archiva-webapp/pom.xml +++ b/archiva-database-decoupling/archiva-modules/archiva-web/archiva-webapp/pom.xml @@ -171,11 +171,6 @@ org.springframework spring-web - - org.apache.derby - derby - provided - javax.mail mail diff --git a/archiva-database-decoupling/branch-working-notes.txt b/archiva-database-decoupling/branch-working-notes.txt new file mode 100644 index 000000000..c8a4db677 --- /dev/null +++ b/archiva-database-decoupling/branch-working-notes.txt @@ -0,0 +1,28 @@ +Stage 1: remove use of database and index from core consumers (move implementation into respective database and index modules) + +Done! + +Stage 2: separate model from JPOX annotated classes, centralising JPOX use in database + +* archiva-model to be reviewed, possibly split into a basic model with extensible parts. See metadata proposal +* add consumer to generate Archiva metadata at same time as database model + +Stage 3: add a basic repository querying API for base artifact information and retrieval of metadata + +* RSS, browse +* consider repository-api refactorings +* at this point, should be able to have functional Archiva without a database +* note that metadata need not be stored with the artifacts themselves, but will be by default + +Stage 4: incorporation of event API + +* used to centralise arrival, removal, etc of files/artifacts in the repository +* errors should be events as well to avoid exceptions in the logs and instead meaningful handling/reporting + +Stage 5: isolate scanning code + +* Repository should operate without scanning code, it should push events if enabled +* better assessment of its progress, performance +* removal of database / repository scanning duality - all operations are driven by the event bus +* move some database operations to a housekeeping scheduled task (same for index), make scheduled tasks a listable item based on available plugins + -- 2.39.5