Search in sources :

Example 81 with Configuration

use of org.apache.archiva.configuration.model.Configuration in project archiva by apache.

the class RepositoryGroupHandler method put.

/**
 * Adds a new repository group or updates the repository with the same id, if it exists already.
 * The configuration is saved immediately.
 *
 * @param repositoryGroupConfiguration the repository configuration
 * @return the updated or created repository
 * @throws RepositoryException if an error occurs, or the configuration is not valid.
 */
@Override
public RepositoryGroup put(RepositoryGroupConfiguration repositoryGroupConfiguration) throws RepositoryException {
    final String id = repositoryGroupConfiguration.getId();
    final RepositoryType repositoryType = RepositoryType.valueOf(repositoryGroupConfiguration.getType());
    final RepositoryProvider provider = getProvider(repositoryType);
    RepositoryGroup currentRepository;
    ReentrantReadWriteLock.WriteLock configLock = this.getConfigurationHandler().getLock().writeLock();
    configLock.lock();
    try {
        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);
            }
            registerNewRepository(repositoryGroupConfiguration, currentRepository, configuration, updated);
        } catch (IndeterminateConfigurationException | RegistryException | RepositoryException e) {
            // Trying a rollback
            if (oldRepository != null) {
                RepositoryGroupConfiguration oldCfg = provider.getRepositoryGroupConfiguration(oldRepository);
                provider.updateRepositoryGroupInstance((EditableRepositoryGroup) currentRepository, oldCfg);
                rollback(configuration, oldRepository, e, oldCfg);
            } else {
                getRepositories().remove(id);
            }
            log.error("Could not save the configuration for repository group {}: {}", id, e.getMessage(), e);
            if (e instanceof RepositoryException) {
                throw (RepositoryException) e;
            } else {
                throw new RepositoryException("Could not save the configuration for repository group " + id + ": " + e.getMessage());
            }
        }
    } finally {
        configLock.unlock();
    }
    return currentRepository;
}
Also used : RepositoryGroupConfiguration(org.apache.archiva.configuration.model.RepositoryGroupConfiguration) ManagedRepositoryConfiguration(org.apache.archiva.configuration.model.ManagedRepositoryConfiguration) RepositoryGroupConfiguration(org.apache.archiva.configuration.model.RepositoryGroupConfiguration) Configuration(org.apache.archiva.configuration.model.Configuration) RepositoryType(org.apache.archiva.repository.RepositoryType) IndeterminateConfigurationException(org.apache.archiva.configuration.provider.IndeterminateConfigurationException) RepositoryException(org.apache.archiva.repository.RepositoryException) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) RegistryException(org.apache.archiva.components.registry.RegistryException) EditableRepositoryGroup(org.apache.archiva.repository.EditableRepositoryGroup) RepositoryGroup(org.apache.archiva.repository.RepositoryGroup) RepositoryProvider(org.apache.archiva.repository.RepositoryProvider) EditableRepositoryGroup(org.apache.archiva.repository.EditableRepositoryGroup)

Example 82 with Configuration

use of org.apache.archiva.configuration.model.Configuration in project archiva by apache.

the class RemoteRepositoryHandler method put.

