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.base.ArchivaRepositoryRegistry;
26 import org.apache.archiva.repository.base.ConfigurationHandler;
27 import org.apache.archiva.repository.validation.CheckedResult;
28 import org.apache.archiva.repository.EditableRepository;
29 import org.apache.archiva.repository.EditableRepositoryGroup;
30 import org.apache.archiva.repository.ManagedRepository;
31 import org.apache.archiva.repository.Repository;
32 import org.apache.archiva.repository.RepositoryException;
33 import org.apache.archiva.repository.RepositoryGroup;
34 import org.apache.archiva.repository.RepositoryHandler;
35 import org.apache.archiva.repository.RepositoryProvider;
36 import org.apache.archiva.repository.RepositoryType;
37 import org.apache.archiva.repository.event.RepositoryEvent;
38 import org.apache.archiva.repository.features.IndexCreationFeature;
39 import org.apache.archiva.repository.storage.StorageAsset;
40 import org.apache.archiva.repository.validation.CombinedValidator;
41 import org.apache.archiva.repository.validation.RepositoryChecker;
42 import org.apache.archiva.repository.validation.RepositoryValidator;
43 import org.apache.archiva.repository.validation.ValidationResponse;
44 import org.apache.commons.lang3.StringUtils;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47 import org.springframework.stereotype.Service;
49 import javax.annotation.PostConstruct;
50 import javax.annotation.PreDestroy;
51 import javax.inject.Named;
52 import java.io.IOException;
53 import java.nio.file.Files;
54 import java.nio.file.Path;
55 import java.util.Collection;
56 import java.util.Collections;
57 import java.util.HashMap;
58 import java.util.LinkedHashMap;
59 import java.util.List;
61 import java.util.concurrent.locks.ReentrantReadWriteLock;
62 import java.util.stream.Collectors;
64 import static org.apache.archiva.indexer.ArchivaIndexManager.DEFAULT_INDEX_PATH;
67 * This class manages repository groups for the RepositoryRegistry.
68 * It is tightly coupled with the {@link ArchivaRepositoryRegistry}.
70 * @author Martin Stockhammer <martin_s@apache.org>
72 @Service( "repositoryGroupHandler#default" )
73 public class RepositoryGroupHandler implements RepositoryHandler<RepositoryGroup, RepositoryGroupConfiguration>
75 private static final Logger log = LoggerFactory.getLogger( RepositoryGroupHandler.class );
77 private final ArchivaRepositoryRegistry repositoryRegistry;
78 private final ConfigurationHandler configurationHandler;
79 private final MergedRemoteIndexesScheduler mergedRemoteIndexesScheduler;
81 private final Map<String, RepositoryGroup> repositoryGroups = new HashMap<>( );
82 private final RepositoryValidator<RepositoryGroup> validator;
84 private Path groupsDirectory;
88 * Creates a new instance. All dependencies are injected on the constructor.
90 * @param repositoryRegistry the registry. To avoid circular dependencies via DI, this class registers itself on the registry.
91 * @param configurationHandler the configuration handler is used to retrieve and save configuration.
92 * @param mergedRemoteIndexesScheduler the index scheduler is used for merging the indexes from all group members
94 public RepositoryGroupHandler( ArchivaRepositoryRegistry repositoryRegistry,
95 ConfigurationHandler configurationHandler,
96 @Named( "mergedRemoteIndexesScheduler#default" ) MergedRemoteIndexesScheduler mergedRemoteIndexesScheduler,
97 List<RepositoryValidator<? extends Repository>> repositoryGroupValidatorList
100 this.configurationHandler = configurationHandler;
101 this.mergedRemoteIndexesScheduler = mergedRemoteIndexesScheduler;
102 this.repositoryRegistry = repositoryRegistry;
103 List<RepositoryValidator<RepositoryGroup>> validatorList = initValidators( repositoryGroupValidatorList );
104 this.validator = new CombinedValidator<>( RepositoryGroup.class, validatorList );
107 private List<RepositoryValidator<RepositoryGroup>> initValidators(List<RepositoryValidator<? extends Repository>> repositoryGroupValidatorList) {
108 if (repositoryGroupValidatorList!=null && repositoryGroupValidatorList.size()>0) {
109 return repositoryGroupValidatorList.stream( ).filter(
110 v -> v.isFlavour( RepositoryGroup.class )
111 ).map( v -> v.narrowTo( RepositoryGroup.class ) ).collect( Collectors.toList( ) );
113 return Collections.emptyList( );
121 log.debug( "Initializing repository group handler " + repositoryRegistry.toString( ) );
122 initializeStorage( );
123 // We are registering this class on the registry. This is necessary to avoid circular dependencies via injection.
124 this.repositoryRegistry.registerGroupHandler( this );
127 public void initializeFromConfig( )
129 this.repositoryGroups.clear( );
130 this.repositoryGroups.putAll( newInstancesFromConfig( ) );
131 for ( RepositoryGroup group : this.repositoryGroups.values( ) )
133 initializeGroup( group );
137 private void initializeStorage( )
139 Path baseDir = this.configurationHandler.getArchivaConfiguration( ).getRepositoryGroupBaseDir( );
140 if ( !Files.exists( baseDir ) )
144 Files.createDirectories( baseDir );
146 catch ( IOException e )
148 log.error( "Could not create group base directory: {}", e.getMessage( ), e );
151 this.groupsDirectory = baseDir;
154 private void initializeGroup( RepositoryGroup repositoryGroup )
156 StorageAsset indexDirectory = getMergedIndexDirectory( repositoryGroup );
157 if ( !indexDirectory.exists( ) )
161 indexDirectory.create( );
163 catch ( IOException e )
165 log.error( "Could not create index directory {} for group {}: {}", indexDirectory, repositoryGroup.getId( ), e.getMessage( ) );
168 Path groupPath = groupsDirectory.resolve( repositoryGroup.getId( ) );
169 if ( !Files.exists( groupPath ) )
173 Files.createDirectories( groupPath );
175 catch ( IOException e )
177 log.error( "Could not create repository group directory {}", groupPath );
180 mergedRemoteIndexesScheduler.schedule( repositoryGroup,
184 public StorageAsset getMergedIndexDirectory( RepositoryGroup group )
188 return group.getFeature( IndexCreationFeature.class ).get( ).getLocalIndexPath( );
198 public Map<String, RepositoryGroup> newInstancesFromConfig( )
202 List<RepositoryGroupConfiguration> repositoryGroupConfigurations =
203 this.configurationHandler.getBaseConfiguration( ).getRepositoryGroups( );
205 if ( repositoryGroupConfigurations == null )
207 return Collections.emptyMap( );
210 Map<String, RepositoryGroup> repositoryGroupMap = new LinkedHashMap<>( repositoryGroupConfigurations.size( ) );
212 Map<RepositoryType, RepositoryProvider> providerMap = repositoryRegistry.getRepositoryProviderMap( );
213 for ( RepositoryGroupConfiguration repoConfig : repositoryGroupConfigurations )
215 RepositoryType repositoryType = RepositoryType.valueOf( repoConfig.getType( ) );
216 if ( providerMap.containsKey( repositoryType ) )
220 RepositoryGroup repo = createNewRepositoryGroup( providerMap.get( repositoryType ), repoConfig );
221 repositoryGroupMap.put( repo.getId( ), repo );
223 catch ( Exception e )
225 log.error( "Could not create repository group {}: {}", repoConfig.getId( ), e.getMessage( ), e );
229 return repositoryGroupMap;
231 catch ( Throwable e )
233 log.error( "Could not initialize repositories from config: {}", e.getMessage( ), e );
234 return Collections.emptyMap( );
239 public RepositoryGroup newInstance( final RepositoryType type, String id ) throws RepositoryException
241 RepositoryProvider provider = repositoryRegistry.getProvider( type );
242 RepositoryGroupConfiguration config = new RepositoryGroupConfiguration( );
244 return createNewRepositoryGroup( provider, config );
248 public RepositoryGroup newInstance( final RepositoryGroupConfiguration repositoryConfiguration ) throws RepositoryException
250 RepositoryType type = RepositoryType.valueOf( repositoryConfiguration.getType( ) );
251 RepositoryProvider provider = repositoryRegistry.getProvider( type );
252 return createNewRepositoryGroup( provider, repositoryConfiguration );
255 private RepositoryGroup createNewRepositoryGroup( RepositoryProvider provider, RepositoryGroupConfiguration config ) throws RepositoryException
257 RepositoryGroup repositoryGroup = provider.createRepositoryGroup( config );
258 updateReferences( repositoryGroup, config );
259 return repositoryGroup;
264 * Adds a new repository group to the current list, or replaces the repository group definition with
265 * the same id, if it exists already.
266 * The change is saved to the configuration immediately.
268 * @param repositoryGroup the new repository group.
269 * @throws RepositoryException if the new repository group could not be saved to the configuration.
272 public RepositoryGroup put( final RepositoryGroup repositoryGroup ) throws RepositoryException
274 final String id = repositoryGroup.getId( );
275 RepositoryGroup originRepoGroup = repositoryGroups.remove( id );
278 if ( originRepoGroup != null && originRepoGroup != repositoryGroup )
280 this.mergedRemoteIndexesScheduler.unschedule( originRepoGroup );
281 originRepoGroup.close( );
283 RepositoryProvider provider = repositoryRegistry.getProvider( repositoryGroup.getType( ) );
284 RepositoryGroupConfiguration newCfg = provider.getRepositoryGroupConfiguration( repositoryGroup );
285 ReentrantReadWriteLock.WriteLock configLock = this.configurationHandler.getLock( ).writeLock( );
289 Configuration configuration = this.configurationHandler.getBaseConfiguration( );
290 updateReferences( repositoryGroup, newCfg );
291 RepositoryGroupConfiguration oldCfg = configuration.findRepositoryGroupById( id );
292 if ( oldCfg != null )
294 configuration.removeRepositoryGroup( oldCfg );
296 configuration.addRepositoryGroup( newCfg );
297 configurationHandler.save( configuration, ConfigurationHandler.REGISTRY_EVENT_TAG );
298 initializeGroup( repositoryGroup );
302 configLock.unlock( );
304 repositoryGroups.put( id, repositoryGroup );
305 return repositoryGroup;
307 catch ( Exception e )
310 if ( originRepoGroup != null )
312 repositoryGroups.put( id, originRepoGroup );
316 repositoryGroups.remove( id );
318 log.error( "Exception during configuration update {}", e.getMessage( ), e );
319 throw new RepositoryException( "Could not save the configuration" + ( e.getMessage( ) == null ? "" : ": " + e.getMessage( ) ), e);
324 * Adds a new repository group or updates the repository with the same id, if it exists already.
325 * The configuration is saved immediately.
327 * @param repositoryGroupConfiguration the repository configuration
328 * @return the updated or created repository
329 * @throws RepositoryException if an error occurs, or the configuration is not valid.
332 public RepositoryGroup put( RepositoryGroupConfiguration repositoryGroupConfiguration ) throws RepositoryException
334 final String id = repositoryGroupConfiguration.getId( );
335 final RepositoryType repositoryType = RepositoryType.valueOf( repositoryGroupConfiguration.getType( ) );
336 final RepositoryProvider provider = repositoryRegistry.getProvider( repositoryType );
337 RepositoryGroup currentRepository;
338 ReentrantReadWriteLock.WriteLock configLock = this.configurationHandler.getLock( ).writeLock( );
342 Configuration configuration = this.configurationHandler.getBaseConfiguration( );
343 currentRepository = repositoryRegistry.getRepositoryGroup( id );
344 RepositoryGroup oldRepository = currentRepository == null ? null : clone( currentRepository );
348 if (currentRepository==null) {
349 currentRepository = put( repositoryGroupConfiguration, configuration );
352 setRepositoryGroupDefaults( repositoryGroupConfiguration );
353 provider.updateRepositoryGroupInstance( (EditableRepositoryGroup) currentRepository, repositoryGroupConfiguration );
355 configurationHandler.save( configuration, ConfigurationHandler.REGISTRY_EVENT_TAG );
356 updateReferences( currentRepository, repositoryGroupConfiguration );
357 initializeGroup( currentRepository );
358 this.repositoryGroups.put( id, currentRepository );
360 catch ( IndeterminateConfigurationException | RegistryException | RepositoryException e )
363 if ( oldRepository != null )
365 RepositoryGroupConfiguration oldCfg = provider.getRepositoryGroupConfiguration( oldRepository );
366 provider.updateRepositoryGroupInstance( (EditableRepositoryGroup) currentRepository, oldCfg);
367 replaceOrAddRepositoryConfig( oldCfg, configuration );
370 configurationHandler.save( configuration, ConfigurationHandler.REGISTRY_EVENT_TAG );
372 catch ( IndeterminateConfigurationException | RegistryException indeterminateConfigurationException )
374 log.error( "Fatal error, config save during rollback failed: {}", e.getMessage( ), e );
376 updateReferences( oldRepository, oldCfg );
377 initializeGroup( oldRepository );
379 log.error( "Could not save the configuration for repository group {}: {}", id, e.getMessage( ), e );
380 if (e instanceof RepositoryException) {
381 throw (RepositoryException) e;
384 throw new RepositoryException( "Could not save the configuration for repository group " + id + ": " + e.getMessage( ) );
390 configLock.unlock( );
392 return currentRepository;
396 public RepositoryGroup put( RepositoryGroupConfiguration repositoryGroupConfiguration, Configuration configuration ) throws RepositoryException
398 final String id = repositoryGroupConfiguration.getId( );
399 final RepositoryType repoType = RepositoryType.valueOf( repositoryGroupConfiguration.getType( ) );
400 RepositoryGroup repo;
401 setRepositoryGroupDefaults( repositoryGroupConfiguration );
402 if ( repositoryGroups.containsKey( id ) )
404 repo = clone( repositoryGroups.get( id ) );
405 if ( repo instanceof EditableRepositoryGroup )
407 repositoryRegistry.getProvider( repoType ).updateRepositoryGroupInstance( (EditableRepositoryGroup) repo, repositoryGroupConfiguration );
411 throw new RepositoryException( "The repository is not editable " + id );
416 repo = repositoryRegistry.getProvider( repoType ).createRepositoryGroup( repositoryGroupConfiguration );
418 replaceOrAddRepositoryConfig( repositoryGroupConfiguration, configuration );
419 updateReferences( repo, repositoryGroupConfiguration );
424 public <D> CheckedResult<RepositoryGroup, D> putWithCheck( RepositoryGroupConfiguration repositoryConfiguration, RepositoryChecker<RepositoryGroup, D> checker ) throws RepositoryException
426 final String id = repositoryConfiguration.getId( );
427 RepositoryGroup currentGroup = repositoryGroups.get( id );
428 Configuration configuration = configurationHandler.getBaseConfiguration( );
429 RepositoryGroup repositoryGroup = put( repositoryConfiguration, configuration );
430 CheckedResult<RepositoryGroup, D> result;
431 if ( currentGroup == null )
433 result = checker.apply( repositoryGroup );
437 result = checker.applyForUpdate( repositoryGroup );
439 if ( result.isValid( ) )
441 put( result.getRepository() );
447 private void setRepositoryGroupDefaults( RepositoryGroupConfiguration repositoryGroupConfiguration )
449 if ( StringUtils.isEmpty( repositoryGroupConfiguration.getMergedIndexPath( ) ) )
451 repositoryGroupConfiguration.setMergedIndexPath( DEFAULT_INDEX_PATH );
453 if ( repositoryGroupConfiguration.getMergedIndexTtl( ) <= 0 )
455 repositoryGroupConfiguration.setMergedIndexTtl( 300 );
457 if ( StringUtils.isEmpty( repositoryGroupConfiguration.getCronExpression( ) ) )
459 repositoryGroupConfiguration.setCronExpression( "0 0 03 ? * MON" );
463 private void replaceOrAddRepositoryConfig( RepositoryGroupConfiguration repositoryGroupConfiguration, Configuration configuration )
465 RepositoryGroupConfiguration oldCfg = configuration.findRepositoryGroupById( repositoryGroupConfiguration.getId( ) );
466 if ( oldCfg != null )
468 configuration.removeRepositoryGroup( oldCfg );
470 configuration.addRepositoryGroup( repositoryGroupConfiguration );
473 public void removeRepositoryFromGroups( ManagedRepository repo )
477 repositoryGroups.values( ).stream( ).filter( repoGroup -> repoGroup instanceof EditableRepository ).
478 map( repoGroup -> (EditableRepositoryGroup) repoGroup ).forEach( repoGroup -> repoGroup.removeRepository( repo ) );
483 * Removes a repository group from the registry and configuration, if it exists.
484 * The change is saved to the configuration immediately.
486 * @param id the id of the repository group to remove
487 * @throws RepositoryException if a error occurs during configuration save
490 public void remove( final String id ) throws RepositoryException
492 RepositoryGroup repo = get( id );
497 repo = repositoryGroups.remove( id );
500 this.mergedRemoteIndexesScheduler.unschedule( repo );
502 Configuration configuration = this.configurationHandler.getBaseConfiguration( );
503 RepositoryGroupConfiguration cfg = configuration.findRepositoryGroupById( id );
506 configuration.removeRepositoryGroup( cfg );
508 this.configurationHandler.save( configuration, ConfigurationHandler.REGISTRY_EVENT_TAG );
512 catch ( RegistryException | IndeterminateConfigurationException e )
515 log.error( "Could not save config after repository removal: {}", e.getMessage( ), e );
516 repositoryGroups.put( repo.getId( ), repo );
517 throw new RepositoryException( "Could not save configuration after repository removal: " + e.getMessage( ) );
523 public void remove( String id, Configuration configuration ) throws RepositoryException
525 RepositoryGroup repo = repositoryGroups.get( id );
528 repo = repositoryGroups.remove( id );
531 this.mergedRemoteIndexesScheduler.unschedule( repo );
533 RepositoryGroupConfiguration cfg = configuration.findRepositoryGroupById( id );
536 configuration.removeRepositoryGroup( cfg );
544 public RepositoryGroup get( String groupId )
546 return repositoryGroups.get( groupId );
550 public RepositoryGroup clone( RepositoryGroup repo ) throws RepositoryException
552 RepositoryProvider provider = repositoryRegistry.getProvider( repo.getType( ) );
553 RepositoryGroupConfiguration cfg = provider.getRepositoryGroupConfiguration( repo );
554 RepositoryGroup cloned = provider.createRepositoryGroup( cfg );
555 cloned.registerEventHandler( RepositoryEvent.ANY, repositoryRegistry );
560 public void updateReferences( RepositoryGroup repo, RepositoryGroupConfiguration repositoryConfiguration ) throws RepositoryException
562 if ( repo instanceof EditableRepositoryGroup )
564 EditableRepositoryGroup eGroup = (EditableRepositoryGroup) repo;
565 eGroup.setRepositories( repositoryConfiguration.getRepositories( ).stream( )
566 .map( repositoryRegistry::getManagedRepository ).collect( Collectors.toList( ) ) );
572 public Collection<RepositoryGroup> getAll( )
574 return repositoryGroups.values( );
578 public RepositoryValidator<RepositoryGroup> getValidator( )
580 return this.validator;
584 public ValidationResponse<RepositoryGroup> validateRepository( RepositoryGroup repository )
590 public ValidationResponse<RepositoryGroup> validateRepositoryForUpdate( RepositoryGroup repository )
596 public boolean has( String id )
598 return repositoryGroups.containsKey( id );
602 private void destroy( )
610 for ( RepositoryGroup group : repositoryGroups.values( ) )
614 mergedRemoteIndexesScheduler.unschedule( group );
617 catch ( Throwable e )
619 log.error( "Could not close repository group {}: {}", group.getId( ), e.getMessage( ) );
622 this.repositoryGroups.clear( );