Search in sources :

Example 1 with IndexingContext

use of org.apache.maven.index.context.IndexingContext in project archiva by apache.

the class ArchivaIndexManagerMock method addArtifactsToIndex.

@Override
public void addArtifactsToIndex(final ArchivaIndexingContext context, final Collection<URI> artifactReference) throws IndexUpdateFailedException {
    final URI ctxUri = context.getPath();
    executeUpdateFunction(context, indexingContext -> {
        Collection<ArtifactContext> artifacts = artifactReference.stream().map(r -> artifactContextProducer.getArtifactContext(indexingContext, Paths.get(ctxUri.resolve(r)).toFile())).collect(Collectors.toList());
        try {
            indexer.addArtifactsToIndex(artifacts, indexingContext);
        } catch (IOException e) {
            log.error("IOException while adding artifact {}", e.getMessage(), e);
            throw new IndexUpdateFailedException("Error occured while adding artifact to index of " + context.getId() + (StringUtils.isNotEmpty(e.getMessage()) ? ": " + e.getMessage() : ""));
        }
    });
}
Also used : AuthenticationException(org.apache.maven.wagon.authentication.AuthenticationException) ArtifactContext(org.apache.maven.index.ArtifactContext) StringUtils(org.apache.commons.lang.StringUtils) PathUtil(org.apache.archiva.common.utils.PathUtil) UnsupportedBaseContextException(org.apache.archiva.indexer.UnsupportedBaseContextException) LoggerFactory(org.slf4j.LoggerFactory) IndexCreator(org.apache.maven.index.context.IndexCreator) ArchivaIndexManager(org.apache.archiva.indexer.ArchivaIndexManager) ArchivaConfiguration(org.apache.archiva.configuration.ArchivaConfiguration) WagonFactoryException(org.apache.archiva.proxy.common.WagonFactoryException) NetworkProxyAdmin(org.apache.archiva.admin.model.networkproxy.NetworkProxyAdmin) PasswordCredentials(org.apache.archiva.repository.PasswordCredentials) Map(java.util.Map) FileUtils(org.apache.archiva.common.utils.FileUtils) IndexCreationFailedException(org.apache.archiva.indexer.IndexCreationFailedException) UnsupportedRepositoryTypeException(org.apache.archiva.repository.UnsupportedRepositoryTypeException) RemoteIndexFeature(org.apache.archiva.repository.features.RemoteIndexFeature) URI(java.net.URI) TransferFailedException(org.apache.maven.wagon.TransferFailedException) Path(java.nio.file.Path) ArchivaIndexingContext(org.apache.archiva.indexer.ArchivaIndexingContext) AbstractHttpClientWagon(org.apache.maven.wagon.shared.http.AbstractHttpClientWagon) TransferEvent(org.apache.maven.wagon.events.TransferEvent) RepositoryAdminException(org.apache.archiva.admin.model.RepositoryAdminException) IndexCreationFeature(org.apache.archiva.repository.features.IndexCreationFeature) HttpMethodConfiguration(org.apache.maven.wagon.shared.http.HttpMethodConfiguration) ProxyInfo(org.apache.maven.wagon.proxy.ProxyInfo) Collection(java.util.Collection) ConnectionException(org.apache.maven.wagon.ConnectionException) AuthorizationException(org.apache.maven.wagon.authorization.AuthorizationException) Collectors(java.util.stream.Collectors) Indexer(org.apache.maven.index.Indexer) FileNotFoundException(java.io.FileNotFoundException) ManagedRepository(org.apache.archiva.repository.ManagedRepository) IndexUpdateRequest(org.apache.maven.index.updater.IndexUpdateRequest) IndexUpdateFailedException(org.apache.archiva.indexer.IndexUpdateFailedException) List(java.util.List) HttpConfiguration(org.apache.maven.wagon.shared.http.HttpConfiguration) IndexFormatTooOldException(org.apache.maven.index_shaded.lucene.index.IndexFormatTooOldException) IndexerEngine(org.apache.maven.index.IndexerEngine) EditableRepository(org.apache.archiva.repository.EditableRepository) WagonFactoryRequest(org.apache.archiva.proxy.common.WagonFactoryRequest) Scanner(org.apache.maven.index.Scanner) NetworkProxy(org.apache.archiva.admin.model.beans.NetworkProxy) ArtifactContextProducer(org.apache.maven.index.ArtifactContextProducer) WagonFactory(org.apache.archiva.proxy.common.WagonFactory) Inject(javax.inject.Inject) Repository(org.apache.archiva.repository.Repository) RemoteRepository(org.apache.archiva.repository.RemoteRepository) Service(org.springframework.stereotype.Service) IndexPackingRequest(org.apache.maven.index.packer.IndexPackingRequest) ResourceFetcher(org.apache.maven.index.updater.ResourceFetcher) StreamWagon(org.apache.maven.wagon.StreamWagon) Logger(org.slf4j.Logger) IndexPacker(org.apache.maven.index.packer.IndexPacker) MalformedURLException(java.net.MalformedURLException) Files(java.nio.file.Files) DefaultScannerListener(org.apache.maven.index.DefaultScannerListener) ScanningRequest(org.apache.maven.index.ScanningRequest) AuthenticationInfo(org.apache.maven.wagon.authentication.AuthenticationInfo) IOException(java.io.IOException) RepositoryType(org.apache.archiva.repository.RepositoryType) IndexingContext(org.apache.maven.index.context.IndexingContext) ConcurrentSkipListSet(java.util.concurrent.ConcurrentSkipListSet) ResourceDoesNotExistException(org.apache.maven.wagon.ResourceDoesNotExistException) Paths(java.nio.file.Paths) TransferListener(org.apache.maven.wagon.events.TransferListener) Wagon(org.apache.maven.wagon.Wagon) ScanningResult(org.apache.maven.index.ScanningResult) InputStream(java.io.InputStream) ArtifactContext(org.apache.maven.index.ArtifactContext) IOException(java.io.IOException) IndexUpdateFailedException(org.apache.archiva.indexer.IndexUpdateFailedException) URI(java.net.URI)

