From: Olivier Lamy Date: Wed, 2 Nov 2011 22:30:08 +0000 (+0000) Subject: add a convenient method to create index in repository admin service X-Git-Tag: archiva-1.4-M2~188 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=43eae0e795122cfd4db1aacad9fba6fbad88cbac;p=archiva.git add a convenient method to create index in repository admin service git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1196836 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-api/src/main/java/org/apache/archiva/admin/model/managed/ManagedRepositoryAdmin.java b/archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-api/src/main/java/org/apache/archiva/admin/model/managed/ManagedRepositoryAdmin.java index ff3c1ed03..156f7b6d3 100644 --- a/archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-api/src/main/java/org/apache/archiva/admin/model/managed/ManagedRepositoryAdmin.java +++ b/archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-api/src/main/java/org/apache/archiva/admin/model/managed/ManagedRepositoryAdmin.java @@ -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; + } diff --git a/archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-default/src/main/java/org/apache/archiva/admin/repository/managed/DefaultManagedRepositoryAdmin.java b/archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-default/src/main/java/org/apache/archiva/admin/repository/managed/DefaultManagedRepositoryAdmin.java index 15d2bc823..5348630a2 100644 --- a/archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-default/src/main/java/org/apache/archiva/admin/repository/managed/DefaultManagedRepositoryAdmin.java +++ b/archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-default/src/main/java/org/apache/archiva/admin/repository/managed/DefaultManagedRepositoryAdmin.java @@ -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 getManagedRepositories() throws RepositoryAdminException { @@ -494,6 +512,62 @@ public class DefaultManagedRepositoryAdmin } configuration.addManagedRepository( repository ); + + } + + public IndexingContext createIndexContext( ManagedRepository repository ) + throws RepositoryAdminException + { + try + { + List 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 )