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
This commit is contained in:
Olivier Lamy 2011-11-02 22:30:45 +00:00
parent e86c15f7b8
commit eb125e9a34
3 changed files with 113 additions and 3 deletions

View File

@ -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<String, RemoteRepository> getRemoteRepositoriesAsMap()
throws RepositoryAdminException;
/**
* @param repository
* @return
* @throws RepositoryAdminException
* @since 1.4-M2
*/
IndexingContext createIndexContext( RemoteRepository repository )
throws RepositoryAdminException;
}

View File

@ -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 )

View File

@ -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<RemoteRepository> 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<? extends IndexCreator> 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();