Search in sources :

Example 16 with EditableRepositoryGroup

use of org.apache.archiva.repository.EditableRepositoryGroup in project archiva by apache.

the class DefaultRepositoryGroupAdmin method addRepositoryToGroup.

@Override
public Boolean addRepositoryToGroup(String repositoryGroupId, String repositoryId, AuditInformation auditInformation) throws RepositoryAdminException {
    org.apache.archiva.repository.RepositoryGroup repositoryGroup = repositoryRegistry.getRepositoryGroup(repositoryGroupId);
    if (repositoryGroup == null) {
        throw EntityNotFoundException.ofMessage("Repository group with id " + repositoryGroupId + " doesn't not exists so cannot add repository to it", repositoryGroupId);
    }
    if (!(repositoryGroup instanceof EditableRepositoryGroup)) {
        throw RepositoryAdminException.ofKey("repository_group.not_editable", repositoryGroupId);
    }
    EditableRepositoryGroup editableRepositoryGroup = (EditableRepositoryGroup) repositoryGroup;
    if (editableRepositoryGroup.getRepositories().stream().anyMatch(repo -> repositoryId.equals(repo.getId()))) {
        throw new EntityExistsException("Repository group with id " + repositoryGroupId + " already contain repository with id" + repositoryId);
    }
    org.apache.archiva.repository.ManagedRepository managedRepo = repositoryRegistry.getManagedRepository(repositoryId);
    if (managedRepo == null) {
        throw EntityNotFoundException.ofMessage("Repository with id " + repositoryId + " does not exist", repositoryId);
    }
    editableRepositoryGroup.addRepository(managedRepo);
    try {
        repositoryRegistry.putRepositoryGroup(editableRepositoryGroup);
    } catch (RepositoryException e) {
        throw new RepositoryAdminException("Could not store the repository group " + repositoryGroupId, e);
    }
    triggerAuditEvent(repositoryGroup.getId(), null, AuditEvent.ADD_REPO_TO_GROUP, auditInformation);
    return Boolean.TRUE;
}
Also used : RepositoryException(org.apache.archiva.repository.RepositoryException) RepositoryAdminException(org.apache.archiva.admin.model.RepositoryAdminException) EditableRepositoryGroup(org.apache.archiva.repository.EditableRepositoryGroup) EntityExistsException(org.apache.archiva.admin.model.EntityExistsException)

Example 17 with EditableRepositoryGroup

use of org.apache.archiva.repository.EditableRepositoryGroup 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 18 with EditableRepositoryGroup

use of org.apache.archiva.repository.EditableRepositoryGroup in project archiva by apache.

the class BasicRepositoryGroupValidatorTest method applyWithExisting.

@Test
void applyWithExisting() throws IOException, RepositoryException {
    BasicRepositoryGroupValidator validator = new BasicRepositoryGroupValidator(configurationHandler);
    validator.setRepositoryRegistry(repositoryRegistry);
    EditableRepositoryGroup group = createGroup("test", "test");
    group.setMergedIndexTTL(360);
    repositoryRegistry.putRepositoryGroup(group);
    EditableRepositoryGroup group2 = createGroup("test", "test2");
    group2.setMergedIndexTTL(360);
    ValidationResponse<RepositoryGroup> result = validator.apply(group2);
    assertNotNull(result);
    assertFalse(result.isValid());
    assertEquals("group_exists", result.getResult().get("id").get(0).getType());
}
Also used : EditableRepositoryGroup(org.apache.archiva.repository.EditableRepositoryGroup) RepositoryGroup(org.apache.archiva.repository.RepositoryGroup) EditableRepositoryGroup(org.apache.archiva.repository.EditableRepositoryGroup) Test(org.junit.jupiter.api.Test)

Example 19 with EditableRepositoryGroup

use of org.apache.archiva.repository.EditableRepositoryGroup in project archiva by apache.

the class RepositoryGroupHandler method put.

@Override
public RepositoryGroup put(RepositoryGroupConfiguration repositoryGroupConfiguration, Configuration configuration) throws RepositoryException {
    final String id = repositoryGroupConfiguration.getId();
    final RepositoryType repoType = RepositoryType.valueOf(repositoryGroupConfiguration.getType());
    RepositoryGroup repo;
    setRepositoryGroupDefaults(repositoryGroupConfiguration);
    if (getRepositories().containsKey(id)) {
        repo = clone(getRepositories().get(id), id);
        if (repo instanceof EditableRepositoryGroup) {
            getProvider(repoType).updateRepositoryGroupInstance((EditableRepositoryGroup) repo, repositoryGroupConfiguration);
        } else {
            throw new RepositoryException("The repository is not editable " + id);
        }
    } else {
        repo = getProvider(repoType).createRepositoryGroup(repositoryGroupConfiguration);
        setLastState(repo, RepositoryState.CREATED);
    }
    replaceOrAddRepositoryConfig(repositoryGroupConfiguration, configuration);
    updateReferences(repo, repositoryGroupConfiguration);
    setLastState(repo, RepositoryState.REFERENCES_SET);
    return repo;
}
Also used : EditableRepositoryGroup(org.apache.archiva.repository.EditableRepositoryGroup) RepositoryGroup(org.apache.archiva.repository.RepositoryGroup) RepositoryType(org.apache.archiva.repository.RepositoryType) RepositoryException(org.apache.archiva.repository.RepositoryException) EditableRepositoryGroup(org.apache.archiva.repository.EditableRepositoryGroup)

Aggregations

EditableRepositoryGroup (org.apache.archiva.repository.EditableRepositoryGroup)19 RepositoryGroup (org.apache.archiva.repository.RepositoryGroup)12 Test (org.junit.jupiter.api.Test)11 RepositoryException (org.apache.archiva.repository.RepositoryException)6 Map (java.util.Map)4 ValidationError (org.apache.archiva.repository.validation.ValidationError)3 EntityExistsException (org.apache.archiva.admin.model.EntityExistsException)2 RepositoryAdminException (org.apache.archiva.admin.model.RepositoryAdminException)2 Configuration (org.apache.archiva.configuration.model.Configuration)2 ManagedRepositoryConfiguration (org.apache.archiva.configuration.model.ManagedRepositoryConfiguration)2 RepositoryGroupConfiguration (org.apache.archiva.configuration.model.RepositoryGroupConfiguration)2 RepositoryType (org.apache.archiva.repository.RepositoryType)2 ArchivaRestServiceException (org.apache.archiva.rest.api.v2.svc.ArchivaRestServiceException)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 Collectors (java.util.stream.Collectors)1