Example 2 with IndexingContext

use of org.apache.maven.index.context.IndexingContext in project archiva by apache.

the class ArchivaIndexManagerMock method move.

@Override
public ArchivaIndexingContext move(ArchivaIndexingContext context, Repository repo) throws IndexCreationFailedException {
    if (context == null) {
        return null;
    }
    if (context.supports(IndexingContext.class)) {
        try {
            Path newPath = getIndexPath(repo);
            IndexingContext ctx = context.getBaseContext(IndexingContext.class);
            Path oldPath = ctx.getIndexDirectoryFile().toPath();
            if (oldPath.equals(newPath)) {
                // Nothing to do, if path does not change
                return context;
            }
            if (!Files.exists(oldPath)) {
                return createContext(repo);
            } else if (context.isEmpty()) {
                context.close();
                return createContext(repo);
            } else {
                context.close(false);
                Files.move(oldPath, newPath);
                return createContext(repo);
            }
        } catch (IOException e) {
            log.error("IOException while moving index directory {}", e.getMessage(), e);
            throw new IndexCreationFailedException("Could not recreated the index.", e);
        } catch (UnsupportedBaseContextException e) {
            throw new IndexCreationFailedException("The given context, is not a maven context.");
        }
    } else {
        throw new IndexCreationFailedException("Bad context type. This is not a maven context.");
    }
}
Also used : Path(java.nio.file.Path) UnsupportedBaseContextException(org.apache.archiva.indexer.UnsupportedBaseContextException) IndexCreationFailedException(org.apache.archiva.indexer.IndexCreationFailedException) ArchivaIndexingContext(org.apache.archiva.indexer.ArchivaIndexingContext) IndexingContext(org.apache.maven.index.context.IndexingContext) IOException(java.io.IOException)

Example 3 with IndexingContext

use of org.apache.maven.index.context.IndexingContext in project archiva by apache.

the class ArchivaIndexManagerMock method executeUpdateFunction.

/*
     * This method is used to do some actions around the update execution code. And to make sure, that no other
     * method is running on the same index.
     */
private void executeUpdateFunction(ArchivaIndexingContext context, IndexUpdateConsumer function) throws IndexUpdateFailedException {
    IndexingContext indexingContext = null;
    try {
        indexingContext = getMvnContext(context);
    } catch (UnsupportedBaseContextException e) {
        throw new IndexUpdateFailedException("Maven index is not supported by this context", e);
    }
    final Path ctxPath = getIndexPath(context);
    int loop = MAX_WAIT;
    boolean active = false;
    while (loop-- > 0 && !active) {
        active = activeContexts.add(ctxPath);
        try {
            Thread.currentThread().sleep(WAIT_TIME);
        } catch (InterruptedException e) {
        // Ignore this
        }
    }
    if (active) {
        try {
            function.accept(indexingContext);
        } finally {
            activeContexts.remove(ctxPath);
        }
    } else {
        throw new IndexUpdateFailedException("Timeout while waiting for index release on context " + context.getId());
    }
}
Also used : Path(java.nio.file.Path) UnsupportedBaseContextException(org.apache.archiva.indexer.UnsupportedBaseContextException) ArchivaIndexingContext(org.apache.archiva.indexer.ArchivaIndexingContext) IndexingContext(org.apache.maven.index.context.IndexingContext) IndexUpdateFailedException(org.apache.archiva.indexer.IndexUpdateFailedException)