@Override
public RemoteRepository put(RemoteRepositoryConfiguration repositoryConfiguration) throws RepositoryException {
    final String id = repositoryConfiguration.getId();
    final RepositoryType repositoryType = RepositoryType.valueOf(repositoryConfiguration.getType());
    final RepositoryProvider provider = getProvider(repositoryType);
    ReentrantReadWriteLock.WriteLock configLock = this.getConfigurationHandler().getLock().writeLock();
    configLock.lock();
    RemoteRepository repo = null;
    RemoteRepository 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(repositoryConfiguration, configuration);
        } else {
            setRepositoryDefaults(repositoryConfiguration);
            provider.updateRemoteInstance((EditableRemoteRepository) repo, repositoryConfiguration);
            updated = true;
            pushEvent(LifecycleEvent.UPDATED, repo);
        }
        registerNewRepository(repositoryConfiguration, repo, configuration, updated);
    } catch (IndeterminateConfigurationException | RegistryException e) {
        if (oldRepository != null) {
            RemoteRepositoryConfiguration oldCfg = provider.getRemoteConfiguration(oldRepository);
            provider.updateRemoteInstance((EditableRemoteRepository) 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;
}
Also used : ProxyConnectorConfiguration(org.apache.archiva.configuration.model.ProxyConnectorConfiguration) Configuration(org.apache.archiva.configuration.model.Configuration) RemoteRepositoryConfiguration(org.apache.archiva.configuration.model.RemoteRepositoryConfiguration) RepositoryType(org.apache.archiva.repository.RepositoryType) IndeterminateConfigurationException(org.apache.archiva.configuration.provider.IndeterminateConfigurationException) RemoteRepository(org.apache.archiva.repository.RemoteRepository) EditableRemoteRepository(org.apache.archiva.repository.EditableRemoteRepository) RepositoryException(org.apache.archiva.repository.RepositoryException) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) RegistryException(org.apache.archiva.components.registry.RegistryException) RemoteRepositoryConfiguration(org.apache.archiva.configuration.model.RemoteRepositoryConfiguration) RepositoryProvider(org.apache.archiva.repository.RepositoryProvider) EditableRemoteRepository(org.apache.archiva.repository.EditableRemoteRepository)

Example 83 with Configuration

use of org.apache.archiva.configuration.model.Configuration in project archiva by apache.

the class RemoteRepositoryHandler method put.

@Override
public RemoteRepository put(RemoteRepository repository) throws RepositoryException {
    final String id = repository.getId();
    RemoteRepository originRepo = getRepositories().remove(id);
    if (originRepo == null && repositoryHandlerManager.isRegisteredId(id)) {
        throw new RepositoryException("There exists a repository with id " + id + ". Could not update with managed repository.");
    }
    try {
        if (originRepo != null && repository != originRepo) {
            deactivateRepository(originRepo);
            pushEvent(LifecycleEvent.UNREGISTERED, originRepo);
        }
        RepositoryProvider provider = getProvider(repository.getType());
        RemoteRepositoryConfiguration newCfg = provider.getRemoteConfiguration(repository);
        getConfigurationHandler().getLock().writeLock().lock();
        try {
            Configuration configuration = getConfigurationHandler().getBaseConfiguration();
            updateReferences(repository, newCfg);
            RemoteRepositoryConfiguration oldCfg = configuration.findRemoteRepositoryById(id);
            if (oldCfg != null) {
                configuration.removeRemoteRepository(oldCfg);
            }
            configuration.addRemoteRepository(newCfg);
            getConfigurationHandler().save(configuration, ConfigurationHandler.REGISTRY_EVENT_TAG);
            setLastState(repository, RepositoryState.SAVED);
            activateRepository(repository);
        } finally {
            getConfigurationHandler().getLock().writeLock().unlock();
        }
        getRepositories().put(id, repository);
        setLastState(repository, RepositoryState.REGISTERED);
        return repository;
    } 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()));
    }
}
Also used : ProxyConnectorConfiguration(org.apache.archiva.configuration.model.ProxyConnectorConfiguration) Configuration(org.apache.archiva.configuration.model.Configuration) RemoteRepositoryConfiguration(org.apache.archiva.configuration.model.RemoteRepositoryConfiguration) RemoteRepositoryConfiguration(org.apache.archiva.configuration.model.RemoteRepositoryConfiguration) RemoteRepository(org.apache.archiva.repository.RemoteRepository) EditableRemoteRepository(org.apache.archiva.repository.EditableRemoteRepository) RepositoryException(org.apache.archiva.repository.RepositoryException) RepositoryProvider(org.apache.archiva.repository.RepositoryProvider) IndexCreationFailedException(org.apache.archiva.indexer.IndexCreationFailedException) RegistryException(org.apache.archiva.components.registry.RegistryException) RepositoryException(org.apache.archiva.repository.RepositoryException) IndeterminateConfigurationException(org.apache.archiva.configuration.provider.IndeterminateConfigurationException)

