use of org.apache.archiva.repository.storage.StorageAsset in project archiva by apache.
the class MavenIndexManager 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 MavenIndexManager 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();
Path newFilePath = newPath.getFilePath();
if (oldPath.equals(newFilePath)) {
// 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);
if (Files.exists(newFilePath)) {
FileUtils.copyContent(oldPath, newFilePath);
FileUtils.deleteDirectory(oldPath);
} else {
Files.move(oldPath, newFilePath);
}
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 MavenIndexManager method createRemoteContext.
private IndexingContext createRemoteContext(RemoteRepository remoteRepository) throws IOException {
String contextKey = "remote-" + remoteRepository.getId();
// create remote repository path
Path repoDir = remoteRepository.getRoot().getFilePath();
if (!Files.exists(repoDir)) {
Files.createDirectories(repoDir);
}
StorageAsset indexDirectory;
// is there configured indexDirectory ?
if (remoteRepository.supportsFeature(RemoteIndexFeature.class)) {
RemoteIndexFeature rif = remoteRepository.getFeature(RemoteIndexFeature.class);
indexDirectory = getIndexPath(remoteRepository);
String remoteIndexUrl = calculateIndexRemoteUrl(remoteRepository.getLocation(), rif);
try {
return getIndexingContext(remoteRepository, contextKey, repoDir, indexDirectory, remoteIndexUrl);
} 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", remoteRepository.getId());
org.apache.archiva.common.utils.FileUtils.deleteDirectory(indexDirectory.getFilePath());
return getIndexingContext(remoteRepository, contextKey, repoDir, indexDirectory, remoteIndexUrl);
}
} else {
throw new IOException("No remote index defined");
}
}
use of org.apache.archiva.repository.storage.StorageAsset in project archiva by apache.
the class MavenIndexManager method getIndexPath.
private StorageAsset getIndexPath(URI indexDirUri, RepositoryStorage repoStorage, String defaultDir) throws IOException {
StorageAsset rootAsset = repoStorage.getRoot();
RepositoryStorage storage = rootAsset.getStorage();
Path indexDirectory;
Path repositoryPath = rootAsset.getFilePath().toAbsolutePath();
StorageAsset indexDir;
if (!StringUtils.isEmpty(indexDirUri.toString())) {
indexDirectory = PathUtil.getPathFromUri(indexDirUri);
// not absolute so create it in repository directory
if (indexDirectory.isAbsolute() && !indexDirectory.startsWith(repositoryPath)) {
if (storage instanceof FilesystemStorage) {
FilesystemStorage fsStorage = (FilesystemStorage) storage;
FilesystemStorage indexStorage = new FilesystemStorage(indexDirectory.getParent(), fsStorage.getFileLockManager());
indexDir = indexStorage.getAsset(indexDirectory.getFileName().toString());
} else {
throw new IOException("The given storage is not file based.");
}
} else if (indexDirectory.isAbsolute()) {
indexDir = storage.getAsset(repositoryPath.relativize(indexDirectory).toString());
} else {
indexDir = storage.getAsset(indexDirectory.toString());
}
} else {
indexDir = storage.getAsset(defaultDir);
}
if (!indexDir.exists()) {
indexDir.create(AssetType.CONTAINER);
}
return indexDir;
}
use of org.apache.archiva.repository.storage.StorageAsset 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.getRoot().getFilePath();
if (!Files.exists(repositoryDirectory)) {
try {
Files.createDirectories(repositoryDirectory);
} catch (IOException e) {
log.error("Could not create directory {}", repositoryDirectory);
}
}
StorageAsset indexDirectory;
if (repository.supportsFeature(IndexCreationFeature.class)) {
indexDirectory = getIndexPath(repository);
log.debug("Preparing index at {}", indexDirectory);
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");
}
}
Aggregations