From 0ef19fe33800beaa5640e88cdfd42ecf2ffa67c1 Mon Sep 17 00:00:00 2001 From: Martin Stockhammer Date: Tue, 27 Mar 2018 07:31:04 +0200 Subject: [PATCH] Moving repo features to API module --- .../archiva/indexer/ArchivaIndexManager.java | 7 +++ .../archiva/indexer/GenericIndexManager.java | 51 ++++++++++++++++ .../archiva/repository/RepositoryEvent.java | 12 ++-- .../repository/features/AbstractFeature.java | 0 .../features/ArtifactCleanupFeature.java | 0 .../features/IndexCreationEvent.java | 3 +- .../features/IndexCreationFeature.java | 15 ++++- .../features/RemoteIndexFeature.java | 0 .../features/StagingRepositoryFeature.java | 0 .../repository/BasicManagedRepository.java | 2 +- .../repository/RepositoryRegistry.java | 58 ++++++++++--------- .../mock/ArchivaIndexManagerMock.java | 5 ++ 12 files changed, 117 insertions(+), 36 deletions(-) rename archiva-modules/archiva-base/{archiva-repository-layer => archiva-repository-api}/src/main/java/org/apache/archiva/repository/features/AbstractFeature.java (100%) rename archiva-modules/archiva-base/{archiva-repository-layer => archiva-repository-api}/src/main/java/org/apache/archiva/repository/features/ArtifactCleanupFeature.java (100%) rename archiva-modules/archiva-base/{archiva-repository-layer => archiva-repository-api}/src/main/java/org/apache/archiva/repository/features/IndexCreationEvent.java (90%) rename archiva-modules/archiva-base/{archiva-repository-layer => archiva-repository-api}/src/main/java/org/apache/archiva/repository/features/IndexCreationFeature.java (89%) rename archiva-modules/archiva-base/{archiva-repository-layer => archiva-repository-api}/src/main/java/org/apache/archiva/repository/features/RemoteIndexFeature.java (100%) rename archiva-modules/archiva-base/{archiva-repository-layer => archiva-repository-api}/src/main/java/org/apache/archiva/repository/features/StagingRepositoryFeature.java (100%) diff --git a/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/indexer/ArchivaIndexManager.java b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/indexer/ArchivaIndexManager.java index 63a8a9297..5e64d23c9 100644 --- a/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/indexer/ArchivaIndexManager.java +++ b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/indexer/ArchivaIndexManager.java @@ -24,6 +24,7 @@ import org.apache.archiva.repository.RepositoryEventListener; import org.apache.archiva.repository.RepositoryType; import java.net.URI; +import java.nio.file.Path; import java.util.Collection; public interface ArchivaIndexManager { @@ -95,4 +96,10 @@ public interface ArchivaIndexManager { * @throws IndexCreationFailedException */ ArchivaIndexingContext move(ArchivaIndexingContext context, Repository repo) throws IndexCreationFailedException; + + /** + * Returns the local path where the index is stored. + * @return + */ + public void updateLocalIndexPath(Repository repo); } diff --git a/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/indexer/GenericIndexManager.java b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/indexer/GenericIndexManager.java index 8d8072e32..8a059f894 100644 --- a/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/indexer/GenericIndexManager.java +++ b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/indexer/GenericIndexManager.java @@ -19,17 +19,29 @@ package org.apache.archiva.indexer; * under the License. */ +import org.apache.archiva.common.utils.PathUtil; import org.apache.archiva.repository.Repository; import org.apache.archiva.repository.RepositoryEvent; import org.apache.archiva.repository.RepositoryType; +import org.apache.archiva.repository.features.IndexCreationFeature; +import org.apache.commons.lang.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; +import java.io.IOException; import java.net.URI; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.Collection; @Service("indexManager#none") public class GenericIndexManager implements ArchivaIndexManager { + private final Logger log = LoggerFactory.getLogger(GenericIndexManager.class); + + public static final String DEFAULT_INDEXER_DIR = ".indexer"; + @Override public void pack(ArchivaIndexingContext context) { @@ -75,4 +87,43 @@ public class GenericIndexManager implements ArchivaIndexManager { return null; } + @Override + public void updateLocalIndexPath(Repository repo) { + if (repo.supportsFeature(IndexCreationFeature.class)) { + IndexCreationFeature icf = repo.getFeature(IndexCreationFeature.class).get(); + try { + icf.setLocalIndexPath(getIndexPath(repo)); + } catch (IOException e) { + log.error("Could not set local index path for {}. New URI: {}", repo.getId(), icf.getIndexPath()); + } + } + } + + private Path getIndexPath(Repository repo) throws IOException { + IndexCreationFeature icf = repo.getFeature(IndexCreationFeature.class).get(); + Path repoDir = repo.getLocalPath(); + URI indexDir = icf.getIndexPath(); + Path indexDirectory = null; + if ( ! StringUtils.isEmpty(indexDir.toString( ) ) ) + { + + indexDirectory = PathUtil.getPathFromUri( indexDir ); + // not absolute so create it in repository directory + if ( !indexDirectory.isAbsolute( ) ) + { + indexDirectory = repoDir.resolve( indexDirectory ); + } + } + else + { + indexDirectory = repoDir.resolve( DEFAULT_INDEXER_DIR); + } + + if ( !Files.exists( indexDirectory ) ) + { + Files.createDirectories( indexDirectory ); + } + return indexDirectory; + } + } diff --git a/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/RepositoryEvent.java b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/RepositoryEvent.java index d8bdf95fe..8a9db881f 100644 --- a/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/RepositoryEvent.java +++ b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/RepositoryEvent.java @@ -29,12 +29,12 @@ import java.time.LocalDateTime; public class RepositoryEvent { final EventType type; - final String repo; + final Repository repo; final T value; final T oldValue; final LocalDateTime instant; - public RepositoryEvent(EventType type, String repo, T oldValue, T value) { + public RepositoryEvent(EventType type, Repository repo, T oldValue, T value) { this.type = type; this.repo = repo; this.value = value; @@ -47,19 +47,19 @@ public class RepositoryEvent { } - EventType getType() { + public EventType getType() { return type; }; - String getRepositoryId() { + public Repository getRepository() { return repo; }; - T getValue() { + public T getValue() { return value; } - T getOldValue() { + public T getOldValue() { return oldValue; } diff --git a/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/features/AbstractFeature.java b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/features/AbstractFeature.java similarity index 100% rename from archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/features/AbstractFeature.java rename to archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/features/AbstractFeature.java diff --git a/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/features/ArtifactCleanupFeature.java b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/features/ArtifactCleanupFeature.java similarity index 100% rename from archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/features/ArtifactCleanupFeature.java rename to archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/features/ArtifactCleanupFeature.java diff --git a/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/features/IndexCreationEvent.java b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/features/IndexCreationEvent.java similarity index 90% rename from archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/features/IndexCreationEvent.java rename to archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/features/IndexCreationEvent.java index f871dc98c..4c9dc8eae 100644 --- a/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/features/IndexCreationEvent.java +++ b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/features/IndexCreationEvent.java @@ -19,6 +19,7 @@ package org.apache.archiva.repository.features; * under the License. */ +import org.apache.archiva.repository.Repository; import org.apache.archiva.repository.RepositoryEvent; import java.net.URI; @@ -29,7 +30,7 @@ public class IndexCreationEvent extends RepositoryEvent { URI_CHANGE } - IndexCreationEvent(String repo, URI oldValue, URI value) { + IndexCreationEvent(Repository repo, URI oldValue, URI value) { super(Index.URI_CHANGE, repo, oldValue, value); } } diff --git a/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/features/IndexCreationFeature.java b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/features/IndexCreationFeature.java similarity index 89% rename from archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/features/IndexCreationFeature.java rename to archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/features/IndexCreationFeature.java index eaf532fac..0aa44a20b 100644 --- a/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/features/IndexCreationFeature.java +++ b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/features/IndexCreationFeature.java @@ -26,6 +26,7 @@ import org.apache.commons.lang.StringUtils; import java.net.URI; import java.net.URISyntaxException; +import java.nio.file.Path; import java.util.ArrayList; import java.util.EventListener; import java.util.List; @@ -42,9 +43,11 @@ public class IndexCreationFeature extends AbstractFeature implements RepositoryF private URI indexPath; - private String repo; + private Path localIndexPath; - public IndexCreationFeature(String repoId, RepositoryEventListener listener) { + private Repository repo; + + public IndexCreationFeature(Repository repoId, RepositoryEventListener listener) { super(listener); this.repo = repoId; try @@ -109,4 +112,12 @@ public class IndexCreationFeature extends AbstractFeature implements RepositoryF public boolean hasIndex() { return this.indexPath!=null && !StringUtils.isEmpty( this.indexPath.getPath() ); } + + public Path getLocalIndexPath() { + return localIndexPath; + } + + public void setLocalIndexPath(Path localIndexPath) { + this.localIndexPath = localIndexPath; + } } diff --git a/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/features/RemoteIndexFeature.java b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/features/RemoteIndexFeature.java similarity index 100% rename from archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/features/RemoteIndexFeature.java rename to archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/features/RemoteIndexFeature.java diff --git a/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/features/StagingRepositoryFeature.java b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/features/StagingRepositoryFeature.java similarity index 100% rename from archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/features/StagingRepositoryFeature.java rename to archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/features/StagingRepositoryFeature.java diff --git a/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/BasicManagedRepository.java b/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/BasicManagedRepository.java index 8df9883a0..6aa2fe927 100644 --- a/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/BasicManagedRepository.java +++ b/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/BasicManagedRepository.java @@ -64,7 +64,7 @@ public class BasicManagedRepository extends AbstractManagedRepository } private void initFeatures() { - IndexCreationFeature indexCreationFeature = new IndexCreationFeature(this.getId(), this); + IndexCreationFeature indexCreationFeature = new IndexCreationFeature(this, this); addFeature( artifactCleanupFeature ); addFeature( indexCreationFeature ); addFeature( stagingRepositoryFeature ); diff --git a/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/RepositoryRegistry.java b/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/RepositoryRegistry.java index 17a08f760..984a2f6f6 100644 --- a/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/RepositoryRegistry.java +++ b/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/RepositoryRegistry.java @@ -19,19 +19,14 @@ package org.apache.archiva.repository; * under the License. */ -import org.apache.archiva.configuration.ArchivaConfiguration; -import org.apache.archiva.configuration.Configuration; -import org.apache.archiva.configuration.ConfigurationEvent; -import org.apache.archiva.configuration.ConfigurationListener; -import org.apache.archiva.configuration.IndeterminateConfigurationException; -import org.apache.archiva.configuration.ManagedRepositoryConfiguration; -import org.apache.archiva.configuration.RemoteRepositoryConfiguration; +import org.apache.archiva.configuration.*; import org.apache.archiva.indexer.*; +import org.apache.archiva.metadata.model.facets.AuditEvent; import org.apache.archiva.redback.components.registry.RegistryException; -import org.apache.archiva.repository.features.ArtifactCleanupFeature; import org.apache.archiva.repository.features.IndexCreationEvent; import org.apache.archiva.repository.features.IndexCreationFeature; import org.apache.archiva.repository.features.StagingRepositoryFeature; +import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; @@ -40,7 +35,6 @@ import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import javax.inject.Inject; import javax.inject.Named; -import java.io.IOException; import java.util.*; import java.util.concurrent.locks.ReentrantReadWriteLock; import java.util.stream.Collectors; @@ -239,6 +233,7 @@ public class RepositoryRegistry implements ConfigurationListener, RepositoryEven ArchivaIndexManager idxManager = getIndexManager(editableRepo.getType()); try { editableRepo.setIndexingContext(idxManager.createContext(editableRepo)); + idxManager.updateLocalIndexPath(editableRepo); } catch (IndexCreationFailedException e) { throw new RepositoryException("Could not create index for repository "+editableRepo.getId()+": "+e.getMessage(),e); } @@ -855,6 +850,21 @@ public class RepositoryRegistry implements ConfigurationListener, RepositoryEven } + + private void doRemoveRepo(RemoteRepository repo, Configuration configuration) { + repo.close(); + RemoteRepositoryConfiguration cfg = configuration.findRemoteRepositoryById(repo.getId()); + if (cfg != null) { + configuration.removeRemoteRepository(cfg); + } + List proxyConnectors = new ArrayList<>(configuration.getProxyConnectors()); + for (ProxyConnectorConfiguration proxyConnector : proxyConnectors) { + if (StringUtils.equals(proxyConnector.getTargetRepoId(), repo.getId())) { + configuration.removeProxyConnector(proxyConnector); + } + } + } + /** * Removes the remote repository from the registry and configuration. * The change is saved to the configuration immediately. @@ -864,23 +874,18 @@ public class RepositoryRegistry implements ConfigurationListener, RepositoryEven */ public void removeRepository( RemoteRepository remoteRepository ) throws RepositoryException { + final String id = remoteRepository.getId(); RemoteRepository repo = getRemoteRepository( id ); if (repo!=null) { rwLock.writeLock().lock(); try { repo = remoteRepositories.remove( id ); - if (repo!=null) { - repo.close(); Configuration configuration = getArchivaConfiguration().getConfiguration(); - RemoteRepositoryConfiguration cfg = configuration.findRemoteRepositoryById( id ); - if (cfg!=null) { - configuration.removeRemoteRepository( cfg ); - } + doRemoveRepo(repo, configuration); getArchivaConfiguration().save( configuration ); } - } catch ( RegistryException | IndeterminateConfigurationException e ) { @@ -904,11 +909,7 @@ public class RepositoryRegistry implements ConfigurationListener, RepositoryEven try { repo = remoteRepositories.remove( id ); if (repo!=null) { - repo.close(); - RemoteRepositoryConfiguration cfg = configuration.findRemoteRepositoryById( id ); - if (cfg!=null) { - configuration.removeRemoteRepository( cfg ); - } + doRemoveRepo(repo, configuration); } } finally { @@ -1017,13 +1018,18 @@ public class RepositoryRegistry implements ConfigurationListener, RepositoryEven @Override public void raise(RepositoryEvent event) { if (event.getType().equals(IndexCreationEvent.Index.URI_CHANGE)) { - if (managedRepositories.containsKey(event.getRepositoryId()) || - remoteRepositories.containsKey(event.getRepositoryId())) { - EditableRepository repo = (EditableRepository) getRepository(event.getRepositoryId()); + if (managedRepositories.containsKey(event.getRepository().getId()) || + remoteRepositories.containsKey(event.getRepository().getId())) { + EditableRepository repo = (EditableRepository) event.getRepository(); if (repo != null && repo.getIndexingContext()!=null) { try { - ArchivaIndexingContext newCtx = getIndexManager(repo.getType()).move(repo.getIndexingContext(), repo); - repo.setIndexingContext(newCtx); + ArchivaIndexManager idxmgr = getIndexManager(repo.getType()); + if (idxmgr != null) { + ArchivaIndexingContext newCtx = idxmgr.move(repo.getIndexingContext(), repo); + repo.setIndexingContext(newCtx); + idxmgr.updateLocalIndexPath(repo); + } + } catch (IndexCreationFailedException e) { log.error("Could not move index to new directory {}", e.getMessage(), e); } diff --git a/archiva-modules/archiva-base/archiva-repository-layer/src/test/java/org/apache/archiva/repository/mock/ArchivaIndexManagerMock.java b/archiva-modules/archiva-base/archiva-repository-layer/src/test/java/org/apache/archiva/repository/mock/ArchivaIndexManagerMock.java index e81bcff08..65d8196b9 100644 --- a/archiva-modules/archiva-base/archiva-repository-layer/src/test/java/org/apache/archiva/repository/mock/ArchivaIndexManagerMock.java +++ b/archiva-modules/archiva-base/archiva-repository-layer/src/test/java/org/apache/archiva/repository/mock/ArchivaIndexManagerMock.java @@ -82,4 +82,9 @@ public class ArchivaIndexManagerMock implements ArchivaIndexManager { public ArchivaIndexingContext move(ArchivaIndexingContext context, Repository repo) throws IndexCreationFailedException { return null; } + + @Override + public void updateLocalIndexPath(Repository repo) { + + } } -- 2.39.5