Example 84 with Configuration

use of org.apache.archiva.configuration.model.Configuration in project archiva by apache.

the class ArchivaRepositoryRegistryTest method removeRemoteRepositoryWithoutSave.

@Test
public void removeRemoteRepositoryWithoutSave() throws Exception {
    Configuration configuration = archivaConfiguration.getConfiguration();
    assertEquals(1, repositoryRegistry.getRemoteRepositories().size());
    RemoteRepository repo = repositoryRegistry.getRemoteRepository("central");
    repositoryRegistry.removeRepository(repo, configuration);
    assertEquals(0, repositoryRegistry.getRemoteRepositories().size());
    assertTrue(repositoryRegistry.getRemoteRepositories().stream().noneMatch(rep -> rep.getId().equals("central")));
    archivaConfiguration.reload();
    repositoryRegistry.reload();
    assertEquals(1, repositoryRegistry.getRemoteRepositories().size());
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) ArchivaConfiguration(org.apache.archiva.configuration.provider.ArchivaConfiguration) ManagedRepositoryConfiguration(org.apache.archiva.configuration.model.ManagedRepositoryConfiguration) URL(java.net.URL) URISyntaxException(java.net.URISyntaxException) StandardCopyOption(java.nio.file.StandardCopyOption) AfterAll(org.junit.jupiter.api.AfterAll) Inject(javax.inject.Inject) ReleaseScheme(org.apache.archiva.repository.ReleaseScheme) Repository(org.apache.archiva.repository.Repository) RemoteRepository(org.apache.archiva.repository.RemoteRepository) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) ManagedRepositoryHandler(org.apache.archiva.repository.base.managed.ManagedRepositoryHandler) BeforeAll(org.junit.jupiter.api.BeforeAll) Path(java.nio.file.Path) Configuration(org.apache.archiva.configuration.model.Configuration) RemoteRepositoryConfiguration(org.apache.archiva.configuration.model.RemoteRepositoryConfiguration) SpringExtension(org.springframework.test.context.junit.jupiter.SpringExtension) Files(java.nio.file.Files) RepositoryException(org.apache.archiva.repository.RepositoryException) RemoteRepositoryHandler(org.apache.archiva.repository.base.remote.RemoteRepositoryHandler) Collection(java.util.Collection) RepositoryRegistry(org.apache.archiva.repository.RepositoryRegistry) IOException(java.io.IOException) RepositoryType(org.apache.archiva.repository.RepositoryType) ManagedRepository(org.apache.archiva.repository.ManagedRepository) BasicRemoteRepository(org.apache.archiva.repository.base.remote.BasicRemoteRepository) Test(org.junit.jupiter.api.Test) AfterEach(org.junit.jupiter.api.AfterEach) List(java.util.List) Paths(java.nio.file.Paths) ContextConfiguration(org.springframework.test.context.ContextConfiguration) Assertions(org.junit.jupiter.api.Assertions) RepositoryGroupHandler(org.apache.archiva.repository.base.group.RepositoryGroupHandler) BasicManagedRepository(org.apache.archiva.repository.base.managed.BasicManagedRepository) ArchivaConfiguration(org.apache.archiva.configuration.provider.ArchivaConfiguration) ManagedRepositoryConfiguration(org.apache.archiva.configuration.model.ManagedRepositoryConfiguration) Configuration(org.apache.archiva.configuration.model.Configuration) RemoteRepositoryConfiguration(org.apache.archiva.configuration.model.RemoteRepositoryConfiguration) ContextConfiguration(org.springframework.test.context.ContextConfiguration) RemoteRepository(org.apache.archiva.repository.RemoteRepository) BasicRemoteRepository(org.apache.archiva.repository.base.remote.BasicRemoteRepository) Test(org.junit.jupiter.api.Test)

Example 85 with Configuration

use of org.apache.archiva.configuration.model.Configuration in project archiva by apache.

