]> source.dussan.org Git - archiva.git/commitdiff
add a convenient method to create index in repository admin service
authorOlivier Lamy <olamy@apache.org>
Wed, 2 Nov 2011 22:30:08 +0000 (22:30 +0000)
committerOlivier Lamy <olamy@apache.org>
Wed, 2 Nov 2011 22:30:08 +0000 (22:30 +0000)
git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1196836 13f79535-47bb-0310-9956-ffa450edef68

archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-api/src/main/java/org/apache/archiva/admin/model/managed/ManagedRepositoryAdmin.java
archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-default/src/main/java/org/apache/archiva/admin/repository/managed/DefaultManagedRepositoryAdmin.java

index ff3c1ed033009d7c4bd6659308ec4fb0e114feeb..156f7b6d38a56e2dc755bcfe77e4f6fa68e31d48 100644 (file)
@@ -22,6 +22,7 @@ package org.apache.archiva.admin.model.managed;
 import org.apache.archiva.admin.model.AuditInformation;
 import org.apache.archiva.admin.model.RepositoryAdminException;
 import org.apache.archiva.admin.model.beans.ManagedRepository;
+import org.apache.maven.index.context.IndexingContext;
 
 import java.util.List;
 import java.util.Map;
@@ -53,5 +54,15 @@ public interface ManagedRepositoryAdmin
                                      AuditInformation auditInformation, boolean resetStats )
         throws RepositoryAdminException;
 
+    /**
+     *
+     * @param repository
+     * @return
+     * @throws RepositoryAdminException
+     * @since 1.4-M2
+     */
+    IndexingContext createIndexContext( ManagedRepository repository )
+            throws RepositoryAdminException;
+
 
 }
index 15d2bc823e1d57ba94856f157d5921efceb6b466..5348630a20c09f28dda697828319eff36ce5d586 100644 (file)
@@ -24,8 +24,13 @@ import org.apache.archiva.admin.model.beans.ManagedRepository;
 import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
 import org.apache.archiva.admin.repository.AbstractRepositoryAdmin;
 import org.apache.archiva.audit.AuditEvent;
+import org.apache.archiva.common.plexusbridge.MavenIndexerUtils;
 import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
 import org.apache.archiva.common.plexusbridge.PlexusSisuBridgeException;
+import org.apache.archiva.configuration.Configuration;
+import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
+import org.apache.archiva.configuration.ProxyConnectorConfiguration;
+import org.apache.archiva.configuration.RepositoryGroupConfiguration;
 import org.apache.archiva.metadata.repository.MetadataRepository;
 import org.apache.archiva.metadata.repository.MetadataRepositoryException;
 import org.apache.archiva.metadata.repository.RepositorySession;
@@ -37,12 +42,10 @@ import org.apache.archiva.security.common.ArchivaRoleConstants;
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.lang.StringUtils;
 import org.apache.commons.validator.GenericValidator;
-import org.apache.archiva.configuration.Configuration;
-import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
-import org.apache.archiva.configuration.ProxyConnectorConfiguration;
-import org.apache.archiva.configuration.RepositoryGroupConfiguration;
 import org.apache.maven.index.NexusIndexer;
+import org.apache.maven.index.context.IndexCreator;
 import org.apache.maven.index.context.IndexingContext;
+import org.apache.maven.index.context.UnsupportedExistingLuceneIndexException;
 import org.codehaus.plexus.redback.role.RoleManager;
 import org.codehaus.plexus.redback.role.RoleManagerException;
 import org.codehaus.plexus.taskqueue.TaskQueueException;
@@ -51,10 +54,12 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.stereotype.Service;
 
+import javax.annotation.PostConstruct;
 import javax.inject.Inject;
 import javax.inject.Named;
 import java.io.File;
 import java.io.IOException;
+import java.net.MalformedURLException;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.HashMap;
@@ -93,10 +98,23 @@ public class DefaultManagedRepositoryAdmin
     @Inject
     private PlexusSisuBridge plexusSisuBridge;
 
+    @Inject
+    private MavenIndexerUtils mavenIndexerUtils;
 
     @Inject
     protected RoleManager roleManager;
 
+    @PostConstruct
+    private void initialize()
+        throws RepositoryAdminException
+    {
+        // initialize index context on start
+        for ( ManagedRepository managedRepository : getManagedRepositories() )
+        {
+            createIndexContext( managedRepository );
+        }
+    }
+
     public List<ManagedRepository> getManagedRepositories()
         throws RepositoryAdminException
     {
@@ -494,6 +512,62 @@ public class DefaultManagedRepositoryAdmin
         }
 
         configuration.addManagedRepository( repository );
+
+    }
+
+    public IndexingContext createIndexContext( ManagedRepository repository )
+        throws RepositoryAdminException
+    {
+        try
+        {
+            List<? extends IndexCreator> indexCreators = mavenIndexerUtils.getAllIndexCreators();
+            NexusIndexer indexer = plexusSisuBridge.lookup( NexusIndexer.class );
+
+            IndexingContext context = indexer.getIndexingContexts().get( repository.getId() );
+
+            if ( context != null )
+            {
+                log.debug( "skip adding repository with id {} as already exists", repository.getId() );
+                return context;
+            }
+
+            String indexDir = repository.getIndexDirectory();
+            File managedRepository = new File( repository.getLocation() );
+
+            File indexDirectory = null;
+            if ( indexDir != null && !"".equals( indexDir ) )
+            {
+                indexDirectory = new File( repository.getIndexDirectory() );
+            }
+            else
+            {
+                indexDirectory = new File( managedRepository, ".indexer" );
+            }
+
+            context =
+                indexer.addIndexingContext( repository.getId(), repository.getId(), managedRepository, indexDirectory,
+                                            managedRepository.toURI().toURL().toExternalForm(),
+                                            indexDirectory.toURI().toURL().toString(), indexCreators );
+
+            context.setSearchable( repository.isScanned() );
+            return context;
+        }
+        catch ( MalformedURLException e )
+        {
+            throw new RepositoryAdminException( e.getMessage(), e );
+        }
+        catch ( IOException e )
+        {
+            throw new RepositoryAdminException( e.getMessage(), e );
+        }
+        catch ( PlexusSisuBridgeException e )
+        {
+            throw new RepositoryAdminException( e.getMessage(), e );
+        }
+        catch ( UnsupportedExistingLuceneIndexException e )
+        {
+            throw new RepositoryAdminException( e.getMessage(), e );
+        }
     }
 
     private ManagedRepositoryConfiguration getStageRepoConfig( ManagedRepositoryConfiguration repository )