You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DefaultManagedRepositoryAdmin.java 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. package org.apache.archiva.admin.repository.managed;
  2. /*
  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
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  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
  18. * under the License.
  19. */
  20. import org.apache.archiva.admin.model.AuditInformation;
  21. import org.apache.archiva.admin.model.RepositoryAdminException;
  22. import org.apache.archiva.admin.model.beans.ManagedRepository;
  23. import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
  24. import org.apache.archiva.admin.repository.AbstractRepositoryAdmin;
  25. import org.apache.archiva.configuration.Configuration;
  26. import org.apache.archiva.configuration.IndeterminateConfigurationException;
  27. import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
  28. import org.apache.archiva.configuration.ProxyConnectorConfiguration;
  29. import org.apache.archiva.configuration.RepositoryGroupConfiguration;
  30. import org.apache.archiva.indexer.ArchivaIndexManager;
  31. import org.apache.archiva.indexer.IndexManagerFactory;
  32. import org.apache.archiva.indexer.IndexUpdateFailedException;
  33. import org.apache.archiva.metadata.model.facets.AuditEvent;
  34. import org.apache.archiva.metadata.repository.MetadataRepository;
  35. import org.apache.archiva.metadata.repository.MetadataRepositoryException;
  36. import org.apache.archiva.metadata.repository.RepositorySession;
  37. import org.apache.archiva.metadata.repository.RepositorySessionFactory;
  38. import org.apache.archiva.metadata.repository.stats.model.RepositoryStatisticsManager;
  39. import org.apache.archiva.redback.components.cache.Cache;
  40. import org.apache.archiva.redback.components.registry.RegistryException;
  41. import org.apache.archiva.redback.components.taskqueue.TaskQueueException;
  42. import org.apache.archiva.redback.role.RoleManager;
  43. import org.apache.archiva.redback.role.RoleManagerException;
  44. import org.apache.archiva.repository.ReleaseScheme;
  45. import org.apache.archiva.repository.RepositoryException;
  46. import org.apache.archiva.repository.RepositoryRegistry;
  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.lang.StringUtils;
  54. import org.slf4j.Logger;
  55. import org.slf4j.LoggerFactory;
  56. import org.springframework.stereotype.Service;
  57. import javax.annotation.PostConstruct;
  58. import javax.annotation.PreDestroy;
  59. import javax.inject.Inject;
  60. import javax.inject.Named;
  61. import java.io.IOException;
  62. import java.nio.file.Path;
  63. import java.nio.file.Paths;
  64. import java.util.ArrayList;
  65. import java.util.Collection;
  66. import java.util.List;
  67. import java.util.Map;
  68. import java.util.stream.Collectors;
  69. /**
  70. * FIXME review the staging mechanism to have a per user session one
  71. *
  72. * @author Olivier Lamy
  73. */
  74. @Service("managedRepositoryAdmin#default")
  75. public class DefaultManagedRepositoryAdmin
  76. extends AbstractRepositoryAdmin
  77. implements ManagedRepositoryAdmin
  78. {
  79. private Logger log = LoggerFactory.getLogger( getClass() );
  80. public static final String STAGE_REPO_ID_END = "-stage";
  81. @Inject
  82. private RepositoryRegistry repositoryRegistry;
  83. @Inject
  84. @Named(value = "archivaTaskScheduler#repository")
  85. private RepositoryArchivaTaskScheduler repositoryTaskScheduler;
  86. /**
  87. * FIXME: this could be multiple implementations and needs to be configured.
  88. */
  89. @Inject
  90. private RepositorySessionFactory repositorySessionFactory;
  91. @Inject
  92. private RepositoryStatisticsManager repositoryStatisticsManager;
  93. @Inject
  94. protected RoleManager roleManager;
  95. @Inject
  96. @Named(value = "cache#namespaces")
  97. private Cache<String, Collection<String>> namespacesCache;
  98. @Inject
  99. private IndexManagerFactory indexManagerFactory;
  100. @PostConstruct
  101. public void initialize()
  102. throws RepositoryAdminException, RoleManagerException
  103. {
  104. // initialize index context on start and check roles here
  105. for ( ManagedRepository managedRepository : getManagedRepositories() )
  106. {
  107. log.debug("Initializating {}", managedRepository.getId());
  108. addRepositoryRoles( managedRepository.getId() );
  109. }
  110. }
  111. @PreDestroy
  112. public void shutdown()
  113. throws RepositoryAdminException
  114. {
  115. }
  116. /*
  117. * Conversion between the repository from the registry and the serialized DTO for the admin API
  118. */
  119. private ManagedRepository convertRepo( org.apache.archiva.repository.ManagedRepository repo ) {
  120. if (repo==null) {
  121. return null;
  122. }
  123. ManagedRepository adminRepo = new ManagedRepository( getArchivaConfiguration().getDefaultLocale() );
  124. setBaseRepoAttributes( adminRepo, repo );
  125. adminRepo.setLocation( convertUriToString( repo.getLocation()) );
  126. adminRepo.setReleases(repo.getActiveReleaseSchemes().contains( ReleaseScheme.RELEASE ));
  127. adminRepo.setSnapshots( repo.getActiveReleaseSchemes().contains(ReleaseScheme.SNAPSHOT) );
  128. adminRepo.setBlockRedeployments( repo.blocksRedeployments() );
  129. adminRepo.setCronExpression( repo.getSchedulingDefinition() );
  130. if (repo.supportsFeature( IndexCreationFeature.class )) {
  131. IndexCreationFeature icf = repo.getFeature( IndexCreationFeature.class ).get();
  132. adminRepo.setSkipPackedIndexCreation( icf.isSkipPackedIndexCreation() );
  133. }
  134. adminRepo.setScanned( repo.isScanned() );
  135. if (repo.supportsFeature( ArtifactCleanupFeature.class) ) {
  136. ArtifactCleanupFeature acf = repo.getFeature( ArtifactCleanupFeature.class ).get();
  137. adminRepo.setRetentionPeriod( acf.getRetentionPeriod().getDays() );
  138. adminRepo.setRetentionCount( acf.getRetentionCount() );
  139. adminRepo.setDeleteReleasedSnapshots( acf.isDeleteReleasedSnapshots() );
  140. }
  141. if (repo.supportsFeature( StagingRepositoryFeature.class )) {
  142. StagingRepositoryFeature stf = repo.getFeature( StagingRepositoryFeature.class ).get();
  143. adminRepo.setStageRepoNeeded( stf.isStageRepoNeeded() );
  144. if (stf.getStagingRepository()!=null) {
  145. adminRepo.setStagingRepository( convertRepo( stf.getStagingRepository() ) );
  146. }
  147. }
  148. return adminRepo;
  149. }
  150. private ManagedRepositoryConfiguration getRepositoryConfiguration(ManagedRepository repo) {
  151. ManagedRepositoryConfiguration repoConfig = new ManagedRepositoryConfiguration();
  152. setBaseRepoAttributes( repoConfig, repo );
  153. repoConfig.setBlockRedeployments( repo.isBlockRedeployments( ) );
  154. repoConfig.setReleases( repo.isReleases() );
  155. repoConfig.setSnapshots( repo.isSnapshots() );
  156. repoConfig.setScanned( repo.isScanned() );
  157. repoConfig.setLocation( getRepositoryCommonValidator().removeExpressions( repo.getLocation() ) );
  158. repoConfig.setRefreshCronExpression( repo.getCronExpression() );
  159. repoConfig.setRetentionPeriod( repo.getRetentionPeriod() );
  160. repoConfig.setRetentionCount( repo.getRetentionCount());
  161. repoConfig.setDeleteReleasedSnapshots( repo.isDeleteReleasedSnapshots() );
  162. repoConfig.setSkipPackedIndexCreation( repo.isSkipPackedIndexCreation());
  163. repoConfig.setStageRepoNeeded( repo.isStageRepoNeeded() );
  164. return repoConfig;
  165. }
  166. @Override
  167. public List<ManagedRepository> getManagedRepositories()
  168. throws RepositoryAdminException
  169. {
  170. return repositoryRegistry.getManagedRepositories().stream().map( rep -> this.convertRepo( rep ) ).collect( Collectors.toList());
  171. }
  172. @Override
  173. public Map<String, ManagedRepository> getManagedRepositoriesAsMap()
  174. throws RepositoryAdminException
  175. {
  176. return repositoryRegistry.getManagedRepositories().stream().collect( Collectors.toMap( e -> e.getId(), e -> convertRepo( e ) ) );
  177. }
  178. @Override
  179. public ManagedRepository getManagedRepository( String repositoryId )
  180. throws RepositoryAdminException
  181. {
  182. return convertRepo( repositoryRegistry.getManagedRepository( repositoryId ) );
  183. }
  184. @Override
  185. public Boolean addManagedRepository( ManagedRepository managedRepository, boolean needStageRepo,
  186. AuditInformation auditInformation )
  187. throws RepositoryAdminException
  188. {
  189. log.debug("addManagedRepository {}, {}, {}", managedRepository.getId(), needStageRepo, auditInformation);
  190. getRepositoryCommonValidator().basicValidation( managedRepository, false );
  191. getRepositoryCommonValidator().validateManagedRepository( managedRepository );
  192. triggerAuditEvent( managedRepository.getId(), null, AuditEvent.ADD_MANAGED_REPO, auditInformation );
  193. ManagedRepositoryConfiguration repoConfig = getRepositoryConfiguration( managedRepository );
  194. if (needStageRepo) {
  195. repoConfig.setStageRepoNeeded( true );
  196. }
  197. Configuration configuration = getArchivaConfiguration().getConfiguration();
  198. try
  199. {
  200. org.apache.archiva.repository.ManagedRepository newRepo = repositoryRegistry.putRepository( repoConfig, configuration );
  201. log.debug("Added new repository {}", newRepo.getId());
  202. org.apache.archiva.repository.ManagedRepository stagingRepo = null;
  203. addRepositoryRoles( newRepo.getId() );
  204. if ( newRepo.supportsFeature( StagingRepositoryFeature.class )) {
  205. StagingRepositoryFeature stf = newRepo.getFeature( StagingRepositoryFeature.class ).get();
  206. stagingRepo = stf.getStagingRepository();
  207. if (stf.isStageRepoNeeded() && stagingRepo != null) {
  208. addRepositoryRoles( stagingRepo.getId() );
  209. triggerAuditEvent( stagingRepo.getId(), null, AuditEvent.ADD_MANAGED_REPO, auditInformation );
  210. }
  211. }
  212. saveConfiguration( configuration );
  213. //MRM-1342 Repository statistics report doesn't appear to be working correctly
  214. //scan repository when adding of repository is successful
  215. try
  216. {
  217. if ( newRepo.isScanned())
  218. {
  219. scanRepository( newRepo.getId(), true );
  220. }
  221. if ( stagingRepo!=null && stagingRepo.isScanned() )
  222. {
  223. scanRepository( stagingRepo.getId(), true );
  224. }
  225. }
  226. catch ( Exception e )
  227. {
  228. log.warn("Unable to scan repository [{}]: {}", newRepo.getId(), e.getMessage(), e);
  229. }
  230. }
  231. catch ( RepositoryException e )
  232. {
  233. log.error("Could not add managed repository {}"+managedRepository);
  234. throw new RepositoryAdminException( "Could not add repository "+e.getMessage() );
  235. }
  236. catch ( RoleManagerException e )
  237. {
  238. log.error("Could not add repository roles for repository [{}]: {}", managedRepository.getId(), e.getMessage(), e);
  239. throw new RepositoryAdminException( "Could not add roles to repository "+e.getMessage() );
  240. }
  241. return Boolean.TRUE;
  242. }
  243. @Override
  244. public Boolean deleteManagedRepository( String repositoryId, AuditInformation auditInformation,
  245. boolean deleteContent )
  246. throws RepositoryAdminException
  247. {
  248. Configuration config = getArchivaConfiguration().getConfiguration();
  249. ManagedRepositoryConfiguration repoConfig=config.findManagedRepositoryById( repositoryId );
  250. if (repoConfig!=null) {
  251. log.debug("Repo location " + repoConfig.getLocation());
  252. org.apache.archiva.repository.ManagedRepository repo = repositoryRegistry.getManagedRepository(repositoryId);
  253. org.apache.archiva.repository.ManagedRepository stagingRepository = null;
  254. if (repo != null) {
  255. try {
  256. if (repo.supportsFeature(StagingRepositoryFeature.class)) {
  257. stagingRepository = repo.getFeature(StagingRepositoryFeature.class).get().getStagingRepository();
  258. }
  259. repositoryRegistry.removeRepository(repo, config);
  260. } catch (RepositoryException e) {
  261. log.error("Removal of repository {} failed: {}", repositoryId, e.getMessage(), e);
  262. throw new RepositoryAdminException("Removal of repository " + repositoryId + " failed.");
  263. }
  264. } else {
  265. throw new RepositoryAdminException("A repository with that id does not exist");
  266. }
  267. triggerAuditEvent(repositoryId, null, AuditEvent.DELETE_MANAGED_REPO, auditInformation);
  268. if (repoConfig != null) {
  269. deleteManagedRepository(repoConfig, deleteContent, config, false);
  270. }
  271. // stage repo exists ?
  272. if (stagingRepository != null) {
  273. // do not trigger event when deleting the staged one
  274. ManagedRepositoryConfiguration stagingRepositoryConfig = config.findManagedRepositoryById(stagingRepository.getId());
  275. try {
  276. repositoryRegistry.removeRepository(stagingRepository);
  277. if (stagingRepositoryConfig != null) {
  278. deleteManagedRepository(stagingRepositoryConfig, deleteContent, config, true);
  279. }
  280. } catch (RepositoryException e) {
  281. log.error("Removal of staging repository {} failed: {}", stagingRepository.getId(), e.getMessage(), e);
  282. }
  283. }
  284. try {
  285. saveConfiguration(config);
  286. } catch (Exception e) {
  287. throw new RepositoryAdminException("Error saving configuration for delete action" + e.getMessage(), e);
  288. }
  289. return Boolean.TRUE;
  290. } else {
  291. return Boolean.FALSE;
  292. }
  293. }
  294. private Boolean deleteManagedRepository( ManagedRepositoryConfiguration repository, boolean deleteContent,
  295. Configuration config, boolean stagedOne )
  296. throws RepositoryAdminException
  297. {
  298. if ( !stagedOne )
  299. {
  300. RepositorySession repositorySession = getRepositorySessionFactory().createSession();
  301. try
  302. {
  303. MetadataRepository metadataRepository = repositorySession.getRepository();
  304. metadataRepository.removeRepository( repository.getId() );
  305. //invalidate cache
  306. namespacesCache.remove( repository.getId() );
  307. log.debug( "call repositoryStatisticsManager.deleteStatistics" );
  308. getRepositoryStatisticsManager().deleteStatistics( metadataRepository, repository.getId() );
  309. repositorySession.save();
  310. }
  311. catch ( MetadataRepositoryException e )
  312. {
  313. //throw new RepositoryAdminException( e.getMessage(), e );
  314. log.warn( "skip error during removing repository from MetadataRepository:{}", e.getMessage(), e );
  315. }
  316. finally
  317. {
  318. repositorySession.close();
  319. }
  320. }
  321. if ( deleteContent )
  322. {
  323. // TODO could be async ? as directory can be huge
  324. Path dir = Paths.get( repository.getLocation() );
  325. org.apache.archiva.common.utils.FileUtils.deleteQuietly( dir );
  326. }
  327. // olamy: copy list for reading as a unit test in webapp fail with ConcurrentModificationException
  328. List<ProxyConnectorConfiguration> proxyConnectors = new ArrayList<>( config.getProxyConnectors() );
  329. for ( ProxyConnectorConfiguration proxyConnector : proxyConnectors )
  330. {
  331. if ( StringUtils.equals( proxyConnector.getSourceRepoId(), repository.getId() ) )
  332. {
  333. config.removeProxyConnector( proxyConnector );
  334. }
  335. }
  336. Map<String, List<String>> repoToGroupMap = config.getRepositoryToGroupMap();
  337. if ( repoToGroupMap != null )
  338. {
  339. if ( repoToGroupMap.containsKey( repository.getId() ) )
  340. {
  341. List<String> repoGroups = repoToGroupMap.get( repository.getId() );
  342. for ( String repoGroup : repoGroups )
  343. {
  344. // copy to prevent UnsupportedOperationException
  345. RepositoryGroupConfiguration repositoryGroupConfiguration =
  346. config.findRepositoryGroupById( repoGroup );
  347. List<String> repos = new ArrayList<>( repositoryGroupConfiguration.getRepositories() );
  348. config.removeRepositoryGroup( repositoryGroupConfiguration );
  349. repos.remove( repository.getId() );
  350. repositoryGroupConfiguration.setRepositories( repos );
  351. config.addRepositoryGroup( repositoryGroupConfiguration );
  352. }
  353. }
  354. }
  355. try
  356. {
  357. removeRepositoryRoles( repository );
  358. }
  359. catch ( RoleManagerException e )
  360. {
  361. throw new RepositoryAdminException(
  362. "fail to remove repository roles for repository " + repository.getId() + " : " + e.getMessage(), e );
  363. }
  364. try {
  365. final RepositoryRegistry reg = getRepositoryRegistry();
  366. if (reg.getManagedRepository(repository.getId())!=null) {
  367. reg.removeRepository(reg.getManagedRepository(repository.getId()));
  368. }
  369. } catch (RepositoryException e) {
  370. throw new RepositoryAdminException("Removal of repository "+repository.getId()+ " failed: "+e.getMessage());
  371. }
  372. saveConfiguration( config );
  373. return Boolean.TRUE;
  374. }
  375. ArchivaIndexManager getIndexManager(ManagedRepository managedRepository) {
  376. org.apache.archiva.repository.ManagedRepository repo = getRepositoryRegistry().getManagedRepository(managedRepository.getId());
  377. return indexManagerFactory.getIndexManager(repo.getType());
  378. }
  379. @Override
  380. public Boolean updateManagedRepository( ManagedRepository managedRepository, boolean needStageRepo,
  381. AuditInformation auditInformation, boolean resetStats )
  382. throws RepositoryAdminException
  383. {
  384. log.debug( "updateManagedConfiguration repo {} needStage {} resetStats {} ", managedRepository, needStageRepo,
  385. resetStats );
  386. // Ensure that the fields are valid.
  387. getRepositoryCommonValidator().basicValidation( managedRepository, true );
  388. getRepositoryCommonValidator().validateManagedRepository( managedRepository );
  389. Configuration configuration = getArchivaConfiguration().getConfiguration();
  390. ManagedRepositoryConfiguration updatedRepoConfig = getRepositoryConfiguration( managedRepository );
  391. updatedRepoConfig.setStageRepoNeeded( needStageRepo );
  392. org.apache.archiva.repository.ManagedRepository oldRepo = repositoryRegistry.getManagedRepository( managedRepository.getId( ) );
  393. boolean stagingExists = false;
  394. if (oldRepo.supportsFeature( StagingRepositoryFeature.class ) ){
  395. stagingExists = oldRepo.getFeature( StagingRepositoryFeature.class ).get().getStagingRepository() != null;
  396. }
  397. boolean updateIndexContext = !StringUtils.equals( updatedRepoConfig.getIndexDir(), managedRepository.getIndexDirectory() );
  398. org.apache.archiva.repository.ManagedRepository newRepo;
  399. // TODO remove content from old if path has changed !!!!!
  400. try
  401. {
  402. newRepo = repositoryRegistry.putRepository( updatedRepoConfig, configuration );
  403. if (newRepo.supportsFeature( StagingRepositoryFeature.class )) {
  404. org.apache.archiva.repository.ManagedRepository stagingRepo = newRepo.getFeature( StagingRepositoryFeature.class ).get( ).getStagingRepository( );
  405. if (stagingRepo!=null && !stagingExists)
  406. {
  407. triggerAuditEvent( stagingRepo.getId(), null, AuditEvent.ADD_MANAGED_REPO, auditInformation );
  408. addRepositoryRoles( stagingRepo.getId( ) );
  409. }
  410. }
  411. }
  412. catch ( RepositoryException e )
  413. {
  414. log.error("Could not update repository {}: {}", managedRepository.getId(), e.getMessage(), e);
  415. throw new RepositoryAdminException( "Could not update repository "+managedRepository.getId());
  416. }
  417. catch ( RoleManagerException e ) {
  418. log.error("Error during role update of stage repo {}", managedRepository.getId(), e);
  419. throw new RepositoryAdminException( "Could not update repository "+managedRepository.getId());
  420. }
  421. triggerAuditEvent( managedRepository.getId(), null, AuditEvent.MODIFY_MANAGED_REPO,
  422. auditInformation );
  423. try
  424. {
  425. getArchivaConfiguration().save(configuration);
  426. }
  427. catch ( RegistryException | IndeterminateConfigurationException e )
  428. {
  429. log.error("Could not save repository configuration: {}", e.getMessage(), e);
  430. throw new RepositoryAdminException( "Could not save repository configuration: "+e.getMessage() );
  431. }
  432. // Save the repository configuration.
  433. RepositorySession repositorySession = getRepositorySessionFactory().createSession();
  434. try
  435. {
  436. if ( resetStats )
  437. {
  438. log.debug( "call repositoryStatisticsManager.deleteStatistics" );
  439. getRepositoryStatisticsManager().deleteStatistics( repositorySession.getRepository(),
  440. managedRepository.getId() );
  441. repositorySession.save();
  442. }
  443. }
  444. catch ( MetadataRepositoryException e )
  445. {
  446. throw new RepositoryAdminException( e.getMessage(), e );
  447. }
  448. finally
  449. {
  450. repositorySession.close();
  451. }
  452. if ( updateIndexContext )
  453. {
  454. try
  455. {
  456. repositoryRegistry.resetIndexingContext(newRepo);
  457. } catch (IndexUpdateFailedException e) {
  458. e.printStackTrace();
  459. }
  460. }
  461. return true;
  462. }
  463. //--------------------------
  464. // utils methods
  465. //--------------------------
  466. protected void addRepository( ManagedRepositoryConfiguration repository, Configuration configuration )
  467. throws RepositoryAdminException, IOException
  468. {
  469. try
  470. {
  471. getRepositoryRegistry().putRepository( repository, configuration );
  472. }
  473. catch ( RepositoryException e )
  474. {
  475. throw new RepositoryAdminException( "Could not add the repository to the registry. Cause: "+e.getMessage() );
  476. }
  477. }
  478. public Boolean scanRepository( String repositoryId, boolean fullScan )
  479. {
  480. if ( getRepositoryTaskScheduler().isProcessingRepositoryTask( repositoryId ) )
  481. {
  482. log.info( "scanning of repository with id {} already scheduled", repositoryId );
  483. }
  484. RepositoryTask task = new RepositoryTask();
  485. task.setRepositoryId( repositoryId );
  486. task.setScanAll( fullScan );
  487. try
  488. {
  489. getRepositoryTaskScheduler().queueTask( task );
  490. }
  491. catch ( TaskQueueException e )
  492. {
  493. log.error( "failed to schedule scanning of repo with id {}", repositoryId, e );
  494. return false;
  495. }
  496. return true;
  497. }
  498. private void addRepositoryRoles( String repoId )
  499. throws RoleManagerException
  500. {
  501. // TODO: double check these are configured on start up
  502. // TODO: belongs in the business logic
  503. if ( !getRoleManager().templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId ) )
  504. {
  505. getRoleManager().createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId );
  506. }
  507. if ( !getRoleManager().templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoId ) )
  508. {
  509. getRoleManager().createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoId );
  510. }
  511. }
  512. protected void removeRepositoryRoles( ManagedRepositoryConfiguration existingRepository )
  513. throws RoleManagerException
  514. {
  515. String repoId = existingRepository.getId();
  516. if ( getRoleManager().templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoId ) )
  517. {
  518. getRoleManager().removeTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoId );
  519. }
  520. if ( getRoleManager().templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId ) )
  521. {
  522. getRoleManager().removeTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId );
  523. }
  524. log.debug( "removed user roles associated with repository {}", repoId );
  525. }
  526. //--------------------------
  527. // setters/getters
  528. //--------------------------
  529. public RoleManager getRoleManager()
  530. {
  531. return roleManager;
  532. }
  533. public void setRoleManager( RoleManager roleManager )
  534. {
  535. this.roleManager = roleManager;
  536. }
  537. public RepositoryStatisticsManager getRepositoryStatisticsManager()
  538. {
  539. return repositoryStatisticsManager;
  540. }
  541. public void setRepositoryStatisticsManager( RepositoryStatisticsManager repositoryStatisticsManager )
  542. {
  543. this.repositoryStatisticsManager = repositoryStatisticsManager;
  544. }
  545. public RepositorySessionFactory getRepositorySessionFactory()
  546. {
  547. return repositorySessionFactory;
  548. }
  549. public void setRepositorySessionFactory( RepositorySessionFactory repositorySessionFactory )
  550. {
  551. this.repositorySessionFactory = repositorySessionFactory;
  552. }
  553. public RepositoryArchivaTaskScheduler getRepositoryTaskScheduler()
  554. {
  555. return repositoryTaskScheduler;
  556. }
  557. public void setRepositoryTaskScheduler( RepositoryArchivaTaskScheduler repositoryTaskScheduler )
  558. {
  559. this.repositoryTaskScheduler = repositoryTaskScheduler;
  560. }
  561. public RepositoryRegistry getRepositoryRegistry( )
  562. {
  563. return repositoryRegistry;
  564. }
  565. public void setRepositoryRegistry( RepositoryRegistry repositoryRegistry )
  566. {
  567. this.repositoryRegistry = repositoryRegistry;
  568. }
  569. }