the class ArchivaRepositoryRegistryTest method putRemoteRepositoryFromConfigWithoutSave.

@Test
public void putRemoteRepositoryFromConfigWithoutSave() throws Exception {
    Configuration configuration = archivaConfiguration.getConfiguration();
    RemoteRepositoryConfiguration cfg = new RemoteRepositoryConfiguration();
    Configuration newConfiguration = new Configuration();
    cfg.setId("test002");
    cfg.setName("This is test 002");
    RemoteRepository repo = repositoryRegistry.putRepository(cfg, newConfiguration);
    assertNotNull(repo);
    assertEquals("test002", repo.getId());
    assertEquals("This is test 002", repo.getName());
    assertNotNull(repo.getContent());
    archivaConfiguration.reload();
    assertEquals(1, archivaConfiguration.getConfiguration().getRemoteRepositories().size());
    Collection<RemoteRepository> repos = repositoryRegistry.getRemoteRepositories();
    assertEquals(1, repos.size());
    assertEquals(1, newConfiguration.getRemoteRepositories().size());
    RemoteRepository internalRepo = repositoryRegistry.getRemoteRepository("central");
    cfg = new RemoteRepositoryConfiguration();
    cfg.setId("central");
    cfg.setName("This is central test 002");
    repo = repositoryRegistry.putRepository(cfg, newConfiguration);
    assertEquals("This is central test 002", repo.getName());
    assertEquals(2, newConfiguration.getRemoteRepositories().size());
    repositoryRegistry.reload();
    assertEquals(1, repositoryRegistry.getRemoteRepositories().size());
}
Also used : ArchivaConfiguration(org.apache.archiva.configuration.provider.ArchivaConfiguration) ManagedRepositoryConfiguration(org.apache.archiva.configuration.model.ManagedRepositoryConfiguration) Configuration(org.apache.archiva.configuration.model.Configuration) RemoteRepositoryConfiguration(org.apache.archiva.configuration.model.RemoteRepositoryConfiguration) ContextConfiguration(org.springframework.test.context.ContextConfiguration) RemoteRepositoryConfiguration(org.apache.archiva.configuration.model.RemoteRepositoryConfiguration) RemoteRepository(org.apache.archiva.repository.RemoteRepository) BasicRemoteRepository(org.apache.archiva.repository.base.remote.BasicRemoteRepository) Test(org.junit.jupiter.api.Test)

Aggregations

Configuration (org.apache.archiva.configuration.model.Configuration)94 ManagedRepositoryConfiguration (org.apache.archiva.configuration.model.ManagedRepositoryConfiguration)56 ContextConfiguration (org.springframework.test.context.ContextConfiguration)45 RemoteRepositoryConfiguration (org.apache.archiva.configuration.model.RemoteRepositoryConfiguration)42 ProxyConnectorConfiguration (org.apache.archiva.configuration.model.ProxyConnectorConfiguration)34 ArchivaConfiguration (org.apache.archiva.configuration.provider.ArchivaConfiguration)34 WebappConfiguration (org.apache.archiva.configuration.model.WebappConfiguration)32 RepositoryGroupConfiguration (org.apache.archiva.configuration.model.RepositoryGroupConfiguration)29 Path (java.nio.file.Path)27 Test (org.junit.Test)26 NetworkProxyConfiguration (org.apache.archiva.configuration.model.NetworkProxyConfiguration)23 RepositoryScanningConfiguration (org.apache.archiva.configuration.model.RepositoryScanningConfiguration)23 RepositoryException (org.apache.archiva.repository.RepositoryException)22 RepositoryAdminException (org.apache.archiva.admin.model.RepositoryAdminException)14 ManagedRepository (org.apache.archiva.repository.ManagedRepository)13 IOException (java.io.IOException)12 ArrayList (java.util.ArrayList)12 RemoteRepository (org.apache.archiva.repository.RemoteRepository)12 Test (org.junit.jupiter.api.Test)12 LegacyArtifactPath (org.apache.archiva.configuration.model.LegacyArtifactPath)11