Search in sources :

Example 1 with LifecycleEvent

use of org.apache.archiva.repository.event.LifecycleEvent in project archiva by apache.

the class RepositoryGroupHandler method put.

/**
 * Adds a new repository group to the current list, or replaces the repository group definition with
 * the same id, if it exists already.
 * The change is saved to the configuration immediately.
 *
 * @param repositoryGroup the new repository group.
 * @throws RepositoryException if the new repository group could not be saved to the configuration.
 */
@Override
public RepositoryGroup put(final RepositoryGroup repositoryGroup) throws RepositoryException {
    final String id = repositoryGroup.getId();
    RepositoryGroup originRepoGroup = getRepositories().remove(id);
    try {
        if (originRepoGroup != null && originRepoGroup != repositoryGroup) {
            deactivateRepository(originRepoGroup);
            pushEvent(new LifecycleEvent(LifecycleEvent.UNREGISTERED, this, originRepoGroup));
        }
        RepositoryProvider provider = getProvider(repositoryGroup.getType());
        RepositoryGroupConfiguration newCfg = provider.getRepositoryGroupConfiguration(repositoryGroup);
        ReentrantReadWriteLock.WriteLock configLock = this.getConfigurationHandler().getLock().writeLock();
        configLock.lock();
        try {
            Configuration configuration = this.getConfigurationHandler().getBaseConfiguration();
            updateReferences(repositoryGroup, newCfg);
            RepositoryGroupConfiguration oldCfg = configuration.findRepositoryGroupById(id);
            if (oldCfg != null) {
                configuration.removeRepositoryGroup(oldCfg);
            }
            configuration.addRepositoryGroup(newCfg);
            getConfigurationHandler().save(configuration, ConfigurationHandler.REGISTRY_EVENT_TAG);
            setLastState(repositoryGroup, RepositoryState.SAVED);
            activateRepository(repositoryGroup);
        } finally {
            configLock.unlock();
        }
        getRepositories().put(id, repositoryGroup);
        setLastState(repositoryGroup, RepositoryState.REGISTERED);
        return repositoryGroup;
    } catch (Exception e) {
        // Rollback
        if (originRepoGroup != null) {
            getRepositories().put(id, originRepoGroup);
        } 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()), e);
    }
}
Also used : RepositoryGroupConfiguration(org.apache.archiva.configuration.model.RepositoryGroupConfiguration) EditableRepositoryGroup(org.apache.archiva.repository.EditableRepositoryGroup) RepositoryGroup(org.apache.archiva.repository.RepositoryGroup) ManagedRepositoryConfiguration(org.apache.archiva.configuration.model.ManagedRepositoryConfiguration) RepositoryGroupConfiguration(org.apache.archiva.configuration.model.RepositoryGroupConfiguration) Configuration(org.apache.archiva.configuration.model.Configuration) LifecycleEvent(org.apache.archiva.repository.event.LifecycleEvent) RepositoryException(org.apache.archiva.repository.RepositoryException) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) RepositoryProvider(org.apache.archiva.repository.RepositoryProvider) RegistryException(org.apache.archiva.components.registry.RegistryException) RepositoryException(org.apache.archiva.repository.RepositoryException) IOException(java.io.IOException) IndeterminateConfigurationException(org.apache.archiva.configuration.provider.IndeterminateConfigurationException)

Example 2 with LifecycleEvent

use of org.apache.archiva.repository.event.LifecycleEvent in project archiva by apache.

the class ManagedRepositoryHandler method updateReferences.

@Override
public void updateReferences(ManagedRepository repo, ManagedRepositoryConfiguration repositoryConfiguration) throws RepositoryException {
    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);
        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, repositoryHandlerManager);
}
Also used : ManagedRepository(org.apache.archiva.repository.ManagedRepository) EditableManagedRepository(org.apache.archiva.repository.EditableManagedRepository) ManagedRepositoryConfiguration(org.apache.archiva.configuration.model.ManagedRepositoryConfiguration) Configuration(org.apache.archiva.configuration.model.Configuration) EditableManagedRepository(org.apache.archiva.repository.EditableManagedRepository) LifecycleEvent(org.apache.archiva.repository.event.LifecycleEvent) StagingRepositoryFeature(org.apache.archiva.repository.features.StagingRepositoryFeature) RepositoryProvider(org.apache.archiva.repository.RepositoryProvider)

Aggregations

Configuration (org.apache.archiva.configuration.model.Configuration)2 ManagedRepositoryConfiguration (org.apache.archiva.configuration.model.ManagedRepositoryConfiguration)2 RepositoryProvider (org.apache.archiva.repository.RepositoryProvider)2 LifecycleEvent (org.apache.archiva.repository.event.LifecycleEvent)2 IOException (java.io.IOException)1 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)1 RegistryException (org.apache.archiva.components.registry.RegistryException)1 RepositoryGroupConfiguration (org.apache.archiva.configuration.model.RepositoryGroupConfiguration)1 IndeterminateConfigurationException (org.apache.archiva.configuration.provider.IndeterminateConfigurationException)1 EditableManagedRepository (org.apache.archiva.repository.EditableManagedRepository)1 EditableRepositoryGroup (org.apache.archiva.repository.EditableRepositoryGroup)1 ManagedRepository (org.apache.archiva.repository.ManagedRepository)1 RepositoryException (org.apache.archiva.repository.RepositoryException)1 RepositoryGroup (org.apache.archiva.repository.RepositoryGroup)1 StagingRepositoryFeature (org.apache.archiva.repository.features.StagingRepositoryFeature)1