1 package org.apache.archiva.admin.repository.managed;
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
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
21 import org.apache.archiva.admin.model.AuditInformation;
22 import org.apache.archiva.admin.model.RepositoryAdminException;
23 import org.apache.archiva.admin.model.beans.ManagedRepository;
24 import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
25 import org.apache.archiva.admin.repository.AbstractRepositoryAdmin;
26 import org.apache.archiva.configuration.Configuration;
27 import org.apache.archiva.configuration.IndeterminateConfigurationException;
28 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
29 import org.apache.archiva.configuration.ProxyConnectorConfiguration;
30 import org.apache.archiva.configuration.RepositoryGroupConfiguration;
31 import org.apache.archiva.indexer.ArchivaIndexManager;
32 import org.apache.archiva.indexer.IndexManagerFactory;
33 import org.apache.archiva.indexer.IndexUpdateFailedException;
34 import org.apache.archiva.metadata.model.facets.AuditEvent;
35 import org.apache.archiva.metadata.repository.*;
36 import org.apache.archiva.metadata.repository.stats.model.RepositoryStatisticsManager;
37 import org.apache.archiva.components.cache.Cache;
38 import org.apache.archiva.components.registry.RegistryException;
39 import org.apache.archiva.redback.components.taskqueue.TaskQueueException;
40 import org.apache.archiva.redback.role.RoleManager;
41 import org.apache.archiva.redback.role.RoleManagerException;
42 import org.apache.archiva.repository.ReleaseScheme;
43 import org.apache.archiva.repository.RepositoryException;
44 import org.apache.archiva.repository.RepositoryRegistry;
45 import org.apache.archiva.repository.features.ArtifactCleanupFeature;
46 import org.apache.archiva.repository.features.IndexCreationFeature;
47 import org.apache.archiva.repository.features.StagingRepositoryFeature;
48 import org.apache.archiva.scheduler.repository.model.RepositoryArchivaTaskScheduler;
49 import org.apache.archiva.scheduler.repository.model.RepositoryTask;
50 import org.apache.archiva.security.common.ArchivaRoleConstants;
51 import org.apache.commons.lang3.StringUtils;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54 import org.springframework.stereotype.Service;
56 import javax.annotation.PostConstruct;
57 import javax.annotation.PreDestroy;
58 import javax.inject.Inject;
59 import javax.inject.Named;
60 import java.io.IOException;
61 import java.nio.file.Path;
62 import java.nio.file.Paths;
63 import java.util.ArrayList;
64 import java.util.Collection;
65 import java.util.List;
67 import java.util.stream.Collectors;
70 * FIXME review the staging mechanism to have a per user session one
72 * @author Olivier Lamy
74 @Service("managedRepositoryAdmin#default")
75 public class DefaultManagedRepositoryAdmin
76 extends AbstractRepositoryAdmin
77 implements ManagedRepositoryAdmin
80 private Logger log = LoggerFactory.getLogger( getClass() );
82 public static final String STAGE_REPO_ID_END = "-stage";
86 private RepositoryRegistry repositoryRegistry;
89 @Named(value = "archivaTaskScheduler#repository")
90 private RepositoryArchivaTaskScheduler repositoryTaskScheduler;
93 * FIXME: this could be multiple implementations and needs to be configured.
96 private RepositorySessionFactory repositorySessionFactory;
99 private RepositoryStatisticsManager repositoryStatisticsManager;
102 protected RoleManager roleManager;
105 @Named(value = "cache#namespaces")
106 private Cache<String, Collection<String>> namespacesCache;
109 private IndexManagerFactory indexManagerFactory;
115 public void initialize()
116 throws RepositoryAdminException, RoleManagerException
118 // initialize index context on start and check roles here
119 for ( ManagedRepository managedRepository : getManagedRepositories() )
121 log.debug("Initializating {}", managedRepository.getId());
122 addRepositoryRoles( managedRepository.getId() );
128 public void shutdown()
129 throws RepositoryAdminException
134 * Conversion between the repository from the registry and the serialized DTO for the admin API
136 private ManagedRepository convertRepo( org.apache.archiva.repository.ManagedRepository repo ) {
140 ManagedRepository adminRepo = new ManagedRepository( getArchivaConfiguration().getDefaultLocale() );
141 setBaseRepoAttributes( adminRepo, repo );
142 adminRepo.setLocation( convertUriToString( repo.getLocation()) );
143 adminRepo.setReleases(repo.getActiveReleaseSchemes().contains( ReleaseScheme.RELEASE ));
144 adminRepo.setSnapshots( repo.getActiveReleaseSchemes().contains(ReleaseScheme.SNAPSHOT) );
145 adminRepo.setBlockRedeployments( repo.blocksRedeployments() );
146 adminRepo.setCronExpression( repo.getSchedulingDefinition() );
147 if (repo.supportsFeature( IndexCreationFeature.class )) {
148 IndexCreationFeature icf = repo.getFeature( IndexCreationFeature.class ).get();
149 adminRepo.setSkipPackedIndexCreation( icf.isSkipPackedIndexCreation() );
151 adminRepo.setScanned( repo.isScanned() );
152 if (repo.supportsFeature( ArtifactCleanupFeature.class) ) {
153 ArtifactCleanupFeature acf = repo.getFeature( ArtifactCleanupFeature.class ).get();
154 adminRepo.setRetentionPeriod( acf.getRetentionPeriod().getDays() );
155 adminRepo.setRetentionCount( acf.getRetentionCount() );
156 adminRepo.setDeleteReleasedSnapshots( acf.isDeleteReleasedSnapshots() );
159 if (repo.supportsFeature( StagingRepositoryFeature.class )) {
160 StagingRepositoryFeature stf = repo.getFeature( StagingRepositoryFeature.class ).get();
161 adminRepo.setStageRepoNeeded( stf.isStageRepoNeeded() );
162 if (stf.getStagingRepository()!=null) {
163 adminRepo.setStagingRepository( convertRepo( stf.getStagingRepository() ) );
169 private ManagedRepositoryConfiguration getRepositoryConfiguration(ManagedRepository repo) {
170 ManagedRepositoryConfiguration repoConfig = new ManagedRepositoryConfiguration();
171 setBaseRepoAttributes( repoConfig, repo );
172 repoConfig.setBlockRedeployments( repo.isBlockRedeployments( ) );
173 repoConfig.setReleases( repo.isReleases() );
174 repoConfig.setSnapshots( repo.isSnapshots() );
175 repoConfig.setScanned( repo.isScanned() );
176 repoConfig.setLocation( getRepositoryCommonValidator().removeExpressions( repo.getLocation() ) );
177 repoConfig.setRefreshCronExpression( repo.getCronExpression() );
178 repoConfig.setRetentionPeriod( repo.getRetentionPeriod() );
179 repoConfig.setRetentionCount( repo.getRetentionCount());
180 repoConfig.setDeleteReleasedSnapshots( repo.isDeleteReleasedSnapshots() );
181 repoConfig.setSkipPackedIndexCreation( repo.isSkipPackedIndexCreation());
182 repoConfig.setStageRepoNeeded( repo.isStageRepoNeeded() );
188 public List<ManagedRepository> getManagedRepositories()
189 throws RepositoryAdminException
192 return repositoryRegistry.getManagedRepositories().stream().map( rep -> this.convertRepo( rep ) ).collect( Collectors.toList());
196 public Map<String, ManagedRepository> getManagedRepositoriesAsMap()
197 throws RepositoryAdminException
199 return repositoryRegistry.getManagedRepositories().stream().collect( Collectors.toMap( e -> e.getId(), e -> convertRepo( e ) ) );
203 public ManagedRepository getManagedRepository( String repositoryId )
204 throws RepositoryAdminException
206 return convertRepo( repositoryRegistry.getManagedRepository( repositoryId ) );
210 public Boolean addManagedRepository( ManagedRepository managedRepository, boolean needStageRepo,
211 AuditInformation auditInformation )
212 throws RepositoryAdminException
214 log.debug("addManagedRepository {}, {}, {}", managedRepository.getId(), needStageRepo, auditInformation);
216 getRepositoryCommonValidator().basicValidation( managedRepository, false );
217 getRepositoryCommonValidator().validateManagedRepository( managedRepository );
218 triggerAuditEvent( managedRepository.getId(), null, AuditEvent.ADD_MANAGED_REPO, auditInformation );
219 ManagedRepositoryConfiguration repoConfig = getRepositoryConfiguration( managedRepository );
221 repoConfig.setStageRepoNeeded( true );
223 Configuration configuration = getArchivaConfiguration().getConfiguration();
226 org.apache.archiva.repository.ManagedRepository newRepo = repositoryRegistry.putRepository( repoConfig, configuration );
227 log.debug("Added new repository {}", newRepo.getId());
228 org.apache.archiva.repository.ManagedRepository stagingRepo = null;
229 addRepositoryRoles( newRepo.getId() );
230 if ( newRepo.supportsFeature( StagingRepositoryFeature.class )) {
231 StagingRepositoryFeature stf = newRepo.getFeature( StagingRepositoryFeature.class ).get();
232 stagingRepo = stf.getStagingRepository();
233 if (stf.isStageRepoNeeded() && stagingRepo != null) {
234 addRepositoryRoles( stagingRepo.getId() );
235 triggerAuditEvent( stagingRepo.getId(), null, AuditEvent.ADD_MANAGED_REPO, auditInformation );
238 saveConfiguration( configuration );
239 //MRM-1342 Repository statistics report doesn't appear to be working correctly
240 //scan repository when adding of repository is successful
243 if ( newRepo.isScanned())
245 scanRepository( newRepo.getId(), true );
248 if ( stagingRepo!=null && stagingRepo.isScanned() )
250 scanRepository( stagingRepo.getId(), true );
253 catch ( Exception e )
255 log.warn("Unable to scan repository [{}]: {}", newRepo.getId(), e.getMessage(), e);
258 catch ( RepositoryException e )
260 log.error("Could not add managed repository {}"+managedRepository);
261 throw new RepositoryAdminException( "Could not add repository "+e.getMessage() );
263 catch ( RoleManagerException e )
265 log.error("Could not add repository roles for repository [{}]: {}", managedRepository.getId(), e.getMessage(), e);
266 throw new RepositoryAdminException( "Could not add roles to repository "+e.getMessage() );
275 public Boolean deleteManagedRepository( String repositoryId, AuditInformation auditInformation,
276 boolean deleteContent )
277 throws RepositoryAdminException
279 Configuration config = getArchivaConfiguration().getConfiguration();
280 ManagedRepositoryConfiguration repoConfig=config.findManagedRepositoryById( repositoryId );
281 if (repoConfig!=null) {
283 log.debug("Repo location " + repoConfig.getLocation());
285 org.apache.archiva.repository.ManagedRepository repo = repositoryRegistry.getManagedRepository(repositoryId);
286 org.apache.archiva.repository.ManagedRepository stagingRepository = null;
289 if (repo.supportsFeature(StagingRepositoryFeature.class)) {
290 stagingRepository = repo.getFeature(StagingRepositoryFeature.class).get().getStagingRepository();
292 repositoryRegistry.removeRepository(repo, config);
293 } catch (RepositoryException e) {
294 log.error("Removal of repository {} failed: {}", repositoryId, e.getMessage(), e);
295 throw new RepositoryAdminException("Removal of repository " + repositoryId + " failed.");
298 throw new RepositoryAdminException("A repository with that id does not exist");
301 triggerAuditEvent(repositoryId, null, AuditEvent.DELETE_MANAGED_REPO, auditInformation);
302 if (repoConfig != null) {
303 deleteManagedRepository(repoConfig, deleteContent, config, false);
307 // stage repo exists ?
308 if (stagingRepository != null) {
309 // do not trigger event when deleting the staged one
310 ManagedRepositoryConfiguration stagingRepositoryConfig = config.findManagedRepositoryById(stagingRepository.getId());
312 repositoryRegistry.removeRepository(stagingRepository);
313 if (stagingRepositoryConfig != null) {
314 deleteManagedRepository(stagingRepositoryConfig, deleteContent, config, true);
316 } catch (RepositoryException e) {
317 log.error("Removal of staging repository {} failed: {}", stagingRepository.getId(), e.getMessage(), e);
322 saveConfiguration(config);
323 } catch (Exception e) {
324 throw new RepositoryAdminException("Error saving configuration for delete action" + e.getMessage(), e);
329 return Boolean.FALSE;
333 private Boolean deleteManagedRepository( ManagedRepositoryConfiguration repository, boolean deleteContent,
334 Configuration config, boolean stagedOne )
335 throws RepositoryAdminException
340 boolean success=false;
341 try(RepositorySession repositorySession = getRepositorySessionFactory().createSession())
343 MetadataRepository metadataRepository = repositorySession.getRepository();
344 metadataRepository.removeRepository(repositorySession , repository.getId() );
346 namespacesCache.remove( repository.getId() );
347 repositorySession.save();
350 catch ( MetadataRepositoryException e )
352 //throw new RepositoryAdminException( e.getMessage(), e );
353 log.warn( "skip error during removing repository from MetadataRepository:{}", e.getMessage(), e );
355 } catch (MetadataSessionException e) {
356 log.warn( "skip error during removing repository from MetadataRepository:{}", e.getMessage(), e );
361 log.debug( "call repositoryStatisticsManager.deleteStatistics" );
364 getRepositoryStatisticsManager( ).deleteStatistics( repository.getId( ) );
366 catch ( MetadataRepositoryException e )
368 e.printStackTrace( );
376 // TODO could be async ? as directory can be huge
377 Path dir = Paths.get( repository.getLocation() );
378 org.apache.archiva.common.utils.FileUtils.deleteQuietly( dir );
381 // olamy: copy list for reading as a unit test in webapp fail with ConcurrentModificationException
382 List<ProxyConnectorConfiguration> proxyConnectors = new ArrayList<>( config.getProxyConnectors() );
383 for ( ProxyConnectorConfiguration proxyConnector : proxyConnectors )
385 if ( StringUtils.equals( proxyConnector.getSourceRepoId(), repository.getId() ) )
387 config.removeProxyConnector( proxyConnector );
391 Map<String, List<String>> repoToGroupMap = config.getRepositoryToGroupMap();
392 if ( repoToGroupMap != null )
394 if ( repoToGroupMap.containsKey( repository.getId() ) )
396 List<String> repoGroups = repoToGroupMap.get( repository.getId() );
397 for ( String repoGroup : repoGroups )
399 // copy to prevent UnsupportedOperationException
400 RepositoryGroupConfiguration repositoryGroupConfiguration =
401 config.findRepositoryGroupById( repoGroup );
402 List<String> repos = new ArrayList<>( repositoryGroupConfiguration.getRepositories() );
403 config.removeRepositoryGroup( repositoryGroupConfiguration );
404 repos.remove( repository.getId() );
405 repositoryGroupConfiguration.setRepositories( repos );
406 config.addRepositoryGroup( repositoryGroupConfiguration );
413 removeRepositoryRoles( repository );
415 catch ( RoleManagerException e )
417 throw new RepositoryAdminException(
418 "fail to remove repository roles for repository " + repository.getId() + " : " + e.getMessage(), e );
422 final RepositoryRegistry reg = getRepositoryRegistry();
423 if (reg.getManagedRepository(repository.getId())!=null) {
424 reg.removeRepository(reg.getManagedRepository(repository.getId()));
426 } catch (RepositoryException e) {
427 throw new RepositoryAdminException("Removal of repository "+repository.getId()+ " failed: "+e.getMessage());
430 saveConfiguration( config );
435 ArchivaIndexManager getIndexManager(ManagedRepository managedRepository) {
436 org.apache.archiva.repository.ManagedRepository repo = getRepositoryRegistry().getManagedRepository(managedRepository.getId());
437 return indexManagerFactory.getIndexManager(repo.getType());
441 public Boolean updateManagedRepository( ManagedRepository managedRepository, boolean needStageRepo,
442 AuditInformation auditInformation, boolean resetStats )
443 throws RepositoryAdminException
446 log.debug( "updateManagedConfiguration repo {} needStage {} resetStats {} ", managedRepository, needStageRepo,
449 // Ensure that the fields are valid.
451 getRepositoryCommonValidator().basicValidation( managedRepository, true );
453 getRepositoryCommonValidator().validateManagedRepository( managedRepository );
455 Configuration configuration = getArchivaConfiguration().getConfiguration();
457 ManagedRepositoryConfiguration updatedRepoConfig = getRepositoryConfiguration( managedRepository );
458 updatedRepoConfig.setStageRepoNeeded( needStageRepo );
460 org.apache.archiva.repository.ManagedRepository oldRepo = repositoryRegistry.getManagedRepository( managedRepository.getId( ) );
461 boolean stagingExists = false;
462 if (oldRepo.supportsFeature( StagingRepositoryFeature.class ) ){
463 stagingExists = oldRepo.getFeature( StagingRepositoryFeature.class ).get().getStagingRepository() != null;
465 boolean updateIndexContext = !StringUtils.equals( updatedRepoConfig.getIndexDir(), managedRepository.getIndexDirectory() );
466 org.apache.archiva.repository.ManagedRepository newRepo;
467 // TODO remove content from old if path has changed !!!!!
470 newRepo = repositoryRegistry.putRepository( updatedRepoConfig, configuration );
471 if (newRepo.supportsFeature( StagingRepositoryFeature.class )) {
472 org.apache.archiva.repository.ManagedRepository stagingRepo = newRepo.getFeature( StagingRepositoryFeature.class ).get( ).getStagingRepository( );
473 if (stagingRepo!=null && !stagingExists)
475 triggerAuditEvent( stagingRepo.getId(), null, AuditEvent.ADD_MANAGED_REPO, auditInformation );
476 addRepositoryRoles( stagingRepo.getId( ) );
482 catch ( RepositoryException e )
484 log.error("Could not update repository {}: {}", managedRepository.getId(), e.getMessage(), e);
485 throw new RepositoryAdminException( "Could not update repository "+managedRepository.getId());
487 catch ( RoleManagerException e ) {
488 log.error("Error during role update of stage repo {}", managedRepository.getId(), e);
489 throw new RepositoryAdminException( "Could not update repository "+managedRepository.getId());
491 triggerAuditEvent( managedRepository.getId(), null, AuditEvent.MODIFY_MANAGED_REPO,
495 getArchivaConfiguration().save(configuration);
497 catch ( RegistryException | IndeterminateConfigurationException e )
499 log.error("Could not save repository configuration: {}", e.getMessage(), e);
500 throw new RepositoryAdminException( "Could not save repository configuration: "+e.getMessage() );
503 // Save the repository configuration.
504 RepositorySession repositorySession = null;
507 repositorySession = getRepositorySessionFactory().createSession();
509 catch ( MetadataRepositoryException e )
511 e.printStackTrace( );
519 log.debug( "call repositoryStatisticsManager.deleteStatistics" );
520 getRepositoryStatisticsManager().deleteStatistics(
521 managedRepository.getId() );
522 repositorySession.save();
526 catch (MetadataRepositoryException | MetadataSessionException e )
528 throw new RepositoryAdminException( e.getMessage(), e );
532 repositorySession.close();
535 if ( updateIndexContext )
540 repositoryRegistry.resetIndexingContext(newRepo);
541 } catch (IndexUpdateFailedException e) {
549 //--------------------------
551 //--------------------------
554 protected void addRepository( ManagedRepositoryConfiguration repository, Configuration configuration )
555 throws RepositoryAdminException, IOException
559 getRepositoryRegistry().putRepository( repository, configuration );
561 catch ( RepositoryException e )
563 throw new RepositoryAdminException( "Could not add the repository to the registry. Cause: "+e.getMessage() );
569 public Boolean scanRepository( String repositoryId, boolean fullScan )
571 if ( getRepositoryTaskScheduler().isProcessingRepositoryTask( repositoryId ) )
573 log.info( "scanning of repository with id {} already scheduled", repositoryId );
575 RepositoryTask task = new RepositoryTask();
576 task.setRepositoryId( repositoryId );
577 task.setScanAll( fullScan );
580 getRepositoryTaskScheduler().queueTask( task );
582 catch ( TaskQueueException e )
584 log.error( "failed to schedule scanning of repo with id {}", repositoryId, e );
591 private void addRepositoryRoles( String repoId )
592 throws RoleManagerException
594 // TODO: double check these are configured on start up
595 // TODO: belongs in the business logic
597 if ( !getRoleManager().templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId ) )
599 getRoleManager().createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId );
602 if ( !getRoleManager().templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoId ) )
604 getRoleManager().createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoId );
608 protected void removeRepositoryRoles( ManagedRepositoryConfiguration existingRepository )
609 throws RoleManagerException
611 String repoId = existingRepository.getId();
613 if ( getRoleManager().templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoId ) )
615 getRoleManager().removeTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoId );
618 if ( getRoleManager().templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId ) )
620 getRoleManager().removeTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId );
623 log.debug( "removed user roles associated with repository {}", repoId );
626 //--------------------------
628 //--------------------------
631 public RoleManager getRoleManager()
636 public void setRoleManager( RoleManager roleManager )
638 this.roleManager = roleManager;
641 public RepositoryStatisticsManager getRepositoryStatisticsManager()
643 return repositoryStatisticsManager;
646 public void setRepositoryStatisticsManager( RepositoryStatisticsManager repositoryStatisticsManager )
648 this.repositoryStatisticsManager = repositoryStatisticsManager;
651 public RepositorySessionFactory getRepositorySessionFactory()
653 return repositorySessionFactory;
656 public void setRepositorySessionFactory( RepositorySessionFactory repositorySessionFactory )
658 this.repositorySessionFactory = repositorySessionFactory;
662 public RepositoryArchivaTaskScheduler getRepositoryTaskScheduler()
664 return repositoryTaskScheduler;
667 public void setRepositoryTaskScheduler( RepositoryArchivaTaskScheduler repositoryTaskScheduler )
669 this.repositoryTaskScheduler = repositoryTaskScheduler;
673 public RepositoryRegistry getRepositoryRegistry( )
675 return repositoryRegistry;
678 public void setRepositoryRegistry( RepositoryRegistry repositoryRegistry )
680 this.repositoryRegistry = repositoryRegistry;