]> source.dussan.org Git - archiva.git/commitdiff
[MRM-1490] Expose Archiva services trough REST : search service add a method to scan...
authorOlivier Lamy <olamy@apache.org>
Tue, 13 Sep 2011 17:30:35 +0000 (17:30 +0000)
committerOlivier Lamy <olamy@apache.org>
Tue, 13 Sep 2011 17:30:35 +0000 (17:30 +0000)
git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1170258 13f79535-47bb-0310-9956-ffa450edef68

archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/services/RepositoriesService.java
archiva-modules/archiva-web/archiva-rest/archiva-rest-services/pom.xml
archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultRepositoriesService.java
archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/SearchServiceTest.java

index 6d3d9957cdb3c3951ebad265b2284e8d499b2254..ea08d7c52ffcf9e725b8578c54df91e38a0164e1 100644 (file)
@@ -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;
 
 }
index 0055e3f777872d3dcc4c8b9f983ca683fb4148d6..bc7122715156075390f4ec4d02b87a772072dda8 100644 (file)
       <artifactId>archiva-configuration</artifactId>
       <scope>runtime</scope>
     </dependency>
+    <dependency>
+      <groupId>org.apache.archiva</groupId>
+      <artifactId>archiva-scheduler-indexing</artifactId>
+    </dependency>
     <dependency>
       <groupId>org.apache.archiva</groupId>
       <artifactId>audit</artifactId>
index 2475ff809b8ddc690858a2581c965e544ecfa89a..a4a62e5764432d6ff36d3be5ba253fc42daf3702 100644 (file)
@@ -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<IndexCreator>(
+                                                        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() );
+        }
+    }
 }
 
 
index 857abf5e307fd3d3b43c5c254a6e898be72c6a65..99eb661e915fb19b1606e5efe9d9fded379aeedf 100644 (file)
@@ -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<Artifact> 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<Artifact> 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 )