123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380 |
- 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.*;
- import org.apache.archiva.indexer.ArchivaIndexManager;
- 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.redback.components.registry.RegistryException;
- import org.apache.archiva.repository.features.IndexCreationEvent;
- import org.apache.archiva.repository.features.IndexCreationFeature;
- import org.apache.archiva.repository.features.StagingRepositoryFeature;
- import org.apache.commons.lang.StringUtils;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.stereotype.Service;
-
- import javax.annotation.PostConstruct;
- import javax.annotation.PreDestroy;
- import javax.inject.Inject;
- import javax.inject.Named;
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.Collections;
- import java.util.HashMap;
- import java.util.LinkedHashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.concurrent.locks.ReentrantReadWriteLock;
- import java.util.stream.Collectors;
- import java.util.stream.Stream;
-
- import static org.apache.archiva.indexer.ArchivaIndexManager.DEFAULT_INDEX_PATH;
-
- /**
- * Registry for repositories. This is the central entry point for repositories. It provides methods for
- * retrieving, adding and removing repositories.
- *
- * The modification methods addXX and removeXX persist the changes immediately to the configuration. If the
- * configuration save fails the changes are rolled back.
- *
- * TODO: Audit events
- */
- @Service( "repositoryRegistry" )
- public class RepositoryRegistry implements ConfigurationListener, RepositoryEventHandler, RepositoryEventListener {
-
- private static final Logger log = LoggerFactory.getLogger( RepositoryRegistry.class );
-
- /**
- * We inject all repository providers
- */
- @Inject
- List<RepositoryProvider> repositoryProviders;
-
- @Inject
- IndexManagerFactory indexManagerFactory;
-
- @Inject
- ArchivaConfiguration archivaConfiguration;
-
- @Inject
- @Named("repositoryContentFactory#default")
- RepositoryContentFactory repositoryContentFactory;
-
- private List<RepositoryEventListener> listeners = new ArrayList<>();
-
-
- private Map<String, ManagedRepository> managedRepositories = new HashMap<>( );
- private Map<String, ManagedRepository> uManagedRepository = Collections.unmodifiableMap( managedRepositories );
-
- private Map<String, RemoteRepository> remoteRepositories = new HashMap<>( );
- private Map<String, RemoteRepository> uRemoteRepositories = Collections.unmodifiableMap( remoteRepositories );
-
- private Map<String, RepositoryGroup> repositoryGroups = new HashMap<>();
- private Map<String, RepositoryGroup> uRepositoryGroups = Collections.unmodifiableMap(repositoryGroups);
-
- private ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock( );
-
- public void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration) {
- this.archivaConfiguration = archivaConfiguration;
- }
-
- @PostConstruct
- private void initialize( )
- {
- rwLock.writeLock( ).lock( );
- try
- {
- log.debug("Initializing repository registry");
- for(ManagedRepository rep : managedRepositories.values()) {
- rep.close();
- }
- managedRepositories.clear( );
- managedRepositories.putAll( getManagedRepositoriesFromConfig( ) );
- for (RemoteRepository repo : remoteRepositories.values()) {
- repo.close();
- }
- remoteRepositories.clear( );
- remoteRepositories.putAll( getRemoteRepositoriesFromConfig( ) );
-
- repositoryGroups.clear();
- repositoryGroups.putAll(getRepositorGroupsFromConfig());
-
- // archivaConfiguration.addChangeListener(this);
- archivaConfiguration.addListener(this);
- }
- finally
- {
- rwLock.writeLock( ).unlock( );
- }
- }
-
- @PreDestroy
- public void destroy() {
- for(ManagedRepository rep : managedRepositories.values()) {
- rep.close();
- }
- for (RemoteRepository repo : remoteRepositories.values()) {
- repo.close();
- }
- }
-
-
-
- private Map<RepositoryType, RepositoryProvider> createProviderMap( )
- {
- Map<RepositoryType, RepositoryProvider> map = new HashMap<>( );
- if ( repositoryProviders != null )
- {
- for ( RepositoryProvider provider : repositoryProviders )
- {
- for ( RepositoryType type : provider.provides( ) )
- {
- map.put( type, provider );
- }
- }
- }
- return map;
- }
-
- private RepositoryProvider getProvider( RepositoryType type ) throws RepositoryException
- {
- return repositoryProviders.stream( ).filter( repositoryProvider -> repositoryProvider.provides( ).contains( type ) ).findFirst( ).orElseThrow( ( ) -> new RepositoryException( "Repository type cannot be handled: " + type ) );
- }
-
- private Map<String, ManagedRepository> getManagedRepositoriesFromConfig( )
- {
- try
- {
- List<ManagedRepositoryConfiguration> managedRepoConfigs =
- getArchivaConfiguration( ).getConfiguration( ).getManagedRepositories( );
-
- if ( managedRepoConfigs == null )
- {
- return Collections.emptyMap();
- }
-
- Map<String, ManagedRepository> managedRepos = new LinkedHashMap<>( managedRepoConfigs.size( ) );
-
- Map<RepositoryType, RepositoryProvider> providerMap = createProviderMap( );
- for ( ManagedRepositoryConfiguration repoConfig : managedRepoConfigs )
- {
- if (managedRepos.containsKey(repoConfig.getId())) {
- log.warn( "Duplicate repository definitions for {} in config found.", repoConfig.getId( ) );
- continue;
- }
- RepositoryType repositoryType = RepositoryType.valueOf( repoConfig.getType( ) );
- if ( providerMap.containsKey( repositoryType ) )
- {
- try
- {
- ManagedRepository repo = createNewManagedRepository( providerMap.get( repositoryType ), repoConfig );
- managedRepos.put( repo.getId( ), repo );
- }
- catch ( Exception e )
- {
- log.error( "Could not create managed repository {}: {}", repoConfig.getId( ), e.getMessage( ), e );
- }
- }
- }
- return managedRepos;
- } catch (Throwable e) {
- log.error("Could not initialize repositories from config: {}",e.getMessage(), e );
- //noinspection unchecked
- return Collections.emptyMap();
- }
- }
-
- private ManagedRepository createNewManagedRepository( RepositoryProvider provider, ManagedRepositoryConfiguration cfg ) throws RepositoryException
- {
- log.debug("Creating repo {}", cfg.getId());
- ManagedRepository repo = provider.createManagedInstance( cfg );
- repo.addListener(this);
- updateRepositoryReferences( provider, repo, cfg , null);
- return repo;
-
- }
-
- @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 = getStagingRepository( provider, cfg, configuration);
- managedRepositories.put(stageRepo.getId(), stageRepo);
- feature.setStagingRepository( stageRepo );
- if (configuration!=null) {
- replaceOrAddRepositoryConfig( provider.getManagedConfiguration( stageRepo ), configuration );
- }
- }
- }
- if ( repo instanceof EditableManagedRepository)
- {
- EditableManagedRepository editableRepo = (EditableManagedRepository) repo;
- if (repo.getContent()==null) {
- editableRepo.setContent(repositoryContentFactory.getManagedRepositoryContent(repo));
- }
- log.debug("Index repo: "+repo.hasIndex());
- if (repo.hasIndex() && repo.getIndexingContext()==null) {
- log.debug("Creating indexing context for {}", repo.getId());
- createIndexingContext(editableRepo);
- }
- }
-
- }
-
- public ArchivaIndexManager getIndexManager(RepositoryType type) {
- return indexManagerFactory.getIndexManager(type);
- }
-
- private void createIndexingContext(EditableRepository editableRepo) throws RepositoryException {
- 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);
- }
- }
- }
-
- private ManagedRepository getStagingRepository(RepositoryProvider provider, ManagedRepositoryConfiguration baseRepoCfg, Configuration configuration) throws RepositoryException
- {
- ManagedRepository stageRepo = getManagedRepository( baseRepoCfg.getId( ) + StagingRepositoryFeature.STAGING_REPO_POSTFIX );
- 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 Map<String, RemoteRepository> getRemoteRepositoriesFromConfig( )
- {
- try
- {
- List<RemoteRepositoryConfiguration> remoteRepoConfigs =
- getArchivaConfiguration( ).getConfiguration( ).getRemoteRepositories( );
-
- if ( remoteRepoConfigs == null )
- {
- //noinspection unchecked
- return Collections.emptyMap();
- }
-
- Map<String, RemoteRepository> remoteRepos = new LinkedHashMap<>( remoteRepoConfigs.size( ) );
-
- Map<RepositoryType, RepositoryProvider> providerMap = createProviderMap( );
- for ( RemoteRepositoryConfiguration repoConfig : remoteRepoConfigs )
- {
- RepositoryType repositoryType = RepositoryType.valueOf( repoConfig.getType( ) );
- if ( providerMap.containsKey( repositoryType ) )
- {
- RepositoryProvider provider = getProvider( repositoryType );
- try
- {
-
- RemoteRepository remoteRepository = createNewRemoteRepository( provider, repoConfig );
- remoteRepos.put( repoConfig.getId( ), remoteRepository);
- }
- catch ( Exception e )
- {
- log.error( "Could not create repository {} from config: {}", repoConfig.getId( ), e.getMessage( ), e );
- }
- }
- }
-
- return remoteRepos;
- } catch (Throwable e) {
- log.error("Could not initialize remote repositories from config: {}", e.getMessage(), e);
- //noinspection unchecked
- return Collections.emptyMap();
- }
- }
-
- private RemoteRepository createNewRemoteRepository( RepositoryProvider provider, RemoteRepositoryConfiguration cfg ) throws RepositoryException
- {
- log.debug("Creating remote repo {}", cfg.getId());
- RemoteRepository repo = provider.createRemoteInstance( cfg );
- repo.addListener(this);
- updateRepositoryReferences( provider, repo, cfg , null);
- return repo;
-
- }
-
- @SuppressWarnings( "unchecked" )
- private void updateRepositoryReferences( RepositoryProvider provider, RemoteRepository repo, RemoteRepositoryConfiguration cfg, Configuration configuration) throws RepositoryException
- {
- if ( repo instanceof EditableRemoteRepository && repo.getContent() == null)
- {
- EditableRemoteRepository editableRepo = (EditableRemoteRepository) repo;
- editableRepo.setContent( repositoryContentFactory.getRemoteRepositoryContent( repo ) );
- if (repo.supportsFeature(IndexCreationFeature.class) && repo.getIndexingContext()==null ) {
- createIndexingContext(editableRepo);
- }
- }
- }
-
- private Map<String, RepositoryGroup> getRepositorGroupsFromConfig( )
- {
- try
- {
- List<RepositoryGroupConfiguration> repositoryGroupConfigurations =
- getArchivaConfiguration( ).getConfiguration( ).getRepositoryGroups();
-
- if ( repositoryGroupConfigurations == null )
- {
- return Collections.emptyMap();
- }
-
- Map<String, RepositoryGroup> repositoryGroupMap = new LinkedHashMap<>( repositoryGroupConfigurations.size( ) );
-
- Map<RepositoryType, RepositoryProvider> providerMap = createProviderMap( );
- for ( RepositoryGroupConfiguration repoConfig : repositoryGroupConfigurations )
- {
- RepositoryType repositoryType = RepositoryType.valueOf( repoConfig.getType( ) );
- if ( providerMap.containsKey( repositoryType ) )
- {
- try
- {
- RepositoryGroup repo = createNewRepositoryGroup( providerMap.get( repositoryType ), repoConfig );
- repositoryGroupMap.put( repo.getId( ), repo );
- }
- catch ( Exception e )
- {
- log.error( "Could not create repository group {}: {}", repoConfig.getId( ), e.getMessage( ), e );
- }
- }
- }
- return repositoryGroupMap;
- } catch (Throwable e) {
- log.error("Could not initialize repositories from config: {}",e.getMessage(), e );
- //noinspection unchecked
- return Collections.emptyMap();
- }
- }
-
- RepositoryGroup createNewRepositoryGroup(RepositoryProvider provider, RepositoryGroupConfiguration config) throws RepositoryException {
- RepositoryGroup repositoryGroup = provider.createRepositoryGroup(config);
- repositoryGroup.addListener(this);
- updateRepositoryReferences(provider, repositoryGroup, config);
- return repositoryGroup;
- }
-
- private void updateRepositoryReferences(RepositoryProvider provider, RepositoryGroup group, RepositoryGroupConfiguration configuration) {
- if (group instanceof EditableRepositoryGroup) {
- EditableRepositoryGroup eGroup = (EditableRepositoryGroup) group;
- eGroup.setRepositories(configuration.getRepositories().stream().map(r -> getManagedRepository(r)).collect(Collectors.toList()));
- }
- }
-
- private ArchivaConfiguration getArchivaConfiguration( )
- {
- return this.archivaConfiguration;
- }
-
- /**
- * Returns all repositories that are registered. There is no defined order of the returned repositories.
- *
- * @return a list of managed and remote repositories
- */
- public Collection<Repository> getRepositories( )
- {
- rwLock.readLock( ).lock( );
- try
- {
- return Stream.concat( managedRepositories.values( ).stream( ), remoteRepositories.values( ).stream( )).collect( Collectors.toList( ) );
- }
- finally
- {
- rwLock.readLock( ).unlock( );
- }
- }
-
- /**
- * Returns only the managed repositories. There is no defined order of the returned repositories.
- *
- * @return a list of managed repositories
- */
- public Collection<ManagedRepository> getManagedRepositories( )
- {
- rwLock.readLock().lock();
- try
- {
- return uManagedRepository.values( );
- } finally
- {
- rwLock.readLock().unlock();
- }
- }
-
- /**
- * Returns only the remote repositories. There is no defined order of the returned repositories.
- *
- * @return a list of remote repositories
- */
- public Collection<RemoteRepository> getRemoteRepositories( )
- {
- rwLock.readLock().lock();
- try
- {
- return uRemoteRepositories.values( );
- } finally
- {
- rwLock.readLock().unlock();
- }
- }
-
- public Collection<RepositoryGroup> getRepositoryGroups() {
- rwLock.readLock().lock();
- try {
- return uRepositoryGroups.values();
- } finally {
- rwLock.readLock().unlock();
- }
- }
-
- /**
- * Returns the repository with the given id. The returned repository may be a managed or remote repository.
- * It returns null, if no repository is registered with the given id.
- *
- * @param repoId the repository id
- * @return the repository if found, otherwise null
- */
- public Repository getRepository( String repoId )
- {
- rwLock.readLock( ).lock( );
- try
- {
- log.debug("getRepository {}", repoId);
- if ( managedRepositories.containsKey( repoId ) )
- {
- log.debug("Managed repo");
- return managedRepositories.get( repoId );
- }
- else if (remoteRepositories.containsKey(repoId))
- {
- log.debug("Remote repo");
- return remoteRepositories.get( repoId );
- } else if (repositoryGroups.containsKey(repoId)) {
- return repositoryGroups.get(repoId);
- } else {
- return null;
- }
- }
- finally
- {
- rwLock.readLock( ).unlock( );
- }
- }
-
- /**
- * Convenience method, that returns the managed repository with the given id.
- * It returns null, if no managed repository is registered with this id.
- *
- * @param repoId the repository id
- * @return the managed repository if found, otherwise null
- */
- public ManagedRepository getManagedRepository( String repoId )
- {
- rwLock.readLock( ).lock( );
- try
- {
- return managedRepositories.get( repoId );
- }
- finally
- {
- rwLock.readLock( ).unlock( );
- }
- }
-
- /**
- * Convenience method, that returns the remote repository with the given id.
- * It returns null, if no remote repository is registered with this id.
- *
- * @param repoId the repository id
- * @return the remote repository if found, otherwise null
- */
- public RemoteRepository getRemoteRepository( String repoId )
- {
- rwLock.readLock( ).lock( );
- try
- {
- return remoteRepositories.get( repoId );
- }
- finally
- {
- rwLock.readLock( ).unlock( );
- }
- }
-
- public RepositoryGroup getRepositoryGroup( String groupId ) {
- rwLock.readLock().lock();
- try {
- return repositoryGroups.get(groupId);
- } finally {
- rwLock.readLock().unlock();
- }
- }
-
- /**
- * Adds a new repository to the current list, or replaces the repository definition with
- * the same id, if it exists already.
- * The change is saved to the configuration immediately.
- *
- * @param managedRepository the new repository.
- * @throws RepositoryException if the new repository could not be saved to the configuration.
- */
- public ManagedRepository putRepository( ManagedRepository managedRepository ) throws RepositoryException
- {
- 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.close();
- }
- RepositoryProvider provider = getProvider( managedRepository.getType() );
- ManagedRepositoryConfiguration newCfg = provider.getManagedConfiguration( managedRepository );
- Configuration configuration = getArchivaConfiguration( ).getConfiguration( );
- updateRepositoryReferences( provider, managedRepository, newCfg, configuration );
- ManagedRepositoryConfiguration oldCfg = configuration.findManagedRepositoryById( id );
- if (oldCfg!=null) {
- configuration.removeManagedRepository( oldCfg );
- }
- configuration.addManagedRepository( newCfg );
- getArchivaConfiguration( ).save( configuration );
- return managedRepository;
- }
- catch ( Exception e )
- {
- // Rollback
- 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()) );
- }
- }
- finally
- {
- rwLock.writeLock( ).unlock( );
- }
- }
-
- /**
- * Adds a new repository or updates the repository with the same id, if it exists already.
- * The configuration is saved immediately.
- *
- * @param managedRepositoryConfiguration the repository configuration
- * @return the updated or created repository
- * @throws RepositoryException if an error occurs, or the configuration is not valid.
- */
- public ManagedRepository putRepository( ManagedRepositoryConfiguration managedRepositoryConfiguration) throws RepositoryException
- {
- rwLock.writeLock( ).lock( );
- try
- {
- final String id = managedRepositoryConfiguration.getId();
- final RepositoryType repositoryType = RepositoryType.valueOf( managedRepositoryConfiguration.getType() );
- Configuration configuration = getArchivaConfiguration().getConfiguration();
- ManagedRepository repo = managedRepositories.get(id);
- ManagedRepositoryConfiguration oldCfg = repo!=null ? getProvider( repositoryType ).getManagedConfiguration( repo ) : null;
- repo = putRepository( managedRepositoryConfiguration, configuration );
- try
- {
- getArchivaConfiguration().save(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;
- }
- finally
- {
- rwLock.writeLock( ).unlock( );
- }
-
- }
-
- /**
- * Adds a new repository or updates the repository with the same id. The given configuration object is updated, but
- * the configuration is not saved.
- *
- * @param managedRepositoryConfiguration the new or changed repository configuration
- * @param configuration the configuration object
- * @return the new or updated repository
- * @throws RepositoryException if the configuration cannot be saved or updated
- */
- @SuppressWarnings( "unchecked" )
- public ManagedRepository putRepository( ManagedRepositoryConfiguration managedRepositoryConfiguration, Configuration configuration) throws RepositoryException
- {
- rwLock.writeLock( ).lock( );
- try
- {
- final String id = managedRepositoryConfiguration.getId();
- final RepositoryType repoType = RepositoryType.valueOf( managedRepositoryConfiguration.getType() );
- ManagedRepository repo;
- if (managedRepositories.containsKey( id )) {
- repo = managedRepositories.get(id);
- 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 );
- repo.addListener(this);
- managedRepositories.put(id, repo);
- }
- updateRepositoryReferences( getProvider( repoType ), repo, managedRepositoryConfiguration, configuration );
- replaceOrAddRepositoryConfig( managedRepositoryConfiguration, configuration );
- return repo;
- }
- finally
- {
- rwLock.writeLock( ).unlock( );
- }
- }
-
-
- /**
- * 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.
- */
- public RepositoryGroup putRepositoryGroup( RepositoryGroup repositoryGroup ) throws RepositoryException
- {
- rwLock.writeLock( ).lock( );
- try
- {
- final String id = repositoryGroup.getId();
- RepositoryGroup originRepo = repositoryGroups.put( id, repositoryGroup );
- try
- {
- if (originRepo!=null) {
- originRepo.close();
- }
- RepositoryProvider provider = getProvider( repositoryGroup.getType() );
- RepositoryGroupConfiguration newCfg = provider.getRepositoryGroupConfiguration( repositoryGroup );
- Configuration configuration = getArchivaConfiguration( ).getConfiguration( );
- updateRepositoryReferences( provider, repositoryGroup, newCfg );
- RepositoryGroupConfiguration oldCfg = configuration.findRepositoryGroupById( id );
- if (oldCfg!=null) {
- configuration.removeRepositoryGroup( oldCfg );
- }
- configuration.addRepositoryGroup( newCfg );
- getArchivaConfiguration( ).save( configuration );
- return repositoryGroup;
- }
- catch ( Exception e )
- {
- // Rollback
- if ( originRepo != null )
- {
- repositoryGroups.put( id, originRepo );
- } else {
- repositoryGroups.remove(id);
- }
- log.error("Exception during configuration update {}", e.getMessage(), e);
- throw new RepositoryException( "Could not save the configuration" + (e.getMessage( )==null?"":": "+e.getMessage()) );
- }
- }
- finally
- {
- rwLock.writeLock( ).unlock( );
- }
- }
-
- /**
- * 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.
- */
- public RepositoryGroup putRepositoryGroup( RepositoryGroupConfiguration repositoryGroupConfiguration) throws RepositoryException
- {
- rwLock.writeLock( ).lock( );
- try
- {
- final String id = repositoryGroupConfiguration.getId();
- final RepositoryType repositoryType = RepositoryType.valueOf( repositoryGroupConfiguration.getType() );
- Configuration configuration = getArchivaConfiguration().getConfiguration();
- RepositoryGroup repo = repositoryGroups.get(id);
- RepositoryGroupConfiguration oldCfg = repo!=null ? getProvider( repositoryType ).getRepositoryGroupConfiguration( repo ) : null;
- repo = putRepositoryGroup( repositoryGroupConfiguration, configuration );
- try
- {
- getArchivaConfiguration().save(configuration);
- }
- catch ( IndeterminateConfigurationException | RegistryException e )
- {
- if (oldCfg!=null) {
- getProvider( repositoryType ).updateRepositoryGroupInstance( (EditableRepositoryGroup) repo, oldCfg );
- }
- log.error("Could not save the configuration for repository group {}: {}", id, e.getMessage(),e );
- throw new RepositoryException( "Could not save the configuration for repository group "+id+": "+e.getMessage() );
- }
- return repo;
- }
- finally
- {
- rwLock.writeLock( ).unlock( );
- }
-
- }
-
- /**
- * Adds a new repository group or updates the repository group with the same id. The given configuration object is updated, but
- * the configuration is not saved.
- *
- * @param repositoryGroupConfiguration the new or changed repository configuration
- * @param configuration the configuration object
- * @return the new or updated repository
- * @throws RepositoryException if the configuration cannot be saved or updated
- */
- @SuppressWarnings( "unchecked" )
- public RepositoryGroup putRepositoryGroup( RepositoryGroupConfiguration repositoryGroupConfiguration, Configuration configuration) throws RepositoryException
- {
- rwLock.writeLock( ).lock( );
- try
- {
- final String id = repositoryGroupConfiguration.getId();
- final RepositoryType repoType = RepositoryType.valueOf( repositoryGroupConfiguration.getType() );
- RepositoryGroup repo;
- setRepositoryGroupDefaults(repositoryGroupConfiguration);
- if (repositoryGroups.containsKey( id )) {
- repo = repositoryGroups.get(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 );
- repo.addListener(this);
- repositoryGroups.put(id, repo);
- }
- updateRepositoryReferences( getProvider( repoType ), repo, repositoryGroupConfiguration );
- replaceOrAddRepositoryConfig( repositoryGroupConfiguration, configuration );
- return repo;
- }
- finally
- {
- rwLock.writeLock( ).unlock( );
- }
- }
-
- private void setRepositoryGroupDefaults(RepositoryGroupConfiguration repositoryGroupConfiguration) {
- if (StringUtils.isEmpty(repositoryGroupConfiguration.getMergedIndexPath())) {
- repositoryGroupConfiguration.setMergedIndexPath(DEFAULT_INDEX_PATH);
- }
- if (repositoryGroupConfiguration.getMergedIndexTtl()<=0) {
- repositoryGroupConfiguration.setMergedIndexTtl(300);
- }
- if (StringUtils.isEmpty(repositoryGroupConfiguration.getCronExpression())) {
- repositoryGroupConfiguration.setCronExpression("0 0 03 ? * MON");
- }
- }
-
- private void replaceOrAddRepositoryConfig(ManagedRepositoryConfiguration managedRepositoryConfiguration, Configuration configuration) {
- ManagedRepositoryConfiguration oldCfg = configuration.findManagedRepositoryById( managedRepositoryConfiguration.getId() );
- if ( oldCfg !=null) {
- configuration.removeManagedRepository( oldCfg );
- }
- configuration.addManagedRepository( managedRepositoryConfiguration );
- }
-
- private void replaceOrAddRepositoryConfig(RemoteRepositoryConfiguration remoteRepositoryConfiguration, Configuration configuration) {
- RemoteRepositoryConfiguration oldCfg = configuration.findRemoteRepositoryById( remoteRepositoryConfiguration.getId() );
- if ( oldCfg !=null) {
- configuration.removeRemoteRepository( oldCfg );
- }
- configuration.addRemoteRepository( remoteRepositoryConfiguration );
- }
-
- private void replaceOrAddRepositoryConfig(RepositoryGroupConfiguration repositoryGroupConfiguration, Configuration configuration) {
- RepositoryGroupConfiguration oldCfg = configuration.findRepositoryGroupById( repositoryGroupConfiguration.getId() );
- if ( oldCfg !=null) {
- configuration.removeRepositoryGroup( oldCfg );
- }
- configuration.addRepositoryGroup( repositoryGroupConfiguration);
- }
-
- public RemoteRepository putRepository( RemoteRepository remoteRepository, Configuration configuration) throws RepositoryException
- {
- rwLock.writeLock( ).lock( );
- try
- {
- final String id = remoteRepository.getId();
- if (managedRepositories.containsKey( id )) {
- throw new RepositoryException( "There exists a managed repository with id "+id+". Could not update with remote repository." );
- }
- RemoteRepository originRepo = remoteRepositories.put( id, remoteRepository );
- RemoteRepositoryConfiguration oldCfg=null;
- RemoteRepositoryConfiguration newCfg;
- try
- {
- if (originRepo!=null) {
- originRepo.close();
- }
- final RepositoryProvider provider = getProvider( remoteRepository.getType() );
- newCfg = provider.getRemoteConfiguration( remoteRepository );
- updateRepositoryReferences( provider, remoteRepository, newCfg, configuration );
- oldCfg = configuration.findRemoteRepositoryById( id );
- if (oldCfg!=null) {
- configuration.removeRemoteRepository( oldCfg );
- }
- configuration.addRemoteRepository( newCfg );
- return remoteRepository;
- }
- catch ( Exception e )
- {
- // Rollback
- if ( originRepo != null )
- {
- remoteRepositories.put( id, originRepo );
- } else {
- remoteRepositories.remove( id);
- }
- if (oldCfg!=null) {
- RemoteRepositoryConfiguration cfg = configuration.findRemoteRepositoryById( id );
- if (cfg!=null) {
- configuration.removeRemoteRepository( cfg );
- configuration.addRemoteRepository( oldCfg );
- }
- }
- log.error("Error while adding remote repository {}", e.getMessage(), e);
- throw new RepositoryException( "Could not save the configuration" + (e.getMessage( )==null?"":": "+e.getMessage()) );
- }
- }
- finally
- {
- rwLock.writeLock( ).unlock( );
- }
- }
-
- /**
- * Adds a remote repository, or overwrites the repository definition with the same id, if it exists already.
- * The modification is saved to the configuration immediately.
- *
- * @param remoteRepository the remote repository to add
- * @throws RepositoryException if an error occurs during configuration save
- */
- public RemoteRepository putRepository( RemoteRepository remoteRepository ) throws RepositoryException
- {
- rwLock.writeLock( ).lock( );
- try
- {
- Configuration configuration = getArchivaConfiguration().getConfiguration();
- try
- {
- RemoteRepository repo = putRepository( remoteRepository, configuration );
- getArchivaConfiguration().save(configuration);
- return repo;
- }
- catch ( RegistryException | IndeterminateConfigurationException e )
- {
- log.error("Error while saving remote repository {}", e.getMessage(), e);
- throw new RepositoryException( "Could not save the configuration" + (e.getMessage( )==null?"":": "+e.getMessage()) );
- }
- }
- finally
- {
- rwLock.writeLock( ).unlock( );
- }
- }
-
- /**
- * Adds a new repository or updates the repository with the same id, if it exists already.
- * The configuration is saved immediately.
- *
- * @param remoteRepositoryConfiguration the repository configuration
- * @return the updated or created repository
- * @throws RepositoryException if an error occurs, or the configuration is not valid.
- */
- public RemoteRepository putRepository( RemoteRepositoryConfiguration remoteRepositoryConfiguration) throws RepositoryException
- {
- rwLock.writeLock( ).lock( );
- try
- {
- final String id = remoteRepositoryConfiguration.getId();
- final RepositoryType repositoryType = RepositoryType.valueOf( remoteRepositoryConfiguration.getType() );
- Configuration configuration = getArchivaConfiguration().getConfiguration();
- RemoteRepository repo = remoteRepositories.get(id);
- RemoteRepositoryConfiguration oldCfg = repo!=null ? getProvider( repositoryType ).getRemoteConfiguration( repo ) : null;
- repo = putRepository( remoteRepositoryConfiguration, configuration );
- try
- {
- getArchivaConfiguration().save(configuration);
- }
- catch ( IndeterminateConfigurationException | RegistryException e )
- {
- if (oldCfg!=null) {
- getProvider( repositoryType ).updateRemoteInstance( (EditableRemoteRepository)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;
- }
- finally
- {
- rwLock.writeLock( ).unlock( );
- }
-
- }
-
- /**
- * Adds a new repository or updates the repository with the same id. The given configuration object is updated, but
- * the configuration is not saved.
- *
- * @param remoteRepositoryConfiguration the new or changed repository configuration
- * @param configuration the configuration object
- * @return the new or updated repository
- * @throws RepositoryException if the configuration cannot be saved or updated
- */
- @SuppressWarnings( "unchecked" )
- public RemoteRepository putRepository( RemoteRepositoryConfiguration remoteRepositoryConfiguration, Configuration configuration) throws RepositoryException
- {
- rwLock.writeLock( ).lock( );
- try
- {
- final String id = remoteRepositoryConfiguration.getId();
- final RepositoryType repoType = RepositoryType.valueOf( remoteRepositoryConfiguration.getType() );
- RemoteRepository repo;
- if (remoteRepositories.containsKey( id )) {
- repo = remoteRepositories.get(id);
- if (repo instanceof EditableRemoteRepository)
- {
- getProvider( repoType ).updateRemoteInstance( (EditableRemoteRepository) repo, remoteRepositoryConfiguration );
- } else {
- throw new RepositoryException( "The repository is not editable "+id );
- }
- } else
- {
- repo = getProvider( repoType ).createRemoteInstance( remoteRepositoryConfiguration );
- repo.addListener(this);
- remoteRepositories.put(id, repo);
- }
- updateRepositoryReferences( getProvider( repoType ), repo, remoteRepositoryConfiguration, configuration );
- replaceOrAddRepositoryConfig( remoteRepositoryConfiguration, configuration );
- return repo;
- }
- finally
- {
- rwLock.writeLock( ).unlock( );
- }
-
-
- }
-
- public void removeRepository(String repoId) throws RepositoryException {
- Repository repo = getRepository(repoId);
- if (repo!=null) {
- removeRepository(repo);
- }
- }
-
- @SuppressWarnings( "unchecked" )
- public void removeRepository(Repository repo) throws RepositoryException
- {
- if (repo==null) {
- log.warn("Trying to remove null repository");
- return;
- }
- if (repo instanceof RemoteRepository ) {
- removeRepository( (RemoteRepository)repo );
- } else if (repo instanceof ManagedRepository) {
- removeRepository( (ManagedRepository)repo);
- } else if (repo instanceof RepositoryGroup ) {
- removeRepositoryGroup((RepositoryGroup) repo);
- }else {
- throw new RepositoryException( "Repository type not known: "+repo.getClass() );
- }
- }
-
- /**
- * Removes a managed repository from the registry and configuration, if it exists.
- * The change is saved to the configuration immediately.
- *
- * @param managedRepository the managed repository to remove
- * @throws RepositoryException if a error occurs during configuration save
- */
- public void removeRepository( ManagedRepository managedRepository ) throws RepositoryException
- {
- 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();
- removeRepositoryFromGroups(repo);
- Configuration configuration = getArchivaConfiguration().getConfiguration();
- ManagedRepositoryConfiguration cfg = configuration.findManagedRepositoryById( id );
- if (cfg!=null) {
- configuration.removeManagedRepository( cfg );
- }
- getArchivaConfiguration().save( configuration );
- }
-
- }
- 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();
- }
- }
- }
-
- private void removeRepositoryFromGroups(ManagedRepository repo) {
- if (repo!=null) {
- repositoryGroups.values().stream().filter(repoGroup -> repoGroup instanceof EditableRepository).
- map(repoGroup -> (EditableRepositoryGroup) repoGroup).forEach(repoGroup -> repoGroup.removeRepository(repo));
- }
- }
-
- public void removeRepository(ManagedRepository managedRepository, Configuration configuration) throws RepositoryException
- {
- 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();
- removeRepositoryFromGroups(repo);
- ManagedRepositoryConfiguration cfg = configuration.findManagedRepositoryById( id );
- if (cfg!=null) {
- configuration.removeManagedRepository( cfg );
- }
- }
- } finally
- {
- rwLock.writeLock().unlock();
- }
- }
-
- }
-
-
- /**
- * Removes a repository group from the registry and configuration, if it exists.
- * The change is saved to the configuration immediately.
- *
- * @param repositoryGroup the repository group to remove
- * @throws RepositoryException if a error occurs during configuration save
- */
- public void removeRepositoryGroup( RepositoryGroup repositoryGroup ) throws RepositoryException
- {
- final String id = repositoryGroup.getId();
- RepositoryGroup repo = getRepositoryGroup( id );
- if (repo!=null) {
- rwLock.writeLock().lock();
- try {
- repo = repositoryGroups.remove( id );
- if (repo!=null) {
- repo.close();
- Configuration configuration = getArchivaConfiguration().getConfiguration();
- RepositoryGroupConfiguration cfg = configuration.findRepositoryGroupById( id );
- if (cfg!=null) {
- configuration.removeRepositoryGroup( cfg );
- }
- getArchivaConfiguration().save( configuration );
- }
-
- }
- 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() );
- } finally
- {
- rwLock.writeLock().unlock();
- }
- }
- }
-
- public void removeRepositoryGroup(RepositoryGroup repositoryGroup, Configuration configuration) throws RepositoryException
- {
- final String id = repositoryGroup.getId();
- RepositoryGroup repo = getRepositoryGroup( id );
- if (repo!=null) {
- rwLock.writeLock().lock();
- try {
- repo = repositoryGroups.remove( id );
- if (repo!=null) {
- repo.close();
- RepositoryGroupConfiguration cfg = configuration.findRepositoryGroupById( id );
- if (cfg!=null) {
- configuration.removeRepositoryGroup( cfg );
- }
- }
- } finally
- {
- rwLock.writeLock().unlock();
- }
- }
-
- }
-
- private void doRemoveRepo(RemoteRepository repo, Configuration configuration) {
- repo.close();
- RemoteRepositoryConfiguration cfg = configuration.findRemoteRepositoryById(repo.getId());
- if (cfg != null) {
- configuration.removeRemoteRepository(cfg);
- }
- List<ProxyConnectorConfiguration> proxyConnectors = new ArrayList<>(configuration.getProxyConnectors());
- for (ProxyConnectorConfiguration proxyConnector : proxyConnectors) {
- if (StringUtils.equals(proxyConnector.getTargetRepoId(), repo.getId())) {
- configuration.removeProxyConnector(proxyConnector);
- }
- }
- }
-
- /**
- * Removes the remote repository from the registry and configuration.
- * The change is saved to the configuration immediately.
- *
- * @param remoteRepository the remote repository to remove
- * @throws RepositoryException if a error occurs during configuration save
- */
- public void removeRepository( RemoteRepository remoteRepository ) throws RepositoryException
- {
-
- final String id = remoteRepository.getId();
- RemoteRepository repo = getRemoteRepository( id );
- if (repo!=null) {
- rwLock.writeLock().lock();
- try {
- repo = remoteRepositories.remove( id );
- if (repo!=null) {
- Configuration configuration = getArchivaConfiguration().getConfiguration();
- doRemoveRepo(repo, configuration);
- getArchivaConfiguration().save( configuration );
- }
- }
- catch ( RegistryException | IndeterminateConfigurationException e )
- {
- // Rollback
- log.error("Could not save config after repository removal: {}", e.getMessage(), e);
- remoteRepositories.put(repo.getId(), repo);
- throw new RepositoryException( "Could not save configuration after repository removal: "+e.getMessage() );
- } finally
- {
- rwLock.writeLock().unlock();
- }
- }
- }
-
- public void removeRepository( RemoteRepository remoteRepository, Configuration configuration) throws RepositoryException
- {
- final String id = remoteRepository.getId();
- RemoteRepository repo = getRemoteRepository( id );
- if (repo!=null) {
- rwLock.writeLock().lock();
- try {
- repo = remoteRepositories.remove( id );
- if (repo!=null) {
- doRemoveRepo(repo, configuration);
- }
- } finally
- {
- rwLock.writeLock().unlock();
- }
- }
-
- }
-
- /**
- * Reloads the registry from the configuration.
- */
- public void reload() {
- initialize();
- }
-
- /**
- * Resets the indexing context of a given repository.
- *
- * @param repo
- * @throws IndexUpdateFailedException
- */
- @SuppressWarnings( "unchecked" )
- public void resetIndexingContext(Repository repo) throws IndexUpdateFailedException {
- if (repo.hasIndex() && repo instanceof EditableRepository) {
- EditableRepository eRepo = (EditableRepository) repo;
- ArchivaIndexingContext newCtx = getIndexManager(repo.getType()).reset(repo.getIndexingContext());
- eRepo.setIndexingContext(newCtx);
- }
- }
-
-
- /**
- * Creates a new repository instance with the same settings as this one. The cloned repository is not
- * registered or saved to the configuration.
- *
- * @param repo The origin repository
- * @return The cloned repository.
- */
- 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);
- }
- RepositoryProvider provider = getProvider(repo.getType());
- ManagedRepositoryConfiguration cfg = provider.getManagedConfiguration(repo);
- cfg.setId(newId);
- ManagedRepository cloned = provider.createManagedInstance(cfg);
- cloned.addListener(this);
- return cloned;
- }
-
- @SuppressWarnings( "unchecked" )
- public <T extends Repository> Repository clone(T repo, String newId) throws RepositoryException {
- if (repo instanceof RemoteRepository ) {
- return this.clone((RemoteRepository)repo, newId);
- } else if (repo instanceof ManagedRepository) {
- return this.clone((ManagedRepository)repo, newId);
- } else {
- throw new RepositoryException("This repository class is not supported "+ repo.getClass().getName());
- }
- }
-
- /**
- * Creates a new repository instance with the same settings as this one. The cloned repository is not
- * registered or saved to the configuration.
- *
- * @param repo The origin repository
- * @return The cloned repository.
- */
- public RemoteRepository clone( RemoteRepository repo, String newId) throws RepositoryException
- {
- if (managedRepositories.containsKey(newId) || remoteRepositories.containsKey(newId)) {
- throw new RepositoryException("The given id exists already "+newId);
- }
- RepositoryProvider provider = getProvider(repo.getType());
- RemoteRepositoryConfiguration cfg = provider.getRemoteConfiguration(repo);
- cfg.setId(newId);
- RemoteRepository cloned = provider.createRemoteInstance(cfg);
- cloned.addListener(this);
- return cloned;
- }
-
-
- @Override
- public void configurationEvent(ConfigurationEvent event) {
-
- }
-
-
- @Override
- public void addListener(RepositoryEventListener listener) {
- if (!this.listeners.contains(listener)) {
- this.listeners.add(listener);
- }
- }
-
- @Override
- public void removeListener(RepositoryEventListener listener) {
- this.listeners.remove(listener);
- }
-
- @Override
- public void clearListeners() {
- this.listeners.clear();
- }
-
- @SuppressWarnings( "unchecked" )
- @Override
- public <T> void raise(RepositoryEvent<T> event) {
- if (event instanceof IndexCreationEvent ) {
- if (managedRepositories.containsKey(event.getRepository().getId()) ||
- remoteRepositories.containsKey(event.getRepository().getId())) {
- EditableRepository repo = (EditableRepository) event.getRepository();
- if (repo != null && repo.getIndexingContext()!=null) {
- try {
- ArchivaIndexManager idxmgr = getIndexManager(repo.getType());
- if (idxmgr != null) {
- ArchivaIndexingContext newCtx = idxmgr.move(repo.getIndexingContext(), repo);
- repo.setIndexingContext(newCtx);
- idxmgr.updateLocalIndexPath(repo);
- }
-
- } catch (IndexCreationFailedException e) {
- log.error("Could not move index to new directory {}", e.getMessage(), e);
- }
- }
- }
- }
- for(RepositoryEventListener listener : listeners) {
- listener.raise(event);
- }
- }
-
- }
|