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() : ""));
}
});
}
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.");
}
}
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());
}
}
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));
}
});
}
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");
}
}
Aggregations