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.components.cache.Cache;
27 import org.apache.archiva.components.taskqueue.TaskQueueException;
28 import org.apache.archiva.configuration.Configuration;
29 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
30 import org.apache.archiva.configuration.ProxyConnectorConfiguration;
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.MetadataRepository;
36 import org.apache.archiva.metadata.repository.MetadataRepositoryException;
37 import org.apache.archiva.metadata.repository.MetadataSessionException;
38 import org.apache.archiva.metadata.repository.RepositorySession;
39 import org.apache.archiva.metadata.repository.RepositorySessionFactory;
40 import org.apache.archiva.metadata.repository.stats.model.RepositoryStatisticsManager;
41 import org.apache.archiva.redback.role.RoleManager;
42 import org.apache.archiva.redback.role.RoleManagerException;
43 import org.apache.archiva.repository.ReleaseScheme;
44 import org.apache.archiva.repository.RepositoryException;
45 import org.apache.archiva.repository.RepositoryRegistry;
46 import org.apache.archiva.repository.base.RepositoryHandlerDependencies;
47 import org.apache.archiva.repository.features.ArtifactCleanupFeature;
48 import org.apache.archiva.repository.features.IndexCreationFeature;
49 import org.apache.archiva.repository.features.StagingRepositoryFeature;
50 import org.apache.archiva.scheduler.repository.model.RepositoryArchivaTaskScheduler;
51 import org.apache.archiva.scheduler.repository.model.RepositoryTask;
52 import org.apache.archiva.security.common.ArchivaRoleConstants;
53 import org.apache.commons.lang3.StringUtils;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
56 import org.springframework.stereotype.Service;
58 import javax.annotation.PostConstruct;
59 import javax.annotation.PreDestroy;
60 import javax.inject.Inject;
61 import javax.inject.Named;
62 import java.io.IOException;
63 import java.nio.file.Path;
64 import java.nio.file.Paths;
65 import java.util.ArrayList;
66 import java.util.Collection;
67 import java.util.List;
69 import java.util.stream.Collectors;
72 * FIXME review the staging mechanism to have a per user session one
74 * @author Olivier Lamy
76 @Service("managedRepositoryAdmin#default")
77 public class DefaultManagedRepositoryAdmin
78 extends AbstractRepositoryAdmin
79 implements ManagedRepositoryAdmin
82 private Logger log = LoggerFactory.getLogger( getClass() );
84 public static final String STAGE_REPO_ID_END = "-stage";
88 private RepositoryRegistry repositoryRegistry;
90 @SuppressWarnings( "unused" )
92 private RepositoryHandlerDependencies managedRepositoryHandler;
95 @Named(value = "archivaTaskScheduler#repository")
96 private RepositoryArchivaTaskScheduler repositoryTaskScheduler;
99 * FIXME: this could be multiple implementations and needs to be configured.
102 private RepositorySessionFactory repositorySessionFactory;
105 private RepositoryStatisticsManager repositoryStatisticsManager;
108 protected RoleManager roleManager;
111 @Named(value = "cache#namespaces")
112 private Cache<String, List<String>> namespacesCache;
115 private IndexManagerFactory indexManagerFactory;
121 public void initialize()
122 throws RepositoryAdminException, RoleManagerException
124 // initialize index context on start and check roles here
125 for ( ManagedRepository managedRepository : getManagedRepositories() )
127 log.debug("Initializating {}", managedRepository.getId());
128 addRepositoryRoles( managedRepository.getId() );
134 public void shutdown()
135 throws RepositoryAdminException
140 * Conversion between the repository from the registry and the serialized DTO for the admin API
142 private ManagedRepository convertRepo( org.apache.archiva.repository.ManagedRepository repo ) {
146 ManagedRepository adminRepo = new ManagedRepository( getArchivaConfiguration().getDefaultLocale() );
147 setBaseRepoAttributes( adminRepo, repo );
148 adminRepo.setLocation( convertUriToString( repo.getLocation()) );
149 adminRepo.setReleases(repo.getActiveReleaseSchemes().contains( ReleaseScheme.RELEASE ));
150 adminRepo.setSnapshots( repo.getActiveReleaseSchemes().contains(ReleaseScheme.SNAPSHOT) );
151 adminRepo.setBlockRedeployments( repo.blocksRedeployments() );
152 adminRepo.setCronExpression( repo.getSchedulingDefinition() );
153 if (repo.supportsFeature( IndexCreationFeature.class )) {
154 IndexCreationFeature icf = repo.getFeature( IndexCreationFeature.class ).get();
155 adminRepo.setSkipPackedIndexCreation( icf.isSkipPackedIndexCreation() );
157 adminRepo.setScanned( repo.isScanned() );
158 if (repo.supportsFeature( ArtifactCleanupFeature.class) ) {
159 ArtifactCleanupFeature acf = repo.getFeature( ArtifactCleanupFeature.class ).get();
160 adminRepo.setRetentionPeriod( acf.getRetentionPeriod().getDays() );
161 adminRepo.setRetentionCount( acf.getRetentionCount() );
162 adminRepo.setDeleteReleasedSnapshots( acf.isDeleteReleasedSnapshots() );
165 if (repo.supportsFeature( StagingRepositoryFeature.class )) {
166 StagingRepositoryFeature stf = repo.getFeature( StagingRepositoryFeature.class ).get();
167 adminRepo.setStageRepoNeeded( stf.isStageRepoNeeded() );
168 if (stf.getStagingRepository()!=null) {
169 adminRepo.setStagingRepository( convertRepo( stf.getStagingRepository() ) );
175 private ManagedRepositoryConfiguration getRepositoryConfiguration(ManagedRepository repo) {
176 ManagedRepositoryConfiguration repoConfig = new ManagedRepositoryConfiguration();
177 setBaseRepoAttributes( repoConfig, repo );
178 repoConfig.setBlockRedeployments( repo.isBlockRedeployments( ) );
179 repoConfig.setReleases( repo.isReleases() );
180 repoConfig.setSnapshots( repo.isSnapshots() );
181 repoConfig.setScanned( repo.isScanned() );
182 repoConfig.setLocation( getRepositoryCommonValidator().removeExpressions( repo.getLocation() ) );
183 repoConfig.setRefreshCronExpression( repo.getCronExpression() );
184 repoConfig.setRetentionPeriod( repo.getRetentionPeriod() );
185 repoConfig.setRetentionCount( repo.getRetentionCount());
186 repoConfig.setDeleteReleasedSnapshots( repo.isDeleteReleasedSnapshots() );
187 repoConfig.setSkipPackedIndexCreation( repo.isSkipPackedIndexCreation());
188 repoConfig.setStageRepoNeeded( repo.isStageRepoNeeded() );
194 public List<ManagedRepository> getManagedRepositories()
195 throws RepositoryAdminException
198 return repositoryRegistry.getManagedRepositories().stream().map( rep -> this.convertRepo( rep ) ).collect( Collectors.toList());
202 public Map<String, ManagedRepository> getManagedRepositoriesAsMap()
203 throws RepositoryAdminException
205 return repositoryRegistry.getManagedRepositories().stream().collect( Collectors.toMap( e -> e.getId(), e -> convertRepo( e ) ) );
209 public ManagedRepository getManagedRepository( String repositoryId )
210 throws RepositoryAdminException
212 return convertRepo( repositoryRegistry.getManagedRepository( repositoryId ) );
216 public Boolean addManagedRepository( ManagedRepository managedRepository, boolean needStageRepo,
217 AuditInformation auditInformation )
218 throws RepositoryAdminException
220 log.debug("addManagedRepository {}, {}, {}", managedRepository.getId(), needStageRepo, auditInformation);
222 getRepositoryCommonValidator().basicValidation( managedRepository, false );
223 getRepositoryCommonValidator().validateManagedRepository( managedRepository );
224 triggerAuditEvent( managedRepository.getId(), null, AuditEvent.ADD_MANAGED_REPO, auditInformation );
225 ManagedRepositoryConfiguration repoConfig = getRepositoryConfiguration( managedRepository );
227 repoConfig.setStageRepoNeeded( true );
229 Configuration configuration = getArchivaConfiguration().getConfiguration();
232 org.apache.archiva.repository.ManagedRepository newRepo = repositoryRegistry.putRepository( repoConfig );
233 log.debug("Added new repository {}", newRepo.getId());
234 addRepositoryRoles( newRepo.getId() );
235 //MRM-1342 Repository statistics report doesn't appear to be working correctly
236 //scan repository when adding of repository is successful
239 if ( newRepo.isScanned())
241 scanRepository( newRepo.getId(), true );
244 org.apache.archiva.repository.ManagedRepository stagingRepo = newRepo.getFeature( StagingRepositoryFeature.class ).get( ).getStagingRepository( );
245 if ( stagingRepo!=null)
247 if (stagingRepo.isScanned()) {
248 scanRepository( stagingRepo.getId(), true );
250 addRepositoryRoles( stagingRepo.getId( ) );
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;
288 if (repo.supportsFeature(StagingRepositoryFeature.class)) {
289 stagingRepository = repo.getFeature(StagingRepositoryFeature.class).get().getStagingRepository();
292 throw new RepositoryAdminException("A repository with that id does not exist");
295 triggerAuditEvent(repositoryId, null, AuditEvent.DELETE_MANAGED_REPO, auditInformation);
296 if (repoConfig != null) {
297 deleteManagedRepository(repoConfig, deleteContent, config, false);
301 // stage repo exists ?
302 if (stagingRepository != null) {
303 // do not trigger event when deleting the staged one
304 ManagedRepositoryConfiguration stagingRepositoryConfig = config.findManagedRepositoryById(stagingRepository.getId());
305 if (stagingRepositoryConfig != null) {
306 deleteManagedRepository(stagingRepositoryConfig, deleteContent, config, true);
311 saveConfiguration(config);
312 } catch (Exception e) {
313 throw new RepositoryAdminException("Error saving configuration for delete action" + e.getMessage(), e);
318 return Boolean.FALSE;
322 private Boolean deleteManagedRepository( ManagedRepositoryConfiguration repository, boolean deleteContent,
323 Configuration config, boolean stagedOne )
324 throws RepositoryAdminException
329 boolean success=false;
330 try(RepositorySession repositorySession = getRepositorySessionFactory().createSession())
332 MetadataRepository metadataRepository = repositorySession.getRepository();
333 metadataRepository.removeRepository(repositorySession , repository.getId() );
335 namespacesCache.remove( repository.getId() );
336 repositorySession.save();
339 catch ( MetadataRepositoryException e )
341 //throw new RepositoryAdminException( e.getMessage(), e );
342 log.warn( "skip error during removing repository from MetadataRepository:{}", e.getMessage(), e );
344 } catch (MetadataSessionException e) {
345 log.warn( "skip error during removing repository from MetadataRepository:{}", e.getMessage(), e );
350 log.debug( "call repositoryStatisticsManager.deleteStatistics" );
353 getRepositoryStatisticsManager( ).deleteStatistics( repository.getId( ) );
355 catch ( MetadataRepositoryException e )
357 e.printStackTrace( );
364 // olamy: copy list for reading as a unit test in webapp fail with ConcurrentModificationException
365 List<ProxyConnectorConfiguration> proxyConnectors = new ArrayList<>( config.getProxyConnectors() );
366 for ( ProxyConnectorConfiguration proxyConnector : proxyConnectors )
368 if ( StringUtils.equals( proxyConnector.getSourceRepoId(), repository.getId() ) )
370 config.removeProxyConnector( proxyConnector );
375 removeRepositoryRoles( repository );
377 catch ( RoleManagerException e )
379 throw new RepositoryAdminException(
380 "fail to remove repository roles for repository " + repository.getId() + " : " + e.getMessage(), e );
384 org.apache.archiva.repository.ManagedRepository repo = repositoryRegistry.getManagedRepository( repository.getId( ) );
387 repositoryRegistry.removeRepository( repo, config );
390 // TODO could be async ? as directory can be huge
391 Path dir = Paths.get( repository.getLocation( ) );
392 org.apache.archiva.common.utils.FileUtils.deleteQuietly( dir );
395 } catch (RepositoryException e) {
396 throw new RepositoryAdminException("Removal of repository "+repository.getId()+ " failed: "+e.getMessage());
399 saveConfiguration( config );
405 ArchivaIndexManager getIndexManager(ManagedRepository managedRepository) {
406 org.apache.archiva.repository.ManagedRepository repo = getRepositoryRegistry().getManagedRepository(managedRepository.getId());
407 return indexManagerFactory.getIndexManager(repo.getType());
411 public Boolean updateManagedRepository( ManagedRepository managedRepository, boolean needStageRepo,
412 AuditInformation auditInformation, boolean resetStats )
413 throws RepositoryAdminException
416 log.debug( "updateManagedConfiguration repo {} needStage {} resetStats {} ", managedRepository, needStageRepo,
419 // Ensure that the fields are valid.
421 getRepositoryCommonValidator().basicValidation( managedRepository, true );
423 getRepositoryCommonValidator().validateManagedRepository( managedRepository );
425 ManagedRepositoryConfiguration updatedRepoConfig = getRepositoryConfiguration( managedRepository );
426 updatedRepoConfig.setStageRepoNeeded( needStageRepo );
428 org.apache.archiva.repository.ManagedRepository oldRepo = repositoryRegistry.getManagedRepository( managedRepository.getId( ) );
429 boolean stagingExists = false;
430 if (oldRepo.supportsFeature( StagingRepositoryFeature.class ) ){
431 stagingExists = oldRepo.getFeature( StagingRepositoryFeature.class ).get().getStagingRepository() != null;
433 boolean updateIndexContext = !StringUtils.equals( updatedRepoConfig.getIndexDir(), managedRepository.getIndexDirectory() );
434 org.apache.archiva.repository.ManagedRepository newRepo;
435 // TODO remove content from old if path has changed !!!!!
438 newRepo = repositoryRegistry.putRepository( updatedRepoConfig );
439 if (newRepo.supportsFeature( StagingRepositoryFeature.class )) {
440 org.apache.archiva.repository.ManagedRepository stagingRepo = newRepo.getFeature( StagingRepositoryFeature.class ).get( ).getStagingRepository( );
441 if (stagingRepo!=null && !stagingExists)
443 triggerAuditEvent( stagingRepo.getId(), null, AuditEvent.ADD_MANAGED_REPO, auditInformation );
444 addRepositoryRoles( stagingRepo.getId( ) );
450 catch ( RepositoryException e )
452 log.error("Could not update repository {}: {}", managedRepository.getId(), e.getMessage(), e);
453 throw new RepositoryAdminException( "Could not update repository "+managedRepository.getId());
455 catch ( RoleManagerException e ) {
456 log.error("Error during role update of stage repo {}", managedRepository.getId(), e);
457 throw new RepositoryAdminException( "Could not update repository "+managedRepository.getId());
459 triggerAuditEvent( managedRepository.getId(), null, AuditEvent.MODIFY_MANAGED_REPO,
461 // Save the repository configuration.
462 RepositorySession repositorySession = null;
465 repositorySession = getRepositorySessionFactory().createSession();
467 catch ( MetadataRepositoryException e )
469 e.printStackTrace( );
477 log.debug( "call repositoryStatisticsManager.deleteStatistics" );
478 getRepositoryStatisticsManager().deleteStatistics(
479 managedRepository.getId() );
480 repositorySession.save();
484 catch (MetadataRepositoryException | MetadataSessionException e )
486 throw new RepositoryAdminException( e.getMessage(), e );
490 repositorySession.close();
493 if ( updateIndexContext )
498 repositoryRegistry.resetIndexingContext(newRepo);
499 } catch (IndexUpdateFailedException e) {
507 //--------------------------
509 //--------------------------
512 protected void addRepository( ManagedRepositoryConfiguration repository, Configuration configuration )
513 throws RepositoryAdminException, IOException
517 getRepositoryRegistry().putRepository( repository, configuration );
519 catch ( RepositoryException e )
521 throw new RepositoryAdminException( "Could not add the repository to the registry. Cause: "+e.getMessage() );
527 public Boolean scanRepository( String repositoryId, boolean fullScan )
529 if ( getRepositoryTaskScheduler().isProcessingRepositoryTask( repositoryId ) )
531 log.info( "scanning of repository with id {} already scheduled", repositoryId );
533 RepositoryTask task = new RepositoryTask();
534 task.setRepositoryId( repositoryId );
535 task.setScanAll( fullScan );
538 getRepositoryTaskScheduler().queueTask( task );
540 catch ( TaskQueueException e )
542 log.error( "failed to schedule scanning of repo with id {}", repositoryId, e );
549 private void addRepositoryRoles( String repoId )
550 throws RoleManagerException
552 // TODO: double check these are configured on start up
553 // TODO: belongs in the business logic
555 if ( !getRoleManager().templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId ) )
557 getRoleManager().createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId );
560 if ( !getRoleManager().templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoId ) )
562 getRoleManager().createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoId );
566 protected void removeRepositoryRoles( ManagedRepositoryConfiguration existingRepository )
567 throws RoleManagerException
569 String repoId = existingRepository.getId();
571 if ( getRoleManager().templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoId ) )
573 getRoleManager().removeTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoId );
576 if ( getRoleManager().templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId ) )
578 getRoleManager().removeTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId );
581 log.debug( "removed user roles associated with repository {}", repoId );
584 //--------------------------
586 //--------------------------
589 public RoleManager getRoleManager()
594 public void setRoleManager( RoleManager roleManager )
596 this.roleManager = roleManager;
599 public RepositoryStatisticsManager getRepositoryStatisticsManager()
601 return repositoryStatisticsManager;
604 public void setRepositoryStatisticsManager( RepositoryStatisticsManager repositoryStatisticsManager )
606 this.repositoryStatisticsManager = repositoryStatisticsManager;
609 public RepositorySessionFactory getRepositorySessionFactory()
611 return repositorySessionFactory;
614 public void setRepositorySessionFactory( RepositorySessionFactory repositorySessionFactory )
616 this.repositorySessionFactory = repositorySessionFactory;
620 public RepositoryArchivaTaskScheduler getRepositoryTaskScheduler()
622 return repositoryTaskScheduler;
625 public void setRepositoryTaskScheduler( RepositoryArchivaTaskScheduler repositoryTaskScheduler )
627 this.repositoryTaskScheduler = repositoryTaskScheduler;
631 public RepositoryRegistry getRepositoryRegistry( )
633 return repositoryRegistry;
636 public void setRepositoryRegistry( RepositoryRegistry repositoryRegistry )
638 this.repositoryRegistry = repositoryRegistry;