From eb125e9a34a8b2651269916513a4cf595bf03223 Mon Sep 17 00:00:00 2001 From: Olivier Lamy Date: Wed, 2 Nov 2011 22:30:45 +0000 Subject: [PATCH] add a convenient method to create index in repository admin service for remote repos git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1196838 13f79535-47bb-0310-9956-ffa450edef68 --- .../model/remote/RemoteRepositoryAdmin.java | 10 ++ .../DefaultManagedRepositoryAdmin.java | 9 +- .../remote/DefaultRemoteRepositoryAdmin.java | 97 +++++++++++++++++++ 3 files changed, 113 insertions(+), 3 deletions(-) diff --git a/archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-api/src/main/java/org/apache/archiva/admin/model/remote/RemoteRepositoryAdmin.java b/archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-api/src/main/java/org/apache/archiva/admin/model/remote/RemoteRepositoryAdmin.java index 887ad925b..da841f401 100644 --- a/archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-api/src/main/java/org/apache/archiva/admin/model/remote/RemoteRepositoryAdmin.java +++ b/archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-api/src/main/java/org/apache/archiva/admin/model/remote/RemoteRepositoryAdmin.java @@ -22,6 +22,7 @@ package org.apache.archiva.admin.model.remote; import org.apache.archiva.admin.model.AuditInformation; import org.apache.archiva.admin.model.RepositoryAdminException; import org.apache.archiva.admin.model.beans.RemoteRepository; +import org.apache.maven.index.context.IndexingContext; import java.util.List; import java.util.Map; @@ -49,4 +50,13 @@ public interface RemoteRepositoryAdmin Map getRemoteRepositoriesAsMap() throws RepositoryAdminException; + + /** + * @param repository + * @return + * @throws RepositoryAdminException + * @since 1.4-M2 + */ + IndexingContext createIndexContext( RemoteRepository 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 5348630a2..9f2367490 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 @@ -173,7 +173,7 @@ public class DefaultManagedRepositoryAdmin getRepositoryCommonValidator().basicValidation( managedRepository, false ); triggerAuditEvent( managedRepository.getId(), null, AuditEvent.ADD_MANAGED_REPO, auditInformation ); - return + Boolean res = addManagedRepository( managedRepository.getId(), managedRepository.getLayout(), managedRepository.getName(), managedRepository.getLocation(), managedRepository.isBlockRedeployments(), managedRepository.isReleases(), managedRepository.isSnapshots(), needStageRepo, @@ -182,6 +182,9 @@ public class DefaultManagedRepositoryAdmin managedRepository.isDeleteReleasedSnapshots(), auditInformation, getArchivaConfiguration().getConfiguration() ) != null; + createIndexContext( managedRepository ); + return res; + } private ManagedRepositoryConfiguration addManagedRepository( String repoId, String layout, String name, @@ -486,7 +489,7 @@ public class DefaultManagedRepositoryAdmin { repositorySession.close(); } - + createIndexContext( managedRepository ); return true; } @@ -512,7 +515,7 @@ public class DefaultManagedRepositoryAdmin } configuration.addManagedRepository( repository ); - + } public IndexingContext createIndexContext( ManagedRepository repository ) diff --git a/archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-default/src/main/java/org/apache/archiva/admin/repository/remote/DefaultRemoteRepositoryAdmin.java b/archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-default/src/main/java/org/apache/archiva/admin/repository/remote/DefaultRemoteRepositoryAdmin.java index 36350af2d..4c703c5cc 100644 --- a/archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-default/src/main/java/org/apache/archiva/admin/repository/remote/DefaultRemoteRepositoryAdmin.java +++ b/archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-default/src/main/java/org/apache/archiva/admin/repository/remote/DefaultRemoteRepositoryAdmin.java @@ -24,12 +24,24 @@ import org.apache.archiva.admin.model.beans.RemoteRepository; import org.apache.archiva.admin.model.remote.RemoteRepositoryAdmin; 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.ProxyConnectorConfiguration; import org.apache.archiva.configuration.RemoteRepositoryConfiguration; import org.apache.commons.lang.StringUtils; +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.springframework.stereotype.Service; +import javax.annotation.PostConstruct; +import javax.inject.Inject; +import java.io.File; +import java.io.IOException; +import java.net.MalformedURLException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -45,6 +57,22 @@ public class DefaultRemoteRepositoryAdmin implements RemoteRepositoryAdmin { + @Inject + private PlexusSisuBridge plexusSisuBridge; + + @Inject + private MavenIndexerUtils mavenIndexerUtils; + + @PostConstruct + private void initialize() + throws RepositoryAdminException + { + for ( RemoteRepository remoteRepository : getRemoteRepositories() ) + { + createIndexContext( remoteRepository ); + } + } + public List getRemoteRepositories() throws RepositoryAdminException @@ -184,6 +212,75 @@ public class DefaultRemoteRepositoryAdmin return map; } + public IndexingContext createIndexContext( RemoteRepository remoteRepository ) + throws RepositoryAdminException + { + try + { + // FIXME get this from ArchivaAdministration + String appServerBase = System.getProperty( "appserver.base" ); + + List indexCreators = mavenIndexerUtils.getAllIndexCreators(); + NexusIndexer nexusIndexer = plexusSisuBridge.lookup( NexusIndexer.class ); + + String contextKey = "remote-" + remoteRepository.getId(); + IndexingContext indexingContext = nexusIndexer.getIndexingContexts().get( contextKey ); + if ( indexingContext != null ) + { + return indexingContext; + } + // create path + File repoDir = new File( appServerBase, "data/remotes/" + remoteRepository.getId() ); + if ( !repoDir.exists() ) + { + repoDir.mkdirs(); + } + File indexDirectory = new File( repoDir, ".index" ); + if ( !indexDirectory.exists() ) + { + indexDirectory.mkdirs(); + } + return nexusIndexer.addIndexingContext( contextKey, remoteRepository.getId(), repoDir, indexDirectory, + remoteRepository.getUrl(), + calculateIndexRemoteUrl( remoteRepository ), + mavenIndexerUtils.getAllIndexCreators() ); + } + 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 ); + } + + } + + protected String calculateIndexRemoteUrl( RemoteRepository remoteRepository ) + { + if ( StringUtils.startsWith( remoteRepository.getRemoteIndexUrl(), "http" ) ) + { + String baseUrl = remoteRepository.getRemoteIndexUrl(); + return baseUrl.endsWith( "/" ) ? StringUtils.substringBeforeLast( baseUrl, "/" ) : baseUrl; + } + String baseUrl = StringUtils.endsWith( remoteRepository.getUrl(), "/" ) ? StringUtils.substringBeforeLast( + remoteRepository.getUrl(), "/" ) : remoteRepository.getUrl(); + + baseUrl = StringUtils.isEmpty( remoteRepository.getRemoteIndexUrl() ) + ? baseUrl + "/.index" + : baseUrl + "/" + remoteRepository.getRemoteIndexUrl(); + return baseUrl; + + } + private RemoteRepositoryConfiguration getRemoteRepositoryConfiguration( RemoteRepository remoteRepository ) { RemoteRepositoryConfiguration remoteRepositoryConfiguration = new RemoteRepositoryConfiguration(); -- 2.39.5