1 package org.apache.archiva.repository.base;
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.components.registry.RegistryException;
23 import org.apache.archiva.configuration.model.AbstractRepositoryConfiguration;
24 import org.apache.archiva.configuration.provider.ArchivaConfiguration;
25 import org.apache.archiva.configuration.model.Configuration;
26 import org.apache.archiva.configuration.provider.ConfigurationEvent;
27 import org.apache.archiva.configuration.provider.ConfigurationListener;
28 import org.apache.archiva.configuration.provider.IndeterminateConfigurationException;
29 import org.apache.archiva.configuration.model.ManagedRepositoryConfiguration;
30 import org.apache.archiva.configuration.model.RemoteRepositoryConfiguration;
31 import org.apache.archiva.configuration.model.RepositoryGroupConfiguration;
32 import org.apache.archiva.event.Event;
33 import org.apache.archiva.event.EventHandler;
34 import org.apache.archiva.event.EventManager;
35 import org.apache.archiva.event.EventType;
36 import org.apache.archiva.indexer.ArchivaIndexManager;
37 import org.apache.archiva.indexer.ArchivaIndexingContext;
38 import org.apache.archiva.indexer.IndexCreationFailedException;
39 import org.apache.archiva.indexer.IndexManagerFactory;
40 import org.apache.archiva.indexer.IndexUpdateFailedException;
41 import org.apache.archiva.repository.EditableRepository;
42 import org.apache.archiva.repository.ManagedRepository;
43 import org.apache.archiva.repository.RemoteRepository;
44 import org.apache.archiva.repository.Repository;
45 import org.apache.archiva.repository.RepositoryException;
46 import org.apache.archiva.repository.RepositoryGroup;
47 import org.apache.archiva.repository.RepositoryHandler;
48 import org.apache.archiva.repository.RepositoryProvider;
49 import org.apache.archiva.repository.RepositoryRegistry;
50 import org.apache.archiva.repository.RepositoryType;
51 import org.apache.archiva.repository.UnsupportedRepositoryTypeException;
52 import org.apache.archiva.repository.event.RepositoryIndexEvent;
53 import org.apache.archiva.repository.event.RepositoryRegistryEvent;
54 import org.apache.archiva.repository.metadata.MetadataReader;
55 import org.apache.archiva.repository.storage.StorageAsset;
56 import org.apache.archiva.repository.validation.CheckedResult;
57 import org.apache.archiva.repository.validation.RepositoryValidator;
58 import org.apache.archiva.repository.validation.ValidationError;
59 import org.apache.archiva.repository.validation.ValidationResponse;
60 import org.apache.commons.collections4.ListUtils;
61 import org.slf4j.Logger;
62 import org.slf4j.LoggerFactory;
63 import org.springframework.stereotype.Service;
65 import javax.annotation.PostConstruct;
66 import javax.annotation.PreDestroy;
67 import javax.inject.Inject;
68 import java.util.Collection;
69 import java.util.Collections;
70 import java.util.HashMap;
71 import java.util.List;
74 import java.util.TreeSet;
75 import java.util.concurrent.atomic.AtomicBoolean;
76 import java.util.concurrent.locks.ReentrantReadWriteLock;
77 import java.util.stream.Collectors;
78 import java.util.stream.Stream;
81 * Registry for repositories. This is the central entry point for repositories. It provides methods for
82 * retrieving, adding and removing repositories.
84 * The modification methods addXX and removeXX persist the changes immediately to the configuration. If the
85 * configuration save fails the changes are rolled back.
91 @Service( "repositoryRegistry" )
92 public class ArchivaRepositoryRegistry implements ConfigurationListener, EventHandler<Event>,
96 private static final Logger log = LoggerFactory.getLogger( RepositoryRegistry.class );
99 * We inject all repository providers
102 List<RepositoryProvider> repositoryProviders;
104 @SuppressWarnings( "SpringJavaInjectionPointsAutowiringInspection" )
106 IndexManagerFactory indexManagerFactory;
109 List<MetadataReader> metadataReaderList;
112 List<RepositoryValidator<? extends Repository>> repositoryValidatorList;
114 private boolean ignoreIndexing = false;
116 private final EventManager eventManager;
119 private final ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock( );
121 private RepositoryHandler<RepositoryGroup, RepositoryGroupConfiguration> groupHandler;
122 private RepositoryHandler<ManagedRepository, ManagedRepositoryConfiguration> managedRepositoryHandler;
123 private RepositoryHandler<RemoteRepository, RemoteRepositoryConfiguration> remoteRepositoryHandler;
125 private final Set<RepositoryValidator<? extends Repository>> validators;
126 private final ConfigurationHandler configurationHandler;
129 private final AtomicBoolean groups_initalized = new AtomicBoolean( false );
130 private final AtomicBoolean managed_initialized = new AtomicBoolean( false );
131 private final AtomicBoolean remote_initialized = new AtomicBoolean( false );
134 public ArchivaRepositoryRegistry( ConfigurationHandler configurationHandler, List<RepositoryValidator<? extends Repository>> validatorList )
136 this.eventManager = new EventManager( this );
137 this.configurationHandler = configurationHandler;
138 this.validators = initValidatorList( validatorList );
142 private Set<RepositoryValidator<? extends Repository>> initValidatorList( List<RepositoryValidator<? extends Repository>> validators )
144 TreeSet<RepositoryValidator<? extends Repository>> val = new TreeSet<>( );
145 for ( RepositoryValidator<? extends Repository> validator : validators )
147 val.add( validator );
148 validator.setRepositoryRegistry( this );
154 public void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration )
156 this.configurationHandler.setArchivaConfiguration( archivaConfiguration );
160 private void initialize( )
162 rwLock.writeLock( ).lock( );
165 log.debug( "Initializing repository registry" );
166 initializeManagedRepositories();
167 initializeRemoteRepositories();
168 initializeRepositoryGroups( );
170 for ( RepositoryProvider provider : repositoryProviders )
172 provider.addRepositoryEventHandler( this );
174 this.configurationHandler.addListener( this );
178 rwLock.writeLock( ).unlock( );
180 pushEvent( new RepositoryRegistryEvent( RepositoryRegistryEvent.RELOADED, this ) );
181 if ( managed_initialized.get( ) && remote_initialized.get( ) && groups_initalized.get( ) )
183 pushEvent( new RepositoryRegistryEvent( RepositoryRegistryEvent.INITIALIZED, this ) );
187 private void initializeRepositoryGroups( )
189 if ( this.groupHandler != null )
191 this.groupHandler.initializeFromConfig( );
192 this.groups_initalized.set( true );
193 pushEvent( new RepositoryRegistryEvent( RepositoryRegistryEvent.GROUPS_INITIALIZED, this ) );
197 private void initializeManagedRepositories( )
199 if ( this.managedRepositoryHandler != null )
201 this.managedRepositoryHandler.initializeFromConfig( );
202 this.managed_initialized.set( true );
203 pushEvent( new RepositoryRegistryEvent( RepositoryRegistryEvent.MANAGED_REPOS_INITIALIZED, this ) );
207 private void initializeRemoteRepositories() {
208 if (this.remoteRepositoryHandler != null ){
209 this.remoteRepositoryHandler.initializeFromConfig( );
210 this.remote_initialized.set( true );
211 pushEvent( new RepositoryRegistryEvent( RepositoryRegistryEvent.REMOTE_REPOS_INITIALIZED, this ) );
216 public void registerGroupHandler( RepositoryHandler<RepositoryGroup, RepositoryGroupConfiguration> groupHandler )
218 this.groupHandler = groupHandler;
219 doRegister( groupHandler );
220 initializeRepositoryGroups( );
221 if ( managed_initialized.get( ) && remote_initialized.get( ) && groups_initalized.get( ) )
223 pushEvent( new RepositoryRegistryEvent( RepositoryRegistryEvent.INITIALIZED, this ) );
227 public void registerManagedRepositoryHandler( RepositoryHandler<ManagedRepository, ManagedRepositoryConfiguration> managedRepositoryHandler )
229 this.managedRepositoryHandler = managedRepositoryHandler;
230 doRegister( managedRepositoryHandler );
231 initializeManagedRepositories();
232 if ( managed_initialized.get( ) && remote_initialized.get( ) && groups_initalized.get( ) )
234 pushEvent( new RepositoryRegistryEvent( RepositoryRegistryEvent.INITIALIZED, this ) );
238 public void registerRemoteRepositoryHandler( RepositoryHandler<RemoteRepository, RemoteRepositoryConfiguration> remoteRepositoryHandler )
240 this.remoteRepositoryHandler = remoteRepositoryHandler;
241 doRegister( remoteRepositoryHandler );
242 initializeRemoteRepositories();
243 if ( managed_initialized.get( ) && remote_initialized.get( ) && groups_initalized.get( ) )
245 pushEvent( new RepositoryRegistryEvent( RepositoryRegistryEvent.INITIALIZED, this ) );
250 public void destroy( )
252 managedRepositoryHandler.close( );
253 remoteRepositoryHandler.close();
254 groupHandler.close( );
255 pushEvent( new RepositoryRegistryEvent( RepositoryRegistryEvent.DESTROYED, this ) );
259 public Map<RepositoryType, RepositoryProvider> getRepositoryProviderMap( )
261 Map<RepositoryType, RepositoryProvider> map = new HashMap<>( );
262 if ( repositoryProviders != null )
264 for ( RepositoryProvider provider : repositoryProviders )
266 for ( RepositoryType type : provider.provides( ) )
268 map.put( type, provider );
275 public RepositoryProvider getProvider( RepositoryType type ) throws RepositoryException
277 return repositoryProviders.stream( ).filter( repositoryProvider -> repositoryProvider.provides( ).contains( type ) ).findFirst( ).orElseThrow( ( ) -> new RepositoryException( "Repository type cannot be handled: " + type ) );
282 public ArchivaIndexManager getIndexManager( RepositoryType type )
284 return indexManagerFactory.getIndexManager( type );
288 public MetadataReader getMetadataReader( final RepositoryType type ) throws UnsupportedRepositoryTypeException
290 if ( metadataReaderList != null )
292 return metadataReaderList.stream( ).filter( mr -> mr.isValidForType( type ) ).findFirst( ).orElseThrow( ( ) -> new UnsupportedRepositoryTypeException( type ) );
296 throw new UnsupportedRepositoryTypeException( type );
302 * Returns all repositories that are registered. There is no defined order of the returned repositories.
304 * @return a list of managed and remote repositories
307 public Collection<Repository> getRepositories( )
309 rwLock.readLock( ).lock( );
312 return Stream.concat( managedRepositoryHandler.getAll().stream( ), remoteRepositoryHandler.getAll().stream( ) ).collect( Collectors.toList( ) );
316 rwLock.readLock( ).unlock( );
321 * Returns only the managed repositories. There is no defined order of the returned repositories.
323 * @return a list of managed repositories
326 public Collection<ManagedRepository> getManagedRepositories( )
328 rwLock.readLock( ).lock( );
331 return managed_initialized.get() ? managedRepositoryHandler.getAll( ) : Collections.emptyList();
335 rwLock.readLock( ).unlock( );
340 * Returns only the remote repositories. There is no defined order of the returned repositories.
342 * @return a list of remote repositories
345 public Collection<RemoteRepository> getRemoteRepositories( )
347 rwLock.readLock( ).lock( );
350 return remote_initialized.get() ? remoteRepositoryHandler.getAll( ) : Collections.emptyList();
354 rwLock.readLock( ).unlock( );
359 public Collection<RepositoryGroup> getRepositoryGroups( )
361 rwLock.readLock( ).lock( );
364 return groupHandler.getAll( );
368 rwLock.readLock( ).unlock( );
373 * Returns the repository with the given id. The returned repository may be a managed or remote repository.
374 * It returns null, if no repository is registered with the given id.
376 * @param repoId the repository id
377 * @return the repository if found, otherwise null
380 public Repository getRepository( String repoId )
382 rwLock.readLock( ).lock( );
385 log.debug( "getRepository {}", repoId );
386 if ( managedRepositoryHandler.hasRepository( repoId ) )
388 log.debug( "Managed repo" );
389 return managedRepositoryHandler.get( repoId );
391 else if ( remoteRepositoryHandler.hasRepository( repoId ) )
393 log.debug( "Remote repo" );
394 return remoteRepositoryHandler.get( repoId );
396 else if ( groupHandler.hasRepository( repoId ) )
398 return groupHandler.get( repoId );
407 rwLock.readLock( ).unlock( );
412 * Convenience method, that returns the managed repository with the given id.
413 * It returns null, if no managed repository is registered with this id.
415 * @param repoId the repository id
416 * @return the managed repository if found, otherwise null
419 public ManagedRepository getManagedRepository( String repoId )
421 rwLock.readLock( ).lock( );
424 return managed_initialized.get() ? managedRepositoryHandler.get( repoId ) : null;
428 rwLock.readLock( ).unlock( );
433 * Convenience method, that returns the remote repository with the given id.
434 * It returns null, if no remote repository is registered with this id.
436 * @param repoId the repository id
437 * @return the remote repository if found, otherwise null
440 public RemoteRepository getRemoteRepository( String repoId )
442 rwLock.readLock( ).lock( );
445 return remote_initialized.get() ? remoteRepositoryHandler.get( repoId ) : null;
449 rwLock.readLock( ).unlock( );
454 public RepositoryGroup getRepositoryGroup( String groupId )
456 rwLock.readLock( ).lock( );
459 return groupHandler.get( groupId );
463 rwLock.readLock( ).unlock( );
468 public boolean hasRepository( String repoId )
470 return ( managedRepositoryHandler != null && managedRepositoryHandler.hasRepository( repoId ) )
471 || ( remoteRepositoryHandler != null && remoteRepositoryHandler.hasRepository( repoId ) )
472 || ( this.groupHandler != null && groupHandler.hasRepository( repoId ) );
476 public boolean hasManagedRepository( String repoId )
478 return managedRepositoryHandler!=null && managedRepositoryHandler.hasRepository( repoId );
482 public boolean hasRemoteRepository( String repoId )
484 return remoteRepositoryHandler!=null && remoteRepositoryHandler.hasRepository( repoId );
488 public boolean hasRepositoryGroup( String groupId )
490 return this.groupHandler != null && groupHandler.hasRepository( groupId );
493 protected void saveConfiguration( Configuration configuration ) throws IndeterminateConfigurationException, RegistryException
495 configurationHandler.save( configuration, ConfigurationHandler.REGISTRY_EVENT_TAG );
499 * Adds a new repository to the current list, or replaces the repository definition with
500 * the same id, if it exists already.
501 * The change is saved to the configuration immediately.
503 * @param managedRepository the new repository.
504 * @throws RepositoryException if the new repository could not be saved to the configuration.
507 public ManagedRepository putRepository( ManagedRepository managedRepository ) throws RepositoryException
509 rwLock.writeLock( ).lock( );
512 return managed_initialized.get() ? managedRepositoryHandler.put( managedRepository ) : null;
516 rwLock.writeLock( ).unlock( );
521 * Adds a new repository or updates the repository with the same id, if it exists already.
522 * The configuration is saved immediately.
524 * @param managedRepositoryConfiguration the repository configuration
525 * @return the updated or created repository
526 * @throws RepositoryException if an error occurs, or the configuration is not valid.
529 public ManagedRepository putRepository( ManagedRepositoryConfiguration managedRepositoryConfiguration ) throws RepositoryException
531 rwLock.writeLock( ).lock( );
534 return managedRepositoryHandler.put( managedRepositoryConfiguration );
538 rwLock.writeLock( ).unlock( );
544 * Adds a new repository or updates the repository with the same id. The given configuration object is updated, but
545 * the configuration is not saved.
547 * @param managedRepositoryConfiguration the new or changed managed repository configuration
548 * @param configuration the configuration object (may be <code>null</code>)
549 * @return the new or updated repository
550 * @throws RepositoryException if the configuration cannot be saved or updated
553 public ManagedRepository putRepository( ManagedRepositoryConfiguration managedRepositoryConfiguration, Configuration configuration ) throws RepositoryException
555 rwLock.writeLock( ).lock( );
558 return managedRepositoryHandler.put( managedRepositoryConfiguration, configuration );
562 rwLock.writeLock( ).unlock( );
567 public CheckedResult<ManagedRepository, Map<String, List<ValidationError>>> putRepositoryAndValidate( ManagedRepositoryConfiguration configuration ) throws RepositoryException
569 rwLock.writeLock().lock();
571 return managedRepositoryHandler.putWithCheck( configuration );
574 rwLock.writeLock( ).unlock( );
579 * Adds a new repository group to the current list, or replaces the repository group definition with
580 * the same id, if it exists already.
581 * The change is saved to the configuration immediately.
583 * @param repositoryGroup the new repository group.
584 * @throws RepositoryException if the new repository group could not be saved to the configuration.
587 public RepositoryGroup putRepositoryGroup( RepositoryGroup repositoryGroup ) throws RepositoryException
589 rwLock.writeLock( ).lock( );
592 if ( this.groupHandler == null )
594 throw new RepositoryException( "Fatal error. RepositoryGroupHandler not registered!" );
596 return this.groupHandler.put( repositoryGroup );
600 rwLock.writeLock( ).unlock( );
605 * Adds a new repository group or updates the repository with the same id, if it exists already.
606 * The configuration is saved immediately.
608 * @param repositoryGroupConfiguration the repository configuration
609 * @return the updated or created repository
610 * @throws RepositoryException if an error occurs, or the configuration is not valid.
613 public RepositoryGroup putRepositoryGroup( RepositoryGroupConfiguration repositoryGroupConfiguration ) throws RepositoryException
615 rwLock.writeLock( ).lock( );
618 return groupHandler.put( repositoryGroupConfiguration );
622 rwLock.writeLock( ).unlock( );
628 public CheckedResult<RepositoryGroup, Map<String, List<ValidationError>>> putRepositoryGroupAndValidate( RepositoryGroupConfiguration repositoryGroupConfiguration )
629 throws RepositoryException
631 rwLock.writeLock( ).lock( );
634 return groupHandler.putWithCheck( repositoryGroupConfiguration );
638 rwLock.writeLock( ).unlock( );
643 * Adds a new repository group or updates the repository group with the same id. The given configuration object is updated, but
644 * the configuration is not saved.
646 * @param repositoryGroupConfiguration The configuration of the new or changed repository group.
647 * @param configuration The configuration object. If it is <code>null</code>, the configuration is not saved.
648 * @return The new or updated repository group
649 * @throws RepositoryException if the configuration cannot be saved or updated
652 public RepositoryGroup putRepositoryGroup( RepositoryGroupConfiguration repositoryGroupConfiguration, Configuration configuration ) throws RepositoryException
654 rwLock.writeLock( ).lock( );
657 return groupHandler.put( repositoryGroupConfiguration, configuration );
661 rwLock.writeLock( ).unlock( );
667 * Adds a remote repository, or overwrites the repository definition with the same id, if it exists already.
668 * The modification is saved to the configuration immediately.
670 * @param remoteRepository the remote repository to add
671 * @throws RepositoryException if an error occurs during configuration save
674 public RemoteRepository putRepository( RemoteRepository remoteRepository ) throws RepositoryException
676 rwLock.writeLock( ).lock( );
679 return remoteRepositoryHandler.put( remoteRepository );
683 rwLock.writeLock( ).unlock( );
688 * Adds a new repository or updates the repository with the same id, if it exists already.
689 * The configuration is saved immediately.
691 * @param remoteRepositoryConfiguration the repository configuration
692 * @return the updated or created repository
693 * @throws RepositoryException if an error occurs, or the configuration is not valid.
696 public RemoteRepository putRepository( RemoteRepositoryConfiguration remoteRepositoryConfiguration ) throws RepositoryException
698 rwLock.writeLock( ).lock( );
701 return remoteRepositoryHandler.put( remoteRepositoryConfiguration );
705 rwLock.writeLock( ).unlock( );
710 public CheckedResult<RemoteRepository, Map<String, List<ValidationError>>> putRepositoryAndValidate( RemoteRepositoryConfiguration remoteRepositoryConfiguration ) throws RepositoryException
712 rwLock.writeLock().lock();
714 return remoteRepositoryHandler.putWithCheck( remoteRepositoryConfiguration );
717 rwLock.writeLock().unlock();
722 * Adds a new repository or updates the repository with the same id. The given configuration object is updated, but
723 * the configuration is not saved.
725 * @param remoteRepositoryConfiguration the new or changed repository configuration
726 * @param configuration the configuration object
727 * @return the new or updated repository
728 * @throws RepositoryException if the configuration cannot be saved or updated
731 public RemoteRepository putRepository( RemoteRepositoryConfiguration remoteRepositoryConfiguration, Configuration configuration ) throws RepositoryException
733 rwLock.writeLock( ).lock( );
736 return remoteRepositoryHandler.put( remoteRepositoryConfiguration, configuration );
740 rwLock.writeLock( ).unlock( );
745 public void removeRepository( String repoId ) throws RepositoryException
747 Repository repo = getRepository( repoId );
750 removeRepository( repo );
755 public void removeRepository( Repository repo ) throws RepositoryException
759 log.warn( "Trying to remove null repository" );
762 if ( repo instanceof RemoteRepository )
764 removeRepository( (RemoteRepository) repo );
766 else if ( repo instanceof ManagedRepository )
768 removeRepository( (ManagedRepository) repo );
770 else if ( repo instanceof RepositoryGroup )
772 removeRepositoryGroup( (RepositoryGroup) repo );
776 throw new RepositoryException( "Repository type not known: " + repo.getClass( ) );
781 * Removes a managed repository from the registry and configuration, if it exists.
782 * The change is saved to the configuration immediately.
784 * @param managedRepository the managed repository to remove
785 * @throws RepositoryException if a error occurs during configuration save
788 public void removeRepository( ManagedRepository managedRepository ) throws RepositoryException
790 if ( managedRepository == null )
794 rwLock.writeLock( ).lock( );
797 if (managed_initialized.get() ) managedRepositoryHandler.remove( managedRepository.getId( ) );
801 rwLock.writeLock( ).unlock( );
807 public void removeRepository( ManagedRepository managedRepository, Configuration configuration ) throws RepositoryException
809 if ( managedRepository == null )
813 rwLock.writeLock( ).lock( );
816 if (managed_initialized.get()) managedRepositoryHandler.remove( managedRepository.getId( ), configuration );
820 rwLock.writeLock( ).unlock( );
827 * Removes a repository group from the registry and configuration, if it exists.
828 * The change is saved to the configuration immediately.
830 * @param repositoryGroup the repository group to remove
831 * @throws RepositoryException if a error occurs during configuration save
834 public void removeRepositoryGroup( RepositoryGroup repositoryGroup ) throws RepositoryException
836 if ( repositoryGroup == null )
840 final String id = repositoryGroup.getId( );
841 if ( groupHandler.hasRepository( id ) )
843 rwLock.writeLock( ).lock( );
846 groupHandler.remove( id );
850 rwLock.writeLock( ).unlock( );
856 public void removeRepositoryGroup( RepositoryGroup repositoryGroup, Configuration configuration ) throws RepositoryException
858 if ( repositoryGroup == null )
862 final String id = repositoryGroup.getId( );
863 if ( groupHandler.hasRepository( id ) )
865 rwLock.writeLock( ).lock( );
868 groupHandler.remove( id, configuration );
872 rwLock.writeLock( ).unlock( );
878 * Removes the remote repository from the registry and configuration.
879 * The change is saved to the configuration immediately.
881 * @param remoteRepository the remote repository to remove
882 * @throws RepositoryException if a error occurs during configuration save
885 public void removeRepository( RemoteRepository remoteRepository ) throws RepositoryException
887 if ( remoteRepository == null )
891 final String id = remoteRepository.getId( );
892 if ( remoteRepositoryHandler.hasRepository( id ) )
894 rwLock.writeLock( ).lock( );
897 remoteRepositoryHandler.remove( id );
901 rwLock.writeLock( ).unlock( );
907 public void removeRepository( RemoteRepository remoteRepository, Configuration configuration ) throws RepositoryException
909 if ( remoteRepository == null )
913 final String id = remoteRepository.getId( );
914 if ( remoteRepositoryHandler.hasRepository( id ) )
916 rwLock.writeLock( ).lock( );
919 remoteRepositoryHandler.remove( id, configuration );
923 rwLock.writeLock( ).unlock( );
929 * Reloads the registry from the configuration.
932 public void reload( )
938 * Resets the indexing context of a given repository.
940 * @param repository The repository
941 * @throws IndexUpdateFailedException If the index could not be resetted.
944 public void resetIndexingContext( Repository repository ) throws IndexUpdateFailedException
946 if ( repository.hasIndex( ) && repository instanceof EditableRepository )
948 EditableRepository eRepo = (EditableRepository) repository;
949 ArchivaIndexingContext newCtx = getIndexManager( repository.getType( ) ).reset( repository.getIndexingContext( ) );
950 eRepo.setIndexingContext( newCtx );
956 * Creates a new repository instance with the same settings as this one. The cloned repository is not
957 * registered or saved to the configuration.
959 * @param repo The origin repository
960 * @return The cloned repository.
962 public ManagedRepository clone( ManagedRepository repo, String newId ) throws RepositoryException
964 if (isRegisteredId( newId )) {
965 throw new RepositoryException( "The new id exists already: " + newId );
967 return managedRepositoryHandler.clone( repo, newId );
971 * Creates a new repository instance with the same settings as this one. The cloned repository is not
972 * registered or saved to the configuration.
974 * @param repo The origin repository
975 * @return The cloned repository.
977 public RemoteRepository clone( RemoteRepository repo, String newId ) throws RepositoryException
979 if (isRegisteredId( newId )) {
980 throw new RepositoryException( "The new id exists already: " + newId );
982 return remoteRepositoryHandler.clone( repo, newId );
985 @SuppressWarnings( "unchecked" )
987 public <T extends Repository> T clone( T repo, String newId ) throws RepositoryException
989 if (isRegisteredId( newId )) {
990 throw new RepositoryException( "The new id exists already: " + newId );
992 if ( repo instanceof RemoteRepository )
994 return (T) remoteRepositoryHandler.clone( (RemoteRepository) repo, newId );
996 else if ( repo instanceof ManagedRepository )
998 return (T) managedRepositoryHandler.clone( (ManagedRepository) repo, newId );
1000 else if (repo instanceof RepositoryGroup) {
1001 return (T) groupHandler.clone( (RepositoryGroup) repo, newId );
1005 throw new RepositoryException( "This repository class is not supported " + repo.getClass( ).getName( ) );
1011 public Repository getRepositoryOfAsset( StorageAsset asset )
1013 if ( asset instanceof Repository )
1015 return (Repository) asset;
1019 return getRepositories( ).stream( ).filter( r -> r.getRoot( )
1020 .getStorage( ).equals( asset.getStorage( ) ) ).findFirst( ).orElse( null );
1025 public <R extends Repository> ValidationResponse<R> validateRepository( R repository )
1027 @SuppressWarnings( "unchecked" ) Map<String, List<ValidationError>> errorMap = this.validators.stream( )
1028 .filter( ( validator ) -> validator.getType( ).equals( RepositoryType.ALL ) || repository.getType( ).equals( validator.getType( ) ) )
1029 .filter( val -> val.isFlavour( repository.getClass( ) ) )
1030 .flatMap( validator -> ( (RepositoryValidator<R>) validator ).apply( repository ).getResult( ).entrySet( ).stream( ) )
1031 .collect( Collectors.toMap(
1033 Map.Entry::getValue,
1036 return new ValidationResponse<>( repository, errorMap );
1040 public <R extends Repository> ValidationResponse<R> validateRepositoryForUpdate( R repository )
1042 @SuppressWarnings( "unchecked" ) Map<String, List<ValidationError>> errorMap = this.validators.stream( )
1043 .filter( ( validator ) -> validator.getType( ).equals( RepositoryType.ALL ) || repository.getType( ).equals( validator.getType( ) ) )
1044 .filter( val -> val.isFlavour( repository.getClass( ) ) )
1045 .flatMap( validator -> ( (RepositoryValidator<R>) validator ).applyForUpdate( repository ).getResult( ).entrySet( ).stream( ) )
1046 .collect( Collectors.toMap(
1048 Map.Entry::getValue,
1051 return new ValidationResponse<>( repository, errorMap );
1055 public void configurationEvent( ConfigurationEvent event )
1057 // We ignore the event, if the save was triggered by ourself
1058 if ( !ConfigurationHandler.REGISTRY_EVENT_TAG.equals( event.getTag( ) ) )
1066 public <T extends Event> void registerEventHandler( EventType<T> type, EventHandler<? super T> eventHandler )
1068 eventManager.registerEventHandler( type, eventHandler );
1073 public <T extends Event> void unregisterEventHandler( EventType<T> type, EventHandler<? super T> eventHandler )
1075 eventManager.unregisterEventHandler( type, eventHandler );
1080 public void handle( Event event )
1082 // To avoid event cycles:
1083 if ( sameOriginator( event ) )
1087 if ( event instanceof RepositoryIndexEvent )
1089 handleIndexCreationEvent( (RepositoryIndexEvent) event );
1091 // We propagate all events to our listeners, but with context of repository registry
1095 private void handleIndexCreationEvent( RepositoryIndexEvent event )
1097 if (!ignoreIndexing && !( event.getRepository() instanceof ManagedRepository ))
1099 EditableRepository repo = (EditableRepository) event.getRepository( );
1102 ArchivaIndexManager idxmgr = getIndexManager( repo.getType( ) );
1103 if ( repo.getIndexingContext( ) != null )
1107 ArchivaIndexingContext newCtx = idxmgr.move( repo.getIndexingContext( ), repo );
1108 repo.setIndexingContext( newCtx );
1109 idxmgr.updateLocalIndexPath( repo );
1112 catch ( IndexCreationFailedException e )
1114 log.error( "Could not move index to new directory: '{}'", e.getMessage( ), e );
1121 ArchivaIndexingContext context = idxmgr.createContext( repo );
1122 repo.setIndexingContext( context );
1123 idxmgr.updateLocalIndexPath( repo );
1125 catch ( IndexCreationFailedException e )
1127 log.error( "Could not create index: '{}'", e.getMessage( ), e );
1134 private boolean sameOriginator( Event event )
1136 if ( event.getSource( ) == this )
1140 else if ( event.hasPreviousEvent( ) )
1142 return sameOriginator( event.getPreviousEvent( ) );
1150 private void pushEvent( Event event )
1152 eventManager.fireEvent( event );
1155 private <R extends Repository, C extends AbstractRepositoryConfiguration> void doRegister( RepositoryHandler<R, C> repositoryHandler )
1157 repositoryHandler.setRepositoryProviders( this.repositoryProviders );
1158 repositoryHandler.setRepositoryValidator( this.repositoryValidatorList );
1161 @SuppressWarnings( "unchecked" )
1163 public void registerHandler( RepositoryHandler<?, ?> handler )
1165 if ( handler.getVariant( ).isAssignableFrom( RepositoryGroup.class ) )
1167 registerGroupHandler( (RepositoryHandler<RepositoryGroup, RepositoryGroupConfiguration>) handler );
1169 else if ( handler.getVariant( ).isAssignableFrom( ManagedRepository.class ) )
1171 registerManagedRepositoryHandler( (RepositoryHandler<ManagedRepository, ManagedRepositoryConfiguration>) handler );
1173 else if ( handler.getVariant().isAssignableFrom( RemoteRepository.class )) {
1174 registerRemoteRepositoryHandler( (RepositoryHandler<RemoteRepository, RemoteRepositoryConfiguration>) handler );
1179 public boolean isRegisteredId( String id )
1181 return hasRepository( id );
1184 @SuppressWarnings( "unchecked" )
1186 public <R extends Repository, C extends AbstractRepositoryConfiguration> RepositoryHandler<R, C> getHandler( Class<R> repositoryClazz, Class<C> configurationClazz )
1188 if ( repositoryClazz.isAssignableFrom( RepositoryGroup.class ) )
1190 return (RepositoryHandler<R, C>) this.groupHandler;
1192 else if ( repositoryClazz.isAssignableFrom( ManagedRepository.class ) )
1194 return (RepositoryHandler<R, C>) this.managedRepositoryHandler;
1196 else if ( repositoryClazz.isAssignableFrom( RemoteRepository.class )) {
1197 return (RepositoryHandler<R, C>) this.remoteRepositoryHandler;
1205 public boolean isIgnoreIndexing( )
1207 return ignoreIndexing;
1210 public void setIgnoreIndexing( boolean ignoreIndexing )
1212 this.ignoreIndexing = ignoreIndexing;