Example 4 with IndexingContext

use of org.apache.maven.index.context.IndexingContext in project archiva by apache.

the class ArchivaIndexManagerMock method scan.

@Override
public void scan(final ArchivaIndexingContext context) throws IndexUpdateFailedException {
    executeUpdateFunction(context, indexingContext -> {
        DefaultScannerListener listener = new DefaultScannerListener(indexingContext, indexerEngine, true, null);
        ScanningRequest request = new ScanningRequest(indexingContext, listener);
        ScanningResult result = scanner.scan(request);
        if (result.hasExceptions()) {
            log.error("Exceptions occured during index scan of " + context.getId());
            result.getExceptions().stream().map(e -> e.getMessage()).distinct().limit(5).forEach(s -> log.error("Message: " + s));
        }
    });
}
Also used : ScanningResult(org.apache.maven.index.ScanningResult) AuthenticationException(org.apache.maven.wagon.authentication.AuthenticationException) ArtifactContext(org.apache.maven.index.ArtifactContext) StringUtils(org.apache.commons.lang.StringUtils) PathUtil(org.apache.archiva.common.utils.PathUtil) UnsupportedBaseContextException(org.apache.archiva.indexer.UnsupportedBaseContextException) LoggerFactory(org.slf4j.LoggerFactory) IndexCreator(org.apache.maven.index.context.IndexCreator) ArchivaIndexManager(org.apache.archiva.indexer.ArchivaIndexManager) ArchivaConfiguration(org.apache.archiva.configuration.ArchivaConfiguration) WagonFactoryException(org.apache.archiva.proxy.common.WagonFactoryException) NetworkProxyAdmin(org.apache.archiva.admin.model.networkproxy.NetworkProxyAdmin) PasswordCredentials(org.apache.archiva.repository.PasswordCredentials) Map(java.util.Map) FileUtils(org.apache.archiva.common.utils.FileUtils) IndexCreationFailedException(org.apache.archiva.indexer.IndexCreationFailedException) UnsupportedRepositoryTypeException(org.apache.archiva.repository.UnsupportedRepositoryTypeException) RemoteIndexFeature(org.apache.archiva.repository.features.RemoteIndexFeature) URI(java.net.URI) TransferFailedException(org.apache.maven.wagon.TransferFailedException) Path(java.nio.file.Path) ArchivaIndexingContext(org.apache.archiva.indexer.ArchivaIndexingContext) AbstractHttpClientWagon(org.apache.maven.wagon.shared.http.AbstractHttpClientWagon) TransferEvent(org.apache.maven.wagon.events.TransferEvent) RepositoryAdminException(org.apache.archiva.admin.model.RepositoryAdminException) IndexCreationFeature(org.apache.archiva.repository.features.IndexCreationFeature) HttpMethodConfiguration(org.apache.maven.wagon.shared.http.HttpMethodConfiguration) ProxyInfo(org.apache.maven.wagon.proxy.ProxyInfo) Collection(java.util.Collection) ConnectionException(org.apache.maven.wagon.ConnectionException) AuthorizationException(org.apache.maven.wagon.authorization.AuthorizationException) Collectors(java.util.stream.Collectors) Indexer(org.apache.maven.index.Indexer) FileNotFoundException(java.io.FileNotFoundException) ManagedRepository(org.apache.archiva.repository.ManagedRepository) IndexUpdateRequest(org.apache.maven.index.updater.IndexUpdateRequest) IndexUpdateFailedException(org.apache.archiva.indexer.IndexUpdateFailedException) List(java.util.List) HttpConfiguration(org.apache.maven.wagon.shared.http.HttpConfiguration) IndexFormatTooOldException(org.apache.maven.index_shaded.lucene.index.IndexFormatTooOldException) IndexerEngine(org.apache.maven.index.IndexerEngine) EditableRepository(org.apache.archiva.repository.EditableRepository) WagonFactoryRequest(org.apache.archiva.proxy.common.WagonFactoryRequest) Scanner(org.apache.maven.index.Scanner) NetworkProxy(org.apache.archiva.admin.model.beans.NetworkProxy) ArtifactContextProducer(org.apache.maven.index.ArtifactContextProducer) WagonFactory(org.apache.archiva.proxy.common.WagonFactory) Inject(javax.inject.Inject) Repository(org.apache.archiva.repository.Repository) RemoteRepository(org.apache.archiva.repository.RemoteRepository) Service(org.springframework.stereotype.Service) IndexPackingRequest(org.apache.maven.index.packer.IndexPackingRequest) ResourceFetcher(org.apache.maven.index.updater.ResourceFetcher) StreamWagon(org.apache.maven.wagon.StreamWagon) Logger(org.slf4j.Logger) IndexPacker(org.apache.maven.index.packer.IndexPacker) MalformedURLException(java.net.MalformedURLException) Files(java.nio.file.Files) DefaultScannerListener(org.apache.maven.index.DefaultScannerListener) ScanningRequest(org.apache.maven.index.ScanningRequest) AuthenticationInfo(org.apache.maven.wagon.authentication.AuthenticationInfo) IOException(java.io.IOException) RepositoryType(org.apache.archiva.repository.RepositoryType) IndexingContext(org.apache.maven.index.context.IndexingContext) ConcurrentSkipListSet(java.util.concurrent.ConcurrentSkipListSet) ResourceDoesNotExistException(org.apache.maven.wagon.ResourceDoesNotExistException) Paths(java.nio.file.Paths) TransferListener(org.apache.maven.wagon.events.TransferListener) Wagon(org.apache.maven.wagon.Wagon) ScanningResult(org.apache.maven.index.ScanningResult) InputStream(java.io.InputStream) ScanningRequest(org.apache.maven.index.ScanningRequest) DefaultScannerListener(org.apache.maven.index.DefaultScannerListener)

