1 package org.apache.archiva.repository.base.group;
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
20 import org.apache.archiva.components.registry.RegistryException;
21 import org.apache.archiva.configuration.Configuration;
22 import org.apache.archiva.configuration.IndeterminateConfigurationException;
23 import org.apache.archiva.configuration.RepositoryGroupConfiguration;
24 import org.apache.archiva.indexer.merger.MergedRemoteIndexesScheduler;
25 import org.apache.archiva.repository.RepositoryState;
26 import org.apache.archiva.repository.base.AbstractRepositoryHandler;
27 import org.apache.archiva.repository.base.ArchivaRepositoryRegistry;
28 import org.apache.archiva.repository.base.ConfigurationHandler;
29 import org.apache.archiva.repository.validation.CheckedResult;
30 import org.apache.archiva.repository.EditableRepository;
31 import org.apache.archiva.repository.EditableRepositoryGroup;
32 import org.apache.archiva.repository.ManagedRepository;
33 import org.apache.archiva.repository.Repository;
34 import org.apache.archiva.repository.RepositoryException;
35 import org.apache.archiva.repository.RepositoryGroup;
36 import org.apache.archiva.repository.RepositoryHandler;
37 import org.apache.archiva.repository.RepositoryProvider;
38 import org.apache.archiva.repository.RepositoryType;
39 import org.apache.archiva.repository.event.RepositoryEvent;
40 import org.apache.archiva.repository.features.IndexCreationFeature;
41 import org.apache.archiva.repository.storage.StorageAsset;
42 import org.apache.archiva.repository.validation.RepositoryChecker;
43 import org.apache.archiva.repository.validation.RepositoryValidator;
44 import org.apache.archiva.repository.validation.ValidationError;
45 import org.apache.commons.lang3.StringUtils;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48 import org.springframework.stereotype.Service;
50 import javax.annotation.PostConstruct;
51 import javax.annotation.PreDestroy;
52 import javax.inject.Named;
53 import java.io.IOException;
54 import java.nio.file.Files;
55 import java.nio.file.Path;
56 import java.util.Collection;
57 import java.util.Collections;
58 import java.util.HashMap;
59 import java.util.Iterator;
60 import java.util.LinkedHashMap;
61 import java.util.List;
63 import java.util.Optional;
64 import java.util.concurrent.locks.ReentrantReadWriteLock;
65 import java.util.stream.Collectors;
67 import static org.apache.archiva.indexer.ArchivaIndexManager.DEFAULT_INDEX_PATH;
70 * This class manages repository groups for the RepositoryRegistry.
71 * It is tightly coupled with the {@link ArchivaRepositoryRegistry}.
73 * @author Martin Stockhammer <martin_s@apache.org>
75 @Service( "repositoryGroupHandler#default" )
76 public class RepositoryGroupHandler
77 extends AbstractRepositoryHandler<RepositoryGroup, RepositoryGroupConfiguration>
78 implements RepositoryHandler<RepositoryGroup, RepositoryGroupConfiguration>
80 private static final Logger log = LoggerFactory.getLogger( RepositoryGroupHandler.class );
82 private final ArchivaRepositoryRegistry repositoryRegistry;
83 private final ConfigurationHandler configurationHandler;
84 private final MergedRemoteIndexesScheduler mergedRemoteIndexesScheduler;
86 private final Map<String, RepositoryGroup> repositoryGroups = new HashMap<>( );
87 private final RepositoryValidator<RepositoryGroup> validator;
89 private Path groupsDirectory;
93 * Creates a new instance. All dependencies are injected on the constructor.
95 * @param repositoryRegistry the registry. To avoid circular dependencies via DI, this class registers itself on the registry.
96 * @param configurationHandler the configuration handler is used to retrieve and save configuration.
97 * @param mergedRemoteIndexesScheduler the index scheduler is used for merging the indexes from all group members
98 * @param repositoryValidatorList the list of validators that are registered
100 public RepositoryGroupHandler( ArchivaRepositoryRegistry repositoryRegistry,
101 ConfigurationHandler configurationHandler,
102 @Named( "mergedRemoteIndexesScheduler#default" ) MergedRemoteIndexesScheduler mergedRemoteIndexesScheduler,
103 List<RepositoryValidator<? extends Repository>> repositoryValidatorList
106 this.configurationHandler = configurationHandler;
107 this.mergedRemoteIndexesScheduler = mergedRemoteIndexesScheduler;
108 this.repositoryRegistry = repositoryRegistry;
109 this.validator = getCombinedValidatdor( RepositoryGroup.class, repositoryValidatorList );
116 log.debug( "Initializing repository group handler " + repositoryRegistry.toString( ) );
117 initializeStorage( );
118 // We are registering this class on the registry. This is necessary to avoid circular dependencies via injection.
119 this.repositoryRegistry.registerGroupHandler( this );
123 public void initializeFromConfig( )
125 this.repositoryGroups.clear( );
126 this.repositoryGroups.putAll( newInstancesFromConfig( ) );
127 for ( RepositoryGroup group : this.repositoryGroups.values( ) )
129 activateRepository( group );
133 private void initializeStorage( )
135 Path baseDir = this.configurationHandler.getArchivaConfiguration( ).getRepositoryGroupBaseDir( );
136 if ( !Files.exists( baseDir ) )
140 Files.createDirectories( baseDir );
142 catch ( IOException e )
144 log.error( "Could not create group base directory: {}", e.getMessage( ), e );
147 this.groupsDirectory = baseDir;
151 public void activateRepository( RepositoryGroup repositoryGroup )
153 StorageAsset indexDirectory = getMergedIndexDirectory( repositoryGroup );
154 if ( !indexDirectory.exists( ) )
158 indexDirectory.create( );
160 catch ( IOException e )
162 log.error( "Could not create index directory {} for group {}: {}", indexDirectory, repositoryGroup.getId( ), e.getMessage( ) );
165 Path groupPath = groupsDirectory.resolve( repositoryGroup.getId( ) );
166 if ( !Files.exists( groupPath ) )
170 Files.createDirectories( groupPath );
172 catch ( IOException e )
174 log.error( "Could not create repository group directory {}", groupPath );
177 mergedRemoteIndexesScheduler.schedule( repositoryGroup,
179 setLastState( repositoryGroup, RepositoryState.INITIALIZED );
182 public StorageAsset getMergedIndexDirectory( RepositoryGroup group )
186 return group.getFeature( IndexCreationFeature.class ).get( ).getLocalIndexPath( );
196 public Map<String, RepositoryGroup> newInstancesFromConfig( )
200 List<RepositoryGroupConfiguration> repositoryGroupConfigurations =
201 this.configurationHandler.getBaseConfiguration( ).getRepositoryGroups( );
203 if ( repositoryGroupConfigurations == null )
205 return Collections.emptyMap( );
208 Map<String, RepositoryGroup> repositoryGroupMap = new LinkedHashMap<>( repositoryGroupConfigurations.size( ) );
210 Map<RepositoryType, RepositoryProvider> providerMap = repositoryRegistry.getRepositoryProviderMap( );
211 for ( RepositoryGroupConfiguration repoConfig : repositoryGroupConfigurations )
213 RepositoryType repositoryType = RepositoryType.valueOf( repoConfig.getType( ) );
214 if ( providerMap.containsKey( repositoryType ) )
218 RepositoryGroup repo = createNewRepositoryGroup( providerMap.get( repositoryType ), repoConfig );
219 repositoryGroupMap.put( repo.getId( ), repo );
221 catch ( Exception e )
223 log.error( "Could not create repository group {}: {}", repoConfig.getId( ), e.getMessage( ), e );
227 return repositoryGroupMap;
229 catch ( Throwable e )
231 log.error( "Could not initialize repositories from config: {}", e.getMessage( ), e );
232 return Collections.emptyMap( );
237 public RepositoryGroup newInstance( final RepositoryType type, String id ) throws RepositoryException
239 RepositoryProvider provider = repositoryRegistry.getProvider( type );
240 RepositoryGroupConfiguration config = new RepositoryGroupConfiguration( );
242 return createNewRepositoryGroup( provider, config );
246 public RepositoryGroup newInstance( final RepositoryGroupConfiguration repositoryConfiguration ) throws RepositoryException
248 RepositoryType type = RepositoryType.valueOf( repositoryConfiguration.getType( ) );
249 RepositoryProvider provider = repositoryRegistry.getProvider( type );
250 return createNewRepositoryGroup( provider, repositoryConfiguration );
253 private RepositoryGroup createNewRepositoryGroup( RepositoryProvider provider, RepositoryGroupConfiguration config ) throws RepositoryException
255 RepositoryGroup repositoryGroup = provider.createRepositoryGroup( config );
256 updateReferences( repositoryGroup, config );
257 if (repositoryGroup instanceof EditableRepository)
259 ( (EditableRepository) repositoryGroup ).setLastState( RepositoryState.REFERENCES_SET );
261 return repositoryGroup;
265 * Adds a new repository group to the current list, or replaces the repository group definition with
266 * the same id, if it exists already.
267 * The change is saved to the configuration immediately.
269 * @param repositoryGroup the new repository group.
270 * @throws RepositoryException if the new repository group could not be saved to the configuration.
273 public RepositoryGroup put( final RepositoryGroup repositoryGroup ) throws RepositoryException
275 final String id = repositoryGroup.getId( );
276 RepositoryGroup originRepoGroup = repositoryGroups.remove( id );
279 if ( originRepoGroup != null && originRepoGroup != repositoryGroup )
281 this.mergedRemoteIndexesScheduler.unschedule( originRepoGroup );
282 originRepoGroup.close( );
284 RepositoryProvider provider = repositoryRegistry.getProvider( repositoryGroup.getType( ) );
285 RepositoryGroupConfiguration newCfg = provider.getRepositoryGroupConfiguration( repositoryGroup );
286 ReentrantReadWriteLock.WriteLock configLock = this.configurationHandler.getLock( ).writeLock( );
290 Configuration configuration = this.configurationHandler.getBaseConfiguration( );
291 updateReferences( repositoryGroup, newCfg );
292 RepositoryGroupConfiguration oldCfg = configuration.findRepositoryGroupById( id );
293 if ( oldCfg != null )
295 configuration.removeRepositoryGroup( oldCfg );
297 configuration.addRepositoryGroup( newCfg );
298 configurationHandler.save( configuration, ConfigurationHandler.REGISTRY_EVENT_TAG );
299 setLastState( repositoryGroup, RepositoryState.SAVED );
300 activateRepository( repositoryGroup );
304 configLock.unlock( );
306 repositoryGroups.put( id, repositoryGroup );
307 setLastState( repositoryGroup, RepositoryState.REGISTERED );
308 return repositoryGroup;
310 catch ( Exception e )
313 if ( originRepoGroup != null )
315 repositoryGroups.put( id, originRepoGroup );
319 repositoryGroups.remove( id );
321 log.error( "Exception during configuration update {}", e.getMessage( ), e );
322 throw new RepositoryException( "Could not save the configuration" + ( e.getMessage( ) == null ? "" : ": " + e.getMessage( ) ), e);
327 * Adds a new repository group or updates the repository with the same id, if it exists already.
328 * The configuration is saved immediately.
330 * @param repositoryGroupConfiguration the repository configuration
331 * @return the updated or created repository
332 * @throws RepositoryException if an error occurs, or the configuration is not valid.
335 public RepositoryGroup put( RepositoryGroupConfiguration repositoryGroupConfiguration ) throws RepositoryException
337 final String id = repositoryGroupConfiguration.getId( );
338 final RepositoryType repositoryType = RepositoryType.valueOf( repositoryGroupConfiguration.getType( ) );
339 final RepositoryProvider provider = repositoryRegistry.getProvider( repositoryType );
340 RepositoryGroup currentRepository;
341 ReentrantReadWriteLock.WriteLock configLock = this.configurationHandler.getLock( ).writeLock( );
345 Configuration configuration = this.configurationHandler.getBaseConfiguration( );
346 currentRepository = repositoryRegistry.getRepositoryGroup( id );
347 RepositoryGroup oldRepository = currentRepository == null ? null : clone( currentRepository );
351 if (currentRepository==null) {
352 currentRepository = put( repositoryGroupConfiguration, configuration );
355 setRepositoryGroupDefaults( repositoryGroupConfiguration );
356 provider.updateRepositoryGroupInstance( (EditableRepositoryGroup) currentRepository, repositoryGroupConfiguration );
358 configurationHandler.save( configuration, ConfigurationHandler.REGISTRY_EVENT_TAG );
359 updateReferences( currentRepository, repositoryGroupConfiguration );
360 setLastState( currentRepository, RepositoryState.REFERENCES_SET );
361 activateRepository( currentRepository );
362 this.repositoryGroups.put( id, currentRepository );
363 setLastState( currentRepository, RepositoryState.REGISTERED );
365 catch ( IndeterminateConfigurationException | RegistryException | RepositoryException e )
368 if ( oldRepository != null )
370 RepositoryGroupConfiguration oldCfg = provider.getRepositoryGroupConfiguration( oldRepository );
371 provider.updateRepositoryGroupInstance( (EditableRepositoryGroup) currentRepository, oldCfg);
372 replaceOrAddRepositoryConfig( oldCfg, configuration );
375 configurationHandler.save( configuration, ConfigurationHandler.REGISTRY_EVENT_TAG );
377 catch ( IndeterminateConfigurationException | RegistryException indeterminateConfigurationException )
379 log.error( "Fatal error, config save during rollback failed: {}", e.getMessage( ), e );
381 updateReferences( oldRepository, oldCfg );
382 setLastState( oldRepository, RepositoryState.REFERENCES_SET );
383 activateRepository( oldRepository );
384 repositoryGroups.put( id, oldRepository );
385 setLastState( oldRepository, RepositoryState.REGISTERED );
387 repositoryGroups.remove( id );
389 log.error( "Could not save the configuration for repository group {}: {}", id, e.getMessage( ), e );
390 if (e instanceof RepositoryException) {
391 throw (RepositoryException) e;
394 throw new RepositoryException( "Could not save the configuration for repository group " + id + ": " + e.getMessage( ) );
400 configLock.unlock( );
402 return currentRepository;
406 public RepositoryGroup put( RepositoryGroupConfiguration repositoryGroupConfiguration, Configuration configuration ) throws RepositoryException
408 final String id = repositoryGroupConfiguration.getId( );
409 final RepositoryType repoType = RepositoryType.valueOf( repositoryGroupConfiguration.getType( ) );
410 RepositoryGroup repo;
411 setRepositoryGroupDefaults( repositoryGroupConfiguration );
412 if ( repositoryGroups.containsKey( id ) )
414 repo = clone( repositoryGroups.get( id ) );
415 if ( repo instanceof EditableRepositoryGroup )
417 repositoryRegistry.getProvider( repoType ).updateRepositoryGroupInstance( (EditableRepositoryGroup) repo, repositoryGroupConfiguration );
421 throw new RepositoryException( "The repository is not editable " + id );
426 repo = repositoryRegistry.getProvider( repoType ).createRepositoryGroup( repositoryGroupConfiguration );
427 setLastState( repo, RepositoryState.CREATED );
429 replaceOrAddRepositoryConfig( repositoryGroupConfiguration, configuration );
430 updateReferences( repo, repositoryGroupConfiguration );
431 setLastState( repo, RepositoryState.REFERENCES_SET );
436 public <D> CheckedResult<RepositoryGroup, D> putWithCheck( RepositoryGroupConfiguration repositoryConfiguration, RepositoryChecker<RepositoryGroup, D> checker ) throws RepositoryException
438 final String id = repositoryConfiguration.getId( );
439 RepositoryGroup currentGroup = repositoryGroups.get( id );
440 Configuration configuration = configurationHandler.getBaseConfiguration( );
441 RepositoryGroup repositoryGroup = put( repositoryConfiguration, configuration );
442 CheckedResult<RepositoryGroup, D> result;
443 if ( currentGroup == null )
445 result = checker.apply( repositoryGroup );
449 result = checker.applyForUpdate( repositoryGroup );
451 if ( result.isValid( ) )
453 put( result.getRepository() );
459 private void setRepositoryGroupDefaults( RepositoryGroupConfiguration repositoryGroupConfiguration )
461 if ( StringUtils.isEmpty( repositoryGroupConfiguration.getMergedIndexPath( ) ) )
463 repositoryGroupConfiguration.setMergedIndexPath( DEFAULT_INDEX_PATH );
465 if ( repositoryGroupConfiguration.getMergedIndexTtl( ) <= 0 )
467 repositoryGroupConfiguration.setMergedIndexTtl( 300 );
469 if ( StringUtils.isEmpty( repositoryGroupConfiguration.getCronExpression( ) ) )
471 repositoryGroupConfiguration.setCronExpression( "0 0 03 ? * MON" );
475 private void replaceOrAddRepositoryConfig( RepositoryGroupConfiguration repositoryGroupConfiguration, Configuration configuration )
477 RepositoryGroupConfiguration oldCfg = configuration.findRepositoryGroupById( repositoryGroupConfiguration.getId( ) );
478 if ( oldCfg != null )
480 configuration.removeRepositoryGroup( oldCfg );
482 configuration.addRepositoryGroup( repositoryGroupConfiguration );
485 public void removeRepositoryFromGroups( ManagedRepository repo )
489 repositoryGroups.values( ).stream( ).filter( repoGroup -> repoGroup instanceof EditableRepository ).
490 map( repoGroup -> (EditableRepositoryGroup) repoGroup ).forEach( repoGroup -> repoGroup.removeRepository( repo ) );
495 * Removes a repository group from the registry and configuration, if it exists.
496 * The change is saved to the configuration immediately.
498 * @param id the id of the repository group to remove
499 * @throws RepositoryException if a error occurs during configuration save
502 public void remove( final String id ) throws RepositoryException
504 RepositoryGroup repo = get( id );
509 repo = repositoryGroups.remove( id );
512 this.mergedRemoteIndexesScheduler.unschedule( repo );
514 Configuration configuration = this.configurationHandler.getBaseConfiguration( );
515 RepositoryGroupConfiguration cfg = configuration.findRepositoryGroupById( id );
518 configuration.removeRepositoryGroup( cfg );
520 this.configurationHandler.save( configuration, ConfigurationHandler.REGISTRY_EVENT_TAG );
521 setLastState( repo, RepositoryState.UNREGISTERED );
525 catch ( RegistryException | IndeterminateConfigurationException e )
528 log.error( "Could not save config after repository removal: {}", e.getMessage( ), e );
529 repositoryGroups.put( repo.getId( ), repo );
530 throw new RepositoryException( "Could not save configuration after repository removal: " + e.getMessage( ) );
536 public void remove( String id, Configuration configuration ) throws RepositoryException
538 RepositoryGroup repo = repositoryGroups.get( id );
541 repo = repositoryGroups.remove( id );
544 this.mergedRemoteIndexesScheduler.unschedule( repo );
546 RepositoryGroupConfiguration cfg = configuration.findRepositoryGroupById( id );
549 configuration.removeRepositoryGroup( cfg );
551 setLastState( repo, RepositoryState.UNREGISTERED );
554 Iterator<RepositoryGroupConfiguration> cfgIter = configuration.getRepositoryGroups( ).iterator( );
555 while(cfgIter.hasNext()) {
556 RepositoryGroupConfiguration el = cfgIter.next( );
557 if (id.equals( el.getId() )) {
565 public RepositoryGroup get( String groupId )
567 return repositoryGroups.get( groupId );
571 public RepositoryGroup clone( RepositoryGroup repo ) throws RepositoryException
573 RepositoryProvider provider = repositoryRegistry.getProvider( repo.getType( ) );
574 RepositoryGroupConfiguration cfg = provider.getRepositoryGroupConfiguration( repo );
575 RepositoryGroup cloned = provider.createRepositoryGroup( cfg );
576 cloned.registerEventHandler( RepositoryEvent.ANY, repositoryRegistry );
577 setLastState( cloned, RepositoryState.CREATED );
582 public void updateReferences( RepositoryGroup repo, RepositoryGroupConfiguration repositoryConfiguration ) throws RepositoryException
584 if ( repo instanceof EditableRepositoryGroup && repositoryConfiguration!=null)
586 EditableRepositoryGroup eGroup = (EditableRepositoryGroup) repo;
587 eGroup.setRepositories( repositoryConfiguration.getRepositories( ).stream( )
588 .map( repositoryRegistry::getManagedRepository ).collect( Collectors.toList( ) ) );
594 public Collection<RepositoryGroup> getAll( )
596 return repositoryGroups.values( );
600 public RepositoryValidator<RepositoryGroup> getValidator( )
602 return this.validator;
606 public CheckedResult<RepositoryGroup, Map<String, List<ValidationError>>> validateRepository( RepositoryGroup repository )
608 return this.validator.apply( repository );
613 public CheckedResult<RepositoryGroup,Map<String, List<ValidationError>>> validateRepositoryForUpdate( RepositoryGroup repository )
615 return this.validator.applyForUpdate( repository );
618 public boolean hasRepository( String id )
620 return repositoryGroups.containsKey( id );
624 private void destroy( )
632 for ( RepositoryGroup group : repositoryGroups.values( ) )
636 mergedRemoteIndexesScheduler.unschedule( group );
639 catch ( Throwable e )
641 log.error( "Could not close repository group {}: {}", group.getId( ), e.getMessage( ) );
644 this.repositoryGroups.clear( );