From: Martin Stockhammer Date: Mon, 9 Aug 2021 21:40:20 +0000 (+0200) Subject: Refactoring repository registry X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=3e959d5b07d1cbd0f6111b59d8928e9502aa258f;p=archiva.git Refactoring repository registry --- diff --git a/archiva-modules/archiva-base/archiva-configuration/src/main/java/org/apache/archiva/configuration/DefaultArchivaConfiguration.java b/archiva-modules/archiva-base/archiva-configuration/src/main/java/org/apache/archiva/configuration/DefaultArchivaConfiguration.java index ee80fc8fc..54961b97b 100644 --- a/archiva-modules/archiva-base/archiva-configuration/src/main/java/org/apache/archiva/configuration/DefaultArchivaConfiguration.java +++ b/archiva-modules/archiva-base/archiva-configuration/src/main/java/org/apache/archiva/configuration/DefaultArchivaConfiguration.java @@ -430,7 +430,7 @@ public class DefaultArchivaConfiguration if (section == null) { section = baseSection; if (section == null) { - section = createDefaultConfigurationFile(); + section = createDefaultConfigurationFile(eventTag); } } else if (baseSection != null) { Collection keys = baseSection.getKeys(); @@ -513,7 +513,7 @@ public class DefaultArchivaConfiguration } } - private Registry createDefaultConfigurationFile() + private Registry createDefaultConfigurationFile(String eventTag) throws RegistryException { // TODO: may not be needed under commons-configuration 1.4 - check @@ -543,7 +543,7 @@ public class DefaultArchivaConfiguration addRegistryChangeListener(regListener); } - triggerEvent(ConfigurationEvent.SAVED, "default-file"); + triggerEvent(ConfigurationEvent.SAVED, eventTag==null?"default-file":eventTag); Registry section = registry.getSection(KEY + ".user"); if (section == null) { diff --git a/archiva-modules/archiva-base/archiva-configuration/src/main/java/org/apache/archiva/configuration/RepositoryGroupConfiguration.java b/archiva-modules/archiva-base/archiva-configuration/src/main/java/org/apache/archiva/configuration/RepositoryGroupConfiguration.java index d0dc381e4..877ccaadc 100644 --- a/archiva-modules/archiva-base/archiva-configuration/src/main/java/org/apache/archiva/configuration/RepositoryGroupConfiguration.java +++ b/archiva-modules/archiva-base/archiva-configuration/src/main/java/org/apache/archiva/configuration/RepositoryGroupConfiguration.java @@ -29,7 +29,7 @@ import java.util.List; * @version $Revision$ $Date$ */ @SuppressWarnings( "all" ) -public class RepositoryGroupConfiguration +public class RepositoryGroupConfiguration extends AbstractRepositoryConfiguration implements Serializable { diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-consumer-archetype/src/main/resources/archetype-resources/src/test/java/SimpleArtifactConsumerTest.java b/archiva-modules/archiva-base/archiva-consumers/archiva-consumer-archetype/src/main/resources/archetype-resources/src/test/java/SimpleArtifactConsumerTest.java index dcb92399b..9f3f2f3f5 100644 --- a/archiva-modules/archiva-base/archiva-consumers/archiva-consumer-archetype/src/main/resources/archetype-resources/src/test/java/SimpleArtifactConsumerTest.java +++ b/archiva-modules/archiva-base/archiva-consumers/archiva-consumer-archetype/src/main/resources/archetype-resources/src/test/java/SimpleArtifactConsumerTest.java @@ -21,10 +21,13 @@ package $package; import org.apache.archiva.metadata.repository.MetadataRepository; import org.apache.archiva.metadata.repository.RepositorySessionFactory; +import org.apache.archiva.repository.base.ArchivaRepositoryRegistry; import org.apache.archiva.repository.base.managed.BasicManagedRepository; +import org.apache.archiva.repository.base.managed.ManagedRepositoryHandler; import org.apache.archiva.repository.RepositoryException; import org.apache.archiva.repository.RepositoryRegistry; import org.junit.Before; +import org.junit.After; import org.junit.Test; import org.junit.runner.RunWith; import org.slf4j.Logger; @@ -52,7 +55,15 @@ public class SimpleArtifactConsumerTest private SimpleArtifactConsumer consumer; @Inject - private RepositoryRegistry repositoryRegistry; + private ArchivaRepositoryRegistry repositoryRegistry; + + @SuppressWarnings( "unused" ) + @Inject + private ManagedRepositoryHandler managedRepositoryHandler; + + @SuppressWarnings( "unused" ) + @Inject + private org.apache.archiva.repository.base.group.RepositoryGroupHandler repositoryGroupHandler; @Inject private RepositorySessionFactory repositorySessionFactory; @@ -70,9 +81,20 @@ public class SimpleArtifactConsumerTest setUpMockRepository(); } + @After + public void tearDown() { + try + { + repositoryRegistry.removeRepository( "test-consumer-repository" ); + } catch (Throwable ex) { + log.error( "Could not remove repository: {}", ex.getMessage( ) ); + } + } + private void setUpMockRepository() throws IOException, RepositoryException { + ((ArchivaRepositoryRegistry)repositoryRegistry).setIgnoreIndexing( true ); Path repoDir = Paths.get( "target/test-consumer-repo" ); Files.createDirectories( repoDir ); repoDir.toFile().deleteOnExit(); diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/consumers/core/AbstractArtifactConsumerTest.java b/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/consumers/core/AbstractArtifactConsumerTest.java index e0197f19c..b8f6d55e1 100644 --- a/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/consumers/core/AbstractArtifactConsumerTest.java +++ b/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/consumers/core/AbstractArtifactConsumerTest.java @@ -25,7 +25,10 @@ import org.apache.archiva.configuration.FileType; import org.apache.archiva.configuration.FileTypes; import org.apache.archiva.consumers.KnownRepositoryContentConsumer; import org.apache.archiva.consumers.functors.ConsumerWantsFilePredicate; +import org.apache.archiva.repository.RepositoryRegistry; import org.apache.archiva.repository.base.ArchivaRepositoryRegistry; +import org.apache.archiva.repository.base.group.RepositoryGroupHandler; +import org.apache.archiva.repository.base.managed.ManagedRepositoryHandler; import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner; import org.apache.commons.lang3.StringUtils; import org.junit.After; @@ -56,6 +59,19 @@ public abstract class AbstractArtifactConsumerTest @Inject ArchivaConfiguration archivaConfiguration; + @SuppressWarnings( "unused" ) + @Inject + RepositoryRegistry repositoryRegistry; + + @SuppressWarnings( "unused" ) + @Inject + RepositoryGroupHandler repositoryGroupHandler; + + @SuppressWarnings( "unused" ) + @Inject + ManagedRepositoryHandler managedRepositoryHandler; + + @Before public void setUp() throws Exception diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/consumers/core/repository/AbstractRepositoryPurgeTest.java b/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/consumers/core/repository/AbstractRepositoryPurgeTest.java index ed42ef475..5300a4a67 100644 --- a/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/consumers/core/repository/AbstractRepositoryPurgeTest.java +++ b/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/consumers/core/repository/AbstractRepositoryPurgeTest.java @@ -25,6 +25,7 @@ import org.apache.archiva.metadata.repository.RepositorySession; import org.apache.archiva.metadata.repository.RepositorySessionFactory; import org.apache.archiva.repository.ManagedRepositoryContent; import org.apache.archiva.repository.base.group.RepositoryGroupHandler; +import org.apache.archiva.repository.base.managed.ManagedRepositoryHandler; import org.apache.archiva.repository.maven.metadata.storage.Maven2RepositoryPathTranslator; import org.apache.archiva.repository.base.managed.BasicManagedRepository; import org.apache.archiva.repository.ReleaseScheme; @@ -115,6 +116,10 @@ public abstract class AbstractRepositoryPurgeTest @Inject RepositoryGroupHandler repositoryGroupHandler; + @SuppressWarnings( "unused" ) + @Inject + ManagedRepositoryHandler managedRepositoryHandler; + @Before public void setUp() diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-indexer-consumers/src/test/java/org/apache/archiva/consumers/lucene/NexusIndexerConsumerTest.java b/archiva-modules/archiva-base/archiva-consumers/archiva-indexer-consumers/src/test/java/org/apache/archiva/consumers/lucene/NexusIndexerConsumerTest.java index eb735e6fd..d6198e3d1 100644 --- a/archiva-modules/archiva-base/archiva-consumers/archiva-indexer-consumers/src/test/java/org/apache/archiva/consumers/lucene/NexusIndexerConsumerTest.java +++ b/archiva-modules/archiva-base/archiva-consumers/archiva-indexer-consumers/src/test/java/org/apache/archiva/consumers/lucene/NexusIndexerConsumerTest.java @@ -28,6 +28,7 @@ import org.apache.archiva.repository.base.ArchivaRepositoryRegistry; import org.apache.archiva.repository.base.managed.BasicManagedRepository; import org.apache.archiva.repository.ReleaseScheme; import org.apache.archiva.repository.base.group.RepositoryGroupHandler; +import org.apache.archiva.repository.base.managed.ManagedRepositoryHandler; import org.apache.archiva.scheduler.ArchivaTaskScheduler; import org.apache.archiva.scheduler.indexing.ArtifactIndexingTask; import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner; @@ -101,6 +102,10 @@ public class NexusIndexerConsumerTest @Inject RepositoryGroupHandler repositoryGroupHandler; + @SuppressWarnings( "unused" ) + @Inject + ManagedRepositoryHandler managedRepositoryHandler; + @Override @Before public void setUp() diff --git a/archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-default/src/main/java/org/apache/archiva/admin/repository/managed/DefaultManagedRepositoryAdmin.java b/archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-default/src/main/java/org/apache/archiva/admin/repository/managed/DefaultManagedRepositoryAdmin.java index 4917cb7a7..b8cd3a893 100644 --- a/archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-default/src/main/java/org/apache/archiva/admin/repository/managed/DefaultManagedRepositoryAdmin.java +++ b/archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-default/src/main/java/org/apache/archiva/admin/repository/managed/DefaultManagedRepositoryAdmin.java @@ -40,8 +40,11 @@ import org.apache.archiva.components.taskqueue.TaskQueueException; import org.apache.archiva.redback.role.RoleManager; import org.apache.archiva.redback.role.RoleManagerException; import org.apache.archiva.repository.ReleaseScheme; +import org.apache.archiva.repository.Repository; import org.apache.archiva.repository.RepositoryException; import org.apache.archiva.repository.RepositoryRegistry; +import org.apache.archiva.repository.base.group.RepositoryGroupHandler; +import org.apache.archiva.repository.base.managed.ManagedRepositoryHandler; import org.apache.archiva.repository.features.ArtifactCleanupFeature; import org.apache.archiva.repository.features.IndexCreationFeature; import org.apache.archiva.repository.features.StagingRepositoryFeature; @@ -85,6 +88,12 @@ public class DefaultManagedRepositoryAdmin @Inject private RepositoryRegistry repositoryRegistry; + @Inject + private ManagedRepositoryHandler managedRepositoryHandler; + + @Inject + private RepositoryGroupHandler repositoryGroupHandler; + @Inject @Named(value = "archivaTaskScheduler#repository") private RepositoryArchivaTaskScheduler repositoryTaskScheduler; @@ -223,19 +232,9 @@ public class DefaultManagedRepositoryAdmin Configuration configuration = getArchivaConfiguration().getConfiguration(); try { - org.apache.archiva.repository.ManagedRepository newRepo = repositoryRegistry.putRepository( repoConfig, configuration ); + org.apache.archiva.repository.ManagedRepository newRepo = repositoryRegistry.putRepository( repoConfig ); log.debug("Added new repository {}", newRepo.getId()); - org.apache.archiva.repository.ManagedRepository stagingRepo = null; addRepositoryRoles( newRepo.getId() ); - if ( newRepo.supportsFeature( StagingRepositoryFeature.class )) { - StagingRepositoryFeature stf = newRepo.getFeature( StagingRepositoryFeature.class ).get(); - stagingRepo = stf.getStagingRepository(); - if (stf.isStageRepoNeeded() && stagingRepo != null) { - addRepositoryRoles( stagingRepo.getId() ); - triggerAuditEvent( stagingRepo.getId(), null, AuditEvent.ADD_MANAGED_REPO, auditInformation ); - } - } - saveConfiguration( configuration ); //MRM-1342 Repository statistics report doesn't appear to be working correctly //scan repository when adding of repository is successful try @@ -245,9 +244,13 @@ public class DefaultManagedRepositoryAdmin scanRepository( newRepo.getId(), true ); } - if ( stagingRepo!=null && stagingRepo.isScanned() ) + org.apache.archiva.repository.ManagedRepository stagingRepo = newRepo.getFeature( StagingRepositoryFeature.class ).get( ).getStagingRepository( ); + if ( stagingRepo!=null) { - scanRepository( stagingRepo.getId(), true ); + if (stagingRepo.isScanned()) { + scanRepository( stagingRepo.getId(), true ); + } + addRepositoryRoles( stagingRepo.getId( ) ); } } catch ( Exception e ) @@ -285,14 +288,8 @@ public class DefaultManagedRepositoryAdmin org.apache.archiva.repository.ManagedRepository repo = repositoryRegistry.getManagedRepository(repositoryId); org.apache.archiva.repository.ManagedRepository stagingRepository = null; if (repo != null) { - try { - if (repo.supportsFeature(StagingRepositoryFeature.class)) { - stagingRepository = repo.getFeature(StagingRepositoryFeature.class).get().getStagingRepository(); - } - repositoryRegistry.removeRepository(repo, config); - } catch (RepositoryException e) { - log.error("Removal of repository {} failed: {}", repositoryId, e.getMessage(), e); - throw new RepositoryAdminException("Removal of repository " + repositoryId + " failed."); + if (repo.supportsFeature(StagingRepositoryFeature.class)) { + stagingRepository = repo.getFeature(StagingRepositoryFeature.class).get().getStagingRepository(); } } else { throw new RepositoryAdminException("A repository with that id does not exist"); @@ -308,13 +305,8 @@ public class DefaultManagedRepositoryAdmin if (stagingRepository != null) { // do not trigger event when deleting the staged one ManagedRepositoryConfiguration stagingRepositoryConfig = config.findManagedRepositoryById(stagingRepository.getId()); - try { - repositoryRegistry.removeRepository(stagingRepository); - if (stagingRepositoryConfig != null) { - deleteManagedRepository(stagingRepositoryConfig, deleteContent, config, true); - } - } catch (RepositoryException e) { - log.error("Removal of staging repository {} failed: {}", stagingRepository.getId(), e.getMessage(), e); + if (stagingRepositoryConfig != null) { + deleteManagedRepository(stagingRepositoryConfig, deleteContent, config, true); } } @@ -371,12 +363,6 @@ public class DefaultManagedRepositoryAdmin } - if ( deleteContent ) - { - // TODO could be async ? as directory can be huge - Path dir = Paths.get( repository.getLocation() ); - org.apache.archiva.common.utils.FileUtils.deleteQuietly( dir ); - } // olamy: copy list for reading as a unit test in webapp fail with ConcurrentModificationException List proxyConnectors = new ArrayList<>( config.getProxyConnectors() ); @@ -387,27 +373,6 @@ public class DefaultManagedRepositoryAdmin config.removeProxyConnector( proxyConnector ); } } - - Map> repoToGroupMap = config.getRepositoryToGroupMap(); - if ( repoToGroupMap != null ) - { - if ( repoToGroupMap.containsKey( repository.getId() ) ) - { - List repoGroups = repoToGroupMap.get( repository.getId() ); - for ( String repoGroup : repoGroups ) - { - // copy to prevent UnsupportedOperationException - RepositoryGroupConfiguration repositoryGroupConfiguration = - config.findRepositoryGroupById( repoGroup ); - List repos = new ArrayList<>( repositoryGroupConfiguration.getRepositories() ); - config.removeRepositoryGroup( repositoryGroupConfiguration ); - repos.remove( repository.getId() ); - repositoryGroupConfiguration.setRepositories( repos ); - config.addRepositoryGroup( repositoryGroupConfiguration ); - } - } - } - try { removeRepositoryRoles( repository ); @@ -419,9 +384,16 @@ public class DefaultManagedRepositoryAdmin } try { - final RepositoryRegistry reg = getRepositoryRegistry(); - if (reg.getManagedRepository(repository.getId())!=null) { - reg.removeRepository(reg.getManagedRepository(repository.getId())); + org.apache.archiva.repository.ManagedRepository repo = repositoryRegistry.getManagedRepository( repository.getId( ) ); + if (repo!=null) + { + repositoryRegistry.removeRepository( repo, config ); + if ( deleteContent ) + { + // TODO could be async ? as directory can be huge + Path dir = Paths.get( repository.getLocation( ) ); + org.apache.archiva.common.utils.FileUtils.deleteQuietly( dir ); + } } } catch (RepositoryException e) { throw new RepositoryAdminException("Removal of repository "+repository.getId()+ " failed: "+e.getMessage()); @@ -429,6 +401,7 @@ public class DefaultManagedRepositoryAdmin saveConfiguration( config ); + return Boolean.TRUE; } @@ -452,8 +425,6 @@ public class DefaultManagedRepositoryAdmin getRepositoryCommonValidator().validateManagedRepository( managedRepository ); - Configuration configuration = getArchivaConfiguration().getConfiguration(); - ManagedRepositoryConfiguration updatedRepoConfig = getRepositoryConfiguration( managedRepository ); updatedRepoConfig.setStageRepoNeeded( needStageRepo ); @@ -467,7 +438,7 @@ public class DefaultManagedRepositoryAdmin // TODO remove content from old if path has changed !!!!! try { - newRepo = repositoryRegistry.putRepository( updatedRepoConfig, configuration ); + newRepo = repositoryRegistry.putRepository( updatedRepoConfig ); if (newRepo.supportsFeature( StagingRepositoryFeature.class )) { org.apache.archiva.repository.ManagedRepository stagingRepo = newRepo.getFeature( StagingRepositoryFeature.class ).get( ).getStagingRepository( ); if (stagingRepo!=null && !stagingExists) @@ -490,16 +461,6 @@ public class DefaultManagedRepositoryAdmin } triggerAuditEvent( managedRepository.getId(), null, AuditEvent.MODIFY_MANAGED_REPO, auditInformation ); - try - { - getArchivaConfiguration().save(configuration); - } - catch ( RegistryException | IndeterminateConfigurationException e ) - { - log.error("Could not save repository configuration: {}", e.getMessage(), e); - throw new RepositoryAdminException( "Could not save repository configuration: "+e.getMessage() ); - } - // Save the repository configuration. RepositorySession repositorySession = null; try diff --git a/archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-default/src/test/java/org/apache/archiva/admin/repository/AbstractRepositoryAdminTest.java b/archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-default/src/test/java/org/apache/archiva/admin/repository/AbstractRepositoryAdminTest.java index 406faf36e..738ea3278 100644 --- a/archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-default/src/test/java/org/apache/archiva/admin/repository/AbstractRepositoryAdminTest.java +++ b/archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-default/src/test/java/org/apache/archiva/admin/repository/AbstractRepositoryAdminTest.java @@ -31,7 +31,9 @@ import org.apache.archiva.configuration.ArchivaConfiguration; import org.apache.archiva.redback.role.RoleManager; import org.apache.archiva.redback.users.User; import org.apache.archiva.redback.users.memory.SimpleUser; +import org.apache.archiva.repository.RepositoryRegistry; import org.apache.archiva.repository.base.group.RepositoryGroupHandler; +import org.apache.archiva.repository.base.managed.ManagedRepositoryHandler; import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner; import org.apache.commons.lang3.StringUtils; import org.junit.Before; @@ -84,10 +86,18 @@ public abstract class AbstractRepositoryAdminTest @Inject private ArchivaConfiguration archivaConfiguration; + @Inject + protected RepositoryRegistry repositoryRegistry; + + @SuppressWarnings( "unused" ) + @Inject + ManagedRepositoryHandler managedRepositoryHandler; + @SuppressWarnings( "unused" ) @Inject RepositoryGroupHandler repositoryGroupHandler; + @Before public void initialize() { Path confFile = Paths.get(APPSERVER_BASE_PATH, "conf/archiva.xml"); diff --git a/archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-default/src/test/java/org/apache/archiva/admin/repository/group/RepositoryGroupAdminTest.java b/archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-default/src/test/java/org/apache/archiva/admin/repository/group/RepositoryGroupAdminTest.java index 767c70ac8..384d8a9e9 100644 --- a/archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-default/src/test/java/org/apache/archiva/admin/repository/group/RepositoryGroupAdminTest.java +++ b/archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-default/src/test/java/org/apache/archiva/admin/repository/group/RepositoryGroupAdminTest.java @@ -27,6 +27,7 @@ import org.apache.archiva.metadata.model.facets.AuditEvent; import org.apache.archiva.repository.Repository; import org.apache.archiva.repository.RepositoryRegistry; import org.apache.archiva.repository.base.group.RepositoryGroupHandler; +import org.apache.archiva.repository.base.managed.ManagedRepositoryHandler; import org.junit.Test; import javax.inject.Inject; @@ -42,11 +43,6 @@ public class RepositoryGroupAdminTest @Inject RepositoryGroupAdmin repositoryGroupAdmin; - @Inject - RepositoryRegistry repositoryRegistry; - - @Inject - RepositoryGroupHandler repositoryGroupHandler; @Test public void addAndDeleteGroup() diff --git a/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/RepositoryHandler.java b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/RepositoryHandler.java index 9ecc323c4..2285c5557 100644 --- a/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/RepositoryHandler.java +++ b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/RepositoryHandler.java @@ -17,6 +17,7 @@ package org.apache.archiva.repository; * under the License. */ +import org.apache.archiva.configuration.AbstractRepositoryConfiguration; import org.apache.archiva.configuration.Configuration; import org.apache.archiva.repository.validation.CheckedResult; import org.apache.archiva.repository.validation.RepositoryChecker; @@ -44,7 +45,7 @@ import java.util.Map; * * @author Martin Stockhammer */ -public interface RepositoryHandler +public interface RepositoryHandler { /** @@ -58,6 +59,12 @@ public interface RepositoryHandler */ void activateRepository( R repository ); + /** + * Reset the repository. E.g. stops scheduling. + * @param repository + */ + void deactivateRepository( R repository ); + /** * Creates new instances from the archiva configuration. The instances are not registered in the registry. * @@ -163,9 +170,10 @@ public interface RepositoryHandler * Clones a given repository without registering. * * @param repo the repository that should be cloned + * @param newId the new identifier of the cloned instance * @return a newly created instance with the same repository data */ - R clone( R repo ) throws RepositoryException; + R clone( R repo, String newId ) throws RepositoryException; /** * Updates the references and stores updates in the given configuration instance. @@ -221,6 +229,13 @@ public interface RepositoryHandler */ boolean hasRepository( String id ); + /** + * This is called, when another variant repository was removed. This is needed only for certain variants. + * + * @param repository + */ + void processOtherVariantRemoval( Repository repository ); + /** * Initializes the handler. This method must be called before using the repository handler. */ @@ -231,4 +246,20 @@ public interface RepositoryHandler */ void close( ); + + /** + * Sets the repository provider list + * @param providers + */ + void setRepositoryProviders( List providers ); + + /** + * Sets the list of repository validators + * @param repositoryValidatorList + */ + void setRepositoryValidator( List> repositoryValidatorList ); + + Class getVariant(); + + Class getConfigurationVariant(); } diff --git a/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/RepositoryHandlerManager.java b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/RepositoryHandlerManager.java new file mode 100644 index 000000000..d3270d184 --- /dev/null +++ b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/RepositoryHandlerManager.java @@ -0,0 +1,35 @@ +package org.apache.archiva.repository; +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import org.apache.archiva.configuration.AbstractRepositoryConfiguration; +import org.apache.archiva.event.Event; +import org.apache.archiva.event.EventHandler; + +/** + * @author Martin Stockhammer + */ +public interface RepositoryHandlerManager extends EventHandler +{ + RepositoryHandler getHandler( Class repositoryClazz, Class configurationClazz); + + void registerHandler( RepositoryHandler handler ); + + boolean isRegisteredId(String id); + +} diff --git a/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/RepositoryRegistry.java b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/RepositoryRegistry.java index ba96fe0b1..bf02d2867 100644 --- a/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/RepositoryRegistry.java +++ b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/RepositoryRegistry.java @@ -50,7 +50,7 @@ import java.util.Map; * @author Martin Stockhammer */ @SuppressWarnings( "UnusedReturnValue" ) -public interface RepositoryRegistry extends EventSource +public interface RepositoryRegistry extends EventSource, RepositoryHandlerManager { /** * Set the configuration for the registry diff --git a/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/RepositoryState.java b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/RepositoryState.java index 4b092362c..3d7d63118 100644 --- a/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/RepositoryState.java +++ b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/RepositoryState.java @@ -22,7 +22,7 @@ package org.apache.archiva.repository; */ public enum RepositoryState { - CREATED(0),REFERENCES_SET(100),SAVED(200),INITIALIZED(300),REGISTERED(400),UNREGISTERED(500),CLOSED(600); + CREATED(0),REFERENCES_SET(100),SAVED(200),INITIALIZED(300),REGISTERED(400),DEACTIVATED(500), UNREGISTERED(600),CLOSED(700); private final int orderNumber; diff --git a/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/base/AbstractRepositoryHandler.java b/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/base/AbstractRepositoryHandler.java index 24bbd6294..819905598 100644 --- a/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/base/AbstractRepositoryHandler.java +++ b/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/base/AbstractRepositoryHandler.java @@ -17,51 +17,367 @@ package org.apache.archiva.repository.base; * under the License. */ +import org.apache.archiva.components.registry.RegistryException; +import org.apache.archiva.configuration.AbstractRepositoryConfiguration; +import org.apache.archiva.configuration.Configuration; +import org.apache.archiva.configuration.IndeterminateConfigurationException; +import org.apache.archiva.event.Event; +import org.apache.archiva.event.EventManager; +import org.apache.archiva.event.EventType; import org.apache.archiva.repository.EditableRepository; import org.apache.archiva.repository.Repository; +import org.apache.archiva.repository.RepositoryException; import org.apache.archiva.repository.RepositoryHandler; +import org.apache.archiva.repository.RepositoryProvider; import org.apache.archiva.repository.RepositoryState; -import org.apache.archiva.repository.base.group.RepositoryGroupHandler; +import org.apache.archiva.repository.RepositoryType; +import org.apache.archiva.repository.event.LifecycleEvent; +import org.apache.archiva.repository.validation.CheckedResult; import org.apache.archiva.repository.validation.CombinedValidator; +import org.apache.archiva.repository.validation.RepositoryChecker; import org.apache.archiva.repository.validation.RepositoryValidator; +import org.apache.archiva.repository.validation.ValidationError; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.Collection; import java.util.Collections; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.stream.Collectors; /** * Base abstract class for repository handlers. + * * @author Martin Stockhammer */ -public abstract class AbstractRepositoryHandler implements RepositoryHandler +public abstract class AbstractRepositoryHandler implements RepositoryHandler { private static final Logger log = LoggerFactory.getLogger( AbstractRepositoryHandler.class ); - protected List> initValidators( Class clazz, List> repositoryGroupValidatorList) { - if (repositoryGroupValidatorList!=null && repositoryGroupValidatorList.size()>0) { + protected final Map providerMap = new HashMap<>( ); + private CombinedValidator combinedValidator; + private final Class repositoryClazz; + private final Class configurationClazz; + private final EventManager eventManager; + private final Map repositoryMap = new HashMap<>( ); + private final ConfigurationHandler configurationHandler; + + public AbstractRepositoryHandler(Class repositoryClazz, Class configurationClazz, ConfigurationHandler configurationHandler) { + this.repositoryClazz = repositoryClazz; + this.configurationClazz = configurationClazz; + this.eventManager = new EventManager( this ); + this.configurationHandler = configurationHandler; + } + + protected List> initValidators( Class clazz, List> repositoryGroupValidatorList ) + { + if ( repositoryGroupValidatorList != null && repositoryGroupValidatorList.size( ) > 0 ) + { return repositoryGroupValidatorList.stream( ).filter( v -> v.isFlavour( clazz ) ).map( v -> v.narrowTo( clazz ) ).collect( Collectors.toList( ) ); - } else { + } + else + { return Collections.emptyList( ); } } - protected CombinedValidator getCombinedValidatdor(Class clazz, List> repositoryGroupValidatorList) { + protected CombinedValidator getCombinedValidator( Class clazz, List> repositoryGroupValidatorList ) + { return new CombinedValidator<>( clazz, initValidators( clazz, repositoryGroupValidatorList ) ); } - protected void setLastState(Repository repo, RepositoryState state) { - if (repo instanceof EditableRepository ) { - if (state.getOrderNumber()>repo.getLastState().getOrderNumber()) + protected void setLastState( R repo, RepositoryState state ) + { + RepositoryState currentState = repo.getLastState( ); + if ( repo instanceof EditableRepository ) + { + if ( state.getOrderNumber( ) > repo.getLastState( ).getOrderNumber( ) ) { ( (EditableRepository) repo ).setLastState( state ); } - } else { - log.error( "Found a not editable repository instance: {}, {}", repo.getId( ), repo.getClass().getName() ); } + else + { + log.error( "Found a not editable repository instance: {}, {}", repo.getId( ), repo.getClass( ).getName( ) ); + } + if (state == RepositoryState.REGISTERED && state != currentState ) { + pushEvent( LifecycleEvent.REGISTERED, repo ); + } else if (state == RepositoryState.UNREGISTERED && state != currentState) { + pushEvent( LifecycleEvent.UNREGISTERED, repo ); + } + } + + @Override + public void setRepositoryProviders( List providers ) + { + if ( providers != null ) + { + for ( RepositoryProvider provider : providers ) + { + for ( RepositoryType type : provider.provides( ) ) + { + providerMap.put( type, provider ); + } + } + } + } + + protected RepositoryProvider getProvider(RepositoryType type) { + return providerMap.get( type ); + } + + @Override + public void setRepositoryValidator( List> repositoryValidatorList ) + { + this.combinedValidator = getCombinedValidator( repositoryClazz, repositoryValidatorList ); + } + + protected CombinedValidator getCombinedValidator() { + return this.combinedValidator; + } + + @Override + public Class getVariant( ) + { + return this.repositoryClazz; + } + + @Override + public Class getConfigurationVariant( ) + { + return this.configurationClazz; + } + + @Override + public void processOtherVariantRemoval( Repository repository ) + { + // Default: do nothing + } + + protected void pushEvent( Event event ) + { + eventManager.fireEvent( event ); + } + + protected void pushEvent(EventType event, R repo) { + pushEvent( new LifecycleEvent( event, this, repo ) ); + } + + protected Map getRepositories() { + return repositoryMap; + } + + protected ConfigurationHandler getConfigurationHandler() { + return configurationHandler; + } + + @Override + public void activateRepository( R repository ) + { + // + } + + @Override + public void deactivateRepository( R repository ) + { + + } + + @Override + public abstract R newInstance( C repositoryConfiguration ) throws RepositoryException; + + @Override + public CheckedResult putWithCheck( C repositoryConfiguration, RepositoryChecker checker ) throws RepositoryException + { + final String id = repositoryConfiguration.getId( ); + R currentRepository = getRepositories().get( id ); + R managedRepository = newInstance( repositoryConfiguration ); + CheckedResult result; + if ( currentRepository == null ) + { + result = checker.apply( managedRepository ); + } + else + { + result = checker.applyForUpdate( managedRepository ); + } + if ( result.isValid( ) ) + { + put( result.getRepository() ); + } + return result; + } + + protected abstract C findRepositoryConfiguration(Configuration configuration, String id); + + protected abstract void removeRepositoryConfiguration(Configuration configuration, C repoConfiguration ); + + protected abstract void addRepositoryConfiguration( Configuration configuration, C repoConfiguration ); + + /** + * Removes a repository group from the registry and configuration, if it exists. + * The change is saved to the configuration immediately. + * + * @param id the id of the repository group to remove + * @throws RepositoryException if an error occurs during configuration save + */ + @Override + public void remove( String id ) throws RepositoryException + { + R repo = get( id ); + if ( repo != null ) + { + try + { + repo = getRepositories().remove( id ); + if ( repo != null ) + { + deactivateRepository( repo ); + Configuration configuration = this.configurationHandler.getBaseConfiguration( ); + C cfg = findRepositoryConfiguration( configuration, id ); + if ( cfg != null ) + { + removeRepositoryConfiguration( configuration, cfg ); + } + this.configurationHandler.save( configuration, ConfigurationHandler.REGISTRY_EVENT_TAG ); + setLastState( repo, RepositoryState.UNREGISTERED ); + } + + } + catch ( RegistryException | IndeterminateConfigurationException e ) + { + // Rollback + log.error( "Could not save config after repository removal: {}", e.getMessage( ), e ); + getRepositories().put( repo.getId( ), repo ); + throw new RepositoryException( "Could not save configuration after repository removal: " + e.getMessage( ) ); + } + } + } + + @Override + public void remove( String id, Configuration configuration ) throws RepositoryException + { + R repo = getRepositories().get( id ); + if ( repo != null ) + { + repo = getRepositories().remove( id ); + if ( repo != null ) + { + deactivateRepository( repo ); + } + setLastState( repo, RepositoryState.UNREGISTERED ); + } + C cfg = findRepositoryConfiguration(configuration, id ); + if ( cfg != null ) + { + removeRepositoryConfiguration(configuration, cfg ); + } + } + + @Override + public R get( String groupId ) + { + return getRepositories().get( groupId ); + } + + @Override + public Collection getAll( ) + { + return Collections.unmodifiableCollection( getRepositories( ).values( ) ); + } + + @Override + public RepositoryValidator getValidator( ) + { + return getCombinedValidator(); + } + + @Override + public CheckedResult>> validateRepository( R repository ) + { + return getCombinedValidator( ).apply( repository ); + + } + + @Override + public CheckedResult>> validateRepositoryForUpdate( R repository ) + { + return getCombinedValidator().applyForUpdate( repository ); + } + @Override + public boolean hasRepository( String id ) + { + return getRepositories().containsKey( id ); + } + + + @Override + public void close( ) + { + for ( R repository : getRepositories().values( ) ) + { + try + { + deactivateRepository( repository ); + } + catch ( Throwable e ) + { + log.error( "Could not close repository {}: {}", repository.getId( ), e.getMessage( ) ); + } + } + getRepositories().clear( ); + } + + protected void registerNewRepository( C repositoryGroupConfiguration, R currentRepository, Configuration configuration, boolean updated ) throws IndeterminateConfigurationException, RegistryException, RepositoryException + { + final String id = repositoryGroupConfiguration.getId( ); + getConfigurationHandler().save( configuration, ConfigurationHandler.REGISTRY_EVENT_TAG ); + updateReferences( currentRepository, repositoryGroupConfiguration ); + if (!updated ) + { + setLastState( currentRepository, RepositoryState.REFERENCES_SET ); + } + activateRepository( currentRepository ); + getRepositories().put( id, currentRepository ); + setLastState( currentRepository, RepositoryState.REGISTERED ); + } + + + + protected void rollback( Configuration configuration, R oldRepository, Exception e, C oldCfg ) throws RepositoryException + { + final String id = oldRepository.getId( ); + replaceOrAddRepositoryConfig( oldCfg, configuration ); + try + { + getConfigurationHandler().save( configuration, ConfigurationHandler.REGISTRY_EVENT_TAG ); + } + catch ( IndeterminateConfigurationException | RegistryException indeterminateConfigurationException ) + { + log.error( "Fatal error, config save during rollback failed: {}", e.getMessage( ), e ); + } + updateReferences( oldRepository, oldCfg ); + setLastState( oldRepository, RepositoryState.REFERENCES_SET ); + activateRepository( oldRepository ); + getRepositories().put( id, oldRepository ); + setLastState( oldRepository, RepositoryState.REGISTERED ); + } + + protected void replaceOrAddRepositoryConfig( C repositoryGroupConfiguration, Configuration configuration ) + { + C oldCfg = findRepositoryConfiguration( configuration, repositoryGroupConfiguration.getId( ) ); + if ( oldCfg != null ) + { + removeRepositoryConfiguration(configuration, oldCfg ); + } + addRepositoryConfiguration( configuration, repositoryGroupConfiguration ); + } + + } diff --git a/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/base/ArchivaRepositoryRegistry.java b/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/base/ArchivaRepositoryRegistry.java index 683ad5dd6..399c809e8 100644 --- a/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/base/ArchivaRepositoryRegistry.java +++ b/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/base/ArchivaRepositoryRegistry.java @@ -20,6 +20,7 @@ package org.apache.archiva.repository.base; */ import org.apache.archiva.components.registry.RegistryException; +import org.apache.archiva.configuration.AbstractRepositoryConfiguration; import org.apache.archiva.configuration.ArchivaConfiguration; import org.apache.archiva.configuration.Configuration; import org.apache.archiva.configuration.ConfigurationEvent; @@ -38,8 +39,6 @@ import org.apache.archiva.indexer.ArchivaIndexingContext; import org.apache.archiva.indexer.IndexCreationFailedException; import org.apache.archiva.indexer.IndexManagerFactory; import org.apache.archiva.indexer.IndexUpdateFailedException; -import org.apache.archiva.repository.base.group.RepositoryGroupHandler; -import org.apache.archiva.repository.validation.CheckedResult; import org.apache.archiva.repository.EditableManagedRepository; import org.apache.archiva.repository.EditableRemoteRepository; import org.apache.archiva.repository.EditableRepository; @@ -49,6 +48,7 @@ import org.apache.archiva.repository.Repository; import org.apache.archiva.repository.RepositoryContentFactory; import org.apache.archiva.repository.RepositoryException; import org.apache.archiva.repository.RepositoryGroup; +import org.apache.archiva.repository.RepositoryHandler; import org.apache.archiva.repository.RepositoryProvider; import org.apache.archiva.repository.RepositoryRegistry; import org.apache.archiva.repository.RepositoryType; @@ -61,6 +61,7 @@ import org.apache.archiva.repository.features.IndexCreationFeature; import org.apache.archiva.repository.features.StagingRepositoryFeature; import org.apache.archiva.repository.metadata.MetadataReader; import org.apache.archiva.repository.storage.StorageAsset; +import org.apache.archiva.repository.validation.CheckedResult; import org.apache.archiva.repository.validation.RepositoryValidator; import org.apache.archiva.repository.validation.ValidationError; import org.apache.archiva.repository.validation.ValidationResponse; @@ -118,23 +119,26 @@ public class ArchivaRepositoryRegistry implements ConfigurationListener, EventHa @Inject List metadataReaderList; + @Inject + List> repositoryValidatorList; + @Inject @Named( "repositoryContentFactory#default" ) RepositoryContentFactory repositoryContentFactory; - private final EventManager eventManager; + private boolean ignoreIndexing = false; + private final EventManager eventManager; - private Map managedRepositories = new HashMap<>( ); - private Map uManagedRepository = Collections.unmodifiableMap( managedRepositories ); private Map remoteRepositories = new HashMap<>( ); private Map uRemoteRepositories = Collections.unmodifiableMap( remoteRepositories ); private ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock( ); - private RepositoryGroupHandler groupHandler; + private RepositoryHandler groupHandler; + private RepositoryHandler managedRepositoryHandler; private final Set> validators; private final ConfigurationHandler configurationHandler; @@ -155,7 +159,8 @@ public class ArchivaRepositoryRegistry implements ConfigurationListener, EventHa private Set> initValidatorList( List> validators ) { TreeSet> val = new TreeSet<>( ); - for (RepositoryValidator validator : validators) { + for ( RepositoryValidator validator : validators ) + { val.add( validator ); validator.setRepositoryRegistry( this ); } @@ -175,9 +180,7 @@ public class ArchivaRepositoryRegistry implements ConfigurationListener, EventHa try { log.debug( "Initializing repository registry" ); - updateManagedRepositoriesFromConfig( ); - pushEvent( new RepositoryRegistryEvent( RepositoryRegistryEvent.MANAGED_REPOS_INITIALIZED, this ) ); - managed_initialized.set( true ); + initializeManagedRepositories(); updateRemoteRepositoriesFromConfig( ); pushEvent( new RepositoryRegistryEvent( RepositoryRegistryEvent.REMOTE_REPOS_INITIALIZED, this ) ); remote_initialized.set( true ); @@ -211,9 +214,20 @@ public class ArchivaRepositoryRegistry implements ConfigurationListener, EventHa } } - public void registerGroupHandler( RepositoryGroupHandler groupHandler ) + private void initializeManagedRepositories( ) + { + if ( this.managedRepositoryHandler != null ) + { + this.managedRepositoryHandler.initializeFromConfig( ); + this.managed_initialized.set( true ); + pushEvent( new RepositoryRegistryEvent( RepositoryRegistryEvent.MANAGED_REPOS_INITIALIZED, this ) ); + } + } + + public void registerGroupHandler( RepositoryHandler groupHandler ) { this.groupHandler = groupHandler; + registerRepositoryHandler( groupHandler ); initializeRepositoryGroups( ); if ( managed_initialized.get( ) && remote_initialized.get( ) && groups_initalized.get( ) ) { @@ -221,15 +235,22 @@ public class ArchivaRepositoryRegistry implements ConfigurationListener, EventHa } } + public void registerManagedRepositoryHandler( RepositoryHandler managedRepositoryHandler ) + { + this.managedRepositoryHandler = managedRepositoryHandler; + registerRepositoryHandler( managedRepositoryHandler ); + initializeManagedRepositories(); + if ( managed_initialized.get( ) && remote_initialized.get( ) && groups_initalized.get( ) ) + { + pushEvent( new RepositoryRegistryEvent( RepositoryRegistryEvent.INITIALIZED, this ) ); + } + } + @PreDestroy public void destroy( ) { - for ( ManagedRepository rep : managedRepositories.values( ) ) - { - rep.close( ); - } - managedRepositories.clear( ); + managedRepositoryHandler.close( ); for ( RemoteRepository repo : remoteRepositories.values( ) ) { repo.close( ); @@ -266,100 +287,15 @@ public class ArchivaRepositoryRegistry implements ConfigurationListener, EventHa */ private void updateManagedRepositoriesFromConfig( ) { - try - { - - Set configRepoIds = new HashSet<>( ); - List managedRepoConfigs = - configurationHandler.getBaseConfiguration( ).getManagedRepositories( ); - - if ( managedRepoConfigs == null ) - { - return; - } - - for ( ManagedRepositoryConfiguration repoConfig : managedRepoConfigs ) - { - ManagedRepository repo = putRepository( repoConfig, null ); - configRepoIds.add( repoConfig.getId( ) ); - if ( repo.supportsFeature( StagingRepositoryFeature.class ) ) - { - StagingRepositoryFeature stagF = repo.getFeature( StagingRepositoryFeature.class ).get( ); - if ( stagF.getStagingRepository( ) != null ) - { - configRepoIds.add( stagF.getStagingRepository( ).getId( ) ); - } - } - } - List toRemove = managedRepositories.keySet( ).stream( ).filter( id -> !configRepoIds.contains( id ) ).collect( Collectors.toList( ) ); - for ( String id : toRemove ) - { - ManagedRepository removed = managedRepositories.remove( id ); - removed.close( ); - } - } - catch ( Throwable e ) - { - log.error( "Could not initialize repositories from config: {}", e.getMessage( ), e ); - return; - } + managedRepositoryHandler.initializeFromConfig( ); } - private ManagedRepository createNewManagedRepository( RepositoryProvider provider, ManagedRepositoryConfiguration cfg ) throws RepositoryException - { - log.debug( "Creating repo {}", cfg.getId( ) ); - ManagedRepository repo = provider.createManagedInstance( cfg ); - repo.registerEventHandler( RepositoryEvent.ANY, this ); - updateRepositoryReferences( provider, repo, cfg, null ); - return repo; - - } private String getStagingId( String repoId ) { return repoId + StagingRepositoryFeature.STAGING_REPO_POSTFIX; } - @SuppressWarnings( "unchecked" ) - private void updateRepositoryReferences( RepositoryProvider provider, ManagedRepository repo, ManagedRepositoryConfiguration cfg, Configuration configuration ) throws RepositoryException - { - log.debug( "Updating references of repo {}", repo.getId( ) ); - if ( repo.supportsFeature( StagingRepositoryFeature.class ) ) - { - StagingRepositoryFeature feature = repo.getFeature( StagingRepositoryFeature.class ).get( ); - if ( feature.isStageRepoNeeded( ) && feature.getStagingRepository( ) == null ) - { - ManagedRepository stageRepo = getManagedRepository( getStagingId( repo.getId( ) ) ); - if ( stageRepo == null ) - { - stageRepo = getStagingRepository( provider, cfg, configuration ); - managedRepositories.put( stageRepo.getId( ), stageRepo ); - if ( configuration != null ) - { - replaceOrAddRepositoryConfig( provider.getManagedConfiguration( stageRepo ), configuration ); - } - pushEvent( new LifecycleEvent( LifecycleEvent.REGISTERED, this, stageRepo ) ); - } - feature.setStagingRepository( stageRepo ); - } - } - if ( repo instanceof EditableManagedRepository ) - { - EditableManagedRepository editableRepo = (EditableManagedRepository) repo; - if ( repo.getContent( ) == null ) - { - editableRepo.setContent( repositoryContentFactory.getManagedRepositoryContent( repo ) ); - editableRepo.getContent( ).setRepository( editableRepo ); - } - log.debug( "Index repo: " + repo.hasIndex( ) ); - if ( repo.hasIndex( ) && ( repo.getIndexingContext( ) == null || !repo.getIndexingContext( ).isOpen( ) ) ) - { - log.debug( "Creating indexing context for {}", repo.getId( ) ); - createIndexingContext( editableRepo ); - } - } - repo.registerEventHandler( RepositoryEvent.ANY, this ); - } @Override public ArchivaIndexManager getIndexManager( RepositoryType type ) @@ -397,22 +333,6 @@ public class ArchivaRepositoryRegistry implements ConfigurationListener, EventHa } } - private ManagedRepository getStagingRepository( RepositoryProvider provider, ManagedRepositoryConfiguration baseRepoCfg, Configuration configuration ) throws RepositoryException - { - ManagedRepository stageRepo = getManagedRepository( getStagingId( baseRepoCfg.getId( ) ) ); - if ( stageRepo == null ) - { - stageRepo = provider.createStagingInstance( baseRepoCfg ); - if ( stageRepo.supportsFeature( StagingRepositoryFeature.class ) ) - { - stageRepo.getFeature( StagingRepositoryFeature.class ).get( ).setStageRepoNeeded( false ); - } - ManagedRepositoryConfiguration stageCfg = provider.getManagedConfiguration( stageRepo ); - updateRepositoryReferences( provider, stageRepo, stageCfg, configuration ); - } - return stageRepo; - } - private void updateRemoteRepositoriesFromConfig( ) { @@ -482,7 +402,7 @@ public class ArchivaRepositoryRegistry implements ConfigurationListener, EventHa rwLock.readLock( ).lock( ); try { - return Stream.concat( managedRepositories.values( ).stream( ), remoteRepositories.values( ).stream( ) ).collect( Collectors.toList( ) ); + return Stream.concat( managedRepositoryHandler.getAll().stream( ), remoteRepositories.values( ).stream( ) ).collect( Collectors.toList( ) ); } finally { @@ -501,7 +421,7 @@ public class ArchivaRepositoryRegistry implements ConfigurationListener, EventHa rwLock.readLock( ).lock( ); try { - return uManagedRepository.values( ); + return managed_initialized.get() ? managedRepositoryHandler.getAll( ) : Collections.emptyList(); } finally { @@ -556,10 +476,10 @@ public class ArchivaRepositoryRegistry implements ConfigurationListener, EventHa try { log.debug( "getRepository {}", repoId ); - if ( managedRepositories.containsKey( repoId ) ) + if ( managedRepositoryHandler.hasRepository( repoId ) ) { log.debug( "Managed repo" ); - return managedRepositories.get( repoId ); + return managedRepositoryHandler.get( repoId ); } else if ( remoteRepositories.containsKey( repoId ) ) { @@ -594,7 +514,7 @@ public class ArchivaRepositoryRegistry implements ConfigurationListener, EventHa rwLock.readLock( ).lock( ); try { - return managedRepositories.get( repoId ); + return managed_initialized.get() ? managedRepositoryHandler.get( repoId ) : null; } finally { @@ -640,13 +560,15 @@ public class ArchivaRepositoryRegistry implements ConfigurationListener, EventHa @Override public boolean hasRepository( String repoId ) { - return this.managedRepositories.containsKey( repoId ) || this.remoteRepositories.containsKey( repoId ) || groupHandler.hasRepository( repoId ); + return ( managedRepositoryHandler != null && managedRepositoryHandler.hasRepository( repoId ) ) + || ( this.remoteRepositories != null && this.remoteRepositories.containsKey( repoId ) ) + || ( this.groupHandler != null && groupHandler.hasRepository( repoId ) ); } @Override public boolean hasManagedRepository( String repoId ) { - return this.managedRepositories.containsKey( repoId ); + return managedRepositoryHandler.hasRepository( repoId ); } @Override @@ -658,7 +580,7 @@ public class ArchivaRepositoryRegistry implements ConfigurationListener, EventHa @Override public boolean hasRepositoryGroup( String groupId ) { - return groupHandler.hasRepository( groupId ); + return this.groupHandler != null && groupHandler.hasRepository( groupId ); } protected void saveConfiguration( Configuration configuration ) throws IndeterminateConfigurationException, RegistryException @@ -680,53 +602,7 @@ public class ArchivaRepositoryRegistry implements ConfigurationListener, EventHa rwLock.writeLock( ).lock( ); try { - final String id = managedRepository.getId( ); - if ( remoteRepositories.containsKey( id ) ) - { - throw new RepositoryException( "There exists a remote repository with id " + id + ". Could not update with managed repository." ); - } - ManagedRepository originRepo = managedRepositories.put( id, managedRepository ); - try - { - if ( originRepo != null && originRepo != managedRepository ) - { - originRepo.close( ); - } - RepositoryProvider provider = getProvider( managedRepository.getType( ) ); - ManagedRepositoryConfiguration newCfg = provider.getManagedConfiguration( managedRepository ); - Configuration configuration = configurationHandler.getBaseConfiguration( ); - updateRepositoryReferences( provider, managedRepository, newCfg, configuration ); - ManagedRepositoryConfiguration oldCfg = configuration.findManagedRepositoryById( id ); - if ( oldCfg != null ) - { - configuration.removeManagedRepository( oldCfg ); - } - configuration.addManagedRepository( newCfg ); - saveConfiguration( configuration ); - if ( originRepo != managedRepository ) - { - pushEvent( new LifecycleEvent( LifecycleEvent.REGISTERED, this, managedRepository ) ); - } - else - { - pushEvent( new LifecycleEvent( LifecycleEvent.UPDATED, this, managedRepository ) ); - } - return managedRepository; - } - catch ( Exception e ) - { - // Rollback only partly, because repository is closed already - if ( originRepo != null ) - { - managedRepositories.put( id, originRepo ); - } - else - { - managedRepositories.remove( id ); - } - log.error( "Exception during configuration update {}", e.getMessage( ), e ); - throw new RepositoryException( "Could not save the configuration" + ( e.getMessage( ) == null ? "" : ": " + e.getMessage( ) ) ); - } + return managed_initialized.get() ? managedRepositoryHandler.put( managedRepository ) : null; } finally { @@ -748,26 +624,7 @@ public class ArchivaRepositoryRegistry implements ConfigurationListener, EventHa rwLock.writeLock( ).lock( ); try { - final String id = managedRepositoryConfiguration.getId( ); - final RepositoryType repositoryType = RepositoryType.valueOf( managedRepositoryConfiguration.getType( ) ); - Configuration configuration = configurationHandler.getBaseConfiguration( ); - ManagedRepository repo = managedRepositories.get( id ); - ManagedRepositoryConfiguration oldCfg = repo != null ? getProvider( repositoryType ).getManagedConfiguration( repo ) : null; - repo = putRepository( managedRepositoryConfiguration, configuration ); - try - { - saveConfiguration( configuration ); - } - catch ( IndeterminateConfigurationException | RegistryException e ) - { - if ( oldCfg != null ) - { - getProvider( repositoryType ).updateManagedInstance( (EditableManagedRepository) repo, oldCfg ); - } - log.error( "Could not save the configuration for repository {}: {}", id, e.getMessage( ), e ); - throw new RepositoryException( "Could not save the configuration for repository " + id + ": " + e.getMessage( ) ); - } - return repo; + return managedRepositoryHandler.put( managedRepositoryConfiguration ); } finally { @@ -791,39 +648,7 @@ public class ArchivaRepositoryRegistry implements ConfigurationListener, EventHa rwLock.writeLock( ).lock( ); try { - final String id = managedRepositoryConfiguration.getId( ); - final RepositoryType repoType = RepositoryType.valueOf( managedRepositoryConfiguration.getType( ) ); - ManagedRepository repo; - boolean registeredNew = false; - repo = managedRepositories.get( id ); - if ( repo != null && repo.isOpen( ) ) - { - if ( repo instanceof EditableManagedRepository ) - { - getProvider( repoType ).updateManagedInstance( (EditableManagedRepository) repo, managedRepositoryConfiguration ); - } - else - { - throw new RepositoryException( "The repository is not editable " + id ); - } - } - else - { - repo = getProvider( repoType ).createManagedInstance( managedRepositoryConfiguration ); - managedRepositories.put( id, repo ); - registeredNew = true; - } - updateRepositoryReferences( getProvider( repoType ), repo, managedRepositoryConfiguration, configuration ); - replaceOrAddRepositoryConfig( managedRepositoryConfiguration, configuration ); - if ( registeredNew ) - { - pushEvent( new LifecycleEvent( LifecycleEvent.REGISTERED, this, repo ) ); - } - else - { - pushEvent( new LifecycleEvent( LifecycleEvent.UPDATED, this, repo ) ); - } - return repo; + return managedRepositoryHandler.put( managedRepositoryConfiguration, configuration ); } finally { @@ -888,7 +713,7 @@ public class ArchivaRepositoryRegistry implements ConfigurationListener, EventHa rwLock.writeLock( ).lock( ); try { - return groupHandler.putWithCheck( repositoryGroupConfiguration, groupHandler.getValidator() ); + return groupHandler.putWithCheck( repositoryGroupConfiguration, groupHandler.getValidator( ) ); } finally { @@ -919,19 +744,6 @@ public class ArchivaRepositoryRegistry implements ConfigurationListener, EventHa } } - private void replaceOrAddRepositoryConfig( ManagedRepositoryConfiguration managedRepositoryConfiguration, Configuration configuration ) - { - if ( configuration != null ) - { - ManagedRepositoryConfiguration oldCfg = configuration.findManagedRepositoryById( managedRepositoryConfiguration.getId( ) ); - if ( oldCfg != null ) - { - configuration.removeManagedRepository( oldCfg ); - } - configuration.addManagedRepository( managedRepositoryConfiguration ); - } - } - private void replaceOrAddRepositoryConfig( RemoteRepositoryConfiguration remoteRepositoryConfiguration, Configuration configuration ) { if ( configuration != null ) @@ -945,16 +757,6 @@ public class ArchivaRepositoryRegistry implements ConfigurationListener, EventHa } } - private void replaceOrAddRepositoryConfig( RepositoryGroupConfiguration repositoryGroupConfiguration, Configuration configuration ) - { - RepositoryGroupConfiguration oldCfg = configuration.findRepositoryGroupById( repositoryGroupConfiguration.getId( ) ); - if ( oldCfg != null ) - { - configuration.removeRepositoryGroup( oldCfg ); - } - configuration.addRepositoryGroup( repositoryGroupConfiguration ); - } - @Override public RemoteRepository putRepository( RemoteRepository remoteRepository, Configuration configuration ) throws RepositoryException { @@ -962,7 +764,7 @@ public class ArchivaRepositoryRegistry implements ConfigurationListener, EventHa try { final String id = remoteRepository.getId( ); - if ( managedRepositories.containsKey( id ) ) + if ( managedRepositoryHandler.hasRepository( id ) ) { throw new RepositoryException( "There exists a managed repository with id " + id + ". Could not update with remote repository." ); } @@ -1206,39 +1008,14 @@ public class ArchivaRepositoryRegistry implements ConfigurationListener, EventHa { return; } - final String id = managedRepository.getId( ); - ManagedRepository repo = getManagedRepository( id ); - if ( repo != null ) + rwLock.writeLock( ).lock( ); + try { - rwLock.writeLock( ).lock( ); - try - { - repo = managedRepositories.remove( id ); - if ( repo != null ) - { - repo.close( ); - this.groupHandler.removeRepositoryFromGroups( repo ); - Configuration configuration = configurationHandler.getBaseConfiguration( ); - ManagedRepositoryConfiguration cfg = configuration.findManagedRepositoryById( id ); - if ( cfg != null ) - { - configuration.removeManagedRepository( cfg ); - } - saveConfiguration( configuration ); - } - pushEvent( new LifecycleEvent( LifecycleEvent.UNREGISTERED, this, repo ) ); - } - catch ( RegistryException | IndeterminateConfigurationException e ) - { - // Rollback - log.error( "Could not save config after repository removal: {}", e.getMessage( ), e ); - managedRepositories.put( repo.getId( ), repo ); - throw new RepositoryException( "Could not save configuration after repository removal: " + e.getMessage( ) ); - } - finally - { - rwLock.writeLock( ).unlock( ); - } + if (managed_initialized.get() ) managedRepositoryHandler.remove( managedRepository.getId( ) ); + } + finally + { + rwLock.writeLock( ).unlock( ); } } @@ -1250,31 +1027,15 @@ public class ArchivaRepositoryRegistry implements ConfigurationListener, EventHa { return; } - final String id = managedRepository.getId( ); - ManagedRepository repo = getManagedRepository( id ); - if ( repo != null ) - { rwLock.writeLock( ).lock( ); try { - repo = managedRepositories.remove( id ); - if ( repo != null ) - { - repo.close( ); - this.groupHandler.removeRepositoryFromGroups( repo ); - ManagedRepositoryConfiguration cfg = configuration.findManagedRepositoryById( id ); - if ( cfg != null ) - { - configuration.removeManagedRepository( cfg ); - } - } - pushEvent( new LifecycleEvent( LifecycleEvent.UNREGISTERED, this, repo ) ); + if (managed_initialized.get()) managedRepositoryHandler.remove( managedRepository.getId( ), configuration ); } finally { rwLock.writeLock( ).unlock( ); } - } } @@ -1457,16 +1218,11 @@ public class ArchivaRepositoryRegistry implements ConfigurationListener, EventHa */ public ManagedRepository clone( ManagedRepository repo, String newId ) throws RepositoryException { - if ( managedRepositories.containsKey( newId ) || remoteRepositories.containsKey( newId ) ) - { - throw new RepositoryException( "The given id exists already " + newId ); + if (isRegisteredId( newId )) { + throw new RepositoryException( "The new id exists already: " + newId ); } - RepositoryProvider provider = getProvider( repo.getType( ) ); - ManagedRepositoryConfiguration cfg = provider.getManagedConfiguration( repo ); - cfg.setId( newId ); - ManagedRepository cloned = provider.createManagedInstance( cfg ); - cloned.registerEventHandler( RepositoryEvent.ANY, this ); - return cloned; + EditableManagedRepository newRepo = (EditableManagedRepository) managedRepositoryHandler.clone( repo, newId ); + return newRepo; } @Override @@ -1495,7 +1251,7 @@ public class ArchivaRepositoryRegistry implements ConfigurationListener, EventHa */ public RemoteRepository clone( RemoteRepository repo, String newId ) throws RepositoryException { - if ( managedRepositories.containsKey( newId ) || remoteRepositories.containsKey( newId ) ) + if ( isRegisteredId( newId ) ) { throw new RepositoryException( "The given id exists already " + newId ); } @@ -1526,8 +1282,8 @@ public class ArchivaRepositoryRegistry implements ConfigurationListener, EventHa { Map> errorMap = this.validators.stream( ) .filter( ( validator ) -> validator.getType( ).equals( RepositoryType.ALL ) || repository.getType( ).equals( validator.getType( ) ) ) - .filter( val -> val.isFlavour( repository.getClass() )) - .flatMap( validator -> ((RepositoryValidator)validator).apply( repository ).getResult().entrySet( ).stream( ) ) + .filter( val -> val.isFlavour( repository.getClass( ) ) ) + .flatMap( validator -> ( (RepositoryValidator) validator ).apply( repository ).getResult( ).entrySet( ).stream( ) ) .collect( Collectors.toMap( entry -> entry.getKey( ), entry -> entry.getValue( ), @@ -1541,8 +1297,8 @@ public class ArchivaRepositoryRegistry implements ConfigurationListener, EventHa { Map> errorMap = this.validators.stream( ) .filter( ( validator ) -> validator.getType( ).equals( RepositoryType.ALL ) || repository.getType( ).equals( validator.getType( ) ) ) - .filter( val -> val.isFlavour( repository.getClass() )) - .flatMap( validator -> ((RepositoryValidator)validator).applyForUpdate( repository ).getResult().entrySet( ).stream( ) ) + .filter( val -> val.isFlavour( repository.getClass( ) ) ) + .flatMap( validator -> ( (RepositoryValidator) validator ).applyForUpdate( repository ).getResult( ).entrySet( ).stream( ) ) .collect( Collectors.toMap( entry -> entry.getKey( ), entry -> entry.getValue( ), @@ -1594,36 +1350,39 @@ public class ArchivaRepositoryRegistry implements ConfigurationListener, EventHa private void handleIndexCreationEvent( RepositoryIndexEvent event ) { - RepositoryIndexEvent idxEvent = event; - EditableRepository repo = (EditableRepository) idxEvent.getRepository( ); - if ( repo != null ) + if (!ignoreIndexing && !( event.getRepository() instanceof ManagedRepository )) { - ArchivaIndexManager idxmgr = getIndexManager( repo.getType( ) ); - if ( repo.getIndexingContext( ) != null ) + RepositoryIndexEvent idxEvent = event; + EditableRepository repo = (EditableRepository) idxEvent.getRepository( ); + if ( repo != null ) { - try + ArchivaIndexManager idxmgr = getIndexManager( repo.getType( ) ); + if ( repo.getIndexingContext( ) != null ) { - ArchivaIndexingContext newCtx = idxmgr.move( repo.getIndexingContext( ), repo ); - repo.setIndexingContext( newCtx ); - idxmgr.updateLocalIndexPath( repo ); + try + { + 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 ); + } } - catch ( IndexCreationFailedException e ) - { - log.error( "Could not move index to new directory: '{}'", e.getMessage( ), e ); - } - } - else - { - try - { - ArchivaIndexingContext context = idxmgr.createContext( repo ); - repo.setIndexingContext( context ); - idxmgr.updateLocalIndexPath( repo ); - } - catch ( IndexCreationFailedException e ) + else { - log.error( "Could not create index: '{}'", e.getMessage( ), e ); + try + { + ArchivaIndexingContext context = idxmgr.createContext( repo ); + repo.setIndexingContext( context ); + idxmgr.updateLocalIndexPath( repo ); + } + catch ( IndexCreationFailedException e ) + { + log.error( "Could not create index: '{}'", e.getMessage( ), e ); + } } } } @@ -1650,5 +1409,57 @@ public class ArchivaRepositoryRegistry implements ConfigurationListener, EventHa eventManager.fireEvent( event ); } + private void registerRepositoryHandler( RepositoryHandler repositoryHandler ) + { + repositoryHandler.setRepositoryProviders( this.repositoryProviders ); + repositoryHandler.setRepositoryValidator( this.repositoryValidatorList ); + } + + @Override + public void registerHandler( RepositoryHandler handler ) + { + if ( handler.getVariant( ).isAssignableFrom( RepositoryGroup.class ) ) + { + registerGroupHandler( (RepositoryHandler) handler ); + } + else if ( handler.getVariant( ).isAssignableFrom( ManagedRepository.class ) ) + { + registerManagedRepositoryHandler( (RepositoryHandler) handler ); + } + } + + @Override + public boolean isRegisteredId( String id ) + { + return hasRepository( id ); + } + + @Override + public RepositoryHandler getHandler( Class repositoryClazz, Class configurationClazz ) + { + if ( repositoryClazz.isAssignableFrom( RepositoryGroup.class ) ) + { + return (RepositoryHandler) this.groupHandler; + } + else if ( repositoryClazz.isAssignableFrom( ManagedRepository.class ) ) + { + return (RepositoryHandler) this.managedRepositoryHandler; + } + else + { + return null; + } + } + + public boolean isIgnoreIndexing( ) + { + return ignoreIndexing; + } + + public void setIgnoreIndexing( boolean ignoreIndexing ) + { + this.ignoreIndexing = ignoreIndexing; + } + } diff --git a/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/base/group/RepositoryGroupHandler.java b/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/base/group/RepositoryGroupHandler.java index 2a8a505e6..9285de888 100644 --- a/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/base/group/RepositoryGroupHandler.java +++ b/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/base/group/RepositoryGroupHandler.java @@ -20,13 +20,9 @@ package org.apache.archiva.repository.base.group; import org.apache.archiva.components.registry.RegistryException; import org.apache.archiva.configuration.Configuration; import org.apache.archiva.configuration.IndeterminateConfigurationException; +import org.apache.archiva.configuration.ManagedRepositoryConfiguration; import org.apache.archiva.configuration.RepositoryGroupConfiguration; import org.apache.archiva.indexer.merger.MergedRemoteIndexesScheduler; -import org.apache.archiva.repository.RepositoryState; -import org.apache.archiva.repository.base.AbstractRepositoryHandler; -import org.apache.archiva.repository.base.ArchivaRepositoryRegistry; -import org.apache.archiva.repository.base.ConfigurationHandler; -import org.apache.archiva.repository.validation.CheckedResult; import org.apache.archiva.repository.EditableRepository; import org.apache.archiva.repository.EditableRepositoryGroup; import org.apache.archiva.repository.ManagedRepository; @@ -34,14 +30,17 @@ import org.apache.archiva.repository.Repository; import org.apache.archiva.repository.RepositoryException; import org.apache.archiva.repository.RepositoryGroup; import org.apache.archiva.repository.RepositoryHandler; +import org.apache.archiva.repository.RepositoryHandlerManager; import org.apache.archiva.repository.RepositoryProvider; +import org.apache.archiva.repository.RepositoryState; import org.apache.archiva.repository.RepositoryType; +import org.apache.archiva.repository.base.AbstractRepositoryHandler; +import org.apache.archiva.repository.base.ArchivaRepositoryRegistry; +import org.apache.archiva.repository.base.ConfigurationHandler; +import org.apache.archiva.repository.event.LifecycleEvent; import org.apache.archiva.repository.event.RepositoryEvent; import org.apache.archiva.repository.features.IndexCreationFeature; import org.apache.archiva.repository.storage.StorageAsset; -import org.apache.archiva.repository.validation.RepositoryChecker; -import org.apache.archiva.repository.validation.RepositoryValidator; -import org.apache.archiva.repository.validation.ValidationError; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -53,14 +52,10 @@ import javax.inject.Named; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; -import java.util.Collection; import java.util.Collections; -import java.util.HashMap; -import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; -import java.util.Optional; import java.util.concurrent.locks.ReentrantReadWriteLock; import java.util.stream.Collectors; @@ -79,13 +74,9 @@ public class RepositoryGroupHandler { private static final Logger log = LoggerFactory.getLogger( RepositoryGroupHandler.class ); - private final ArchivaRepositoryRegistry repositoryRegistry; - private final ConfigurationHandler configurationHandler; + private final RepositoryHandlerManager repositoryRegistry; private final MergedRemoteIndexesScheduler mergedRemoteIndexesScheduler; - private final Map repositoryGroups = new HashMap<>( ); - private final RepositoryValidator validator; - private Path groupsDirectory; @@ -95,18 +86,15 @@ public class RepositoryGroupHandler * @param repositoryRegistry the registry. To avoid circular dependencies via DI, this class registers itself on the registry. * @param configurationHandler the configuration handler is used to retrieve and save configuration. * @param mergedRemoteIndexesScheduler the index scheduler is used for merging the indexes from all group members - * @param repositoryValidatorList the list of validators that are registered */ - public RepositoryGroupHandler( ArchivaRepositoryRegistry repositoryRegistry, + public RepositoryGroupHandler( RepositoryHandlerManager repositoryRegistry, ConfigurationHandler configurationHandler, - @Named( "mergedRemoteIndexesScheduler#default" ) MergedRemoteIndexesScheduler mergedRemoteIndexesScheduler, - List> repositoryValidatorList + @Named( "mergedRemoteIndexesScheduler#default" ) MergedRemoteIndexesScheduler mergedRemoteIndexesScheduler ) { - this.configurationHandler = configurationHandler; + super( RepositoryGroup.class, RepositoryGroupConfiguration.class, configurationHandler ); this.mergedRemoteIndexesScheduler = mergedRemoteIndexesScheduler; this.repositoryRegistry = repositoryRegistry; - this.validator = getCombinedValidatdor( RepositoryGroup.class, repositoryValidatorList ); } @Override @@ -116,15 +104,15 @@ public class RepositoryGroupHandler log.debug( "Initializing repository group handler " + repositoryRegistry.toString( ) ); initializeStorage( ); // We are registering this class on the registry. This is necessary to avoid circular dependencies via injection. - this.repositoryRegistry.registerGroupHandler( this ); + this.repositoryRegistry.registerHandler( this ); } @Override public void initializeFromConfig( ) { - this.repositoryGroups.clear( ); - this.repositoryGroups.putAll( newInstancesFromConfig( ) ); - for ( RepositoryGroup group : this.repositoryGroups.values( ) ) + getRepositories().clear( ); + getRepositories().putAll( newInstancesFromConfig( ) ); + for ( RepositoryGroup group : getRepositories().values( ) ) { activateRepository( group ); } @@ -132,7 +120,7 @@ public class RepositoryGroupHandler private void initializeStorage( ) { - Path baseDir = this.configurationHandler.getArchivaConfiguration( ).getRepositoryGroupBaseDir( ); + Path baseDir = this.getConfigurationHandler().getArchivaConfiguration( ).getRepositoryGroupBaseDir( ); if ( !Files.exists( baseDir ) ) { try @@ -179,6 +167,14 @@ public class RepositoryGroupHandler setLastState( repositoryGroup, RepositoryState.INITIALIZED ); } + @Override + public void deactivateRepository( RepositoryGroup repository ) + { + mergedRemoteIndexesScheduler.unschedule( repository ); + repository.close(); + setLastState( repository, RepositoryState.DEACTIVATED ); + } + public StorageAsset getMergedIndexDirectory( RepositoryGroup group ) { if ( group != null ) @@ -198,7 +194,7 @@ public class RepositoryGroupHandler try { List repositoryGroupConfigurations = - this.configurationHandler.getBaseConfiguration( ).getRepositoryGroups( ); + this.getConfigurationHandler().getBaseConfiguration( ).getRepositoryGroups( ); if ( repositoryGroupConfigurations == null ) { @@ -207,11 +203,10 @@ public class RepositoryGroupHandler Map repositoryGroupMap = new LinkedHashMap<>( repositoryGroupConfigurations.size( ) ); - Map providerMap = repositoryRegistry.getRepositoryProviderMap( ); for ( RepositoryGroupConfiguration repoConfig : repositoryGroupConfigurations ) { RepositoryType repositoryType = RepositoryType.valueOf( repoConfig.getType( ) ); - if ( providerMap.containsKey( repositoryType ) ) + if ( super.providerMap.containsKey( repositoryType ) ) { try { @@ -236,7 +231,7 @@ public class RepositoryGroupHandler @Override public RepositoryGroup newInstance( final RepositoryType type, String id ) throws RepositoryException { - RepositoryProvider provider = repositoryRegistry.getProvider( type ); + RepositoryProvider provider = getProvider( type ); RepositoryGroupConfiguration config = new RepositoryGroupConfiguration( ); config.setId( id ); return createNewRepositoryGroup( provider, config ); @@ -246,7 +241,7 @@ public class RepositoryGroupHandler public RepositoryGroup newInstance( final RepositoryGroupConfiguration repositoryConfiguration ) throws RepositoryException { RepositoryType type = RepositoryType.valueOf( repositoryConfiguration.getType( ) ); - RepositoryProvider provider = repositoryRegistry.getProvider( type ); + RepositoryProvider provider = getProvider( type ); return createNewRepositoryGroup( provider, repositoryConfiguration ); } @@ -258,6 +253,7 @@ public class RepositoryGroupHandler { ( (EditableRepository) repositoryGroup ).setLastState( RepositoryState.REFERENCES_SET ); } + repositoryGroup.registerEventHandler( RepositoryEvent.ANY, repositoryRegistry ); return repositoryGroup; } @@ -273,21 +269,21 @@ public class RepositoryGroupHandler public RepositoryGroup put( final RepositoryGroup repositoryGroup ) throws RepositoryException { final String id = repositoryGroup.getId( ); - RepositoryGroup originRepoGroup = repositoryGroups.remove( id ); + RepositoryGroup originRepoGroup = getRepositories().remove( id ); try { if ( originRepoGroup != null && originRepoGroup != repositoryGroup ) { - this.mergedRemoteIndexesScheduler.unschedule( originRepoGroup ); - originRepoGroup.close( ); + deactivateRepository( originRepoGroup ); + pushEvent( new LifecycleEvent( LifecycleEvent.UNREGISTERED, this, originRepoGroup ) ); } - RepositoryProvider provider = repositoryRegistry.getProvider( repositoryGroup.getType( ) ); + RepositoryProvider provider = getProvider( repositoryGroup.getType( ) ); RepositoryGroupConfiguration newCfg = provider.getRepositoryGroupConfiguration( repositoryGroup ); - ReentrantReadWriteLock.WriteLock configLock = this.configurationHandler.getLock( ).writeLock( ); + ReentrantReadWriteLock.WriteLock configLock = this.getConfigurationHandler().getLock( ).writeLock( ); configLock.lock( ); try { - Configuration configuration = this.configurationHandler.getBaseConfiguration( ); + Configuration configuration = this.getConfigurationHandler().getBaseConfiguration( ); updateReferences( repositoryGroup, newCfg ); RepositoryGroupConfiguration oldCfg = configuration.findRepositoryGroupById( id ); if ( oldCfg != null ) @@ -295,7 +291,7 @@ public class RepositoryGroupHandler configuration.removeRepositoryGroup( oldCfg ); } configuration.addRepositoryGroup( newCfg ); - configurationHandler.save( configuration, ConfigurationHandler.REGISTRY_EVENT_TAG ); + getConfigurationHandler().save( configuration, ConfigurationHandler.REGISTRY_EVENT_TAG ); setLastState( repositoryGroup, RepositoryState.SAVED ); activateRepository( repositoryGroup ); } @@ -303,7 +299,7 @@ public class RepositoryGroupHandler { configLock.unlock( ); } - repositoryGroups.put( id, repositoryGroup ); + getRepositories().put( id, repositoryGroup ); setLastState( repositoryGroup, RepositoryState.REGISTERED ); return repositoryGroup; } @@ -312,11 +308,11 @@ public class RepositoryGroupHandler // Rollback if ( originRepoGroup != null ) { - repositoryGroups.put( id, originRepoGroup ); + getRepositories().put( id, originRepoGroup ); } else { - repositoryGroups.remove( id ); + getRepositories().remove( id ); } log.error( "Exception during configuration update {}", e.getMessage( ), e ); throw new RepositoryException( "Could not save the configuration" + ( e.getMessage( ) == null ? "" : ": " + e.getMessage( ) ), e); @@ -336,31 +332,28 @@ public class RepositoryGroupHandler { final String id = repositoryGroupConfiguration.getId( ); final RepositoryType repositoryType = RepositoryType.valueOf( repositoryGroupConfiguration.getType( ) ); - final RepositoryProvider provider = repositoryRegistry.getProvider( repositoryType ); + final RepositoryProvider provider = getProvider( repositoryType ); RepositoryGroup currentRepository; - ReentrantReadWriteLock.WriteLock configLock = this.configurationHandler.getLock( ).writeLock( ); + ReentrantReadWriteLock.WriteLock configLock = this.getConfigurationHandler().getLock( ).writeLock( ); configLock.lock( ); try { - Configuration configuration = this.configurationHandler.getBaseConfiguration( ); - currentRepository = repositoryRegistry.getRepositoryGroup( id ); - RepositoryGroup oldRepository = currentRepository == null ? null : clone( currentRepository ); + Configuration configuration = this.getConfigurationHandler().getBaseConfiguration( ); + currentRepository = getRepositories().get( id ); + RepositoryGroup oldRepository = currentRepository == null ? null : clone( currentRepository, id ); try { - + boolean updated = false; if (currentRepository==null) { currentRepository = put( repositoryGroupConfiguration, configuration ); } else { setRepositoryGroupDefaults( repositoryGroupConfiguration ); provider.updateRepositoryGroupInstance( (EditableRepositoryGroup) currentRepository, repositoryGroupConfiguration ); + updated = true; + pushEvent( LifecycleEvent.UPDATED, currentRepository ); } - configurationHandler.save( configuration, ConfigurationHandler.REGISTRY_EVENT_TAG ); - updateReferences( currentRepository, repositoryGroupConfiguration ); - setLastState( currentRepository, RepositoryState.REFERENCES_SET ); - activateRepository( currentRepository ); - this.repositoryGroups.put( id, currentRepository ); - setLastState( currentRepository, RepositoryState.REGISTERED ); + registerNewRepository( repositoryGroupConfiguration, currentRepository, configuration, updated ); } catch ( IndeterminateConfigurationException | RegistryException | RepositoryException e ) { @@ -368,23 +361,10 @@ public class RepositoryGroupHandler if ( oldRepository != null ) { RepositoryGroupConfiguration oldCfg = provider.getRepositoryGroupConfiguration( oldRepository ); - provider.updateRepositoryGroupInstance( (EditableRepositoryGroup) currentRepository, oldCfg); - replaceOrAddRepositoryConfig( oldCfg, configuration ); - try - { - configurationHandler.save( configuration, ConfigurationHandler.REGISTRY_EVENT_TAG ); - } - catch ( IndeterminateConfigurationException | RegistryException indeterminateConfigurationException ) - { - log.error( "Fatal error, config save during rollback failed: {}", e.getMessage( ), e ); - } - updateReferences( oldRepository, oldCfg ); - setLastState( oldRepository, RepositoryState.REFERENCES_SET ); - activateRepository( oldRepository ); - repositoryGroups.put( id, oldRepository ); - setLastState( oldRepository, RepositoryState.REGISTERED ); + provider.updateRepositoryGroupInstance( (EditableRepositoryGroup) currentRepository, oldCfg ); + rollback( configuration, oldRepository, e, oldCfg ); } else { - repositoryGroups.remove( id ); + getRepositories().remove( id ); } log.error( "Could not save the configuration for repository group {}: {}", id, e.getMessage( ), e ); if (e instanceof RepositoryException) { @@ -409,12 +389,12 @@ public class RepositoryGroupHandler final RepositoryType repoType = RepositoryType.valueOf( repositoryGroupConfiguration.getType( ) ); RepositoryGroup repo; setRepositoryGroupDefaults( repositoryGroupConfiguration ); - if ( repositoryGroups.containsKey( id ) ) + if ( getRepositories().containsKey( id ) ) { - repo = clone( repositoryGroups.get( id ) ); + repo = clone( getRepositories().get( id ), id ); if ( repo instanceof EditableRepositoryGroup ) { - repositoryRegistry.getProvider( repoType ).updateRepositoryGroupInstance( (EditableRepositoryGroup) repo, repositoryGroupConfiguration ); + getProvider( repoType ).updateRepositoryGroupInstance( (EditableRepositoryGroup) repo, repositoryGroupConfiguration ); } else { @@ -423,7 +403,7 @@ public class RepositoryGroupHandler } else { - repo = repositoryRegistry.getProvider( repoType ).createRepositoryGroup( repositoryGroupConfiguration ); + repo = getProvider( repoType ).createRepositoryGroup( repositoryGroupConfiguration ); setLastState( repo, RepositoryState.CREATED ); } replaceOrAddRepositoryConfig( repositoryGroupConfiguration, configuration ); @@ -432,29 +412,6 @@ public class RepositoryGroupHandler return repo; } - @Override - public CheckedResult putWithCheck( RepositoryGroupConfiguration repositoryConfiguration, RepositoryChecker checker ) throws RepositoryException - { - final String id = repositoryConfiguration.getId( ); - RepositoryGroup currentGroup = repositoryGroups.get( id ); - RepositoryGroup repositoryGroup = newInstance( repositoryConfiguration ); - CheckedResult result; - if ( currentGroup == null ) - { - result = checker.apply( repositoryGroup ); - } - else - { - result = checker.applyForUpdate( repositoryGroup ); - } - if ( result.isValid( ) ) - { - put( result.getRepository() ); - } - return result; - } - - private void setRepositoryGroupDefaults( RepositoryGroupConfiguration repositoryGroupConfiguration ) { if ( StringUtils.isEmpty( repositoryGroupConfiguration.getMergedIndexPath( ) ) ) @@ -471,106 +428,23 @@ public class RepositoryGroupHandler } } - private void replaceOrAddRepositoryConfig( RepositoryGroupConfiguration repositoryGroupConfiguration, Configuration configuration ) - { - RepositoryGroupConfiguration oldCfg = configuration.findRepositoryGroupById( repositoryGroupConfiguration.getId( ) ); - if ( oldCfg != null ) - { - configuration.removeRepositoryGroup( oldCfg ); - } - configuration.addRepositoryGroup( repositoryGroupConfiguration ); - } - - public void removeRepositoryFromGroups( ManagedRepository repo ) - { - if ( repo != null ) - { - repositoryGroups.values( ).stream( ).filter( repoGroup -> repoGroup instanceof EditableRepository ). - map( repoGroup -> (EditableRepositoryGroup) repoGroup ).forEach( repoGroup -> repoGroup.removeRepository( repo ) ); - } - } - - /** - * Removes a repository group from the registry and configuration, if it exists. - * The change is saved to the configuration immediately. - * - * @param id the id of the repository group to remove - * @throws RepositoryException if a error occurs during configuration save - */ @Override - public void remove( final String id ) throws RepositoryException + public void processOtherVariantRemoval( Repository repo ) { - RepositoryGroup repo = get( id ); - if ( repo != null ) + if ( repo instanceof ManagedRepository ) { - try - { - repo = repositoryGroups.remove( id ); - if ( repo != null ) - { - this.mergedRemoteIndexesScheduler.unschedule( repo ); - repo.close( ); - Configuration configuration = this.configurationHandler.getBaseConfiguration( ); - RepositoryGroupConfiguration cfg = configuration.findRepositoryGroupById( id ); - if ( cfg != null ) - { - configuration.removeRepositoryGroup( cfg ); - } - this.configurationHandler.save( configuration, ConfigurationHandler.REGISTRY_EVENT_TAG ); - setLastState( repo, RepositoryState.UNREGISTERED ); - } - - } - catch ( RegistryException | IndeterminateConfigurationException e ) - { - // Rollback - log.error( "Could not save config after repository removal: {}", e.getMessage( ), e ); - repositoryGroups.put( repo.getId( ), repo ); - throw new RepositoryException( "Could not save configuration after repository removal: " + e.getMessage( ) ); - } - } - } - - @Override - public void remove( String id, Configuration configuration ) throws RepositoryException - { - RepositoryGroup repo = repositoryGroups.get( id ); - if ( repo != null ) - { - repo = repositoryGroups.remove( id ); - if ( repo != null ) - { - this.mergedRemoteIndexesScheduler.unschedule( repo ); - repo.close( ); - RepositoryGroupConfiguration cfg = configuration.findRepositoryGroupById( id ); - if ( cfg != null ) - { - configuration.removeRepositoryGroup( cfg ); - } - setLastState( repo, RepositoryState.UNREGISTERED ); - } - } - Iterator cfgIter = configuration.getRepositoryGroups( ).iterator( ); - while(cfgIter.hasNext()) { - RepositoryGroupConfiguration el = cfgIter.next( ); - if (id.equals( el.getId() )) { - cfgIter.remove( ); - break; - } + getRepositories().values( ).stream( ).filter( repoGroup -> repoGroup instanceof EditableRepository ). + map( repoGroup -> (EditableRepositoryGroup) repoGroup ).forEach( repoGroup -> repoGroup.removeRepository( (ManagedRepository) repo ) ); } } - @Override - public RepositoryGroup get( String groupId ) - { - return repositoryGroups.get( groupId ); - } @Override - public RepositoryGroup clone( RepositoryGroup repo ) throws RepositoryException + public RepositoryGroup clone( RepositoryGroup repo, String newId ) throws RepositoryException { - RepositoryProvider provider = repositoryRegistry.getProvider( repo.getType( ) ); + RepositoryProvider provider = getProvider( repo.getType( ) ); RepositoryGroupConfiguration cfg = provider.getRepositoryGroupConfiguration( repo ); + cfg.setId( newId ); RepositoryGroup cloned = provider.createRepositoryGroup( cfg ); cloned.registerEventHandler( RepositoryEvent.ANY, repositoryRegistry ); setLastState( cloned, RepositoryState.CREATED ); @@ -578,69 +452,40 @@ public class RepositoryGroupHandler } @Override - public void updateReferences( RepositoryGroup repo, RepositoryGroupConfiguration repositoryConfiguration ) throws RepositoryException + public void updateReferences( RepositoryGroup repo, RepositoryGroupConfiguration repositoryConfiguration ) { if ( repo instanceof EditableRepositoryGroup && repositoryConfiguration!=null) { EditableRepositoryGroup eGroup = (EditableRepositoryGroup) repo; + RepositoryHandler managedHandler = repositoryRegistry.getHandler( ManagedRepository.class, ManagedRepositoryConfiguration.class ); eGroup.setRepositories( repositoryConfiguration.getRepositories( ).stream( ) - .map( repositoryRegistry::getManagedRepository ).collect( Collectors.toList( ) ) ); + .map( managedHandler::get ).collect( Collectors.toList( ) ) ); } } - @Override - public Collection getAll( ) - { - return Collections.unmodifiableCollection( repositoryGroups.values( ) ); - } - @Override - public RepositoryValidator getValidator( ) + @PreDestroy + private void destroy( ) { - return this.validator; + this.close( ); } @Override - public CheckedResult>> validateRepository( RepositoryGroup repository ) + protected RepositoryGroupConfiguration findRepositoryConfiguration( Configuration configuration, String id ) { - return this.validator.apply( repository ); - + return configuration.findRepositoryGroupById( id ); } @Override - public CheckedResult>> validateRepositoryForUpdate( RepositoryGroup repository ) + protected void removeRepositoryConfiguration( Configuration configuration, RepositoryGroupConfiguration cfg ) { - return this.validator.applyForUpdate( repository ); - } - @Override - public boolean hasRepository( String id ) - { - return repositoryGroups.containsKey( id ); - } - - @PreDestroy - private void destroy( ) - { - this.close( ); + configuration.removeRepositoryGroup( cfg ); } @Override - public void close( ) + protected void addRepositoryConfiguration( Configuration configuration, RepositoryGroupConfiguration repoConfiguration ) { - for ( RepositoryGroup group : repositoryGroups.values( ) ) - { - try - { - mergedRemoteIndexesScheduler.unschedule( group ); - group.close( ); - } - catch ( Throwable e ) - { - log.error( "Could not close repository group {}: {}", group.getId( ), e.getMessage( ) ); - } - } - this.repositoryGroups.clear( ); + configuration.addRepositoryGroup( repoConfiguration ); } - } diff --git a/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/base/managed/ManagedRepositoryHandler.java b/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/base/managed/ManagedRepositoryHandler.java index fee66b828..1ce5da635 100644 --- a/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/base/managed/ManagedRepositoryHandler.java +++ b/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/base/managed/ManagedRepositoryHandler.java @@ -17,74 +17,102 @@ package org.apache.archiva.repository.base.managed; * under the License. */ +import org.apache.archiva.components.registry.RegistryException; import org.apache.archiva.configuration.Configuration; +import org.apache.archiva.configuration.IndeterminateConfigurationException; import org.apache.archiva.configuration.ManagedRepositoryConfiguration; +import org.apache.archiva.indexer.ArchivaIndexManager; +import org.apache.archiva.indexer.IndexCreationFailedException; +import org.apache.archiva.indexer.IndexManagerFactory; +import org.apache.archiva.repository.EditableManagedRepository; +import org.apache.archiva.repository.EditableRepository; import org.apache.archiva.repository.ManagedRepository; -import org.apache.archiva.repository.Repository; +import org.apache.archiva.repository.RepositoryContentFactory; import org.apache.archiva.repository.RepositoryException; import org.apache.archiva.repository.RepositoryHandler; +import org.apache.archiva.repository.RepositoryHandlerManager; +import org.apache.archiva.repository.RepositoryProvider; +import org.apache.archiva.repository.RepositoryRegistry; +import org.apache.archiva.repository.RepositoryState; import org.apache.archiva.repository.RepositoryType; import org.apache.archiva.repository.base.AbstractRepositoryHandler; -import org.apache.archiva.repository.base.ArchivaRepositoryRegistry; import org.apache.archiva.repository.base.ConfigurationHandler; +import org.apache.archiva.repository.event.LifecycleEvent; +import org.apache.archiva.repository.event.RepositoryEvent; +import org.apache.archiva.repository.features.IndexCreationFeature; import org.apache.archiva.repository.features.StagingRepositoryFeature; -import org.apache.archiva.repository.validation.CheckedResult; -import org.apache.archiva.repository.validation.RepositoryChecker; -import org.apache.archiva.repository.validation.RepositoryValidator; -import org.apache.archiva.repository.validation.ValidationResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Service; -import java.util.Collection; +import javax.annotation.PostConstruct; +import javax.inject.Named; +import java.io.IOException; +import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; -import java.util.HashSet; import java.util.List; import java.util.Map; -import java.util.Set; -import java.util.stream.Collectors; +import java.util.concurrent.locks.ReentrantReadWriteLock; /** * Handler implementation for managed repositories. * * @author Martin Stockhammer */ +@Service( "managedRepositoryHandler#default" ) public class ManagedRepositoryHandler extends AbstractRepositoryHandler -implements RepositoryHandler + implements RepositoryHandler { private static final Logger log = LoggerFactory.getLogger( ManagedRepositoryHandler.class ); - private final ConfigurationHandler configurationHandler; - private final ArchivaRepositoryRegistry repositoryRegistry; - private final RepositoryValidator validator; - private Map managedRepositories = new HashMap<>( ); - private Map uManagedRepositories = Collections.unmodifiableMap( managedRepositories ); + private final RepositoryHandlerManager repositoryRegistry; + private final RepositoryContentFactory repositoryContentFactory; - public ManagedRepositoryHandler( ArchivaRepositoryRegistry repositoryRegistry, - ConfigurationHandler configurationHandler, - List> repositoryValidatorList ) + IndexManagerFactory indexManagerFactory; + + + public ManagedRepositoryHandler( RepositoryHandlerManager repositoryRegistry, + ConfigurationHandler configurationHandler, IndexManagerFactory indexManagerFactory, + @Named( "repositoryContentFactory#default" ) + RepositoryContentFactory repositoryContentFactory + ) { - this.configurationHandler = configurationHandler; + super( ManagedRepository.class, ManagedRepositoryConfiguration.class, configurationHandler ); this.repositoryRegistry = repositoryRegistry; - this.validator = getCombinedValidatdor( ManagedRepository.class, repositoryValidatorList ); + this.indexManagerFactory = indexManagerFactory; + this.repositoryContentFactory = repositoryContentFactory; } @Override - public void initializeFromConfig( ) + @PostConstruct + public void init( ) + { + log.debug( "Initializing managed repository handler " + repositoryRegistry.toString( ) ); + initializeStorage( ); + // We are registering this class on the registry. This is necessary to avoid circular dependencies via injection. + this.repositoryRegistry.registerHandler( this ); + } + + private void initializeStorage( ) { - this.managedRepositories.clear( ); - this.managedRepositories.putAll( newInstancesFromConfig( ) ); - for ( ManagedRepository managedRepository : this.managedRepositories.values( ) ) - { - activateRepository( managedRepository ); - } } @Override - public void activateRepository( ManagedRepository repository ) + public void initializeFromConfig( ) { + Map currentInstances = new HashMap<>( getRepositories( ) ); + getRepositories().clear(); + getRepositories( ).putAll( newOrUpdateInstancesFromConfig( currentInstances ) ); + for ( ManagedRepository managedRepository : getRepositories( ).values( ) ) + { + activateRepository( managedRepository ); + } + for (ManagedRepository managedRepository : currentInstances.values()) { + deactivateRepository( managedRepository ); + } } @@ -93,148 +121,437 @@ implements RepositoryHandler { try { - Set configRepoIds = new HashSet<>( ); List managedRepoConfigs = - configurationHandler.getBaseConfiguration( ).getManagedRepositories( ); + new ArrayList<>( + getConfigurationHandler( ).getBaseConfiguration( ).getManagedRepositories( ) ); if ( managedRepoConfigs == null ) { - return managedRepositories; + return Collections.emptyMap( ); } + Map result = new HashMap<>( ); for ( ManagedRepositoryConfiguration repoConfig : managedRepoConfigs ) { - ManagedRepository repo = put( repoConfig, null ); - configRepoIds.add( repoConfig.getId( ) ); + ManagedRepository repo = newInstance( repoConfig ); + result.put( repo.getId( ), repo ); if ( repo.supportsFeature( StagingRepositoryFeature.class ) ) { StagingRepositoryFeature stagF = repo.getFeature( StagingRepositoryFeature.class ).get( ); if ( stagF.getStagingRepository( ) != null ) { - configRepoIds.add( stagF.getStagingRepository( ).getId( ) ); + ManagedRepository stagingRepo = getStagingRepository( repo ); + if ( stagingRepo != null ) + { + result.put( stagingRepo.getId( ), stagingRepo ); + } } } } - List toRemove = managedRepositories.keySet( ).stream( ).filter( id -> !configRepoIds.contains( id ) ).collect( Collectors.toList( ) ); - for ( String id : toRemove ) + return result; + } + catch ( Throwable e ) + { + log.error( "Could not initialize repositories from config: {}", e.getMessage( ), e ); + return new HashMap<>( ); + } + } + + public Map newOrUpdateInstancesFromConfig( Map currentInstances) + { + try + { + List managedRepoConfigs = + new ArrayList<>( + getConfigurationHandler( ).getBaseConfiguration( ).getManagedRepositories( ) ); + + if ( managedRepoConfigs == null ) { - ManagedRepository removed = managedRepositories.remove( id ); - removed.close( ); + return Collections.emptyMap( ); } + + Map result = new HashMap<>( ); + for ( ManagedRepositoryConfiguration repoConfig : managedRepoConfigs ) + { + String id = repoConfig.getId( ); + ManagedRepository repo; + if ( currentInstances.containsKey( id ) ) + { + repo = currentInstances.remove( id ); + getProvider( repo.getType() ).updateManagedInstance( (EditableManagedRepository) repo, repoConfig ); + updateReferences( repo, repoConfig ); + } + else + { + repo = newInstance( repoConfig ); + } + result.put( id, repo ); + if ( repo.supportsFeature( StagingRepositoryFeature.class ) ) + { + StagingRepositoryFeature stagF = repo.getFeature( StagingRepositoryFeature.class ).get( ); + if ( stagF.getStagingRepository( ) != null ) + { + String stagingId = getStagingId( id ); + ManagedRepository stagingRepo; + if ( currentInstances.containsKey( stagingId ) ) + { + stagingRepo = currentInstances.remove( stagingId ); + managedRepoConfigs.stream( ).filter( cfg -> stagingId.equals( cfg.getId( ) ) ).findFirst( ).ifPresent( + stagingRepoConfig -> + { + try + { + getProvider( stagingRepo.getType() ).updateManagedInstance( (EditableManagedRepository) stagingRepo, stagingRepoConfig ); + updateReferences( stagingRepo, stagingRepoConfig ); + } + catch ( RepositoryException e ) + { + log.error( "Could not update staging repo {}: {}", stagingId, e.getMessage( ) ); + } + } + ); + } + else + { + stagingRepo = getStagingRepository( repo ); + } + if ( stagingRepo != null ) + { + result.put( stagingRepo.getId( ), stagingRepo ); + } + } + } + } + return result; } catch ( Throwable e ) { log.error( "Could not initialize repositories from config: {}", e.getMessage( ), e ); - return managedRepositories; + return new HashMap<>( ); } - return managedRepositories; } + @Override public ManagedRepository newInstance( RepositoryType type, String id ) throws RepositoryException { - return null; + log.debug( "Creating repo {}", id ); + RepositoryProvider provider = getProvider( type ); + EditableManagedRepository repo; + try + { + repo = provider.createManagedInstance( id, id ); + } + catch ( IOException e ) + { + throw new RepositoryException( "Could not create repository '" + id + "': " + e.getMessage( ) ); + } + repo.registerEventHandler( RepositoryEvent.ANY, repositoryRegistry ); + updateReferences( repo, null ); + repo.setLastState( RepositoryState.REFERENCES_SET ); + return repo; } - @Override - public ManagedRepository newInstance( ManagedRepositoryConfiguration repositoryConfiguration ) throws RepositoryException + private String getStagingId( String repoId ) { - return null; + return repoId + StagingRepositoryFeature.STAGING_REPO_POSTFIX; } - @Override - public ManagedRepository put( ManagedRepository repository ) throws RepositoryException - { - return null; - } - @Override - public ManagedRepository put( ManagedRepositoryConfiguration repositoryConfiguration ) throws RepositoryException + private ManagedRepository getStagingRepository( ManagedRepository baseRepo ) throws RepositoryException { - return null; + ManagedRepository stageRepo = get( getStagingId( baseRepo.getId( ) ) ); + final RepositoryType type = baseRepo.getType( ); + if ( stageRepo == null ) + { + RepositoryProvider provider = getProvider( type ); + ManagedRepositoryConfiguration cfg = provider.getManagedConfiguration( baseRepo ); + stageRepo = provider.createStagingInstance( cfg ); + if ( stageRepo.supportsFeature( StagingRepositoryFeature.class ) ) + { + stageRepo.getFeature( StagingRepositoryFeature.class ).get( ).setStageRepoNeeded( false ); + } + updateReferences( stageRepo, cfg ); + } + return stageRepo; } - @Override - public ManagedRepository put( ManagedRepositoryConfiguration repositoryConfiguration, Configuration configuration ) throws RepositoryException + public ArchivaIndexManager getIndexManager( RepositoryType type ) { - return null; + return indexManagerFactory.getIndexManager( type ); } - @Override - public CheckedResult putWithCheck( ManagedRepositoryConfiguration repositoryConfiguration, RepositoryChecker checker ) throws RepositoryException + private void createIndexingContext( EditableRepository editableRepo ) throws RepositoryException { - return null; + if ( editableRepo.supportsFeature( IndexCreationFeature.class ) ) + { + 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 ); + } + } } + @Override - public void remove( String id ) throws RepositoryException + public ManagedRepository newInstance( ManagedRepositoryConfiguration repositoryConfiguration ) throws RepositoryException { - + RepositoryType type = RepositoryType.valueOf( repositoryConfiguration.getType( ) ); + RepositoryProvider provider = getProvider( type ); + if ( provider == null ) + { + throw new RepositoryException( "Provider not found for repository type: " + repositoryConfiguration.getType( ) ); + } + final ManagedRepository repo = provider.createManagedInstance( repositoryConfiguration ); + repo.registerEventHandler( RepositoryEvent.ANY, repositoryRegistry ); + updateReferences( repo, null ); + if ( repo instanceof EditableRepository ) + { + ( (EditableRepository) repo ).setLastState( RepositoryState.REFERENCES_SET ); + } + return repo; } @Override - public void remove( String id, Configuration configuration ) throws RepositoryException + protected ManagedRepositoryConfiguration findRepositoryConfiguration( final Configuration configuration, final String id ) { - + return configuration.findManagedRepositoryById( id ); } @Override - public ManagedRepository get( String id ) + protected void removeRepositoryConfiguration( final Configuration configuration, final ManagedRepositoryConfiguration repoConfiguration ) { - return null; + configuration.removeManagedRepository( repoConfiguration ); } @Override - public ManagedRepository clone( ManagedRepository repo ) throws RepositoryException + protected void addRepositoryConfiguration( Configuration configuration, ManagedRepositoryConfiguration repoConfiguration ) { - return null; + configuration.addManagedRepository( repoConfiguration ); } @Override - public void updateReferences( ManagedRepository repo, ManagedRepositoryConfiguration repositoryConfiguration ) throws RepositoryException + public ManagedRepository put( ManagedRepository managedRepository ) throws RepositoryException { - + final String id = managedRepository.getId( ); + ManagedRepository originRepo = getRepositories( ).remove( id ); + if ( originRepo == null && repositoryRegistry.isRegisteredId( id ) ) + { + throw new RepositoryException( "There exists a repository with id " + id + ". Could not update with managed repository." ); + } + try + { + if ( originRepo != null && managedRepository != originRepo ) + { + deactivateRepository( originRepo ); + pushEvent( LifecycleEvent.UNREGISTERED, originRepo ); + } + RepositoryProvider provider = getProvider( managedRepository.getType( ) ); + ManagedRepositoryConfiguration newCfg = provider.getManagedConfiguration( managedRepository ); + getConfigurationHandler( ).getLock( ).writeLock( ).lock( ); + try + { + Configuration configuration = getConfigurationHandler( ).getBaseConfiguration( ); + updateReferences( managedRepository, newCfg ); + ManagedRepositoryConfiguration oldCfg = configuration.findManagedRepositoryById( id ); + if ( oldCfg != null ) + { + configuration.removeManagedRepository( oldCfg ); + } + configuration.addManagedRepository( newCfg ); + getConfigurationHandler( ).save( configuration, ConfigurationHandler.REGISTRY_EVENT_TAG ); + setLastState( managedRepository, RepositoryState.SAVED ); + activateRepository( managedRepository ); + } + finally + { + getConfigurationHandler( ).getLock( ).writeLock( ).unlock( ); + } + getRepositories( ).put( id, managedRepository ); + setLastState( managedRepository, RepositoryState.REGISTERED ); + return managedRepository; + } + catch ( Exception e ) + { + // Rollback only partly, because repository is closed already + if ( originRepo != null ) + { + getRepositories( ).put( id, originRepo ); + } + else + { + getRepositories( ).remove( id ); + } + log.error( "Exception during configuration update {}", e.getMessage( ), e ); + throw new RepositoryException( "Could not save the configuration" + ( e.getMessage( ) == null ? "" : ": " + e.getMessage( ) ) ); + } } + @Override - public Collection getAll( ) + public ManagedRepository put( ManagedRepositoryConfiguration managedRepositoryConfiguration ) throws RepositoryException { - return null; + final String id = managedRepositoryConfiguration.getId( ); + final RepositoryType repositoryType = RepositoryType.valueOf( managedRepositoryConfiguration.getType( ) ); + final RepositoryProvider provider = getProvider( repositoryType ); + ReentrantReadWriteLock.WriteLock configLock = this.getConfigurationHandler( ).getLock( ).writeLock( ); + configLock.lock( ); + ManagedRepository repo = null; + ManagedRepository oldRepository = null; + Configuration configuration = null; + try + { + boolean updated = false; + configuration = getConfigurationHandler( ).getBaseConfiguration( ); + repo = getRepositories( ).get( id ); + oldRepository = repo == null ? null : clone( repo, id ); + if ( repo == null ) + { + repo = put( managedRepositoryConfiguration, configuration ); + } + else + { + setManagedRepositoryDefaults( managedRepositoryConfiguration ); + provider.updateManagedInstance( (EditableManagedRepository) repo, managedRepositoryConfiguration ); + updated = true; + pushEvent( LifecycleEvent.UPDATED, repo ); + } + registerNewRepository( managedRepositoryConfiguration, repo, configuration, updated ); + } + catch ( IndeterminateConfigurationException | RegistryException e ) + { + if ( oldRepository != null ) + { + ManagedRepositoryConfiguration oldCfg = provider.getManagedConfiguration( oldRepository ); + provider.updateManagedInstance( (EditableManagedRepository) repo, oldCfg ); + rollback( configuration, oldRepository, e, oldCfg ); + } + else + { + getRepositories( ).remove( id ); + } + log.error( "Could not save the configuration for repository {}: {}", id, e.getMessage( ), e ); + throw new RepositoryException( "Could not save the configuration for repository " + id + ": " + e.getMessage( ) ); + } + finally + { + configLock.unlock( ); + } + return repo; } @Override - public RepositoryValidator getValidator( ) + public ManagedRepository put( ManagedRepositoryConfiguration repositoryConfiguration, Configuration configuration ) throws RepositoryException { - return null; + final String id = repositoryConfiguration.getId( ); + final RepositoryType repoType = RepositoryType.valueOf( repositoryConfiguration.getType( ) ); + ManagedRepository repo; + setManagedRepositoryDefaults( repositoryConfiguration ); + if ( getRepositories( ).containsKey( id ) ) + { + repo = clone( getRepositories( ).get( id ), id ); + if ( repo instanceof EditableManagedRepository ) + { + getProvider( repoType ).updateManagedInstance( (EditableManagedRepository) repo, repositoryConfiguration ); + } + else + { + throw new RepositoryException( "The repository is not editable " + id ); + } + } + else + { + repo = getProvider( repoType ).createManagedInstance( repositoryConfiguration ); + setLastState( repo, RepositoryState.CREATED ); + } + if ( configuration != null ) + { + replaceOrAddRepositoryConfig( repositoryConfiguration, configuration ); + } + updateReferences( repo, repositoryConfiguration ); + setLastState( repo, RepositoryState.REFERENCES_SET ); + return repo; } - @Override - public ValidationResponse validateRepository( ManagedRepository repository ) + @SuppressWarnings( "unused" ) + private void setManagedRepositoryDefaults( ManagedRepositoryConfiguration repositoryConfiguration ) { - return null; + // We do nothing here } @Override - public ValidationResponse validateRepositoryForUpdate( ManagedRepository repository ) + public ManagedRepository clone( ManagedRepository repo, String id ) throws RepositoryException { - return null; + RepositoryProvider provider = getProvider( repo.getType( ) ); + ManagedRepositoryConfiguration cfg = provider.getManagedConfiguration( repo ); + cfg.setId( id ); + ManagedRepository cloned = provider.createManagedInstance( cfg ); + cloned.registerEventHandler( RepositoryEvent.ANY, repositoryRegistry ); + setLastState( cloned, RepositoryState.CREATED ); + return cloned; } + @Override - public boolean hasRepository( String id ) + public void updateReferences( ManagedRepository repo, ManagedRepositoryConfiguration repositoryConfiguration ) throws RepositoryException { - return false; + log.debug( "Updating references of repo {}", repo.getId( ) ); + if ( repo.supportsFeature( StagingRepositoryFeature.class ) ) + { + Configuration configuration = getConfigurationHandler( ).getBaseConfiguration( ); + RepositoryProvider provider = getProvider( repo.getType( ) ); + StagingRepositoryFeature feature = repo.getFeature( StagingRepositoryFeature.class ).get( ); + if ( feature.isStageRepoNeeded( ) && feature.getStagingRepository( ) == null ) + { + ManagedRepository stageRepo = get( getStagingId( repo.getId( ) ) ); + if ( stageRepo == null ) + { + stageRepo = getStagingRepository( repo ); + getRepositories( ).put( stageRepo.getId( ), stageRepo ); + if ( configuration != null ) + { + replaceOrAddRepositoryConfig( provider.getManagedConfiguration( stageRepo ), configuration ); + } + pushEvent( new LifecycleEvent( LifecycleEvent.REGISTERED, this, stageRepo ) ); + } + feature.setStagingRepository( stageRepo ); + } + } + if ( repo instanceof EditableManagedRepository ) + { + EditableManagedRepository editableRepo = (EditableManagedRepository) repo; + if ( repo.getContent( ) == null ) + { + editableRepo.setContent( repositoryContentFactory.getManagedRepositoryContent( repo ) ); + editableRepo.getContent( ).setRepository( editableRepo ); + } + log.debug( "Index repo: " + repo.hasIndex( ) ); + if ( repo.hasIndex( ) && ( repo.getIndexingContext( ) == null || !repo.getIndexingContext( ).isOpen( ) ) ) + { + log.debug( "Creating indexing context for {}", repo.getId( ) ); + createIndexingContext( editableRepo ); + } + } + repo.registerEventHandler( RepositoryEvent.ANY, repositoryRegistry ); } @Override - public void init( ) + public void close( ) { - + getRepositories( ).values( ).stream( ).forEach( + r -> deactivateRepository( r ) + ); } @Override - public void close( ) + public void deactivateRepository( ManagedRepository repository ) { - + repository.close( ); } } diff --git a/archiva-modules/archiva-base/archiva-repository-layer/src/test/java/org/apache/archiva/repository/base/ArchivaRepositoryRegistryTest.java b/archiva-modules/archiva-base/archiva-repository-layer/src/test/java/org/apache/archiva/repository/base/ArchivaRepositoryRegistryTest.java index 6601ad2c0..0b98a1b8e 100644 --- a/archiva-modules/archiva-base/archiva-repository-layer/src/test/java/org/apache/archiva/repository/base/ArchivaRepositoryRegistryTest.java +++ b/archiva-modules/archiva-base/archiva-repository-layer/src/test/java/org/apache/archiva/repository/base/ArchivaRepositoryRegistryTest.java @@ -32,6 +32,7 @@ import org.apache.archiva.repository.RepositoryRegistry; import org.apache.archiva.repository.RepositoryType; import org.apache.archiva.repository.base.group.RepositoryGroupHandler; import org.apache.archiva.repository.base.managed.BasicManagedRepository; +import org.apache.archiva.repository.base.managed.ManagedRepositoryHandler; import org.apache.archiva.repository.base.remote.BasicRemoteRepository; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; @@ -51,6 +52,7 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardCopyOption; import java.util.Collection; +import java.util.List; import static org.junit.jupiter.api.Assertions.*; @@ -73,6 +75,9 @@ public class ArchivaRepositoryRegistryTest @Inject RepositoryGroupHandler repositoryGroupHandler; + @Inject + ManagedRepositoryHandler managedRepositoryHandler; + private static final Path userCfg = Paths.get(System.getProperty( "user.home" ), ".m2/archiva.xml"); private static Path cfgCopy; @@ -269,30 +274,31 @@ public class ArchivaRepositoryRegistryTest public void putManagedRepositoryFromConfigWithoutSave( ) throws Exception { Configuration configuration = archivaConfiguration.getConfiguration(); + int actualSize = configuration.getManagedRepositories( ).size( ); + Configuration newConfiguration = new Configuration( ); ManagedRepositoryConfiguration cfg = new ManagedRepositoryConfiguration(); cfg.setId("test002"); cfg.setName("This is test 002"); - ManagedRepository repo = repositoryRegistry.putRepository( cfg, configuration ); + ManagedRepository repo = repositoryRegistry.putRepository( cfg, newConfiguration ); assertNotNull(repo); assertEquals("test002", repo.getId()); assertEquals("This is test 002", repo.getName()); assertNotNull(repo.getContent()); archivaConfiguration.reload(); - assertEquals(3, archivaConfiguration.getConfiguration().getManagedRepositories().size()); - Collection repos = repositoryRegistry.getManagedRepositories(); - assertEquals(5, repos.size()); + assertEquals(actualSize, configuration.getManagedRepositories().size()); + List repos = newConfiguration.getManagedRepositories( ); + assertEquals(1, repos.size()); ManagedRepository internalRepo = repositoryRegistry.getManagedRepository( "internal" ); cfg = new ManagedRepositoryConfiguration(); cfg.setId("internal"); cfg.setName("This is internal test 002"); - repo = repositoryRegistry.putRepository( cfg, configuration ); - assertSame( internalRepo, repo ); + repo = repositoryRegistry.putRepository( cfg, newConfiguration ); assertEquals("This is internal test 002",repo.getName()); - assertEquals(5, repositoryRegistry.getManagedRepositories().size()); + assertEquals(2, newConfiguration.getManagedRepositories().size()); repositoryRegistry.reload(); - assertEquals(4, repositoryRegistry.getManagedRepositories().size()); + assertEquals(actualSize, configuration.getManagedRepositories().size()); } @Test diff --git a/archiva-modules/archiva-base/archiva-repository-layer/src/test/java/org/apache/archiva/repository/base/group/BasicRepositoryGroupValidatorTest.java b/archiva-modules/archiva-base/archiva-repository-layer/src/test/java/org/apache/archiva/repository/base/group/BasicRepositoryGroupValidatorTest.java index c049b47bd..9470302db 100644 --- a/archiva-modules/archiva-base/archiva-repository-layer/src/test/java/org/apache/archiva/repository/base/group/BasicRepositoryGroupValidatorTest.java +++ b/archiva-modules/archiva-base/archiva-repository-layer/src/test/java/org/apache/archiva/repository/base/group/BasicRepositoryGroupValidatorTest.java @@ -18,8 +18,6 @@ package org.apache.archiva.repository.base.group; * under the License. */ -import org.apache.archiva.common.filelock.DefaultFileLockManager; -import org.apache.archiva.common.filelock.FileLockManager; import org.apache.archiva.repository.EditableManagedRepository; import org.apache.archiva.repository.EditableRepositoryGroup; import org.apache.archiva.repository.ManagedRepository; @@ -28,9 +26,8 @@ import org.apache.archiva.repository.RepositoryGroup; import org.apache.archiva.repository.RepositoryRegistry; import org.apache.archiva.repository.base.ConfigurationHandler; import org.apache.archiva.repository.base.managed.BasicManagedRepository; -import org.apache.archiva.repository.base.managed.BasicManagedRepositoryValidator; +import org.apache.archiva.repository.base.managed.ManagedRepositoryHandler; import org.apache.archiva.repository.mock.ManagedRepositoryContentMock; -import org.apache.archiva.repository.storage.fs.FilesystemStorage; import org.apache.archiva.repository.validation.ValidationResponse; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; @@ -64,6 +61,9 @@ class BasicRepositoryGroupValidatorTest @Inject RepositoryGroupHandler repositoryGroupHandler; + @Inject + ManagedRepositoryHandler managedRepositoryHandler; + Path repoBaseDir; @AfterEach diff --git a/archiva-modules/archiva-base/archiva-repository-layer/src/test/java/org/apache/archiva/repository/base/group/RepositoryGroupHandlerTest.java b/archiva-modules/archiva-base/archiva-repository-layer/src/test/java/org/apache/archiva/repository/base/group/RepositoryGroupHandlerTest.java index 7c114b746..f694f7aee 100644 --- a/archiva-modules/archiva-base/archiva-repository-layer/src/test/java/org/apache/archiva/repository/base/group/RepositoryGroupHandlerTest.java +++ b/archiva-modules/archiva-base/archiva-repository-layer/src/test/java/org/apache/archiva/repository/base/group/RepositoryGroupHandlerTest.java @@ -21,29 +21,29 @@ package org.apache.archiva.repository.base.group; import org.apache.archiva.common.filelock.DefaultFileLockManager; import org.apache.archiva.common.filelock.FileLockManager; import org.apache.archiva.common.utils.FileUtils; -import org.apache.archiva.common.utils.PathUtil; -import org.apache.archiva.components.registry.RegistryException; import org.apache.archiva.configuration.ArchivaConfiguration; import org.apache.archiva.configuration.Configuration; -import org.apache.archiva.configuration.IndeterminateConfigurationException; +import org.apache.archiva.configuration.ManagedRepositoryConfiguration; import org.apache.archiva.configuration.RepositoryGroupConfiguration; import org.apache.archiva.indexer.merger.MergedRemoteIndexesScheduler; import org.apache.archiva.repository.EditableRepositoryGroup; -import org.apache.archiva.repository.Repository; +import org.apache.archiva.repository.ManagedRepository; import org.apache.archiva.repository.RepositoryException; import org.apache.archiva.repository.RepositoryGroup; +import org.apache.archiva.repository.RepositoryHandler; import org.apache.archiva.repository.RepositoryState; import org.apache.archiva.repository.RepositoryType; import org.apache.archiva.repository.base.ArchivaRepositoryRegistry; import org.apache.archiva.repository.base.ConfigurationHandler; +import org.apache.archiva.repository.base.managed.BasicManagedRepository; import org.apache.archiva.repository.storage.fs.FilesystemStorage; import org.apache.archiva.repository.validation.CheckedResult; -import org.apache.archiva.repository.validation.RepositoryValidator; import org.apache.archiva.repository.validation.ValidationError; -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.ArgumentMatchers; import org.mockito.Mock; +import org.mockito.Mockito; import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit.jupiter.SpringExtension; @@ -87,8 +87,8 @@ class RepositoryGroupHandlerTest // @Named( "mergedRemoteIndexesScheduler#default" ) MergedRemoteIndexesScheduler mergedRemoteIndexesScheduler; - @Inject - List> repositoryValidatorList; + @Mock + RepositoryHandler managedRepositoryHandler; @Inject ArchivaConfiguration archivaConfiguration; @@ -142,7 +142,19 @@ class RepositoryGroupHandlerTest private RepositoryGroupHandler createHandler( ) { - RepositoryGroupHandler groupHandler = new RepositoryGroupHandler( repositoryRegistry, configurationHandler, mergedRemoteIndexesScheduler, repositoryValidatorList ); + Mockito.when( managedRepositoryHandler.getVariant( ) ).thenReturn( ManagedRepository.class ); + final ManagedRepository internalRepo; + try + { + internalRepo = getManaged( "internal", "internal"); + } + catch ( IOException e ) + { + throw new Error( e ); + } + Mockito.when( managedRepositoryHandler.get( ArgumentMatchers.eq("internal") ) ).thenReturn( internalRepo ); + repositoryRegistry.registerHandler( managedRepositoryHandler ); + RepositoryGroupHandler groupHandler = new RepositoryGroupHandler( repositoryRegistry, configurationHandler, mergedRemoteIndexesScheduler); groupHandler.init( ); return groupHandler; } @@ -156,6 +168,13 @@ class RepositoryGroupHandlerTest return repoBaseDir; } + protected ManagedRepository getManaged(String id, String name) throws IOException + { + Path path = getRepoBaseDir().resolve( "../managed" ); + FileLockManager lockManager = new DefaultFileLockManager(); + FilesystemStorage storage = new FilesystemStorage(path.toAbsolutePath(), lockManager); + return new BasicManagedRepository( id, name, storage ); + } protected EditableRepositoryGroup createRepository( String id, String name, Path location ) throws IOException @@ -200,9 +219,8 @@ class RepositoryGroupHandlerTest @Test void newInstancesFromConfig( ) { - RepositoryGroupHandler groupHandler = new RepositoryGroupHandler( repositoryRegistry, configurationHandler, mergedRemoteIndexesScheduler, repositoryValidatorList ); + RepositoryGroupHandler groupHandler = createHandler( ); Map instances = groupHandler.newInstancesFromConfig( ); - assertFalse( groupHandler.hasRepository( "test-group-01" ) ); assertTrue( instances.containsKey( "test-group-01" ) ); assertEquals( RepositoryState.REFERENCES_SET, instances.get( "test-group-01" ).getLastState( ) ); } diff --git a/archiva-modules/archiva-base/archiva-repository-layer/src/test/java/org/apache/archiva/repository/base/managed/ManagedRepositoryHandlerTest.java b/archiva-modules/archiva-base/archiva-repository-layer/src/test/java/org/apache/archiva/repository/base/managed/ManagedRepositoryHandlerTest.java new file mode 100644 index 000000000..8811d4a46 --- /dev/null +++ b/archiva-modules/archiva-base/archiva-repository-layer/src/test/java/org/apache/archiva/repository/base/managed/ManagedRepositoryHandlerTest.java @@ -0,0 +1,427 @@ +package org.apache.archiva.repository.base.managed; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import org.apache.archiva.common.filelock.DefaultFileLockManager; +import org.apache.archiva.common.filelock.FileLockManager; +import org.apache.archiva.common.utils.FileUtils; +import org.apache.archiva.configuration.ArchivaConfiguration; +import org.apache.archiva.configuration.Configuration; +import org.apache.archiva.configuration.ManagedRepositoryConfiguration; +import org.apache.archiva.indexer.IndexManagerFactory; +import org.apache.archiva.indexer.merger.MergedRemoteIndexesScheduler; +import org.apache.archiva.repository.EditableManagedRepository; +import org.apache.archiva.repository.ManagedRepository; +import org.apache.archiva.repository.Repository; +import org.apache.archiva.repository.RepositoryContentFactory; +import org.apache.archiva.repository.RepositoryException; +import org.apache.archiva.repository.RepositoryState; +import org.apache.archiva.repository.RepositoryType; +import org.apache.archiva.repository.base.ArchivaRepositoryRegistry; +import org.apache.archiva.repository.base.ConfigurationHandler; +import org.apache.archiva.repository.storage.fs.FilesystemStorage; +import org.apache.archiva.repository.validation.CheckedResult; +import org.apache.archiva.repository.validation.RepositoryValidator; +import org.apache.archiva.repository.validation.ValidationError; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +import javax.inject.Inject; +import javax.inject.Named; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import static org.apache.archiva.repository.validation.ErrorKeys.ISEMPTY; +import static org.junit.jupiter.api.Assertions.*; + +/** + * @author Martin Stockhammer + */ +@ExtendWith( {MockitoExtension.class, SpringExtension.class} ) +@ContextConfiguration( locations = {"classpath*:/META-INF/spring-context.xml", "classpath:/spring-context-managed.xml"} ) +class ManagedRepositoryHandlerTest +{ + static { + initialize(); + } + + @Inject + @Named( "repositoryRegistry" ) + ArchivaRepositoryRegistry repositoryRegistry; + + @Inject + @Named( "repositoryContentFactory#default" ) + RepositoryContentFactory repositoryContentFactory; + + @Inject + IndexManagerFactory indexManagerFactory; + + @Inject + ConfigurationHandler configurationHandler; + + @Inject + List> repositoryValidatorList; + + @Inject + ArchivaConfiguration archivaConfiguration; + + Path repoBaseDir; + + + private static void initialize() { + Path baseDir = Paths.get( FileUtils.getBasedir( ) ); + Path config = baseDir.resolve( "src/test/resources/archiva-managed.xml" ); + if ( Files.exists( config ) ) + { + Path destConfig = baseDir.resolve( "target/test-classes/archiva-managed.xml" ); + try + { + Files.copy( config, destConfig ); + } + catch ( IOException e ) + { + System.err.println( "Could not copy file: " + e.getMessage( ) ); + } + } + } + + // Helper method that removes a repo from the configuration + private void removeRepositoryFromConfig( String id) { + Configuration configuration = configurationHandler.getBaseConfiguration( ); + Iterator iter = configuration.getManagedRepositories().iterator(); + while(iter.hasNext()) { + ManagedRepositoryConfiguration repo = iter.next( ); + if (id.equals(repo.getId())) { + iter.remove(); + break; + } + } + try + { + configurationHandler.save( configuration ); + } + catch ( Throwable e ) + { + System.err.println( "Could not remove repo from config "+id ); + } + } + + private boolean hasRepositoryInConfig( String id) { + assertNotNull( configurationHandler.getBaseConfiguration( ).getManagedRepositories() ); + return configurationHandler.getBaseConfiguration( ).getManagedRepositories( ).stream( ).anyMatch( g -> g != null && id.equals( g.getId( ) ) ) ; + } + + + private ManagedRepositoryHandler createHandler( ) + { + ManagedRepositoryHandler repositoryHandler = new ManagedRepositoryHandler( repositoryRegistry, configurationHandler, indexManagerFactory, repositoryContentFactory ); + repositoryHandler.init( ); + return repositoryHandler; + } + + private Path getRepoBaseDir() throws IOException + { + if (repoBaseDir==null) { + this.repoBaseDir = archivaConfiguration.getRepositoryBaseDir( ).resolve( "managed" ); + Files.createDirectories( this.repoBaseDir ); + } + return repoBaseDir; + } + + protected EditableManagedRepository createRepository( String id, String name, Path location ) throws IOException + { + FileLockManager lockManager = new DefaultFileLockManager(); + FilesystemStorage storage = new FilesystemStorage(location.toAbsolutePath(), lockManager); + BasicManagedRepository repo = new BasicManagedRepository(id, name, storage); + repo.setLocation( location.toAbsolutePath().toUri()); + return repo; + } + + protected EditableManagedRepository createRepository( String id, String name) throws IOException + { + Path dir = getRepoBaseDir( ).resolve( id ); + Files.createDirectories( dir ); + return createRepository( id, name, dir ); + } + + + + + @Test + void initializeFromConfig( ) + { + ManagedRepositoryHandler repoHandler = createHandler( ); + repoHandler.initializeFromConfig( ); + assertEquals( 5, repoHandler.getAll( ).size( ) ); + assertNotNull( repoHandler.get( "test-repo-01" ) ); + assertEquals( "n-test-repo-01", repoHandler.get( "test-repo-01" ).getName() ); + } + + @Test + void activateRepository( ) throws RepositoryException + { + String id = "test-repo-02"; + ManagedRepositoryHandler repoHandler = createHandler( ); + ManagedRepository repo = repoHandler.newInstance( RepositoryType.MAVEN, id ); + repoHandler.activateRepository( repo ); + assertFalse( hasRepositoryInConfig( id )); + assertNull( repoHandler.get( id ) ); + } + + @Test + void newInstancesFromConfig( ) throws RepositoryException + { + final String id = "test-repo-01"; + ManagedRepositoryHandler repoHandler = createHandler( ); + Configuration configuration = new Configuration( ); + repoHandler.remove( "test-repo-01", configuration ); + Map instances = repoHandler.newInstancesFromConfig( ); + assertFalse( repoHandler.hasRepository( id ) ); + assertTrue( instances.containsKey( id ) ); + assertEquals( RepositoryState.REFERENCES_SET, instances.get( id ).getLastState( ) ); + } + + @Test + void newInstance( ) throws RepositoryException + { + String id = "test-repo-03"; + ManagedRepositoryHandler repoHandler = createHandler( ); + ManagedRepository instance = repoHandler.newInstance( RepositoryType.MAVEN, id ); + assertNotNull( instance ); + assertEquals( id, instance.getId( ) ); + assertFalse( repoHandler.hasRepository( id ) ); + assertEquals( RepositoryState.REFERENCES_SET, instance.getLastState( ) ); + assertFalse( hasRepositoryInConfig( id ) ); + } + + + @Test + void put( ) throws IOException, RepositoryException + { + final String id = "test-repo-04"; + try + { + ManagedRepositoryHandler repoHandler = createHandler( ); + EditableManagedRepository repository = createRepository( id, "n-"+id ); + repoHandler.put( repository ); + ManagedRepository storedRepository = repoHandler.get( id ); + assertNotNull( storedRepository ); + assertEquals( id, storedRepository.getId( ) ); + assertEquals( "n-"+id, storedRepository.getName( ) ); + + EditableManagedRepository repository2 = createRepository( id, "n2-"+id ); + repoHandler.put( repository2 ); + storedRepository = repoHandler.get( id ); + assertNotNull( storedRepository ); + assertEquals( id, storedRepository.getId( ) ); + assertEquals( "n2-"+id, storedRepository.getName( ) ); + + assertTrue( hasRepositoryInConfig( id )); + } finally { + removeRepositoryFromConfig( id ); + } + } + + @Test + void putWithConfiguration( ) throws RepositoryException + { + String id = "test-repo-05"; + + try + { + ManagedRepositoryHandler repoHandler = createHandler( ); + ManagedRepositoryConfiguration configuration = new ManagedRepositoryConfiguration( ); + configuration.setId( id ); + configuration.setName( "n-" + id ); + repoHandler.put( configuration ); + + ManagedRepository repo = repoHandler.get( id ); + assertNotNull( repo ); + assertEquals( id, repo.getId( ) ); + assertEquals( "n-" + id, repo.getName( ) ); + assertTrue( hasRepositoryInConfig( id ) ); + } + finally + { + removeRepositoryFromConfig( id ); + } + } + + @Test + void testPutWithoutRegister( ) throws RepositoryException + { + final String id = "test-repo-06"; + ManagedRepositoryHandler repoHandler = createHandler( ); + Configuration aCfg = new Configuration( ); + ManagedRepositoryConfiguration configuration = new ManagedRepositoryConfiguration( ); + configuration.setId( id ); + configuration.setName( "n-"+id ); + repoHandler.put( configuration, aCfg ); + + ManagedRepository repo = repoHandler.get( id ); + assertNull( repo ); + assertFalse( hasRepositoryInConfig( id ) ); + assertTrue( aCfg.getManagedRepositories( ).stream( ).anyMatch( g -> g!=null && id.equals( g.getId( ) ) ) ); + + } + + @Test + void putWithCheck_invalid( ) throws RepositoryException + { + final String id = "test-repo-07"; + final String name = "n-"+id; + try + { + ManagedRepositoryHandler repoHandler = createHandler( ); + BasicManagedRepositoryValidator checker = new BasicManagedRepositoryValidator( configurationHandler ); + checker.setRepositoryRegistry( repositoryRegistry ); + ManagedRepositoryConfiguration configuration = new ManagedRepositoryConfiguration(); + configuration.setId( "" ); + configuration.setName( name ); + CheckedResult>> result = repoHandler.putWithCheck( configuration, checker ); + assertNull( repoHandler.get( id ) ); + assertNotNull( result.getResult( ) ); + assertNotNull( result.getResult( ).get( "id" ) ); + assertEquals( 2, result.getResult( ).get( "id" ).size( ) ); + assertEquals( ISEMPTY, result.getResult( ).get( "id" ).get( 0 ).getType( ) ); + assertFalse( hasRepositoryInConfig( id ) ); + assertFalse( hasRepositoryInConfig( "" ) ); + } finally + { + removeRepositoryFromConfig( id ); + } + } + + @Test + void remove( ) throws RepositoryException + { + final String id = "test-repo-08"; + ManagedRepositoryHandler repoHandler = createHandler( ); + ManagedRepositoryConfiguration configuration = new ManagedRepositoryConfiguration( ); + configuration.setId( id ); + configuration.setName( "n-"+id ); + repoHandler.put( configuration ); + assertTrue( hasRepositoryInConfig( id ) ); + assertNotNull( repoHandler.get( id ) ); + repoHandler.remove( id ); + assertNull( repoHandler.get( id ) ); + assertFalse( hasRepositoryInConfig( id ) ); + } + + @Test + void removeWithoutSave( ) throws RepositoryException + { + final String id = "test-repo-09"; + ManagedRepositoryHandler repoHandler = createHandler( ); + Configuration aCfg = new Configuration( ); + ManagedRepositoryConfiguration configuration = new ManagedRepositoryConfiguration( ); + configuration.setId( id ); + configuration.setName( "n-"+id ); + repoHandler.put( configuration, aCfg ); + assertTrue( aCfg.getManagedRepositories( ).stream( ).anyMatch( g -> g != null && id.equals( g.getId( ) ) ) ); + repoHandler.remove( id, aCfg ); + assertNull( repoHandler.get( id ) ); + assertTrue( aCfg.getManagedRepositories( ).stream( ).noneMatch( g -> g != null && id.equals( g.getId( ) ) ) ); + assertNull( repoHandler.get( id ) ); + + } + + + @Test + void validateRepository( ) throws IOException + { + final String id = "test-repo-10"; + ManagedRepositoryHandler repoHandler = createHandler( ); + EditableManagedRepository repository = createRepository( id, "n-"+id ); + CheckedResult>> result = repoHandler.validateRepository( repository ); + assertNotNull( result ); + assertEquals( 0, result.getResult( ).size( ) ); + + repository = createRepository( id, "n-test-repo-10###" ); + result = repoHandler.validateRepository( repository ); + assertNotNull( result ); + assertEquals( 1, result.getResult( ).size( ) ); + assertNotNull( result.getResult().get( "name" ) ); + + } + + @Test + void validateRepositoryIfExisting( ) throws IOException, RepositoryException + { + final String id = "test-repo-11"; + try + { + ManagedRepositoryHandler repoHandler = createHandler( ); + EditableManagedRepository repository = createRepository( id, "n-" + id ); + repoHandler.put( repository ); + CheckedResult>> result = repoHandler.validateRepository( repository ); + assertNotNull( result ); + assertEquals( 1, result.getResult( ).size( ) ); + } finally + { + removeRepositoryFromConfig( id ); + } + + } + + @Test + void validateRepositoryForUpdate( ) throws IOException, RepositoryException + { + final String id = "test-repo-12"; + try + { + ManagedRepositoryHandler repoHandler = createHandler( ); + EditableManagedRepository repository = createRepository( id, "n-" + id ); + repoHandler.put( repository ); + CheckedResult>> result = repoHandler.validateRepositoryForUpdate( repository ); + assertNotNull( result ); + assertEquals( 0, result.getResult( ).size( ) ); + } finally + { + removeRepositoryFromConfig( id ); + } + } + + @Test + void has( ) throws IOException, RepositoryException + { + final String id = "test-repo-13"; + try + { + ManagedRepositoryHandler repoHandler = createHandler( ); + EditableManagedRepository repository = createRepository( id, "n-" + id ); + assertFalse( repoHandler.hasRepository( id ) ); + repoHandler.put( repository ); + assertTrue( repoHandler.hasRepository( id ) ); + } finally + { + removeRepositoryFromConfig( id ); + } + } + +} \ No newline at end of file diff --git a/archiva-modules/archiva-base/archiva-repository-layer/src/test/resources/archiva-managed.xml b/archiva-modules/archiva-base/archiva-repository-layer/src/test/resources/archiva-managed.xml new file mode 100644 index 000000000..96b63b4d0 --- /dev/null +++ b/archiva-modules/archiva-base/archiva-repository-layer/src/test/resources/archiva-managed.xml @@ -0,0 +1,225 @@ + + + + 3.0.0 + + + internal + Archiva Managed Internal Repository + This is internal repository. + ${appserver.base}/repositories/internal + ${appserver.base}/repositories/internal/.indexer + default + true + false + true + true + 0 0 * * * ? + 30 + + + staging + Repository with staging + This is repository with staging. + ${appserver.base}/repositories/internal + ${appserver.base}/repositories/internal/.indexer + default + true + false + true + true + 0 0 * * * ? + 30 + true + + + snapshots + Archiva Managed Snapshot Repository + ${appserver.base}/repositories/snapshots + ${appserver.base}/repositories/snapshots/.indexer + default + false + true + false + true + 0 0\,30 * * * ? + 30 + + + test-repo-01 + n-test-repo-01 + ${appserver.base}/repositories/test-repo-01 + ${appserver.base}/repositories/test-repo-01/.indexer + default + true + false + false + true + 0 0\,30 * * * ? + 30 + + + + + central + Central Repository + https://repo.maven.apache.org/maven2 + default + 35 + + + + + test-group-01 + Test Group 01 + MAVEN + + internal + + + + + + + internal + central + + + disabled + once + fix + cached + + + **/* + + + + + + + jaxen/jars/jaxen-1.0-FCS-full.jar + jaxen:jaxen:1.0-FCS:full:jar + + + + + + + artifacts + + **/*.pom + **/*.jar + **/*.ear + **/*.war + **/*.car + **/*.sar + **/*.mar + **/*.rar + **/*.dtd + **/*.tld + **/*.tar.gz + **/*.tar.bz2 + **/*.zip + + + + indexable-content + + **/*.txt + **/*.TXT + **/*.block + **/*.config + **/*.pom + **/*.xml + **/*.xsd + **/*.dtd + **/*.tld + + + + auto-remove + + **/*.bak + **/*~ + **/*- + + + + ignored + + **/.htaccess + **/KEYS + **/*.rb + **/*.sh + **/.svn/** + **/.DAV/** + .index/** + .indexer/** + + + + + create-missing-checksums + validate-checksum + validate-signature + index-content + auto-remove + auto-rename + metadata-updater + create-archiva-metadata + duplicate-artifacts + + + + update-db-bad-content + + + + + + true + true + + + + + + jpa + + + cached + + + + + + + http://download.oracle.com/maven + com/sleepycat/je/license.txt + + + https://download.oracle.com/maven + com/sleepycat/je/license.txt + + + + + diff --git a/archiva-modules/archiva-base/archiva-repository-layer/src/test/resources/spring-context-managed.xml b/archiva-modules/archiva-base/archiva-repository-layer/src/test/resources/spring-context-managed.xml new file mode 100644 index 000000000..c19d5bca6 --- /dev/null +++ b/archiva-modules/archiva-base/archiva-repository-layer/src/test/resources/spring-context-managed.xml @@ -0,0 +1,57 @@ + + + + + + + + + + + + + + + + + ]]> + + + + + + + + + + + + \ No newline at end of file diff --git a/archiva-modules/archiva-base/archiva-storage-api/pom.xml b/archiva-modules/archiva-base/archiva-storage-api/pom.xml index 589e7d3f6..0bcf70ae4 100644 --- a/archiva-modules/archiva-base/archiva-storage-api/pom.xml +++ b/archiva-modules/archiva-base/archiva-storage-api/pom.xml @@ -50,11 +50,6 @@ test - - org.mockito - mockito-core - test - org.mockito mockito-core diff --git a/archiva-modules/archiva-maven/archiva-maven-indexer/src/test/java/org/apache/archiva/indexer/maven/MavenIndexManagerTest.java b/archiva-modules/archiva-maven/archiva-maven-indexer/src/test/java/org/apache/archiva/indexer/maven/MavenIndexManagerTest.java index 793a1908e..01215a39d 100644 --- a/archiva-modules/archiva-maven/archiva-maven-indexer/src/test/java/org/apache/archiva/indexer/maven/MavenIndexManagerTest.java +++ b/archiva-modules/archiva-maven/archiva-maven-indexer/src/test/java/org/apache/archiva/indexer/maven/MavenIndexManagerTest.java @@ -25,6 +25,7 @@ import org.apache.archiva.indexer.IndexCreationFailedException; import org.apache.archiva.repository.base.ArchivaRepositoryRegistry; import org.apache.archiva.repository.RepositoryType; import org.apache.archiva.repository.base.group.RepositoryGroupHandler; +import org.apache.archiva.repository.base.managed.ManagedRepositoryHandler; import org.apache.archiva.repository.features.IndexCreationFeature; import org.apache.archiva.repository.features.RemoteIndexFeature; import org.apache.archiva.repository.maven.MavenManagedRepository; @@ -62,6 +63,11 @@ public class MavenIndexManagerTest { @Inject ArchivaRepositoryRegistry repositoryRegistry; + @SuppressWarnings( "unused" ) + @Inject + ManagedRepositoryHandler managedRepositoryHandler; + + @SuppressWarnings( "unused" ) @Inject RepositoryGroupHandler groupHandler; diff --git a/archiva-modules/archiva-maven/archiva-maven-indexer/src/test/java/org/apache/archiva/indexer/maven/search/AbstractMavenRepositorySearch.java b/archiva-modules/archiva-maven/archiva-maven-indexer/src/test/java/org/apache/archiva/indexer/maven/search/AbstractMavenRepositorySearch.java index a5b8f647e..e5c38c0d8 100644 --- a/archiva-modules/archiva-maven/archiva-maven-indexer/src/test/java/org/apache/archiva/indexer/maven/search/AbstractMavenRepositorySearch.java +++ b/archiva-modules/archiva-maven/archiva-maven-indexer/src/test/java/org/apache/archiva/indexer/maven/search/AbstractMavenRepositorySearch.java @@ -32,6 +32,7 @@ import org.apache.archiva.proxy.ProxyRegistry; import org.apache.archiva.repository.base.ArchivaRepositoryRegistry; import org.apache.archiva.repository.Repository; import org.apache.archiva.repository.base.group.RepositoryGroupHandler; +import org.apache.archiva.repository.base.managed.ManagedRepositoryHandler; import org.apache.archiva.repository.features.IndexCreationFeature; import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner; import org.apache.commons.lang3.SystemUtils; @@ -46,6 +47,7 @@ import org.apache.maven.index.Scanner; import org.apache.maven.index.ScanningRequest; import org.apache.maven.index.ScanningResult; import org.apache.maven.index.context.IndexingContext; +import org.apache.maven.index_shaded.lucene.index.IndexUpgrader; import org.easymock.EasyMock; import org.easymock.IMocksControl; import org.junit.After; @@ -93,8 +95,14 @@ public abstract class AbstractMavenRepositorySearch @Inject ArchivaRepositoryRegistry repositoryRegistry; + @SuppressWarnings( "unused" ) @Inject - RepositoryGroupHandler repositoryGroupHandler; + ManagedRepositoryHandler managedRepositoryHandler; + + @SuppressWarnings( "unused" ) + @Inject + RepositoryGroupHandler groupHandler; + @Inject ProxyRegistry proxyRegistry; @@ -124,9 +132,11 @@ public abstract class AbstractMavenRepositorySearch FileUtils.deleteDirectory( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_1 + "/.indexer" ) ); assertFalse( Files.exists(Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_1 + "/.indexer" )) ); + Files.createDirectories( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_1 + "/.indexer" ) ); FileUtils.deleteDirectory( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_2 + "/.indexer" ) ); assertFalse( Files.exists(Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_2 + "/.indexer" )) ); + Files.createDirectories( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_2 + "/.indexer" ) ); archivaConfigControl = EasyMock.createControl(); @@ -212,7 +222,7 @@ public abstract class AbstractMavenRepositorySearch protected void createIndex( String repository, List filesToBeIndexed, boolean scan, Path indexDir, boolean copyFiles) throws Exception { - Repository rRepo = repositoryRegistry.getRepository(repository); + final Repository rRepo = repositoryRegistry.getRepository(repository); IndexCreationFeature icf = rRepo.getFeature(IndexCreationFeature.class).get(); @@ -228,12 +238,12 @@ public abstract class AbstractMavenRepositorySearch Path indexerDirectory = repoDir.resolve(".indexer" ); - if ( Files.exists(indexerDirectory) ) + if ( indexDir == null && Files.exists(indexerDirectory) ) { FileUtils.deleteDirectory( indexerDirectory ); + assertFalse( Files.exists(indexerDirectory) ); } - assertFalse( Files.exists(indexerDirectory) ); Path lockFile = repoDir.resolve(".indexer/write.lock" ); if ( Files.exists(lockFile) ) @@ -247,10 +257,21 @@ public abstract class AbstractMavenRepositorySearch indexDirectory.toFile().deleteOnExit(); FileUtils.deleteDirectory(indexDirectory); icf.setIndexPath(indexDirectory.toUri()); + config.getManagedRepositories( ).stream( ).filter( r -> r.getId( ).equals( rRepo.getId( ) ) ).findFirst( ).ifPresent( r -> + r.setIndexDir( indexDirectory.toAbsolutePath( ).toString( ) ) + ); + // IndexUpgrader.main(new String[]{indexDirectory.toAbsolutePath().toString()}); } else { icf.setIndexPath(indexDir.toUri()); + Files.createDirectories( indexDir ); + config.getManagedRepositories( ).stream( ).filter( r -> r.getId( ).equals( rRepo.getId( ) ) ).findFirst( ).ifPresent( r -> + r.setIndexDir( indexDir.toAbsolutePath( ).toString( ) ) + ); + IndexUpgrader.main(new String[]{indexDir.toAbsolutePath().toString()}); + } + if (copyFiles) { Path repo = Paths.get(org.apache.archiva.common.utils.FileUtils.getBasedir(), "src/test/" + repository); assertTrue(Files.exists(repo)); @@ -269,11 +290,11 @@ public abstract class AbstractMavenRepositorySearch repositoryRegistry.reload(); archivaConfigControl.reset(); - rRepo = repositoryRegistry.getRepository(repository); - icf = rRepo.getFeature(IndexCreationFeature.class).get(); + Repository rRepo2 = repositoryRegistry.getRepository( repository ); + icf = rRepo2.getFeature(IndexCreationFeature.class).get(); - archivaCtx = rRepo.getIndexingContext(); + archivaCtx = rRepo2.getIndexingContext(); context = archivaCtx.getBaseContext(IndexingContext.class); diff --git a/archiva-modules/archiva-maven/archiva-maven-indexer/src/test/java/org/apache/archiva/indexer/maven/search/MavenRepositorySearchOSGITest.java b/archiva-modules/archiva-maven/archiva-maven-indexer/src/test/java/org/apache/archiva/indexer/maven/search/MavenRepositorySearchOSGITest.java index 112083760..416db35c7 100644 --- a/archiva-modules/archiva-maven/archiva-maven-indexer/src/test/java/org/apache/archiva/indexer/maven/search/MavenRepositorySearchOSGITest.java +++ b/archiva-modules/archiva-maven/archiva-maven-indexer/src/test/java/org/apache/archiva/indexer/maven/search/MavenRepositorySearchOSGITest.java @@ -24,6 +24,7 @@ import org.apache.archiva.indexer.search.SearchResultHit; import org.apache.archiva.indexer.search.SearchResults; import org.apache.archiva.repository.base.ArchivaRepositoryRegistry; import org.apache.archiva.repository.base.group.RepositoryGroupHandler; +import org.apache.archiva.repository.base.managed.ManagedRepositoryHandler; import org.easymock.EasyMock; import org.junit.After; import org.junit.Test; @@ -44,8 +45,14 @@ public class MavenRepositorySearchOSGITest @Inject ArchivaRepositoryRegistry repositoryRegistry; + @SuppressWarnings( "unused" ) @Inject - RepositoryGroupHandler repositoryGroupHandler; + ManagedRepositoryHandler managedRepositoryHandler; + + @SuppressWarnings( "unused" ) + @Inject + RepositoryGroupHandler groupHandler; + @After @Override diff --git a/archiva-modules/archiva-maven/archiva-maven-indexer/src/test/java/org/apache/archiva/indexer/maven/search/MavenRepositorySearchPaginateTest.java b/archiva-modules/archiva-maven/archiva-maven-indexer/src/test/java/org/apache/archiva/indexer/maven/search/MavenRepositorySearchPaginateTest.java index 124c4d1b3..5f236e462 100644 --- a/archiva-modules/archiva-maven/archiva-maven-indexer/src/test/java/org/apache/archiva/indexer/maven/search/MavenRepositorySearchPaginateTest.java +++ b/archiva-modules/archiva-maven/archiva-maven-indexer/src/test/java/org/apache/archiva/indexer/maven/search/MavenRepositorySearchPaginateTest.java @@ -25,6 +25,7 @@ import org.apache.archiva.indexer.search.SearchResults; import org.apache.archiva.indexer.util.SearchUtil; import org.apache.archiva.repository.base.ArchivaRepositoryRegistry; import org.apache.archiva.repository.base.group.RepositoryGroupHandler; +import org.apache.archiva.repository.base.managed.ManagedRepositoryHandler; import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner; import org.junit.After; import org.junit.Test; @@ -46,8 +47,14 @@ public class MavenRepositorySearchPaginateTest @Inject ArchivaRepositoryRegistry repositoryRegistry; + @SuppressWarnings( "unused" ) @Inject - RepositoryGroupHandler repositoryGroupHandler; + ManagedRepositoryHandler managedRepositoryHandler; + + @SuppressWarnings( "unused" ) + @Inject + RepositoryGroupHandler groupHandler; + @After public void endTests() { diff --git a/archiva-modules/archiva-maven/archiva-maven-indexer/src/test/java/org/apache/archiva/indexer/maven/search/MavenRepositorySearchTest.java b/archiva-modules/archiva-maven/archiva-maven-indexer/src/test/java/org/apache/archiva/indexer/maven/search/MavenRepositorySearchTest.java index 40914faea..f22ef98aa 100644 --- a/archiva-modules/archiva-maven/archiva-maven-indexer/src/test/java/org/apache/archiva/indexer/maven/search/MavenRepositorySearchTest.java +++ b/archiva-modules/archiva-maven/archiva-maven-indexer/src/test/java/org/apache/archiva/indexer/maven/search/MavenRepositorySearchTest.java @@ -33,6 +33,7 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; +import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; @@ -895,12 +896,12 @@ public class MavenRepositorySearchTest throws Exception { - Path repo = Paths.get("target/repo-release"); + Path repo = Paths.get("target/repo-release-index-test/repo-release"); try { Path indexDirectory = repo.resolve(".indexer"); Path zipFile = Paths.get(Thread.currentThread().getContextClassLoader().getResource("repo-release.zip").toURI()); FileUtils.unzip(zipFile, repo.getParent()); - IndexUpgrader.main(new String[]{indexDirectory.toAbsolutePath().toString()}); +// IndexUpgrader.main(new String[]{indexDirectory.toAbsolutePath().toString(), "-delete-prior-commits"}); createIndex(REPO_RELEASE, Collections.emptyList(), false, indexDirectory, false); // indexer.addIndexingContext( REPO_RELEASE, REPO_RELEASE, repo.toFile(), indexDirectory.toFile(), diff --git a/archiva-modules/archiva-maven/archiva-maven-proxy/src/test/java/org/apache/archiva/proxy/AbstractProxyTestCase.java b/archiva-modules/archiva-maven/archiva-maven-proxy/src/test/java/org/apache/archiva/proxy/AbstractProxyTestCase.java index ec1a5319d..18bc3a3ab 100644 --- a/archiva-modules/archiva-maven/archiva-maven-proxy/src/test/java/org/apache/archiva/proxy/AbstractProxyTestCase.java +++ b/archiva-modules/archiva-maven/archiva-maven-proxy/src/test/java/org/apache/archiva/proxy/AbstractProxyTestCase.java @@ -25,6 +25,7 @@ import org.apache.archiva.policies.*; import org.apache.archiva.proxy.model.RepositoryProxyHandler; import org.apache.archiva.repository.*; import org.apache.archiva.repository.base.managed.BasicManagedRepository; +import org.apache.archiva.repository.base.managed.ManagedRepositoryHandler; import org.apache.archiva.repository.storage.StorageAsset; import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner; import org.apache.maven.wagon.Wagon; @@ -70,6 +71,11 @@ public abstract class AbstractProxyTestCase @Inject RepositoryRegistry repositoryRegistry; + @SuppressWarnings( "unused" ) + @Inject + ManagedRepositoryHandler managedRepositoryHandler; + + protected static final String ID_PROXIED1 = "proxied1"; protected static final String ID_PROXIED1_TARGET = "proxied1-target"; diff --git a/archiva-modules/archiva-maven/archiva-maven-proxy/src/test/java/org/apache/archiva/proxy/CacheFailuresTransferTest.java b/archiva-modules/archiva-maven/archiva-maven-proxy/src/test/java/org/apache/archiva/proxy/CacheFailuresTransferTest.java index 83728f189..94a04f194 100644 --- a/archiva-modules/archiva-maven/archiva-maven-proxy/src/test/java/org/apache/archiva/proxy/CacheFailuresTransferTest.java +++ b/archiva-modules/archiva-maven/archiva-maven-proxy/src/test/java/org/apache/archiva/proxy/CacheFailuresTransferTest.java @@ -25,6 +25,7 @@ import org.apache.archiva.policies.ChecksumPolicy; import org.apache.archiva.policies.ReleasesPolicy; import org.apache.archiva.policies.SnapshotsPolicy; import org.apache.archiva.policies.urlcache.UrlFailureCache; +import org.apache.archiva.repository.base.managed.ManagedRepositoryHandler; import org.apache.archiva.repository.content.BaseRepositoryContentLayout; import org.apache.archiva.repository.content.Artifact; import org.apache.archiva.repository.storage.StorageAsset; @@ -53,6 +54,7 @@ public class CacheFailuresTransferTest @Inject UrlFailureCache urlFailureCache; + @Test public void testGetWithCacheFailuresOn( ) throws Exception diff --git a/archiva-modules/archiva-maven/archiva-maven-proxy/src/test/java/org/apache/archiva/proxy/HttpProxyTransferTest.java b/archiva-modules/archiva-maven/archiva-maven-proxy/src/test/java/org/apache/archiva/proxy/HttpProxyTransferTest.java index a2ab1a299..f7a4cbf40 100644 --- a/archiva-modules/archiva-maven/archiva-maven-proxy/src/test/java/org/apache/archiva/proxy/HttpProxyTransferTest.java +++ b/archiva-modules/archiva-maven/archiva-maven-proxy/src/test/java/org/apache/archiva/proxy/HttpProxyTransferTest.java @@ -30,6 +30,7 @@ import org.apache.archiva.policies.PropagateErrorsOnUpdateDownloadPolicy; import org.apache.archiva.policies.ReleasesPolicy; import org.apache.archiva.policies.SnapshotsPolicy; import org.apache.archiva.proxy.model.RepositoryProxyHandler; +import org.apache.archiva.repository.base.managed.ManagedRepositoryHandler; import org.apache.archiva.repository.content.BaseRepositoryContentLayout; import org.apache.archiva.repository.ManagedRepository; import org.apache.archiva.repository.ManagedRepositoryContent; @@ -100,6 +101,11 @@ public class HttpProxyTransferTest @Inject private ProxyRegistry proxyRegistry; + @SuppressWarnings( "unused" ) + @Inject + ManagedRepositoryHandler managedRepositoryHandler; + + private Server server; protected ManagedRepositoryContent createRepository( String id, String name, String path, String layout ) diff --git a/archiva-modules/archiva-maven/archiva-maven-repository/src/main/java/org/apache/archiva/repository/maven/MavenRepositoryProvider.java b/archiva-modules/archiva-maven/archiva-maven-repository/src/main/java/org/apache/archiva/repository/maven/MavenRepositoryProvider.java index de7d279ee..963621fec 100644 --- a/archiva-modules/archiva-maven/archiva-maven-repository/src/main/java/org/apache/archiva/repository/maven/MavenRepositoryProvider.java +++ b/archiva-modules/archiva-maven/archiva-maven-repository/src/main/java/org/apache/archiva/repository/maven/MavenRepositoryProvider.java @@ -497,7 +497,12 @@ public class MavenRepositoryProvider implements RepositoryProvider { if (StringUtils.isNotBlank(repository.getIndexDir())) { Path indexDir; try { - indexDir = Paths.get(new URI(repository.getIndexDir().startsWith("file://") ? repository.getIndexDir() : "file://" + repository.getIndexDir())); + if (repository.getIndexDir().startsWith( "file:" )) { + indexDir = Paths.get( new URI( repository.getIndexDir( ) ) ); + } else + { + indexDir = Paths.get( repository.getIndexDir( ) ); + } if (indexDir.isAbsolute()) { Path newDir = indexDir.getParent().resolve(indexDir.getFileName() + StagingRepositoryFeature.STAGING_REPO_POSTFIX); log.debug("Changing index directory {} -> {}", indexDir, newDir); diff --git a/archiva-modules/archiva-maven/archiva-maven-repository/src/test/java/org/apache/archiva/repository/maven/metadata/storage/Maven2RepositoryMetadataResolverMRM1411RepoGroupTest.java b/archiva-modules/archiva-maven/archiva-maven-repository/src/test/java/org/apache/archiva/repository/maven/metadata/storage/Maven2RepositoryMetadataResolverMRM1411RepoGroupTest.java index d92beb6df..6f53f8815 100644 --- a/archiva-modules/archiva-maven/archiva-maven-repository/src/test/java/org/apache/archiva/repository/maven/metadata/storage/Maven2RepositoryMetadataResolverMRM1411RepoGroupTest.java +++ b/archiva-modules/archiva-maven/archiva-maven-repository/src/test/java/org/apache/archiva/repository/maven/metadata/storage/Maven2RepositoryMetadataResolverMRM1411RepoGroupTest.java @@ -36,6 +36,7 @@ import org.apache.archiva.metadata.repository.storage.ReadMetadataRequest; import org.apache.archiva.proxy.maven.WagonFactory; import org.apache.archiva.proxy.maven.WagonFactoryRequest; import org.apache.archiva.repository.RepositoryRegistry; +import org.apache.archiva.repository.base.managed.ManagedRepositoryHandler; import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner; import org.apache.commons.io.FileUtils; import org.apache.maven.wagon.Wagon; @@ -100,6 +101,11 @@ public class Maven2RepositoryMetadataResolverMRM1411RepoGroupTest @Inject RepositoryRegistry repositoryRegistry; + @SuppressWarnings( "unused" ) + @Inject + ManagedRepositoryHandler managedRepositoryHandler; + + private WagonFactory wagonFactory; ManagedRepositoryConfiguration testRepo; diff --git a/archiva-modules/archiva-maven/archiva-maven-repository/src/test/java/org/apache/archiva/repository/maven/metadata/storage/Maven2RepositoryMetadataResolverMRM1411Test.java b/archiva-modules/archiva-maven/archiva-maven-repository/src/test/java/org/apache/archiva/repository/maven/metadata/storage/Maven2RepositoryMetadataResolverMRM1411Test.java index 838fea231..96c448b9a 100644 --- a/archiva-modules/archiva-maven/archiva-maven-repository/src/test/java/org/apache/archiva/repository/maven/metadata/storage/Maven2RepositoryMetadataResolverMRM1411Test.java +++ b/archiva-modules/archiva-maven/archiva-maven-repository/src/test/java/org/apache/archiva/repository/maven/metadata/storage/Maven2RepositoryMetadataResolverMRM1411Test.java @@ -37,6 +37,7 @@ import org.apache.archiva.proxy.maven.WagonFactory; import org.apache.archiva.proxy.maven.WagonFactoryRequest; import org.apache.archiva.repository.ReleaseScheme; import org.apache.archiva.repository.RepositoryRegistry; +import org.apache.archiva.repository.base.managed.ManagedRepositoryHandler; import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner; import org.apache.commons.io.FileUtils; import org.apache.maven.wagon.Wagon; @@ -97,6 +98,11 @@ public class Maven2RepositoryMetadataResolverMRM1411Test @Inject RepositoryRegistry repositoryRegistry; + @SuppressWarnings( "unused" ) + @Inject + ManagedRepositoryHandler managedRepositoryHandler; + + private WagonFactory wagonFactory; ManagedRepositoryConfiguration testRepo; @@ -141,6 +147,7 @@ public class Maven2RepositoryMetadataResolverMRM1411Test wagonFactory = mock( WagonFactory.class ); + assertNotNull( storage ); storage.setWagonFactory( wagonFactory ); Wagon wagon = new MockWagon(); @@ -153,6 +160,7 @@ public class Maven2RepositoryMetadataResolverMRM1411Test public void testGetProjectVersionMetadataWithParentSuccessful() throws Exception { + assertNotNull( storage ); copyTestArtifactWithParent( "target/test-classes/com/example/test/test-artifact-module-a", "target/test-repository/com/example/test/test-artifact-module-a" ); copyTestArtifactWithParent( "src/test/resources/com/example/test/test-artifact-parent", @@ -201,12 +209,14 @@ public class Maven2RepositoryMetadataResolverMRM1411Test public void testGetProjectVersionMetadataWithParentNoRemoteReposConfigured() throws Exception { + assertNotNull( storage ); // remove configuration Configuration config = configuration.getConfiguration(); RemoteRepositoryConfiguration remoteRepo = config.findRemoteRepositoryById( TEST_REMOTE_REPO_ID ); config.removeRemoteRepository( remoteRepo ); configuration.save( config ); + assertNotNull( storage ); copyTestArtifactWithParent( "target/test-classes/com/example/test/test-artifact-module-a", "target/test-repository/com/example/test/test-artifact-module-a" ); @@ -233,6 +243,7 @@ public class Maven2RepositoryMetadataResolverMRM1411Test public void testGetProjectVersionMetadataWithParentNotInAnyRemoteRepo() throws Exception { + assertNotNull( storage ); copyTestArtifactWithParent( "target/test-classes/com/example/test/test-artifact-module-a", "target/test-repository/com/example/test/test-artifact-module-a" ); diff --git a/archiva-modules/archiva-maven/archiva-maven-repository/src/test/java/org/apache/archiva/repository/maven/metadata/storage/Maven2RepositoryMetadataResolverManagedReleaseTest.java b/archiva-modules/archiva-maven/archiva-maven-repository/src/test/java/org/apache/archiva/repository/maven/metadata/storage/Maven2RepositoryMetadataResolverManagedReleaseTest.java index 9bb471ae3..dd29a9a8b 100644 --- a/archiva-modules/archiva-maven/archiva-maven-repository/src/test/java/org/apache/archiva/repository/maven/metadata/storage/Maven2RepositoryMetadataResolverManagedReleaseTest.java +++ b/archiva-modules/archiva-maven/archiva-maven-repository/src/test/java/org/apache/archiva/repository/maven/metadata/storage/Maven2RepositoryMetadataResolverManagedReleaseTest.java @@ -25,6 +25,7 @@ import org.apache.archiva.metadata.repository.storage.ReadMetadataRequest; import org.apache.archiva.metadata.repository.storage.RepositoryStorageRuntimeException; import org.apache.archiva.proxy.maven.WagonFactory; import org.apache.archiva.repository.RepositoryRegistry; +import org.apache.archiva.repository.base.managed.ManagedRepositoryHandler; import org.junit.Before; import org.junit.Test; @@ -44,6 +45,11 @@ public class Maven2RepositoryMetadataResolverManagedReleaseTest @Inject RepositoryRegistry repositoryRegistry; + @SuppressWarnings( "unused" ) + @Inject + ManagedRepositoryHandler managedRepositoryHandler; + + private static final String TEST_REPO_ID = "test"; private static final String TEST_REMOTE_REPO_ID = "central"; diff --git a/archiva-modules/archiva-maven/archiva-maven-repository/src/test/java/org/apache/archiva/repository/maven/metadata/storage/Maven2RepositoryMetadataResolverManagedSnapshotTest.java b/archiva-modules/archiva-maven/archiva-maven-repository/src/test/java/org/apache/archiva/repository/maven/metadata/storage/Maven2RepositoryMetadataResolverManagedSnapshotTest.java index bd117a9c1..68b8bf4e6 100644 --- a/archiva-modules/archiva-maven/archiva-maven-repository/src/test/java/org/apache/archiva/repository/maven/metadata/storage/Maven2RepositoryMetadataResolverManagedSnapshotTest.java +++ b/archiva-modules/archiva-maven/archiva-maven-repository/src/test/java/org/apache/archiva/repository/maven/metadata/storage/Maven2RepositoryMetadataResolverManagedSnapshotTest.java @@ -24,6 +24,7 @@ import org.apache.archiva.metadata.model.ProjectVersionMetadata; import org.apache.archiva.metadata.repository.storage.ReadMetadataRequest; import org.apache.archiva.metadata.repository.storage.RepositoryStorageRuntimeException; import org.apache.archiva.repository.RepositoryRegistry; +import org.apache.archiva.repository.base.managed.ManagedRepositoryHandler; import org.junit.Before; import org.junit.Test; @@ -42,6 +43,11 @@ public class Maven2RepositoryMetadataResolverManagedSnapshotTest @Inject RepositoryRegistry repositoryRegistry; + @SuppressWarnings( "unused" ) + @Inject + ManagedRepositoryHandler managedRepositoryHandler; + + private static final String TEST_REPO_ID = "test"; private static final String TEST_REMOTE_REPO_ID = "central"; diff --git a/archiva-modules/archiva-maven/archiva-maven-repository/src/test/java/org/apache/archiva/repository/maven/metadata/storage/Maven2RepositoryMetadataResolverTest.java b/archiva-modules/archiva-maven/archiva-maven-repository/src/test/java/org/apache/archiva/repository/maven/metadata/storage/Maven2RepositoryMetadataResolverTest.java index e3bd6b1d9..b91fc6870 100644 --- a/archiva-modules/archiva-maven/archiva-maven-repository/src/test/java/org/apache/archiva/repository/maven/metadata/storage/Maven2RepositoryMetadataResolverTest.java +++ b/archiva-modules/archiva-maven/archiva-maven-repository/src/test/java/org/apache/archiva/repository/maven/metadata/storage/Maven2RepositoryMetadataResolverTest.java @@ -40,6 +40,7 @@ import org.apache.archiva.metadata.repository.storage.RepositoryStorageMetadataN import org.apache.archiva.proxy.maven.WagonFactory; import org.apache.archiva.proxy.maven.WagonFactoryRequest; import org.apache.archiva.repository.RepositoryRegistry; +import org.apache.archiva.repository.base.managed.ManagedRepositoryHandler; import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner; import org.apache.commons.io.FileUtils; import org.apache.maven.wagon.Wagon; @@ -80,6 +81,11 @@ public class Maven2RepositoryMetadataResolverTest @Inject RepositoryRegistry repositoryRegistry; + @SuppressWarnings( "unused" ) + @Inject + ManagedRepositoryHandler managedRepositoryHandler; + + private static final String TEST_REPO_ID = "test"; private static final String TEST_REMOTE_REPO_ID = "central"; diff --git a/archiva-modules/archiva-maven/archiva-maven-scheduler/src/test/java/org/apache/archiva/scheduler/indexing/maven/ArchivaIndexingTaskExecutorTest.java b/archiva-modules/archiva-maven/archiva-maven-scheduler/src/test/java/org/apache/archiva/scheduler/indexing/maven/ArchivaIndexingTaskExecutorTest.java index f8c4a3443..fe3eb921b 100644 --- a/archiva-modules/archiva-maven/archiva-maven-scheduler/src/test/java/org/apache/archiva/scheduler/indexing/maven/ArchivaIndexingTaskExecutorTest.java +++ b/archiva-modules/archiva-maven/archiva-maven-scheduler/src/test/java/org/apache/archiva/scheduler/indexing/maven/ArchivaIndexingTaskExecutorTest.java @@ -27,6 +27,7 @@ import org.apache.archiva.repository.base.managed.BasicManagedRepository; import org.apache.archiva.repository.ManagedRepository; import org.apache.archiva.repository.ReleaseScheme; import org.apache.archiva.repository.base.group.RepositoryGroupHandler; +import org.apache.archiva.repository.base.managed.ManagedRepositoryHandler; import org.apache.archiva.repository.storage.StorageAsset; import org.apache.archiva.repository.features.IndexCreationFeature; import org.apache.archiva.scheduler.indexing.ArtifactIndexingTask; @@ -74,6 +75,11 @@ public class ArchivaIndexingTaskExecutorTest @Inject ArchivaRepositoryRegistry repositoryRegistry; + @SuppressWarnings( "unused" ) + @Inject + ManagedRepositoryHandler managedRepositoryHandler; + + @SuppressWarnings( "unused" ) @Inject RepositoryGroupHandler repositoryGroupHandler; diff --git a/archiva-modules/archiva-maven/archiva-maven-scheduler/src/test/java/org/apache/archiva/scheduler/indexing/maven/DownloadRemoteIndexTaskTest.java b/archiva-modules/archiva-maven/archiva-maven-scheduler/src/test/java/org/apache/archiva/scheduler/indexing/maven/DownloadRemoteIndexTaskTest.java index 8a4ad112e..bfeb49802 100644 --- a/archiva-modules/archiva-maven/archiva-maven-scheduler/src/test/java/org/apache/archiva/scheduler/indexing/maven/DownloadRemoteIndexTaskTest.java +++ b/archiva-modules/archiva-maven/archiva-maven-scheduler/src/test/java/org/apache/archiva/scheduler/indexing/maven/DownloadRemoteIndexTaskTest.java @@ -29,6 +29,8 @@ import org.apache.archiva.repository.RepositoryProvider; import org.apache.archiva.repository.RepositoryRegistry; import org.apache.archiva.repository.RepositoryType; import org.apache.archiva.repository.UnsupportedURIException; +import org.apache.archiva.repository.base.group.RepositoryGroupHandler; +import org.apache.archiva.repository.base.managed.ManagedRepositoryHandler; import org.apache.archiva.repository.features.IndexCreationFeature; import org.apache.archiva.repository.features.RemoteIndexFeature; import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner; @@ -90,6 +92,15 @@ public class DownloadRemoteIndexTaskTest @Inject RepositoryRegistry repositoryRegistry; + @SuppressWarnings( "unused" ) + @Inject + ManagedRepositoryHandler managedRepositoryHandler; + + @SuppressWarnings( "unused" ) + @Inject + RepositoryGroupHandler repositoryGroupHandler; + + @Inject RepositoryProvider repositoryProvider; diff --git a/archiva-modules/archiva-scheduler/archiva-scheduler-repository/src/test/java/org/apache/archiva/scheduler/repository/AbstractArchivaRepositoryScanningTaskExecutorTest.java b/archiva-modules/archiva-scheduler/archiva-scheduler-repository/src/test/java/org/apache/archiva/scheduler/repository/AbstractArchivaRepositoryScanningTaskExecutorTest.java index edacadf75..904a360a4 100644 --- a/archiva-modules/archiva-scheduler/archiva-scheduler-repository/src/test/java/org/apache/archiva/scheduler/repository/AbstractArchivaRepositoryScanningTaskExecutorTest.java +++ b/archiva-modules/archiva-scheduler/archiva-scheduler-repository/src/test/java/org/apache/archiva/scheduler/repository/AbstractArchivaRepositoryScanningTaskExecutorTest.java @@ -30,6 +30,7 @@ import org.apache.archiva.components.taskqueue.execution.TaskExecutor; import org.apache.archiva.repository.ManagedRepository; import org.apache.archiva.repository.RepositoryRegistry; import org.apache.archiva.repository.base.group.RepositoryGroupHandler; +import org.apache.archiva.repository.base.managed.ManagedRepositoryHandler; import org.apache.archiva.scheduler.repository.model.RepositoryTask; import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner; import org.junit.After; @@ -66,6 +67,11 @@ public abstract class AbstractArchivaRepositoryScanningTaskExecutorTest @Inject RepositoryRegistry repositoryRegistry; + @SuppressWarnings( "unused" ) + @Inject + ManagedRepositoryHandler managedRepositoryHandler; + + @SuppressWarnings( "unused" ) @Inject RepositoryGroupHandler groupHandler; diff --git a/archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/archiva/webdav/AbstractRepositoryServletTestCase.java b/archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/archiva/webdav/AbstractRepositoryServletTestCase.java index 49974fd24..006df7fcf 100644 --- a/archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/archiva/webdav/AbstractRepositoryServletTestCase.java +++ b/archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/archiva/webdav/AbstractRepositoryServletTestCase.java @@ -27,10 +27,12 @@ import org.apache.archiva.configuration.Configuration; import org.apache.archiva.configuration.ManagedRepositoryConfiguration; import org.apache.archiva.configuration.RemoteRepositoryConfiguration; import org.apache.archiva.indexer.ArchivaIndexingContext; +import org.apache.archiva.repository.RepositoryException; import org.apache.archiva.repository.base.ArchivaRepositoryRegistry; import org.apache.archiva.repository.ManagedRepository; import org.apache.archiva.repository.RepositoryType; import org.apache.archiva.repository.base.group.RepositoryGroupHandler; +import org.apache.archiva.repository.base.managed.ManagedRepositoryHandler; import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner; import org.apache.archiva.webdav.httpunit.MkColMethodWebRequest; import org.apache.commons.io.FileUtils; @@ -104,6 +106,10 @@ public abstract class AbstractRepositoryServletTestCase @Inject RepositoryGroupHandler repositoryGroupHandler; + @SuppressWarnings( "unused" ) + @Inject + ManagedRepositoryHandler managedRepositoryHandler; + @Inject ArchivaRepositoryRegistry repositoryRegistry; @@ -166,8 +172,16 @@ public abstract class AbstractRepositoryServletTestCase System.setProperty( "appserver.base", getAppserverBase().toAbsolutePath().toString()); log.info("setUp appserverBase={}, projectBase={}, workingDir={}", getAppserverBase(), getProjectBase(), Paths.get("").toString()); - repositoryRegistry.getRepositories().stream().forEach(r -> r.close()); - + repositoryRegistry.getRepositories().stream().forEach(r -> { + try + { + repositoryRegistry.removeRepository( r ); + } + catch ( RepositoryException e ) + { + e.printStackTrace( ); + } + } ); org.apache.archiva.common.utils.FileUtils.deleteDirectory( getAppserverBase() ); Path testConf = getProjectBase().resolve( "src/test/resources/repository-archiva.xml" ); @@ -191,20 +205,21 @@ public abstract class AbstractRepositoryServletTestCase config.getRemoteRepositories().clear(); saveConfiguration( archivaConfiguration ); + // repositoryRegistry.reload(); - ArchivaIndexingContext ctx = repositoryRegistry.getManagedRepository( REPOID_INTERNAL ).getIndexingContext( ); - try - { - if (repositoryRegistry.getIndexManager(RepositoryType.MAVEN)!=null) { - repositoryRegistry.getIndexManager(RepositoryType.MAVEN).pack(ctx); - } - } finally - { - if (ctx!=null) - { - ctx.close( ); - } - } + // ArchivaIndexingContext ctx = repositoryRegistry.getManagedRepository( REPOID_INTERNAL ).getIndexingContext( ); +// try +// { +// if (repositoryRegistry.getIndexManager(RepositoryType.MAVEN)!=null) { +// repositoryRegistry.getIndexManager(RepositoryType.MAVEN).pack(ctx); +// } +// } finally +// { +// if (ctx!=null) +// { +// ctx.close( ); +// } +// } CacheManager.getInstance().clearAll(); @@ -856,8 +871,8 @@ public abstract class AbstractRepositoryServletTestCase repo.setLocation( location.toAbsolutePath().toString() ); repo.setBlockRedeployments( blockRedeployments ); repo.setType( "MAVEN" ); - repo.setIndexDir(".indexer"); - repo.setPackedIndexDir(".index"); + repo.setIndexDir(location.resolve( ".indexer" ).toAbsolutePath().toString()); + repo.setPackedIndexDir( location.resolve( ".index" ).toAbsolutePath( ).toString( ) ); return repo; } @@ -892,10 +907,13 @@ public abstract class AbstractRepositoryServletTestCase protected void setupCleanRepo( Path repoRootDir ) throws IOException { - org.apache.archiva.common.utils.FileUtils.deleteDirectory( repoRootDir ); - if ( !Files.exists(repoRootDir) ) + if (repoRootDir!=null) { - Files.createDirectories( repoRootDir ); + org.apache.archiva.common.utils.FileUtils.deleteDirectory( repoRootDir ); + if ( !Files.exists( repoRootDir ) ) + { + Files.createDirectories( repoRootDir ); + } } } diff --git a/archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/archiva/webdav/ArchivaDavResourceFactoryTest.java b/archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/archiva/webdav/ArchivaDavResourceFactoryTest.java index 8d1bde66d..9b2dd4809 100644 --- a/archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/archiva/webdav/ArchivaDavResourceFactoryTest.java +++ b/archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/archiva/webdav/ArchivaDavResourceFactoryTest.java @@ -35,6 +35,7 @@ import org.apache.archiva.configuration.RepositoryGroupConfiguration; import org.apache.archiva.metadata.repository.storage.RepositoryPathTranslator; import org.apache.archiva.repository.ManagedRepositoryContent; import org.apache.archiva.repository.base.group.RepositoryGroupHandler; +import org.apache.archiva.repository.base.managed.ManagedRepositoryHandler; import org.apache.archiva.repository.maven.content.MavenContentHelper; import org.apache.archiva.repository.maven.metadata.storage.ArtifactMappingProvider; import org.apache.archiva.proxy.ProxyRegistry; @@ -163,6 +164,10 @@ public class ArchivaDavResourceFactoryTest @Inject RepositoryGroupHandler repositoryGroupHandler; + @SuppressWarnings( "unused" ) + @Inject + ManagedRepositoryHandler managedRepositoryHandler; + public Path getProjectBase() { if (this.projectBase.get()==null) { String pathVal = System.getProperty("mvn.project.base.dir"); @@ -195,7 +200,7 @@ public class ArchivaDavResourceFactoryTest archivaConfiguration = archivaConfigurationControl.createMock( ArchivaConfiguration.class ); config = new Configuration(); - expect( archivaConfiguration.getConfiguration() ).andReturn( config ).times( 2, 20 ); + expect( archivaConfiguration.getConfiguration() ).andReturn( config ).times( 2, 25 ); expect (archivaConfiguration.getDefaultLocale()).andReturn( Locale.getDefault() ).anyTimes(); archivaConfiguration.addListener( EasyMock.anyObject( ) ); expectLastCall().times(0, 4); diff --git a/archiva-modules/metadata/metadata-repository-api/src/test/java/org/apache/archiva/metadata/repository/AbstractMetadataRepositoryTest.java b/archiva-modules/metadata/metadata-repository-api/src/test/java/org/apache/archiva/metadata/repository/AbstractMetadataRepositoryTest.java index b29f7da34..abcd24955 100644 --- a/archiva-modules/metadata/metadata-repository-api/src/test/java/org/apache/archiva/metadata/repository/AbstractMetadataRepositoryTest.java +++ b/archiva-modules/metadata/metadata-repository-api/src/test/java/org/apache/archiva/metadata/repository/AbstractMetadataRepositoryTest.java @@ -24,8 +24,19 @@ import org.apache.archiva.checksum.ChecksumAlgorithm; import org.apache.archiva.metadata.QueryParameter; import org.apache.archiva.metadata.generic.GenericMetadataFacet; import org.apache.archiva.metadata.generic.GenericMetadataFacetFactory; -import org.apache.archiva.metadata.model.*; -import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner; +import org.apache.archiva.metadata.model.ArtifactMetadata; +import org.apache.archiva.metadata.model.CiManagement; +import org.apache.archiva.metadata.model.Dependency; +import org.apache.archiva.metadata.model.IssueManagement; +import org.apache.archiva.metadata.model.License; +import org.apache.archiva.metadata.model.MailingList; +import org.apache.archiva.metadata.model.MetadataFacet; +import org.apache.archiva.metadata.model.MetadataFacetFactory; +import org.apache.archiva.metadata.model.Organization; +import org.apache.archiva.metadata.model.ProjectMetadata; +import org.apache.archiva.metadata.model.ProjectVersionMetadata; +import org.apache.archiva.metadata.model.ProjectVersionReference; +import org.apache.archiva.metadata.model.Scm; import org.junit.Test; import org.junit.runner.RunWith; import org.slf4j.Logger; @@ -36,7 +47,17 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.text.SimpleDateFormat; import java.time.ZoneId; import java.time.ZonedDateTime; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Calendar; +import java.util.Collection; +import java.util.Collections; +import java.util.Comparator; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.TimeZone; import java.util.stream.Collectors; import java.util.stream.Stream; diff --git a/archiva-modules/metadata/metadata-store-provider/metadata-store-cassandra/pom.xml b/archiva-modules/metadata/metadata-store-provider/metadata-store-cassandra/pom.xml index fc76755f2..116125850 100644 --- a/archiva-modules/metadata/metadata-store-provider/metadata-store-cassandra/pom.xml +++ b/archiva-modules/metadata/metadata-store-provider/metadata-store-cassandra/pom.xml @@ -31,7 +31,7 @@ ${project.parent.parent.basedir} - 3.11.10 + 3.11.11