Example 5 with IndexingContext

use of org.apache.maven.index.context.IndexingContext in project archiva by apache.

the class MavenIndexManager method createManagedContext.

private IndexingContext createManagedContext(ManagedRepository repository) throws IOException {
    IndexingContext context;
    // take care first about repository location as can be relative
    Path repositoryDirectory = repository.getLocalPath();
    if (!Files.exists(repositoryDirectory)) {
        try {
            Files.createDirectories(repositoryDirectory);
        } catch (IOException e) {
            log.error("Could not create directory {}", repositoryDirectory);
        }
    }
    Path indexDirectory = null;
    if (repository.supportsFeature(IndexCreationFeature.class)) {
        indexDirectory = getIndexPath(repository);
        String indexUrl = repositoryDirectory.toUri().toURL().toExternalForm();
        try {
            context = getIndexingContext(repository, repository.getId(), repositoryDirectory, indexDirectory, indexUrl);
            context.setSearchable(repository.isScanned());
        } catch (IndexFormatTooOldException e) {
            // existing index with an old lucene format so we need to delete it!!!
            // delete it first then recreate it.
            // 
            log.warn(// 
            "the index of repository {} is too old we have to delete and recreate it", repository.getId());
            org.apache.archiva.common.utils.FileUtils.deleteDirectory(indexDirectory);
            context = getIndexingContext(repository, repository.getId(), repositoryDirectory, indexDirectory, indexUrl);
            context.setSearchable(repository.isScanned());
        }
        return context;
    } else {
        throw new IOException("No repository index defined");
    }
}
Also used : Path(java.nio.file.Path) IndexFormatTooOldException(org.apache.maven.index_shaded.lucene.index.IndexFormatTooOldException) ArchivaIndexingContext(org.apache.archiva.indexer.ArchivaIndexingContext) IndexingContext(org.apache.maven.index.context.IndexingContext) IOException(java.io.IOException)

Aggregations

IndexingContext (org.apache.maven.index.context.IndexingContext)38 Path (java.nio.file.Path)30 ArchivaIndexingContext (org.apache.archiva.indexer.ArchivaIndexingContext)27 IOException (java.io.IOException)24 UnsupportedBaseContextException (org.apache.archiva.indexer.UnsupportedBaseContextException)21 IndexCreationFailedException (org.apache.archiva.indexer.IndexCreationFailedException)15 RemoteRepository (org.apache.archiva.repository.RemoteRepository)15 ManagedRepository (org.apache.archiva.repository.ManagedRepository)14 Inject (javax.inject.Inject)12 IndexUpdateFailedException (org.apache.archiva.indexer.IndexUpdateFailedException)12 UnsupportedRepositoryTypeException (org.apache.archiva.repository.UnsupportedRepositoryTypeException)12 Indexer (org.apache.maven.index.Indexer)12 IndexPacker (org.apache.maven.index.packer.IndexPacker)12 IndexPackingRequest (org.apache.maven.index.packer.IndexPackingRequest)12 IndexFormatTooOldException (org.apache.maven.index_shaded.lucene.index.IndexFormatTooOldException)12 Logger (org.slf4j.Logger)12 LoggerFactory (org.slf4j.LoggerFactory)12 Service (org.springframework.stereotype.Service)12 URI (java.net.URI)11 Files (java.nio.file.Files)11