use of org.apache.archiva.repository.storage.StorageAsset in project archiva by apache.
the class ArchivaIndexManagerMock method removeArtifactsFromIndex.
@Override
public void removeArtifactsFromIndex(ArchivaIndexingContext context, Collection<URI> artifactReference) throws IndexUpdateFailedException {
final StorageAsset ctxUri = context.getPath();
executeUpdateFunction(context, indexingContext -> {
Collection<ArtifactContext> artifacts = artifactReference.stream().map(r -> artifactContextProducer.getArtifactContext(indexingContext, Paths.get(ctxUri.getFilePath().toUri().resolve(r)).toFile())).collect(Collectors.toList());
try {
indexer.deleteArtifactsFromIndex(artifacts, indexingContext);
} catch (IOException e) {
log.error("IOException while removing artifact {}", e.getMessage(), e);
throw new IndexUpdateFailedException("Error occured while removing artifact from index of " + context.getId() + (StringUtils.isNotEmpty(e.getMessage()) ? ": " + e.getMessage() : ""));
}
});
}
use of org.apache.archiva.repository.storage.StorageAsset in project archiva by apache.
the class ArchivaIndexManagerMock method addArtifactsToIndex.
@Override
public void addArtifactsToIndex(final ArchivaIndexingContext context, final Collection<URI> artifactReference) throws IndexUpdateFailedException {
StorageAsset ctxUri = context.getPath();
executeUpdateFunction(context, indexingContext -> {
Collection<ArtifactContext> artifacts = artifactReference.stream().map(r -> artifactContextProducer.getArtifactContext(indexingContext, Paths.get(ctxUri.getFilePath().toUri().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.repository.storage.StorageAsset 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 {
StorageAsset 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.getFilePath());
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.repository.storage.StorageAsset in project archiva by apache.
the class ArchivaIndexManagerMock method createManagedContext.
private IndexingContext createManagedContext(ManagedRepository repository) throws IOException {
IndexingContext context;
// take care first about repository location as can be relative
Path repositoryDirectory = repository.getRoot().getFilePath();
if (!Files.exists(repositoryDirectory)) {
try {
Files.createDirectories(repositoryDirectory);
} catch (IOException e) {
log.error("Could not create directory {}", repositoryDirectory);
}
}
StorageAsset 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.getFilePath());
context = getIndexingContext(repository, repository.getId(), repositoryDirectory, indexDirectory, indexUrl);
context.setSearchable(repository.isScanned());
}
return context;
} else {
throw new IOException("No repository index defined");
}
}
use of org.apache.archiva.repository.storage.StorageAsset in project archiva by apache.
the class StorageUtil method copyRecursively.
/**
* Deletes the given asset and all child assets recursively.
* @param srcAsset The source directory
* @param destAsset The destination directory
* @param stopOnError if <code>true</code> the traversal stops, if an exception is encountered
* @return returns <code>true</code>, if every item was removed. If an IOException was encountered during
* traversal it returns <code>false</code>
*/
public static final boolean copyRecursively(final StorageAsset srcAsset, final StorageAsset destAsset, final boolean stopOnError) throws IOException {
try {
if (srcAsset.isFileBased() && destAsset.isFileBased()) {
Path src = srcAsset.getFilePath();
Path dest = destAsset.getFilePath();
return Files.walk(src).map(source -> {
try {
Files.copy(source, dest.resolve(src.relativize(source)), StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES);
return Boolean.TRUE;
} catch (IOException e) {
if (stopOnError) {
throw new RuntimeException(e);
} else {
return Boolean.FALSE;
}
}
}).reduce((a, b) -> Boolean.logicalAnd(a, b)).get();
} else {
try (Stream<StorageAsset> stream = newAssetStream(srcAsset)) {
if (!destAsset.exists() && srcAsset.isContainer()) {
destAsset.create(AssetType.CONTAINER);
}
return stream.map(a -> {
try {
String relativePath = destAsset.relativize(a);
System.out.println("Destination relative: " + relativePath);
StorageAsset destFile = destAsset.resolve(relativePath);
assert destFile != null;
System.out.println("Destination " + destFile.getPath() + " " + a.isContainer());
if (a.isContainer()) {
destFile.create(AssetType.CONTAINER);
} else {
if (!destFile.getParent().exists()) {
System.out.println("Creating parent " + destFile.getParent());
destFile.getParent().create(AssetType.CONTAINER);
}
System.out.println("Copying " + a.getPath() + "->" + destFile.getPath());
copy(a.getReadChannel(), destFile.getWriteChannel(true));
}
return Boolean.TRUE;
} catch (IOException e) {
LOG.error("Could not copy asset {}: {}", a.getPath(), e.getMessage(), e);
// Returning true, if exception
if (stopOnError) {
throw new RuntimeException(e);
} else {
return Boolean.FALSE;
}
}
}).reduce((a, b) -> Boolean.logicalAnd(a, b)).orElse(Boolean.FALSE);
}
}
} catch (RuntimeException e) {
System.err.println("Exception " + e.getMessage());
e.printStackTrace();
return false;
}
}
Aggregations