From 33577da0f917199c65dc2ed7951141ebcf2c5545 Mon Sep 17 00:00:00 2001 From: Olivier Lamy Date: Fri, 9 Mar 2012 23:44:11 +0000 Subject: [PATCH] add REST method to really scan repositories git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1299102 13f79535-47bb-0310-9956-ffa450edef68 --- .../scanner/RepositoryScanStatistics.java | 15 +++++---- .../repository/scanner/RepositoryScanner.java | 1 + .../archiva-rest/archiva-rest-api/pom.xml | 5 +++ .../api/services/RepositoriesService.java | 16 +++++++++ .../archiva-rest-services/pom.xml | 4 +++ .../services/DefaultRepositoriesService.java | 33 ++++++++++++++++--- 6 files changed, 64 insertions(+), 10 deletions(-) diff --git a/archiva-modules/archiva-base/archiva-repository-scanner/src/main/java/org/apache/archiva/repository/scanner/RepositoryScanStatistics.java b/archiva-modules/archiva-base/archiva-repository-scanner/src/main/java/org/apache/archiva/repository/scanner/RepositoryScanStatistics.java index 5a836214f..e627f0f55 100644 --- a/archiva-modules/archiva-base/archiva-repository-scanner/src/main/java/org/apache/archiva/repository/scanner/RepositoryScanStatistics.java +++ b/archiva-modules/archiva-base/archiva-repository-scanner/src/main/java/org/apache/archiva/repository/scanner/RepositoryScanStatistics.java @@ -19,18 +19,21 @@ package org.apache.archiva.repository.scanner; * under the License. */ -import java.text.SimpleDateFormat; -import java.util.List; -import java.util.Map; - import org.apache.archiva.admin.model.beans.ManagedRepository; import org.apache.commons.collections.CollectionUtils; +import javax.xml.bind.annotation.XmlRootElement; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.List; +import java.util.Map; + /** * RepositoryScanStatistics - extension to the RepositoryContentStatistics model. * * @version $Id$ */ +@XmlRootElement( name = "repositoryScanStatistics" ) public class RepositoryScanStatistics { private transient List knownConsumers; @@ -38,7 +41,7 @@ public class RepositoryScanStatistics private transient List invalidConsumers; private transient long startTimestamp; - + private SimpleDateFormat df = new SimpleDateFormat(); /** @@ -49,7 +52,7 @@ public class RepositoryScanStatistics /** * Field whenGathered */ - private java.util.Date whenGathered; + private Date whenGathered; /** * Field duration diff --git a/archiva-modules/archiva-base/archiva-repository-scanner/src/main/java/org/apache/archiva/repository/scanner/RepositoryScanner.java b/archiva-modules/archiva-base/archiva-repository-scanner/src/main/java/org/apache/archiva/repository/scanner/RepositoryScanner.java index 135f96e17..cf633ca42 100644 --- a/archiva-modules/archiva-base/archiva-repository-scanner/src/main/java/org/apache/archiva/repository/scanner/RepositoryScanner.java +++ b/archiva-modules/archiva-base/archiva-repository-scanner/src/main/java/org/apache/archiva/repository/scanner/RepositoryScanner.java @@ -22,6 +22,7 @@ package org.apache.archiva.repository.scanner; import org.apache.archiva.admin.model.beans.ManagedRepository; import org.apache.archiva.consumers.InvalidRepositoryContentConsumer; import org.apache.archiva.consumers.KnownRepositoryContentConsumer; +import org.springframework.scheduling.annotation.Async; import java.util.List; import java.util.Set; diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/pom.xml b/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/pom.xml index b0478d5a8..c2d6afd46 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/pom.xml +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/pom.xml @@ -50,6 +50,11 @@ metadata-model + + org.apache.archiva + archiva-repository-scanner + + org.codehaus.redback redback-authorization-api diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/services/RepositoriesService.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/services/RepositoriesService.java index 6f3a3a7d6..c747d73f6 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/services/RepositoriesService.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/services/RepositoriesService.java @@ -19,6 +19,7 @@ package org.apache.archiva.rest.api.services; * under the License. */ +import org.apache.archiva.repository.scanner.RepositoryScanStatistics; import org.apache.archiva.rest.api.model.Artifact; import org.apache.archiva.rest.api.model.ArtifactTransferRequest; import org.apache.archiva.security.common.ArchivaRoleConstants; @@ -45,11 +46,26 @@ public interface RepositoriesService @GET @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.TEXT_PLAIN } ) @RedbackAuthorization( permissions = ArchivaRoleConstants.OPERATION_RUN_INDEXER ) + /** + * index repository + */ Boolean scanRepository( @QueryParam( "repositoryId" ) String repositoryId, @QueryParam( "fullScan" ) boolean fullScan ) throws ArchivaRestServiceException; + @Path( "scanRepositoryDirectories/{repositoryId}" ) + @GET + @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.TEXT_PLAIN } ) + @RedbackAuthorization( permissions = ArchivaRoleConstants.OPERATION_RUN_INDEXER ) + /** + * scan directories + * @since 1.4-M3 + */ + RepositoryScanStatistics scanRepositoryDirectories( @PathParam( "repositoryId" ) String repositoryId ) + throws ArchivaRestServiceException; + + @Path( "alreadyScanning/{repositoryId}" ) @GET @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.TEXT_PLAIN } ) diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/pom.xml b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/pom.xml index 8ec24f8e8..3311f8c94 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/pom.xml +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/pom.xml @@ -73,6 +73,10 @@ org.apache.archiva archiva-repository-layer + + org.apache.archiva + archiva-repository-scanner + org.codehaus.redback redback-authorization-api diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultRepositoriesService.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultRepositoriesService.java index 7c85aa08c..936f57e69 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultRepositoriesService.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultRepositoriesService.java @@ -51,6 +51,9 @@ import org.apache.archiva.repository.metadata.MetadataTools; import org.apache.archiva.repository.metadata.RepositoryMetadataException; import org.apache.archiva.repository.metadata.RepositoryMetadataReader; import org.apache.archiva.repository.metadata.RepositoryMetadataWriter; +import org.apache.archiva.repository.scanner.RepositoryScanStatistics; +import org.apache.archiva.repository.scanner.RepositoryScanner; +import org.apache.archiva.repository.scanner.RepositoryScannerException; import org.apache.archiva.rest.api.model.Artifact; import org.apache.archiva.rest.api.model.ArtifactTransferRequest; import org.apache.archiva.rest.api.services.ArchivaRestServiceException; @@ -146,6 +149,9 @@ public class DefaultRepositoriesService @Inject protected List listeners = new ArrayList(); + @Inject + private RepositoryScanner repoScanner; + private ChecksumAlgorithm[] algorithms = new ChecksumAlgorithm[]{ ChecksumAlgorithm.SHA1, ChecksumAlgorithm.MD5 }; public Boolean scanRepository( String repositoryId, boolean fullScan ) @@ -735,19 +741,19 @@ public class DefaultRepositoriesService } catch ( RepositoryException e ) { - throw new ArchivaRestServiceException( "Repository exception: " + e.getMessage(), 400 ); + throw new ArchivaRestServiceException( "Repository exception: " + e.getMessage(), 500 ); } catch ( MetadataResolutionException e ) { - throw new ArchivaRestServiceException( "Repository exception: " + e.getMessage(), 400 ); + throw new ArchivaRestServiceException( "Repository exception: " + e.getMessage(), 500 ); } catch ( MetadataRepositoryException e ) { - throw new ArchivaRestServiceException( "Repository exception: " + e.getMessage(), 400 ); + throw new ArchivaRestServiceException( "Repository exception: " + e.getMessage(), 500 ); } catch ( RepositoryAdminException e ) { - throw new ArchivaRestServiceException( "RepositoryAdmin exception: " + e.getMessage(), 400 ); + throw new ArchivaRestServiceException( "RepositoryAdmin exception: " + e.getMessage(), 500 ); } finally @@ -757,6 +763,25 @@ public class DefaultRepositoriesService return Boolean.TRUE; } + public RepositoryScanStatistics scanRepositoryDirectories( String repositoryId ) + throws ArchivaRestServiceException + { + long sinceWhen = RepositoryScanner.FRESH_SCAN; + try + { + return repoScanner.scan( getManagedRepositoryAdmin().getManagedRepository( repositoryId ), sinceWhen ); + } + catch ( RepositoryScannerException e ) + { + log.error( e.getMessage(), e ); + throw new ArchivaRestServiceException( "RepositoryScannerException exception: " + e.getMessage(), 500 ); + } + catch ( RepositoryAdminException e ) + { + log.error( e.getMessage(), e ); + throw new ArchivaRestServiceException( "RepositoryScannerException exception: " + e.getMessage(), 500 ); + } + } /** * Update artifact level metadata. Creates one if metadata does not exist after artifact deletion.