use of org.apache.archiva.indexer.ArchivaIndexingContext 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.archiva.indexer.ArchivaIndexingContext 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.archiva.indexer.ArchivaIndexingContext 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.archiva.indexer.ArchivaIndexingContext 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.archiva.indexer.ArchivaIndexingContext in project archiva by apache.
the class MavenIndexManager method reset.
@Override
public ArchivaIndexingContext reset(ArchivaIndexingContext context) throws IndexUpdateFailedException {
ArchivaIndexingContext ctx;
executeUpdateFunction(context, indexingContext -> {
try {
indexingContext.close(true);
} catch (IOException e) {
log.warn("Index close failed");
}
try {
FileUtils.deleteDirectory(Paths.get(context.getPath()));
} catch (IOException e) {
throw new IndexUpdateFailedException("Could not delete index files");
}
});
try {
Repository repo = context.getRepository();
ctx = createContext(context.getRepository());
if (repo instanceof EditableRepository) {
((EditableRepository) repo).setIndexingContext(ctx);
}
} catch (IndexCreationFailedException e) {
throw new IndexUpdateFailedException("Could not create index");
}
return ctx;
}
Aggregations