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.AbstractRepositoryConfiguration;
24 import org.apache.archiva.configuration.ArchivaConfiguration;
25 import org.apache.archiva.configuration.Configuration;
26 import org.apache.archiva.configuration.ConfigurationEvent;
27 import org.apache.archiva.configuration.ConfigurationListener;
28 import org.apache.archiva.configuration.IndeterminateConfigurationException;
29 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
30 import org.apache.archiva.configuration.RemoteRepositoryConfiguration;
31 import org.apache.archiva.configuration.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( );
568 * Adds a new repository group to the current list, or replaces the repository group definition with
569 * the same id, if it exists already.
570 * The change is saved to the configuration immediately.
572 * @param repositoryGroup the new repository group.
573 * @throws RepositoryException if the new repository group could not be saved to the configuration.
576 public RepositoryGroup putRepositoryGroup( RepositoryGroup repositoryGroup ) throws RepositoryException
578 rwLock.writeLock( ).lock( );
581 if ( this.groupHandler == null )
583 throw new RepositoryException( "Fatal error. RepositoryGroupHandler not registered!" );
585 return this.groupHandler.put( repositoryGroup );
589 rwLock.writeLock( ).unlock( );
594 * Adds a new repository group or updates the repository with the same id, if it exists already.
595 * The configuration is saved immediately.
597 * @param repositoryGroupConfiguration the repository configuration
598 * @return the updated or created repository
599 * @throws RepositoryException if an error occurs, or the configuration is not valid.
602 public RepositoryGroup putRepositoryGroup( RepositoryGroupConfiguration repositoryGroupConfiguration ) throws RepositoryException
604 rwLock.writeLock( ).lock( );
607 return groupHandler.put( repositoryGroupConfiguration );
611 rwLock.writeLock( ).unlock( );
617 public CheckedResult<RepositoryGroup, Map<String, List<ValidationError>>> putRepositoryGroupAndValidate( RepositoryGroupConfiguration repositoryGroupConfiguration )
618 throws RepositoryException
620 rwLock.writeLock( ).lock( );
623 return groupHandler.putWithCheck( repositoryGroupConfiguration, groupHandler.getValidator( ) );
627 rwLock.writeLock( ).unlock( );
632 * Adds a new repository group or updates the repository group with the same id. The given configuration object is updated, but
633 * the configuration is not saved.
635 * @param repositoryGroupConfiguration The configuration of the new or changed repository group.
636 * @param configuration The configuration object. If it is <code>null</code>, the configuration is not saved.
637 * @return The new or updated repository group
638 * @throws RepositoryException if the configuration cannot be saved or updated
641 public RepositoryGroup putRepositoryGroup( RepositoryGroupConfiguration repositoryGroupConfiguration, Configuration configuration ) throws RepositoryException
643 rwLock.writeLock( ).lock( );
646 return groupHandler.put( repositoryGroupConfiguration, configuration );
650 rwLock.writeLock( ).unlock( );
656 * Adds a remote repository, or overwrites the repository definition with the same id, if it exists already.
657 * The modification is saved to the configuration immediately.
659 * @param remoteRepository the remote repository to add
660 * @throws RepositoryException if an error occurs during configuration save
663 public RemoteRepository putRepository( RemoteRepository remoteRepository ) throws RepositoryException
665 rwLock.writeLock( ).lock( );
668 return remoteRepositoryHandler.put( remoteRepository );
672 rwLock.writeLock( ).unlock( );
677 * Adds a new repository or updates the repository with the same id, if it exists already.
678 * The configuration is saved immediately.
680 * @param remoteRepositoryConfiguration the repository configuration
681 * @return the updated or created repository
682 * @throws RepositoryException if an error occurs, or the configuration is not valid.
685 public RemoteRepository putRepository( RemoteRepositoryConfiguration remoteRepositoryConfiguration ) throws RepositoryException
687 rwLock.writeLock( ).lock( );
690 return remoteRepositoryHandler.put( remoteRepositoryConfiguration );
694 rwLock.writeLock( ).unlock( );
699 * Adds a new repository or updates the repository with the same id. The given configuration object is updated, but
700 * the configuration is not saved.
702 * @param remoteRepositoryConfiguration the new or changed repository configuration
703 * @param configuration the configuration object
704 * @return the new or updated repository
705 * @throws RepositoryException if the configuration cannot be saved or updated
708 public RemoteRepository putRepository( RemoteRepositoryConfiguration remoteRepositoryConfiguration, Configuration configuration ) throws RepositoryException
710 rwLock.writeLock( ).lock( );
713 return remoteRepositoryHandler.put( remoteRepositoryConfiguration, configuration );
717 rwLock.writeLock( ).unlock( );
722 public void removeRepository( String repoId ) throws RepositoryException
724 Repository repo = getRepository( repoId );
727 removeRepository( repo );
732 public void removeRepository( Repository repo ) throws RepositoryException
736 log.warn( "Trying to remove null repository" );
739 if ( repo instanceof RemoteRepository )
741 removeRepository( (RemoteRepository) repo );
743 else if ( repo instanceof ManagedRepository )
745 removeRepository( (ManagedRepository) repo );
747 else if ( repo instanceof RepositoryGroup )
749 removeRepositoryGroup( (RepositoryGroup) repo );
753 throw new RepositoryException( "Repository type not known: " + repo.getClass( ) );
758 * Removes a managed repository from the registry and configuration, if it exists.
759 * The change is saved to the configuration immediately.
761 * @param managedRepository the managed repository to remove
762 * @throws RepositoryException if a error occurs during configuration save
765 public void removeRepository( ManagedRepository managedRepository ) throws RepositoryException
767 if ( managedRepository == null )
771 rwLock.writeLock( ).lock( );
774 if (managed_initialized.get() ) managedRepositoryHandler.remove( managedRepository.getId( ) );
778 rwLock.writeLock( ).unlock( );
784 public void removeRepository( ManagedRepository managedRepository, Configuration configuration ) throws RepositoryException
786 if ( managedRepository == null )
790 rwLock.writeLock( ).lock( );
793 if (managed_initialized.get()) managedRepositoryHandler.remove( managedRepository.getId( ), configuration );
797 rwLock.writeLock( ).unlock( );
804 * Removes a repository group from the registry and configuration, if it exists.
805 * The change is saved to the configuration immediately.
807 * @param repositoryGroup the repository group to remove
808 * @throws RepositoryException if a error occurs during configuration save
811 public void removeRepositoryGroup( RepositoryGroup repositoryGroup ) throws RepositoryException
813 if ( repositoryGroup == null )
817 final String id = repositoryGroup.getId( );
818 if ( groupHandler.hasRepository( id ) )
820 rwLock.writeLock( ).lock( );
823 groupHandler.remove( id );
827 rwLock.writeLock( ).unlock( );
833 public void removeRepositoryGroup( RepositoryGroup repositoryGroup, Configuration configuration ) throws RepositoryException
835 if ( repositoryGroup == null )
839 final String id = repositoryGroup.getId( );
840 if ( groupHandler.hasRepository( id ) )
842 rwLock.writeLock( ).lock( );
845 groupHandler.remove( id, configuration );
849 rwLock.writeLock( ).unlock( );
855 * Removes the remote repository from the registry and configuration.
856 * The change is saved to the configuration immediately.
858 * @param remoteRepository the remote repository to remove
859 * @throws RepositoryException if a error occurs during configuration save
862 public void removeRepository( RemoteRepository remoteRepository ) throws RepositoryException
864 if ( remoteRepository == null )
868 final String id = remoteRepository.getId( );
869 if ( remoteRepositoryHandler.hasRepository( id ) )
871 rwLock.writeLock( ).lock( );
874 remoteRepositoryHandler.remove( id );
878 rwLock.writeLock( ).unlock( );
884 public void removeRepository( RemoteRepository remoteRepository, Configuration configuration ) throws RepositoryException
886 if ( remoteRepository == null )
890 final String id = remoteRepository.getId( );
891 if ( remoteRepositoryHandler.hasRepository( id ) )
893 rwLock.writeLock( ).lock( );
896 remoteRepositoryHandler.remove( id, configuration );
900 rwLock.writeLock( ).unlock( );
906 * Reloads the registry from the configuration.
909 public void reload( )
915 * Resets the indexing context of a given repository.
917 * @param repository The repository
918 * @throws IndexUpdateFailedException If the index could not be resetted.
921 public void resetIndexingContext( Repository repository ) throws IndexUpdateFailedException
923 if ( repository.hasIndex( ) && repository instanceof EditableRepository )
925 EditableRepository eRepo = (EditableRepository) repository;
926 ArchivaIndexingContext newCtx = getIndexManager( repository.getType( ) ).reset( repository.getIndexingContext( ) );
927 eRepo.setIndexingContext( newCtx );
933 * Creates a new repository instance with the same settings as this one. The cloned repository is not
934 * registered or saved to the configuration.
936 * @param repo The origin repository
937 * @return The cloned repository.
939 public ManagedRepository clone( ManagedRepository repo, String newId ) throws RepositoryException
941 if (isRegisteredId( newId )) {
942 throw new RepositoryException( "The new id exists already: " + newId );
944 return managedRepositoryHandler.clone( repo, newId );
948 * Creates a new repository instance with the same settings as this one. The cloned repository is not
949 * registered or saved to the configuration.
951 * @param repo The origin repository
952 * @return The cloned repository.
954 public RemoteRepository clone( RemoteRepository repo, String newId ) throws RepositoryException
956 if (isRegisteredId( newId )) {
957 throw new RepositoryException( "The new id exists already: " + newId );
959 return remoteRepositoryHandler.clone( repo, newId );
962 @SuppressWarnings( "unchecked" )
964 public <T extends Repository> T clone( T repo, String newId ) throws RepositoryException
966 if (isRegisteredId( newId )) {
967 throw new RepositoryException( "The new id exists already: " + newId );
969 if ( repo instanceof RemoteRepository )
971 return (T) remoteRepositoryHandler.clone( (RemoteRepository) repo, newId );
973 else if ( repo instanceof ManagedRepository )
975 return (T) managedRepositoryHandler.clone( (ManagedRepository) repo, newId );
977 else if (repo instanceof RepositoryGroup) {
978 return (T) groupHandler.clone( (RepositoryGroup) repo, newId );
982 throw new RepositoryException( "This repository class is not supported " + repo.getClass( ).getName( ) );
988 public Repository getRepositoryOfAsset( StorageAsset asset )
990 if ( asset instanceof Repository )
992 return (Repository) asset;
996 return getRepositories( ).stream( ).filter( r -> r.getRoot( )
997 .getStorage( ).equals( asset.getStorage( ) ) ).findFirst( ).orElse( null );
1002 public <R extends Repository> ValidationResponse<R> validateRepository( R repository )
1004 @SuppressWarnings( "unchecked" ) Map<String, List<ValidationError>> errorMap = this.validators.stream( )
1005 .filter( ( validator ) -> validator.getType( ).equals( RepositoryType.ALL ) || repository.getType( ).equals( validator.getType( ) ) )
1006 .filter( val -> val.isFlavour( repository.getClass( ) ) )
1007 .flatMap( validator -> ( (RepositoryValidator<R>) validator ).apply( repository ).getResult( ).entrySet( ).stream( ) )
1008 .collect( Collectors.toMap(
1010 Map.Entry::getValue,
1013 return new ValidationResponse<>( repository, errorMap );
1017 public <R extends Repository> ValidationResponse<R> validateRepositoryForUpdate( R repository )
1019 @SuppressWarnings( "unchecked" ) Map<String, List<ValidationError>> errorMap = this.validators.stream( )
1020 .filter( ( validator ) -> validator.getType( ).equals( RepositoryType.ALL ) || repository.getType( ).equals( validator.getType( ) ) )
1021 .filter( val -> val.isFlavour( repository.getClass( ) ) )
1022 .flatMap( validator -> ( (RepositoryValidator<R>) validator ).applyForUpdate( repository ).getResult( ).entrySet( ).stream( ) )
1023 .collect( Collectors.toMap(
1025 Map.Entry::getValue,
1028 return new ValidationResponse<>( repository, errorMap );
1032 public void configurationEvent( ConfigurationEvent event )
1034 // We ignore the event, if the save was triggered by ourself
1035 if ( !ConfigurationHandler.REGISTRY_EVENT_TAG.equals( event.getTag( ) ) )
1043 public <T extends Event> void registerEventHandler( EventType<T> type, EventHandler<? super T> eventHandler )
1045 eventManager.registerEventHandler( type, eventHandler );
1050 public <T extends Event> void unregisterEventHandler( EventType<T> type, EventHandler<? super T> eventHandler )
1052 eventManager.unregisterEventHandler( type, eventHandler );
1057 public void handle( Event event )
1059 // To avoid event cycles:
1060 if ( sameOriginator( event ) )
1064 if ( event instanceof RepositoryIndexEvent )
1066 handleIndexCreationEvent( (RepositoryIndexEvent) event );
1068 // We propagate all events to our listeners, but with context of repository registry
1072 private void handleIndexCreationEvent( RepositoryIndexEvent event )
1074 if (!ignoreIndexing && !( event.getRepository() instanceof ManagedRepository ))
1076 EditableRepository repo = (EditableRepository) event.getRepository( );
1079 ArchivaIndexManager idxmgr = getIndexManager( repo.getType( ) );
1080 if ( repo.getIndexingContext( ) != null )
1084 ArchivaIndexingContext newCtx = idxmgr.move( repo.getIndexingContext( ), repo );
1085 repo.setIndexingContext( newCtx );
1086 idxmgr.updateLocalIndexPath( repo );
1089 catch ( IndexCreationFailedException e )
1091 log.error( "Could not move index to new directory: '{}'", e.getMessage( ), e );
1098 ArchivaIndexingContext context = idxmgr.createContext( repo );
1099 repo.setIndexingContext( context );
1100 idxmgr.updateLocalIndexPath( repo );
1102 catch ( IndexCreationFailedException e )
1104 log.error( "Could not create index: '{}'", e.getMessage( ), e );
1111 private boolean sameOriginator( Event event )
1113 if ( event.getSource( ) == this )
1117 else if ( event.hasPreviousEvent( ) )
1119 return sameOriginator( event.getPreviousEvent( ) );
1127 private void pushEvent( Event event )
1129 eventManager.fireEvent( event );
1132 private <R extends Repository, C extends AbstractRepositoryConfiguration> void doRegister( RepositoryHandler<R, C> repositoryHandler )
1134 repositoryHandler.setRepositoryProviders( this.repositoryProviders );
1135 repositoryHandler.setRepositoryValidator( this.repositoryValidatorList );
1138 @SuppressWarnings( "unchecked" )
1140 public void registerHandler( RepositoryHandler<?, ?> handler )
1142 if ( handler.getVariant( ).isAssignableFrom( RepositoryGroup.class ) )
1144 registerGroupHandler( (RepositoryHandler<RepositoryGroup, RepositoryGroupConfiguration>) handler );
1146 else if ( handler.getVariant( ).isAssignableFrom( ManagedRepository.class ) )
1148 registerManagedRepositoryHandler( (RepositoryHandler<ManagedRepository, ManagedRepositoryConfiguration>) handler );
1150 else if ( handler.getVariant().isAssignableFrom( RemoteRepository.class )) {
1151 registerRemoteRepositoryHandler( (RepositoryHandler<RemoteRepository, RemoteRepositoryConfiguration>) handler );
1156 public boolean isRegisteredId( String id )
1158 return hasRepository( id );
1161 @SuppressWarnings( "unchecked" )
1163 public <R extends Repository, C extends AbstractRepositoryConfiguration> RepositoryHandler<R, C> getHandler( Class<R> repositoryClazz, Class<C> configurationClazz )
1165 if ( repositoryClazz.isAssignableFrom( RepositoryGroup.class ) )
1167 return (RepositoryHandler<R, C>) this.groupHandler;
1169 else if ( repositoryClazz.isAssignableFrom( ManagedRepository.class ) )
1171 return (RepositoryHandler<R, C>) this.managedRepositoryHandler;
1173 else if ( repositoryClazz.isAssignableFrom( RemoteRepository.class )) {
1174 return (RepositoryHandler<R, C>) this.remoteRepositoryHandler;
1182 public boolean isIgnoreIndexing( )
1184 return ignoreIndexing;
1187 public void setIgnoreIndexing( boolean ignoreIndexing )
1189 this.ignoreIndexing = ignoreIndexing;