1 package org.apache.archiva.repository;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.archiva.configuration.*;
23 import org.apache.archiva.indexer.*;
24 import org.apache.archiva.metadata.model.facets.AuditEvent;
25 import org.apache.archiva.redback.components.registry.RegistryException;
26 import org.apache.archiva.repository.features.IndexCreationEvent;
27 import org.apache.archiva.repository.features.IndexCreationFeature;
28 import org.apache.archiva.repository.features.StagingRepositoryFeature;
29 import org.apache.commons.lang.StringUtils;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.stereotype.Service;
34 import javax.annotation.PostConstruct;
35 import javax.annotation.PreDestroy;
36 import javax.inject.Inject;
37 import javax.inject.Named;
39 import java.util.concurrent.locks.ReentrantReadWriteLock;
40 import java.util.stream.Collectors;
41 import java.util.stream.Stream;
44 * Registry for repositories. This is the central entry point for repositories. It provides methods for
45 * retrieving, adding and removing repositories.
47 * The modification methods addXX and removeXX persist the changes immediately to the configuration. If the
48 * configuration save fails the changes are rolled back.
50 * TODO: Audit events should be sent, but we don't want dependency to the repsitory-metadata-api
52 @Service( "repositoryRegistry" )
53 public class RepositoryRegistry implements ConfigurationListener, RepositoryEventHandler, RepositoryEventListener {
55 private static final Logger log = LoggerFactory.getLogger( RepositoryRegistry.class );
58 * We inject all repository providers
61 List<RepositoryProvider> repositoryProviders;
64 IndexManagerFactory indexManagerFactory;
67 ArchivaConfiguration archivaConfiguration;
70 @Named("repositoryContentFactory#default")
71 RepositoryContentFactory repositoryContentFactory;
73 private List<RepositoryEventListener> listeners = new ArrayList<>();
76 private Map<String, ManagedRepository> managedRepositories = new HashMap<>( );
77 private Map<String, ManagedRepository> uManagedRepository = Collections.unmodifiableMap( managedRepositories );
79 private Map<String, RemoteRepository> remoteRepositories = new HashMap<>( );
80 private Map<String, RemoteRepository> uRemoteRepositories = Collections.unmodifiableMap( remoteRepositories );
82 private ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock( );
84 public void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration) {
85 this.archivaConfiguration = archivaConfiguration;
89 private void initialize( )
91 rwLock.writeLock( ).lock( );
94 log.debug("Initializing repository registry");
95 for(ManagedRepository rep : managedRepositories.values()) {
98 managedRepositories.clear( );
99 managedRepositories.putAll( getManagedRepositoriesFromConfig( ) );
100 for (RemoteRepository repo : remoteRepositories.values()) {
103 remoteRepositories.clear( );
104 remoteRepositories.putAll( getRemoteRepositoriesFromConfig( ) );
105 // archivaConfiguration.addChangeListener(this);
106 archivaConfiguration.addListener(this);
110 rwLock.writeLock( ).unlock( );
115 public void destroy() {
116 for(ManagedRepository rep : managedRepositories.values()) {
119 for (RemoteRepository repo : remoteRepositories.values()) {
126 private Map<RepositoryType, RepositoryProvider> createProviderMap( )
128 Map<RepositoryType, RepositoryProvider> map = new HashMap<>( );
129 if ( repositoryProviders != null )
131 for ( RepositoryProvider provider : repositoryProviders )
133 for ( RepositoryType type : provider.provides( ) )
135 map.put( type, provider );
142 private RepositoryProvider getProvider( RepositoryType type ) throws RepositoryException
144 return repositoryProviders.stream( ).filter( repositoryProvider -> repositoryProvider.provides( ).contains( type ) ).findFirst( ).orElseThrow( ( ) -> new RepositoryException( "Repository type cannot be handled: " + type ) );
147 private Map<String, ManagedRepository> getManagedRepositoriesFromConfig( )
151 List<ManagedRepositoryConfiguration> managedRepoConfigs =
152 getArchivaConfiguration( ).getConfiguration( ).getManagedRepositories( );
154 if ( managedRepoConfigs == null )
156 return Collections.EMPTY_MAP;
159 Map<String, ManagedRepository> managedRepos = new LinkedHashMap<>( managedRepoConfigs.size( ) );
161 Map<RepositoryType, RepositoryProvider> providerMap = createProviderMap( );
162 for ( ManagedRepositoryConfiguration repoConfig : managedRepoConfigs )
164 RepositoryType repositoryType = RepositoryType.valueOf( repoConfig.getType( ) );
165 if ( providerMap.containsKey( repositoryType ) )
169 ManagedRepository repo = createNewManagedRepository( providerMap.get( repositoryType ), repoConfig );
170 managedRepos.put( repo.getId( ), repo );
172 catch ( Exception e )
174 log.error( "Could not create managed repository {}: {}", repoConfig.getId( ), e.getMessage( ), e );
179 } catch (Throwable e) {
180 log.error("Could not initialize repositories from config: {}",e.getMessage(), e );
181 //noinspection unchecked
182 return Collections.EMPTY_MAP;
186 private ManagedRepository createNewManagedRepository( RepositoryProvider provider, ManagedRepositoryConfiguration cfg ) throws RepositoryException
188 log.debug("Creating repo {}", cfg.getId());
189 ManagedRepository repo = provider.createManagedInstance( cfg );
190 repo.addListener(this);
191 updateRepositoryReferences( provider, repo, cfg , null);
196 private void updateRepositoryReferences(RepositoryProvider provider, ManagedRepository repo, ManagedRepositoryConfiguration cfg, Configuration configuration) throws RepositoryException
198 log.debug("Updating references of repo {}",repo.getId());
199 if ( repo.supportsFeature( StagingRepositoryFeature.class ) )
201 StagingRepositoryFeature feature = repo.getFeature( StagingRepositoryFeature.class ).get( );
202 if ( feature.isStageRepoNeeded( ) && feature.getStagingRepository() == null)
204 ManagedRepository stageRepo = getStagingRepository( provider, cfg, configuration);
205 managedRepositories.put(stageRepo.getId(), stageRepo);
206 feature.setStagingRepository( stageRepo );
207 if (configuration!=null) {
208 replaceOrAddRepositoryConfig( provider.getManagedConfiguration( stageRepo ), configuration );
212 if ( repo instanceof EditableManagedRepository)
214 EditableManagedRepository editableRepo = (EditableManagedRepository) repo;
215 if (repo.getContent()==null) {
216 editableRepo.setContent(repositoryContentFactory.getManagedRepositoryContent(repo));
218 log.debug("Index repo: "+repo.hasIndex());
219 if (repo.hasIndex() && repo.getIndexingContext()==null) {
220 log.debug("Creating indexing context for {}", repo.getId());
221 createIndexingContext(editableRepo);
227 private ArchivaIndexManager getIndexManager(RepositoryType type) {
228 return indexManagerFactory.getIndexManager(type);
231 private void createIndexingContext(EditableRepository editableRepo) throws RepositoryException {
232 if (editableRepo.supportsFeature(IndexCreationFeature.class)) {
233 ArchivaIndexManager idxManager = getIndexManager(editableRepo.getType());
235 editableRepo.setIndexingContext(idxManager.createContext(editableRepo));
236 idxManager.updateLocalIndexPath(editableRepo);
237 } catch (IndexCreationFailedException e) {
238 throw new RepositoryException("Could not create index for repository "+editableRepo.getId()+": "+e.getMessage(),e);
243 private ManagedRepository getStagingRepository(RepositoryProvider provider, ManagedRepositoryConfiguration baseRepoCfg, Configuration configuration) throws RepositoryException
245 ManagedRepository stageRepo = getManagedRepository( baseRepoCfg.getId( ) + StagingRepositoryFeature.STAGING_REPO_POSTFIX );
246 if ( stageRepo == null )
248 stageRepo = provider.createStagingInstance( baseRepoCfg );
249 if (stageRepo.supportsFeature(StagingRepositoryFeature.class)) {
250 stageRepo.getFeature(StagingRepositoryFeature.class).get().setStageRepoNeeded(false);
252 ManagedRepositoryConfiguration stageCfg = provider.getManagedConfiguration( stageRepo );
253 updateRepositoryReferences( provider, stageRepo, stageCfg, configuration);
261 private Map<String, RemoteRepository> getRemoteRepositoriesFromConfig( )
265 List<RemoteRepositoryConfiguration> remoteRepoConfigs =
266 getArchivaConfiguration( ).getConfiguration( ).getRemoteRepositories( );
268 if ( remoteRepoConfigs == null )
270 //noinspection unchecked
271 return Collections.EMPTY_MAP;
274 Map<String, RemoteRepository> remoteRepos = new LinkedHashMap<>( remoteRepoConfigs.size( ) );
276 Map<RepositoryType, RepositoryProvider> providerMap = createProviderMap( );
277 for ( RemoteRepositoryConfiguration repoConfig : remoteRepoConfigs )
279 RepositoryType repositoryType = RepositoryType.valueOf( repoConfig.getType( ) );
280 if ( providerMap.containsKey( repositoryType ) )
282 RepositoryProvider provider = getProvider( repositoryType );
286 RemoteRepository remoteRepository = createNewRemoteRepository( provider, repoConfig );
287 remoteRepos.put( repoConfig.getId( ), remoteRepository);
289 catch ( Exception e )
291 log.error( "Could not create repository {} from config: {}", repoConfig.getId( ), e.getMessage( ), e );
297 } catch (Throwable e) {
298 log.error("Could not initialize remote repositories from config: {}", e.getMessage(), e);
299 //noinspection unchecked
300 return Collections.EMPTY_MAP;
304 private RemoteRepository createNewRemoteRepository( RepositoryProvider provider, RemoteRepositoryConfiguration cfg ) throws RepositoryException
306 log.debug("Creating remote repo {}", cfg.getId());
307 RemoteRepository repo = provider.createRemoteInstance( cfg );
308 repo.addListener(this);
309 updateRepositoryReferences( provider, repo, cfg , null);
314 private void updateRepositoryReferences( RepositoryProvider provider, RemoteRepository repo, RemoteRepositoryConfiguration cfg, Configuration configuration) throws RepositoryException
316 if ( repo instanceof EditableRemoteRepository && repo.getContent() == null)
318 EditableRemoteRepository editableRepo = (EditableRemoteRepository) repo;
319 if (repo.getContent()==null) {
320 editableRepo.setContent( repositoryContentFactory.getRemoteRepositoryContent( repo ) );
322 if (repo.supportsFeature(IndexCreationFeature.class) && repo.getIndexingContext()==null ) {
323 createIndexingContext(editableRepo);
328 private ArchivaConfiguration getArchivaConfiguration( )
330 return this.archivaConfiguration;
334 * Returns all repositories that are registered. There is no defined order of the returned repositories.
336 * @return a list of managed and remote repositories
338 public Collection<Repository> getRepositories( )
340 rwLock.readLock( ).lock( );
343 return Stream.concat( managedRepositories.values( ).stream( ), remoteRepositories.values( ).stream( ) ).collect( Collectors.toList( ) );
347 rwLock.readLock( ).unlock( );
352 * Returns only the managed repositories. There is no defined order of the returned repositories.
354 * @return a list of managed repositories
356 public Collection<ManagedRepository> getManagedRepositories( )
358 rwLock.readLock().lock();
361 return uManagedRepository.values( );
364 rwLock.readLock().unlock();
369 * Returns only the remote repositories. There is no defined order of the returned repositories.
371 * @return a list of remote repositories
373 public Collection<RemoteRepository> getRemoteRepositories( )
375 rwLock.readLock().lock();
378 return uRemoteRepositories.values( );
381 rwLock.readLock().unlock();
386 * Returns the repository with the given id. The returned repository may be a managed or remote repository.
387 * It returns null, if no repository is registered with the given id.
389 * @param repoId the repository id
390 * @return the repository if found, otherwise null
392 public Repository getRepository( String repoId )
394 rwLock.readLock( ).lock( );
397 log.debug("getRepository {}", repoId);
398 if ( managedRepositories.containsKey( repoId ) )
400 log.debug("Managed repo");
401 return managedRepositories.get( repoId );
405 log.debug("Remote repo");
406 return remoteRepositories.get( repoId );
411 rwLock.readLock( ).unlock( );
416 * Convenience method, that returns the managed repository with the given id.
417 * It returns null, if no managed repository is registered with this id.
419 * @param repoId the repository id
420 * @return the managed repository if found, otherwise null
422 public ManagedRepository getManagedRepository( String repoId )
424 rwLock.readLock( ).lock( );
427 return managedRepositories.get( repoId );
431 rwLock.readLock( ).unlock( );
436 * Convenience method, that returns the remote repository with the given id.
437 * It returns null, if no remote repository is registered with this id.
439 * @param repoId the repository id
440 * @return the remote repository if found, otherwise null
442 public RemoteRepository getRemoteRepository( String repoId )
444 rwLock.readLock( ).lock( );
447 return remoteRepositories.get( repoId );
451 rwLock.readLock( ).unlock( );
456 * Adds a new repository to the current list, or replaces the repository definition with
457 * the same id, if it exists already.
458 * The change is saved to the configuration immediately.
460 * @param managedRepository the new repository.
461 * @throws RepositoryException if the new repository could not be saved to the configuration.
463 public ManagedRepository putRepository( ManagedRepository managedRepository ) throws RepositoryException
465 rwLock.writeLock( ).lock( );
468 final String id = managedRepository.getId();
469 if (remoteRepositories.containsKey( id )) {
470 throw new RepositoryException( "There exists a remote repository with id "+id+". Could not update with managed repository." );
473 ManagedRepository originRepo = managedRepositories.put( id, managedRepository );
476 if (originRepo!=null) {
479 RepositoryProvider provider = getProvider( managedRepository.getType() );
480 ManagedRepositoryConfiguration newCfg = provider.getManagedConfiguration( managedRepository );
481 Configuration configuration = getArchivaConfiguration( ).getConfiguration( );
482 updateRepositoryReferences( provider, managedRepository, newCfg, configuration );
483 ManagedRepositoryConfiguration oldCfg = configuration.findManagedRepositoryById( id );
485 configuration.removeManagedRepository( oldCfg );
487 configuration.addManagedRepository( newCfg );
488 getArchivaConfiguration( ).save( configuration );
489 return managedRepository;
491 catch ( Exception e )
494 if ( originRepo != null )
496 managedRepositories.put( id, originRepo );
498 managedRepositories.remove(id);
500 log.error("Exception during configuration update {}", e.getMessage(), e);
501 throw new RepositoryException( "Could not save the configuration" + (e.getMessage( )==null?"":": "+e.getMessage()) );
506 rwLock.writeLock( ).unlock( );
511 * Adds a new repository or updates the repository with the same id, if it exists already.
512 * The configuration is saved immediately.
514 * @param managedRepositoryConfiguration the repository configuration
515 * @return the updated or created repository
516 * @throws RepositoryException if an error occurs, or the configuration is not valid.
518 public ManagedRepository putRepository( ManagedRepositoryConfiguration managedRepositoryConfiguration) throws RepositoryException
520 rwLock.writeLock( ).lock( );
523 final String id = managedRepositoryConfiguration.getId();
524 final RepositoryType repositoryType = RepositoryType.valueOf( managedRepositoryConfiguration.getType() );
525 Configuration configuration = getArchivaConfiguration().getConfiguration();
526 ManagedRepository repo = managedRepositories.get(id);
527 ManagedRepositoryConfiguration oldCfg = repo!=null ? getProvider( repositoryType ).getManagedConfiguration( repo ) : null;
528 repo = putRepository( managedRepositoryConfiguration, configuration );
531 getArchivaConfiguration().save(configuration);
533 catch ( IndeterminateConfigurationException | RegistryException e )
536 getProvider( repositoryType ).updateManagedInstance( (EditableManagedRepository)repo, oldCfg );
538 log.error("Could not save the configuration for repository {}: {}", id, e.getMessage(),e );
539 throw new RepositoryException( "Could not save the configuration for repository "+id+": "+e.getMessage() );
545 rwLock.writeLock( ).unlock( );
551 * Adds a new repository or updates the repository with the same id. The given configuration object is updated, but
552 * the configuration is not saved.
554 * @param managedRepositoryConfiguration the new or changed repository configuration
555 * @param configuration the configuration object
556 * @return the new or updated repository
557 * @throws RepositoryException if the configuration cannot be saved or updated
559 public ManagedRepository putRepository( ManagedRepositoryConfiguration managedRepositoryConfiguration, Configuration configuration) throws RepositoryException
561 rwLock.writeLock( ).lock( );
564 final String id = managedRepositoryConfiguration.getId();
565 final RepositoryType repoType = RepositoryType.valueOf( managedRepositoryConfiguration.getType() );
566 ManagedRepository repo;
567 if (managedRepositories.containsKey( id )) {
568 repo = managedRepositories.get(id);
569 if (repo instanceof EditableManagedRepository)
571 getProvider( repoType ).updateManagedInstance( (EditableManagedRepository) repo, managedRepositoryConfiguration );
573 throw new RepositoryException( "The repository is not editable "+id );
577 repo = getProvider( repoType ).createManagedInstance( managedRepositoryConfiguration );
578 repo.addListener(this);
579 managedRepositories.put(id, repo);
581 updateRepositoryReferences( getProvider( repoType ), repo, managedRepositoryConfiguration, configuration );
582 replaceOrAddRepositoryConfig( managedRepositoryConfiguration, configuration );
587 rwLock.writeLock( ).unlock( );
591 private void replaceOrAddRepositoryConfig(ManagedRepositoryConfiguration managedRepositoryConfiguration, Configuration configuration) {
592 ManagedRepositoryConfiguration oldCfg = configuration.findManagedRepositoryById( managedRepositoryConfiguration.getId() );
593 if ( oldCfg !=null) {
594 configuration.removeManagedRepository( oldCfg );
596 configuration.addManagedRepository( managedRepositoryConfiguration );
599 private void replaceOrAddRepositoryConfig(RemoteRepositoryConfiguration remoteRepositoryConfiguration, Configuration configuration) {
600 RemoteRepositoryConfiguration oldCfg = configuration.findRemoteRepositoryById( remoteRepositoryConfiguration.getId() );
601 if ( oldCfg !=null) {
602 configuration.removeRemoteRepository( oldCfg );
604 configuration.addRemoteRepository( remoteRepositoryConfiguration );
607 public RemoteRepository putRepository( RemoteRepository remoteRepository, Configuration configuration) throws RepositoryException
609 rwLock.writeLock( ).lock( );
612 final String id = remoteRepository.getId();
613 if (managedRepositories.containsKey( id )) {
614 throw new RepositoryException( "There exists a managed repository with id "+id+". Could not update with remote repository." );
616 RemoteRepository originRepo = remoteRepositories.put( id, remoteRepository );
617 RemoteRepositoryConfiguration oldCfg=null;
618 RemoteRepositoryConfiguration newCfg=null;
621 if (originRepo!=null) {
624 final RepositoryProvider provider = getProvider( remoteRepository.getType() );
625 newCfg = provider.getRemoteConfiguration( remoteRepository );
626 updateRepositoryReferences( provider, remoteRepository, newCfg, configuration );
627 oldCfg = configuration.findRemoteRepositoryById( id );
629 configuration.removeRemoteRepository( oldCfg );
631 configuration.addRemoteRepository( newCfg );
632 return remoteRepository;
634 catch ( Exception e )
637 if ( originRepo != null )
639 remoteRepositories.put( id, originRepo );
641 remoteRepositories.remove( id);
644 RemoteRepositoryConfiguration cfg = configuration.findRemoteRepositoryById( id );
646 configuration.removeRemoteRepository( cfg );
647 configuration.addRemoteRepository( oldCfg );
650 log.error("Error while adding remote repository {}", e.getMessage(), e);
651 throw new RepositoryException( "Could not save the configuration" + (e.getMessage( )==null?"":": "+e.getMessage()) );
656 rwLock.writeLock( ).unlock( );
661 * Adds a remote repository, or overwrites the repository definition with the same id, if it exists already.
662 * The modification is saved to the configuration immediately.
664 * @param remoteRepository the remote repository to add
665 * @throws RepositoryException if an error occurs during configuration save
667 public RemoteRepository putRepository( RemoteRepository remoteRepository ) throws RepositoryException
669 rwLock.writeLock( ).lock( );
672 Configuration configuration = getArchivaConfiguration().getConfiguration();
675 RemoteRepository repo = putRepository( remoteRepository, configuration );
676 getArchivaConfiguration().save(configuration);
679 catch ( RegistryException | IndeterminateConfigurationException e )
681 log.error("Error while saving remote repository {}", e.getMessage(), e);
682 throw new RepositoryException( "Could not save the configuration" + (e.getMessage( )==null?"":": "+e.getMessage()) );
687 rwLock.writeLock( ).unlock( );
692 * Adds a new repository or updates the repository with the same id, if it exists already.
693 * The configuration is saved immediately.
695 * @param remoteRepositoryConfiguration the repository configuration
696 * @return the updated or created repository
697 * @throws RepositoryException if an error occurs, or the configuration is not valid.
699 public RemoteRepository putRepository( RemoteRepositoryConfiguration remoteRepositoryConfiguration) throws RepositoryException
701 rwLock.writeLock( ).lock( );
704 final String id = remoteRepositoryConfiguration.getId();
705 final RepositoryType repositoryType = RepositoryType.valueOf( remoteRepositoryConfiguration.getType() );
706 Configuration configuration = getArchivaConfiguration().getConfiguration();
707 RemoteRepository repo = remoteRepositories.get(id);
708 RemoteRepositoryConfiguration oldCfg = repo!=null ? getProvider( repositoryType ).getRemoteConfiguration( repo ) : null;
709 repo = putRepository( remoteRepositoryConfiguration, configuration );
712 getArchivaConfiguration().save(configuration);
714 catch ( IndeterminateConfigurationException | RegistryException e )
717 getProvider( repositoryType ).updateRemoteInstance( (EditableRemoteRepository)repo, oldCfg );
719 log.error("Could not save the configuration for repository {}: {}", id, e.getMessage(),e );
720 throw new RepositoryException( "Could not save the configuration for repository "+id+": "+e.getMessage() );
726 rwLock.writeLock( ).unlock( );
732 * Adds a new repository or updates the repository with the same id. The given configuration object is updated, but
733 * the configuration is not saved.
735 * @param remoteRepositoryConfiguration the new or changed repository configuration
736 * @param configuration the configuration object
737 * @return the new or updated repository
738 * @throws RepositoryException if the configuration cannot be saved or updated
740 public RemoteRepository putRepository( RemoteRepositoryConfiguration remoteRepositoryConfiguration, Configuration configuration) throws RepositoryException
742 rwLock.writeLock( ).lock( );
745 final String id = remoteRepositoryConfiguration.getId();
746 final RepositoryType repoType = RepositoryType.valueOf( remoteRepositoryConfiguration.getType() );
747 RemoteRepository repo;
748 if (remoteRepositories.containsKey( id )) {
749 repo = remoteRepositories.get(id);
750 if (repo instanceof EditableRemoteRepository)
752 getProvider( repoType ).updateRemoteInstance( (EditableRemoteRepository) repo, remoteRepositoryConfiguration );
754 throw new RepositoryException( "The repository is not editable "+id );
758 repo = getProvider( repoType ).createRemoteInstance( remoteRepositoryConfiguration );
759 repo.addListener(this);
760 remoteRepositories.put(id, repo);
762 updateRepositoryReferences( getProvider( repoType ), repo, remoteRepositoryConfiguration, configuration );
763 replaceOrAddRepositoryConfig( remoteRepositoryConfiguration, configuration );
768 rwLock.writeLock( ).unlock( );
774 public void removeRepository(String repoId) throws RepositoryException {
775 Repository repo = getRepository(repoId);
777 removeRepository(repo);
780 public void removeRepository(Repository repo) throws RepositoryException
782 if (repo instanceof RemoteRepository ) {
783 removeRepository( (RemoteRepository)repo );
784 } else if (repo instanceof ManagedRepository) {
785 removeRepository( (ManagedRepository)repo);
787 throw new RepositoryException( "Repository type not known: "+repo.getClass() );
792 * Removes a managed repository from the registry and configuration, if it exists.
793 * The change is saved to the configuration immediately.
795 * @param managedRepository the managed repository to remove
796 * @throws RepositoryException if a error occurs during configuration save
798 public void removeRepository( ManagedRepository managedRepository ) throws RepositoryException
800 final String id = managedRepository.getId();
801 ManagedRepository repo = getManagedRepository( id );
803 rwLock.writeLock().lock();
805 repo = managedRepositories.remove( id );
808 Configuration configuration = getArchivaConfiguration().getConfiguration();
809 ManagedRepositoryConfiguration cfg = configuration.findManagedRepositoryById( id );
811 configuration.removeManagedRepository( cfg );
813 getArchivaConfiguration().save( configuration );
817 catch ( RegistryException | IndeterminateConfigurationException e )
820 log.error("Could not save config after repository removal: {}", e.getMessage(), e);
821 managedRepositories.put(repo.getId(), repo);
822 throw new RepositoryException( "Could not save configuration after repository removal: "+e.getMessage() );
825 rwLock.writeLock().unlock();
830 public void removeRepository(ManagedRepository managedRepository, Configuration configuration) throws RepositoryException
832 final String id = managedRepository.getId();
833 ManagedRepository repo = getManagedRepository( id );
835 rwLock.writeLock().lock();
837 repo = managedRepositories.remove( id );
840 ManagedRepositoryConfiguration cfg = configuration.findManagedRepositoryById( id );
842 configuration.removeManagedRepository( cfg );
847 rwLock.writeLock().unlock();
854 private void doRemoveRepo(RemoteRepository repo, Configuration configuration) {
856 RemoteRepositoryConfiguration cfg = configuration.findRemoteRepositoryById(repo.getId());
858 configuration.removeRemoteRepository(cfg);
860 List<ProxyConnectorConfiguration> proxyConnectors = new ArrayList<>(configuration.getProxyConnectors());
861 for (ProxyConnectorConfiguration proxyConnector : proxyConnectors) {
862 if (StringUtils.equals(proxyConnector.getTargetRepoId(), repo.getId())) {
863 configuration.removeProxyConnector(proxyConnector);
869 * Removes the remote repository from the registry and configuration.
870 * The change is saved to the configuration immediately.
872 * @param remoteRepository the remote repository to remove
873 * @throws RepositoryException if a error occurs during configuration save
875 public void removeRepository( RemoteRepository remoteRepository ) throws RepositoryException
878 final String id = remoteRepository.getId();
879 RemoteRepository repo = getRemoteRepository( id );
881 rwLock.writeLock().lock();
883 repo = remoteRepositories.remove( id );
885 Configuration configuration = getArchivaConfiguration().getConfiguration();
886 doRemoveRepo(repo, configuration);
887 getArchivaConfiguration().save( configuration );
890 catch ( RegistryException | IndeterminateConfigurationException e )
893 log.error("Could not save config after repository removal: {}", e.getMessage(), e);
894 remoteRepositories.put(repo.getId(), repo);
895 throw new RepositoryException( "Could not save configuration after repository removal: "+e.getMessage() );
898 rwLock.writeLock().unlock();
903 public void removeRepository( RemoteRepository remoteRepository, Configuration configuration) throws RepositoryException
905 final String id = remoteRepository.getId();
906 RemoteRepository repo = getRemoteRepository( id );
908 rwLock.writeLock().lock();
910 repo = remoteRepositories.remove( id );
912 doRemoveRepo(repo, configuration);
916 rwLock.writeLock().unlock();
923 * Reloads the registry from the configuration.
925 public void reload() {
930 * Resets the indexing context of a given repository.
933 * @throws IndexUpdateFailedException
935 public void resetIndexingContext(Repository repo) throws IndexUpdateFailedException {
936 if (repo.hasIndex() && repo instanceof EditableRepository) {
937 EditableRepository eRepo = (EditableRepository) repo;
938 ArchivaIndexingContext newCtx = getIndexManager(repo.getType()).reset(repo.getIndexingContext());
939 eRepo.setIndexingContext(newCtx);
945 * Creates a new repository instance with the same settings as this one. The cloned repository is not
946 * registered or saved to the configuration.
948 * @param repo The origin repository
949 * @return The cloned repository.
951 public ManagedRepository clone(ManagedRepository repo, String newId) throws RepositoryException
953 if (managedRepositories.containsKey(newId) || remoteRepositories.containsKey(newId)) {
954 throw new RepositoryException("The given id exists already "+newId);
956 RepositoryProvider provider = getProvider(repo.getType());
957 ManagedRepositoryConfiguration cfg = provider.getManagedConfiguration(repo);
959 ManagedRepository cloned = provider.createManagedInstance(cfg);
960 cloned.addListener(this);
964 public <T extends Repository> Repository clone(T repo, String newId) throws RepositoryException {
965 if (repo instanceof RemoteRepository ) {
966 return this.clone((RemoteRepository)repo, newId);
967 } else if (repo instanceof ManagedRepository) {
968 return this.clone((ManagedRepository)repo, newId);
970 throw new RepositoryException("This repository class is not supported "+ repo.getClass().getName());
975 * Creates a new repository instance with the same settings as this one. The cloned repository is not
976 * registered or saved to the configuration.
978 * @param repo The origin repository
979 * @return The cloned repository.
981 public RemoteRepository clone( RemoteRepository repo, String newId) throws RepositoryException
983 if (managedRepositories.containsKey(newId) || remoteRepositories.containsKey(newId)) {
984 throw new RepositoryException("The given id exists already "+newId);
986 RepositoryProvider provider = getProvider(repo.getType());
987 RemoteRepositoryConfiguration cfg = provider.getRemoteConfiguration(repo);
989 RemoteRepository cloned = provider.createRemoteInstance(cfg);
990 cloned.addListener(this);
996 public void configurationEvent(ConfigurationEvent event) {
1002 public void addListener(RepositoryEventListener listener) {
1003 if (!this.listeners.contains(listener)) {
1004 this.listeners.add(listener);
1009 public void removeListener(RepositoryEventListener listener) {
1010 this.listeners.remove(listener);
1014 public void clearListeners() {
1015 this.listeners.clear();
1019 public <T> void raise(RepositoryEvent<T> event) {
1020 if (event.getType().equals(IndexCreationEvent.Index.URI_CHANGE)) {
1021 if (managedRepositories.containsKey(event.getRepository().getId()) ||
1022 remoteRepositories.containsKey(event.getRepository().getId())) {
1023 EditableRepository repo = (EditableRepository) event.getRepository();
1024 if (repo != null && repo.getIndexingContext()!=null) {
1026 ArchivaIndexManager idxmgr = getIndexManager(repo.getType());
1027 if (idxmgr != null) {
1028 ArchivaIndexingContext newCtx = idxmgr.move(repo.getIndexingContext(), repo);
1029 repo.setIndexingContext(newCtx);
1030 idxmgr.updateLocalIndexPath(repo);
1033 } catch (IndexCreationFailedException e) {
1034 log.error("Could not move index to new directory {}", e.getMessage(), e);
1039 for(RepositoryEventListener listener : listeners) {
1040 listener.raise(event);