From: Olivier Lamy Date: Tue, 13 Sep 2011 17:30:35 +0000 (+0000) Subject: [MRM-1490] Expose Archiva services trough REST : search service add a method to scan... X-Git-Tag: archiva-1.4-M1~317 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=915ef10186e2e7fa348d334730c4b733121747c3;p=archiva.git [MRM-1490] Expose Archiva services trough REST : search service add a method to scan repo now useful for tests git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1170258 13f79535-47bb-0310-9956-ffa450edef68 --- 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 6d3d9957c..ea08d7c52 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 @@ -42,19 +42,30 @@ public interface RepositoriesService @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.TEXT_PLAIN } ) @RedbackAuthorization( permission = ArchivaRoleConstants.OPERATION_RUN_INDEXER ) Boolean scanRepository( @QueryParam( "repositoryId" ) String repositoryId, - @QueryParam( "fullScan" ) boolean fullScan ); + @QueryParam( "fullScan" ) boolean fullScan ) + throws ArchivaRestServiceException; @Path( "alreadyScanning/{repositoryId}" ) @GET @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.TEXT_PLAIN } ) @RedbackAuthorization( permission = ArchivaRoleConstants.OPERATION_RUN_INDEXER ) - Boolean alreadyScanning( @PathParam( "repositoryId" ) String repositoryId ); + Boolean alreadyScanning( @PathParam( "repositoryId" ) String repositoryId ) + throws ArchivaRestServiceException; @Path( "removeScanningTaskFromQueue/{repositoryId}" ) @GET @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.TEXT_PLAIN } ) @RedbackAuthorization( permission = ArchivaRoleConstants.OPERATION_RUN_INDEXER ) - Boolean removeScanningTaskFromQueue( @PathParam( "repositoryId" ) String repositoryId ); + Boolean removeScanningTaskFromQueue( @PathParam( "repositoryId" ) String repositoryId ) + throws ArchivaRestServiceException; + + @Path( "scanRepositoryNow" ) + @GET + @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.TEXT_PLAIN } ) + @RedbackAuthorization( permission = ArchivaRoleConstants.OPERATION_RUN_INDEXER ) + Boolean scanRepositoryNow( @QueryParam( "repositoryId" ) String repositoryId, + @QueryParam( "fullScan" ) boolean fullScan ) + throws ArchivaRestServiceException; } 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 0055e3f77..bc7122715 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 @@ -46,6 +46,10 @@ archiva-configuration runtime + + org.apache.archiva + archiva-scheduler-indexing + org.apache.archiva audit 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 2475ff809..a4a62e576 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 @@ -19,10 +19,19 @@ package org.apache.archiva.rest.services; * under the License. */ +import org.apache.archiva.admin.model.managed.ManagedRepository; +import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin; +import org.apache.archiva.common.plexusbridge.MavenIndexerUtils; +import org.apache.archiva.common.plexusbridge.PlexusSisuBridge; +import org.apache.archiva.rest.api.services.ArchivaRestServiceException; import org.apache.archiva.rest.api.services.RepositoriesService; +import org.apache.archiva.scheduler.indexing.ArchivaIndexingTaskExecutor; +import org.apache.archiva.scheduler.indexing.ArtifactIndexingTask; import org.apache.archiva.scheduler.repository.RepositoryArchivaTaskScheduler; import org.apache.archiva.scheduler.repository.RepositoryTask; -import org.codehaus.plexus.redback.role.RoleManager; +import org.apache.maven.index.NexusIndexer; +import org.apache.maven.index.context.IndexCreator; +import org.apache.maven.index.context.IndexingContext; import org.codehaus.plexus.taskqueue.TaskQueueException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -31,6 +40,7 @@ import org.springframework.stereotype.Service; import javax.inject.Inject; import javax.inject.Named; import javax.ws.rs.PathParam; +import java.util.ArrayList; /** * @author Olivier Lamy @@ -43,13 +53,22 @@ public class DefaultRepositoriesService { private Logger log = LoggerFactory.getLogger( getClass() ); - @Inject - protected RoleManager roleManager; - @Inject @Named( value = "archivaTaskScheduler#repository" ) private RepositoryArchivaTaskScheduler repositoryTaskScheduler; + @Inject + @Named( value = "taskExecutor#indexing" ) + private ArchivaIndexingTaskExecutor archivaIndexingTaskExecutor; + + @Inject + private ManagedRepositoryAdmin managedRepositoryAdmin; + + @Inject + private PlexusSisuBridge plexusSisuBridge; + + @Inject + private MavenIndexerUtils mavenIndexerUtils; // FIXME olamy move this to repository admin component ! public Boolean scanRepository( String repositoryId, boolean fullScan ) @@ -92,6 +111,32 @@ public class DefaultRepositoriesService return false; } } + + public Boolean scanRepositoryNow( String repositoryId, boolean fullScan ) + throws ArchivaRestServiceException + { + + try + { + ManagedRepository repository = managedRepositoryAdmin.getManagedRepository( repositoryId ); + + IndexingContext context = + ArtifactIndexingTask.createContext( repository, plexusSisuBridge.lookup( NexusIndexer.class ), + new ArrayList( + mavenIndexerUtils.getAllIndexCreators() ) ); + ArtifactIndexingTask task = + new ArtifactIndexingTask( repository, null, ArtifactIndexingTask.Action.FINISH, context ); + task.setExecuteOnEntireRepo( true ); + task.setOnlyUpdate( false ); + + archivaIndexingTaskExecutor.executeTask( task ); + return Boolean.TRUE; + } + catch ( Exception e ) + { + throw new ArchivaRestServiceException( e.getMessage() ); + } + } } diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/SearchServiceTest.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/SearchServiceTest.java index 857abf5e3..99eb661e9 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/SearchServiceTest.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/SearchServiceTest.java @@ -39,12 +39,6 @@ public class SearchServiceTest throws Exception { - // olamy temporary disabled due to huge refactoring - if (true) - { - return; - } - String testRepoId = "test-repo"; // force guest user creation if not exists if ( getUserService( authorizationHeader ).getGuestUser() == null ) @@ -52,6 +46,22 @@ public class SearchServiceTest assertNotNull( getUserService( authorizationHeader ).createGuestUser() ); } + createAndIndexRepo( testRepoId ); + + SearchService searchService = getSearchService( authorizationHeader ); + + List artifacts = searchService.quickSearch( "commons-logging" ); + + assertNotNull( artifacts ); + assertTrue( " empty results for commons-logging search", artifacts.size() > 0 ); + log.info( "artifacts for commons-logginf search {}", artifacts ); + + deleteTestRepo( testRepoId ); + } + + private void createAndIndexRepo( String testRepoId ) + throws Exception + { File targetRepo = new File( System.getProperty( "targetDir", "./target" ), "test-repo" ); if ( targetRepo.exists() ) @@ -67,28 +77,14 @@ public class SearchServiceTest managedRepository.setId( testRepoId ); managedRepository.setName( "test repo" ); managedRepository.setCronExpression( "* * * * * ?" ); + managedRepository.setScanned( false ); managedRepository.setLocation( targetRepo.getPath() ); ManagedRepositoriesService service = getManagedRepositoriesService( authorizationHeader ); service.addManagedRepository( managedRepository ); - getRepositoriesService( authorizationHeader ).scanRepository( testRepoId, true ); - - while ( getRepositoriesService( authorizationHeader ).alreadyScanning( testRepoId ) ) - { - Thread.sleep( 1000 ); - } - - SearchService searchService = getSearchService( authorizationHeader ); - - List artifacts = searchService.quickSearch( "commons-logging" ); - - assertNotNull( artifacts ); - assertTrue( " empty results for commons-logging search", artifacts.size() > 0 ); - log.info( "artifacts for commons-logginf search {}", artifacts ); - - deleteTestRepo( testRepoId ); + getRepositoriesService( authorizationHeader ).scanRepositoryNow( testRepoId, true ); } private void deleteTestRepo( String id )