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 33KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  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.audit.AuditEvent;
  26. import org.apache.archiva.common.plexusbridge.MavenIndexerUtils;
  27. import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
  28. import org.apache.archiva.common.plexusbridge.PlexusSisuBridgeException;
  29. import org.apache.archiva.configuration.Configuration;
  30. import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
  31. import org.apache.archiva.configuration.ProxyConnectorConfiguration;
  32. import org.apache.archiva.configuration.RepositoryGroupConfiguration;
  33. import org.apache.archiva.metadata.repository.MetadataRepository;
  34. import org.apache.archiva.metadata.repository.MetadataRepositoryException;
  35. import org.apache.archiva.metadata.repository.RepositorySession;
  36. import org.apache.archiva.metadata.repository.RepositorySessionFactory;
  37. import org.apache.archiva.metadata.repository.stats.RepositoryStatisticsManager;
  38. import org.apache.archiva.redback.components.cache.Cache;
  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.scheduler.repository.model.RepositoryArchivaTaskScheduler;
  43. import org.apache.archiva.scheduler.repository.model.RepositoryTask;
  44. import org.apache.archiva.security.common.ArchivaRoleConstants;
  45. import org.apache.commons.io.FileUtils;
  46. import org.apache.commons.lang.StringUtils;
  47. import org.apache.maven.index.NexusIndexer;
  48. import org.apache.maven.index.context.IndexCreator;
  49. import org.apache.maven.index.context.IndexingContext;
  50. import org.apache.maven.index.context.UnsupportedExistingLuceneIndexException;
  51. import org.slf4j.Logger;
  52. import org.slf4j.LoggerFactory;
  53. import org.springframework.stereotype.Service;
  54. import javax.annotation.PostConstruct;
  55. import javax.annotation.PreDestroy;
  56. import javax.inject.Inject;
  57. import javax.inject.Named;
  58. import java.io.File;
  59. import java.io.IOException;
  60. import java.net.MalformedURLException;
  61. import java.util.ArrayList;
  62. import java.util.Collection;
  63. import java.util.Collections;
  64. import java.util.HashMap;
  65. import java.util.List;
  66. import java.util.Map;
  67. /**
  68. * FIXME review the staging mechanism to have a per user session one
  69. *
  70. * @author Olivier Lamy
  71. */
  72. @Service( "managedRepositoryAdmin#default" )
  73. public class DefaultManagedRepositoryAdmin
  74. extends AbstractRepositoryAdmin
  75. implements ManagedRepositoryAdmin
  76. {
  77. private Logger log = LoggerFactory.getLogger( getClass() );
  78. public static final String STAGE_REPO_ID_END = "-stage";
  79. @Inject
  80. @Named( value = "archivaTaskScheduler#repository" )
  81. private RepositoryArchivaTaskScheduler repositoryTaskScheduler;
  82. /**
  83. * FIXME: this could be multiple implementations and needs to be configured.
  84. */
  85. @Inject
  86. private RepositorySessionFactory repositorySessionFactory;
  87. @Inject
  88. private RepositoryStatisticsManager repositoryStatisticsManager;
  89. @Inject
  90. private PlexusSisuBridge plexusSisuBridge;
  91. @Inject
  92. private MavenIndexerUtils mavenIndexerUtils;
  93. @Inject
  94. protected RoleManager roleManager;
  95. @Inject
  96. @Named( value = "cache#namespaces" )
  97. private Cache<String, Collection<String>> namespacesCache;
  98. // fields
  99. List<? extends IndexCreator> indexCreators;
  100. NexusIndexer indexer;
  101. @PostConstruct
  102. public void initialize()
  103. throws RepositoryAdminException, RoleManagerException
  104. {
  105. try
  106. {
  107. indexCreators = mavenIndexerUtils.getAllIndexCreators();
  108. indexer = plexusSisuBridge.lookup( NexusIndexer.class );
  109. }
  110. catch ( PlexusSisuBridgeException e )
  111. {
  112. throw new RepositoryAdminException( e.getMessage(), e );
  113. }
  114. // initialize index context on start and check roles here
  115. for ( ManagedRepository managedRepository : getManagedRepositories() )
  116. {
  117. createIndexContext( managedRepository );
  118. addRepositoryRoles( managedRepository.getId() );
  119. }
  120. }
  121. @PreDestroy
  122. public void shutdown()
  123. throws RepositoryAdminException
  124. {
  125. try
  126. {
  127. // close index on shutdown
  128. for ( ManagedRepository managedRepository : getManagedRepositories() )
  129. {
  130. IndexingContext context = indexer.getIndexingContexts().get( managedRepository.getId() );
  131. if ( context != null )
  132. {
  133. indexer.removeIndexingContext( context, false );
  134. }
  135. }
  136. }
  137. catch ( IOException e )
  138. {
  139. throw new RepositoryAdminException( e.getMessage(), e );
  140. }
  141. }
  142. public List<ManagedRepository> getManagedRepositories()
  143. throws RepositoryAdminException
  144. {
  145. List<ManagedRepositoryConfiguration> managedRepoConfigs =
  146. getArchivaConfiguration().getConfiguration().getManagedRepositories();
  147. if ( managedRepoConfigs == null )
  148. {
  149. return Collections.emptyList();
  150. }
  151. List<ManagedRepository> managedRepos = new ArrayList<ManagedRepository>( managedRepoConfigs.size() );
  152. for ( ManagedRepositoryConfiguration repoConfig : managedRepoConfigs )
  153. {
  154. ManagedRepository repo =
  155. new ManagedRepository( repoConfig.getId(), repoConfig.getName(), repoConfig.getLocation(),
  156. repoConfig.getLayout(), repoConfig.isSnapshots(), repoConfig.isReleases(),
  157. repoConfig.isBlockRedeployments(), repoConfig.getRefreshCronExpression(),
  158. repoConfig.getIndexDir(), repoConfig.isScanned(), repoConfig.getDaysOlder(),
  159. repoConfig.getRetentionCount(), repoConfig.isDeleteReleasedSnapshots(),
  160. repoConfig.isStageRepoNeeded() );
  161. repo.setDescription( repoConfig.getDescription() );
  162. repo.setSkipPackedIndexCreation( repoConfig.isSkipPackedIndexCreation() );
  163. managedRepos.add( repo );
  164. }
  165. return managedRepos;
  166. }
  167. public Map<String, ManagedRepository> getManagedRepositoriesAsMap()
  168. throws RepositoryAdminException
  169. {
  170. List<ManagedRepository> managedRepositories = getManagedRepositories();
  171. Map<String, ManagedRepository> repositoriesMap =
  172. new HashMap<String, ManagedRepository>( managedRepositories.size() );
  173. for ( ManagedRepository managedRepository : managedRepositories )
  174. {
  175. repositoriesMap.put( managedRepository.getId(), managedRepository );
  176. }
  177. return repositoriesMap;
  178. }
  179. public ManagedRepository getManagedRepository( String repositoryId )
  180. throws RepositoryAdminException
  181. {
  182. List<ManagedRepository> repos = getManagedRepositories();
  183. for ( ManagedRepository repo : repos )
  184. {
  185. if ( StringUtils.equals( repo.getId(), repositoryId ) )
  186. {
  187. return repo;
  188. }
  189. }
  190. return null;
  191. }
  192. public Boolean addManagedRepository( ManagedRepository managedRepository, boolean needStageRepo,
  193. AuditInformation auditInformation )
  194. throws RepositoryAdminException
  195. {
  196. getRepositoryCommonValidator().basicValidation( managedRepository, false );
  197. getRepositoryCommonValidator().validateManagedRepository( managedRepository );
  198. triggerAuditEvent( managedRepository.getId(), null, AuditEvent.ADD_MANAGED_REPO, auditInformation );
  199. Boolean res =
  200. addManagedRepository( managedRepository.getId(), managedRepository.getLayout(), managedRepository.getName(),
  201. managedRepository.getLocation(), managedRepository.isBlockRedeployments(),
  202. managedRepository.isReleases(), managedRepository.isSnapshots(), needStageRepo,
  203. managedRepository.getCronExpression(), managedRepository.getIndexDirectory(),
  204. managedRepository.getDaysOlder(), managedRepository.getRetentionCount(),
  205. managedRepository.isDeleteReleasedSnapshots(), managedRepository.getDescription(),
  206. managedRepository.isSkipPackedIndexCreation(), managedRepository.isScanned(),
  207. auditInformation, getArchivaConfiguration().getConfiguration() ) != null;
  208. createIndexContext( managedRepository );
  209. return res;
  210. }
  211. private ManagedRepositoryConfiguration addManagedRepository( String repoId, String layout, String name,
  212. String location, boolean blockRedeployments,
  213. boolean releasesIncluded, boolean snapshotsIncluded,
  214. boolean stageRepoNeeded, String cronExpression,
  215. String indexDir, int daysOlder, int retentionCount,
  216. boolean deteleReleasedSnapshots, String description,
  217. boolean skipPackedIndexCreation, boolean scanned,
  218. AuditInformation auditInformation,
  219. Configuration config )
  220. throws RepositoryAdminException
  221. {
  222. ManagedRepositoryConfiguration repository = new ManagedRepositoryConfiguration();
  223. repository.setId( repoId );
  224. repository.setBlockRedeployments( blockRedeployments );
  225. repository.setReleases( releasesIncluded );
  226. repository.setSnapshots( snapshotsIncluded );
  227. repository.setScanned( scanned );
  228. repository.setName( name );
  229. repository.setLocation( getRepositoryCommonValidator().removeExpressions( location ) );
  230. repository.setLayout( layout );
  231. repository.setRefreshCronExpression( cronExpression );
  232. repository.setIndexDir( indexDir );
  233. repository.setDaysOlder( daysOlder );
  234. repository.setRetentionCount( retentionCount );
  235. repository.setDeleteReleasedSnapshots( deteleReleasedSnapshots );
  236. repository.setIndexDir( indexDir );
  237. repository.setDescription( description );
  238. repository.setSkipPackedIndexCreation( skipPackedIndexCreation );
  239. repository.setStageRepoNeeded( stageRepoNeeded );
  240. try
  241. {
  242. addRepository( repository, config );
  243. addRepositoryRoles( repository.getId() );
  244. if ( stageRepoNeeded )
  245. {
  246. ManagedRepositoryConfiguration stagingRepository = getStageRepoConfig( repository );
  247. addRepository( stagingRepository, config );
  248. addRepositoryRoles( stagingRepository.getId() );
  249. triggerAuditEvent( stagingRepository.getId(), null, AuditEvent.ADD_MANAGED_REPO, auditInformation );
  250. }
  251. }
  252. catch ( RoleManagerException e )
  253. {
  254. throw new RepositoryAdminException( "failed to add repository roles " + e.getMessage(), e );
  255. }
  256. catch ( IOException e )
  257. {
  258. throw new RepositoryAdminException( "failed to add repository " + e.getMessage(), e );
  259. }
  260. saveConfiguration( config );
  261. //MRM-1342 Repository statistics report doesn't appear to be working correctly
  262. //scan repository when adding of repository is successful
  263. try
  264. {
  265. if ( scanned )
  266. {
  267. scanRepository( repoId, true );
  268. }
  269. // TODO need a better to define scanning or not for staged repo
  270. if ( stageRepoNeeded && scanned )
  271. {
  272. ManagedRepositoryConfiguration stagingRepository = getStageRepoConfig( repository );
  273. scanRepository( stagingRepository.getId(), true );
  274. }
  275. }
  276. catch ( Exception e )
  277. {
  278. log.warn( new StringBuilder( "Unable to scan repository [" ).append( repoId ).append( "]: " ).append(
  279. e.getMessage() ).toString(), e );
  280. }
  281. return repository;
  282. }
  283. public Boolean deleteManagedRepository( String repositoryId, AuditInformation auditInformation,
  284. boolean deleteContent )
  285. throws RepositoryAdminException
  286. {
  287. Configuration config = getArchivaConfiguration().getConfiguration();
  288. ManagedRepositoryConfiguration repository = config.findManagedRepositoryById( repositoryId );
  289. if ( repository == null )
  290. {
  291. throw new RepositoryAdminException( "A repository with that id does not exist" );
  292. }
  293. triggerAuditEvent( repositoryId, null, AuditEvent.DELETE_MANAGED_REPO, auditInformation );
  294. deleteManagedRepository( repository, deleteContent, config, false );
  295. // stage repo exists ?
  296. ManagedRepositoryConfiguration stagingRepository =
  297. getArchivaConfiguration().getConfiguration().findManagedRepositoryById( repositoryId + STAGE_REPO_ID_END );
  298. if ( stagingRepository != null )
  299. {
  300. // do not trigger event when deleting the staged one
  301. deleteManagedRepository( stagingRepository, deleteContent, config, true );
  302. }
  303. try
  304. {
  305. saveConfiguration( config );
  306. }
  307. catch ( Exception e )
  308. {
  309. throw new RepositoryAdminException( "Error saving configuration for delete action" + e.getMessage(), e );
  310. }
  311. return Boolean.TRUE;
  312. }
  313. private Boolean deleteManagedRepository( ManagedRepositoryConfiguration repository, boolean deleteContent,
  314. Configuration config, boolean stagedOne )
  315. throws RepositoryAdminException
  316. {
  317. try
  318. {
  319. NexusIndexer nexusIndexer = plexusSisuBridge.lookup( NexusIndexer.class );
  320. IndexingContext context = nexusIndexer.getIndexingContexts().get( repository.getId() );
  321. if ( context != null )
  322. {
  323. // delete content only if directory exists
  324. nexusIndexer.removeIndexingContext( context,
  325. deleteContent && context.getIndexDirectoryFile().exists() );
  326. }
  327. }
  328. catch ( PlexusSisuBridgeException e )
  329. {
  330. throw new RepositoryAdminException( e.getMessage(), e );
  331. }
  332. catch ( IOException e )
  333. {
  334. throw new RepositoryAdminException( e.getMessage(), e );
  335. }
  336. if ( !stagedOne )
  337. {
  338. RepositorySession repositorySession = getRepositorySessionFactory().createSession();
  339. try
  340. {
  341. MetadataRepository metadataRepository = repositorySession.getRepository();
  342. metadataRepository.removeRepository( repository.getId() );
  343. //invalidate cache
  344. namespacesCache.remove( repository.getId() );
  345. log.debug( "call repositoryStatisticsManager.deleteStatistics" );
  346. getRepositoryStatisticsManager().deleteStatistics( metadataRepository, repository.getId() );
  347. repositorySession.save();
  348. }
  349. catch ( MetadataRepositoryException e )
  350. {
  351. //throw new RepositoryAdminException( e.getMessage(), e );
  352. log.warn( "skip error during removing repository from MetadataRepository:{}", e.getMessage(), e );
  353. }
  354. finally
  355. {
  356. repositorySession.close();
  357. }
  358. }
  359. config.removeManagedRepository( repository );
  360. if ( deleteContent )
  361. {
  362. // TODO could be async ? as directory can be huge
  363. File dir = new File( repository.getLocation() );
  364. if ( !FileUtils.deleteQuietly( dir ) )
  365. {
  366. throw new RepositoryAdminException( "Cannot delete repository " + dir );
  367. }
  368. }
  369. // olamy: copy list for reading as a unit test in webapp fail with ConcurrentModificationException
  370. List<ProxyConnectorConfiguration> proxyConnectors =
  371. new ArrayList<>( config.getProxyConnectors() );
  372. for ( ProxyConnectorConfiguration proxyConnector : proxyConnectors )
  373. {
  374. if ( StringUtils.equals( proxyConnector.getSourceRepoId(), repository.getId() ) )
  375. {
  376. config.removeProxyConnector( proxyConnector );
  377. }
  378. }
  379. Map<String, List<String>> repoToGroupMap = config.getRepositoryToGroupMap();
  380. if ( repoToGroupMap != null )
  381. {
  382. if ( repoToGroupMap.containsKey( repository.getId() ) )
  383. {
  384. List<String> repoGroups = repoToGroupMap.get( repository.getId() );
  385. for ( String repoGroup : repoGroups )
  386. {
  387. // copy to prevent UnsupportedOperationException
  388. RepositoryGroupConfiguration repositoryGroupConfiguration =
  389. config.findRepositoryGroupById( repoGroup );
  390. List<String> repos = new ArrayList<>( repositoryGroupConfiguration.getRepositories() );
  391. config.removeRepositoryGroup( repositoryGroupConfiguration );
  392. repos.remove( repository.getId() );
  393. repositoryGroupConfiguration.setRepositories( repos );
  394. config.addRepositoryGroup( repositoryGroupConfiguration );
  395. }
  396. }
  397. }
  398. try
  399. {
  400. removeRepositoryRoles( repository );
  401. }
  402. catch ( RoleManagerException e )
  403. {
  404. throw new RepositoryAdminException(
  405. "fail to remove repository roles for repository " + repository.getId() + " : " + e.getMessage(), e );
  406. }
  407. saveConfiguration( config );
  408. return Boolean.TRUE;
  409. }
  410. public Boolean updateManagedRepository( ManagedRepository managedRepository, boolean needStageRepo,
  411. AuditInformation auditInformation, boolean resetStats )
  412. throws RepositoryAdminException
  413. {
  414. log.debug( "updateManagedConfiguration repo {} needStage {} resetStats {} ", managedRepository, needStageRepo,
  415. resetStats );
  416. // Ensure that the fields are valid.
  417. getRepositoryCommonValidator().basicValidation( managedRepository, true );
  418. getRepositoryCommonValidator().validateManagedRepository( managedRepository );
  419. Configuration configuration = getArchivaConfiguration().getConfiguration();
  420. ManagedRepositoryConfiguration toremove = configuration.findManagedRepositoryById( managedRepository.getId() );
  421. boolean updateIndexContext = false;
  422. if ( toremove != null )
  423. {
  424. configuration.removeManagedRepository( toremove );
  425. updateIndexContext = !StringUtils.equals( toremove.getIndexDir(), managedRepository.getIndexDirectory() );
  426. }
  427. ManagedRepositoryConfiguration stagingRepository = getStageRepoConfig( toremove );
  428. // TODO remove content from old if path has changed !!!!!
  429. if ( stagingRepository != null )
  430. {
  431. configuration.removeManagedRepository( stagingRepository );
  432. }
  433. ManagedRepositoryConfiguration managedRepositoryConfiguration =
  434. addManagedRepository( managedRepository.getId(), managedRepository.getLayout(), managedRepository.getName(),
  435. managedRepository.getLocation(), managedRepository.isBlockRedeployments(),
  436. managedRepository.isReleases(), managedRepository.isSnapshots(), needStageRepo,
  437. managedRepository.getCronExpression(), managedRepository.getIndexDirectory(),
  438. managedRepository.getDaysOlder(), managedRepository.getRetentionCount(),
  439. managedRepository.isDeleteReleasedSnapshots(), managedRepository.getDescription(),
  440. managedRepository.isSkipPackedIndexCreation(), managedRepository.isScanned(),
  441. auditInformation, getArchivaConfiguration().getConfiguration() );
  442. // Save the repository configuration.
  443. RepositorySession repositorySession = getRepositorySessionFactory().createSession();
  444. try
  445. {
  446. triggerAuditEvent( managedRepositoryConfiguration.getId(), null, AuditEvent.MODIFY_MANAGED_REPO,
  447. auditInformation );
  448. saveConfiguration( this.getArchivaConfiguration().getConfiguration() );
  449. if ( resetStats )
  450. {
  451. log.debug( "call repositoryStatisticsManager.deleteStatistics" );
  452. getRepositoryStatisticsManager().deleteStatistics( repositorySession.getRepository(),
  453. managedRepositoryConfiguration.getId() );
  454. repositorySession.save();
  455. }
  456. }
  457. catch ( MetadataRepositoryException e )
  458. {
  459. throw new RepositoryAdminException( e.getMessage(), e );
  460. }
  461. finally
  462. {
  463. repositorySession.close();
  464. }
  465. if ( updateIndexContext )
  466. {
  467. try
  468. {
  469. IndexingContext indexingContext = indexer.getIndexingContexts().get( managedRepository.getId() );
  470. if ( indexingContext != null )
  471. {
  472. indexer.removeIndexingContext( indexingContext, true );
  473. }
  474. // delete directory too as only content is deleted
  475. File indexDirectory = indexingContext.getIndexDirectoryFile();
  476. FileUtils.deleteDirectory( indexDirectory );
  477. createIndexContext( managedRepository );
  478. }
  479. catch ( IOException e )
  480. {
  481. throw new RepositoryAdminException( e.getMessage(), e );
  482. }
  483. }
  484. return true;
  485. }
  486. //--------------------------
  487. // utils methods
  488. //--------------------------
  489. protected void addRepository( ManagedRepositoryConfiguration repository, Configuration configuration )
  490. throws RepositoryAdminException, IOException
  491. {
  492. // Normalize the path
  493. File file = new File( repository.getLocation() );
  494. if ( !file.isAbsolute() )
  495. {
  496. // add appserver.base/repositories
  497. file = new File( getRegistry().getString( "appserver.base" ) + File.separatorChar + "repositories",
  498. repository.getLocation() );
  499. }
  500. repository.setLocation( file.getCanonicalPath() );
  501. if ( !file.exists() )
  502. {
  503. file.mkdirs();
  504. }
  505. if ( !file.exists() || !file.isDirectory() )
  506. {
  507. throw new RepositoryAdminException(
  508. "Unable to add repository - no write access, can not create the root directory: " + file );
  509. }
  510. configuration.addManagedRepository( repository );
  511. }
  512. public IndexingContext createIndexContext( ManagedRepository repository )
  513. throws RepositoryAdminException
  514. {
  515. IndexingContext context = indexer.getIndexingContexts().get( repository.getId() );
  516. if ( context != null )
  517. {
  518. log.debug( "skip creating repository indexingContent with id {} as already exists", repository.getId() );
  519. return context;
  520. }
  521. // take care first about repository location as can be relative
  522. File repositoryDirectory = new File( repository.getLocation() );
  523. if ( !repositoryDirectory.isAbsolute() )
  524. {
  525. repositoryDirectory =
  526. new File( getRegistry().getString( "appserver.base" ) + File.separatorChar + "repositories",
  527. repository.getLocation() );
  528. }
  529. if ( !repositoryDirectory.exists() )
  530. {
  531. repositoryDirectory.mkdirs();
  532. }
  533. try
  534. {
  535. String indexDir = repository.getIndexDirectory();
  536. //File managedRepository = new File( repository.getLocation() );
  537. File indexDirectory = null;
  538. if ( StringUtils.isNotBlank( indexDir ) )
  539. {
  540. indexDirectory = new File( repository.getIndexDirectory() );
  541. // not absolute so create it in repository directory
  542. if ( !indexDirectory.isAbsolute() )
  543. {
  544. indexDirectory = new File( repositoryDirectory, repository.getIndexDirectory() );
  545. }
  546. repository.setIndexDirectory( indexDirectory.getAbsolutePath() );
  547. }
  548. else
  549. {
  550. indexDirectory = new File( repositoryDirectory, ".indexer" );
  551. if ( !repositoryDirectory.isAbsolute() )
  552. {
  553. indexDirectory = new File( repositoryDirectory, ".indexer" );
  554. }
  555. repository.setIndexDirectory( indexDirectory.getAbsolutePath() );
  556. }
  557. if ( !indexDirectory.exists() )
  558. {
  559. indexDirectory.mkdirs();
  560. }
  561. context = indexer.getIndexingContexts().get( repository.getId() );
  562. if ( context == null )
  563. {
  564. context = indexer.addIndexingContext( repository.getId(), repository.getId(), repositoryDirectory,
  565. indexDirectory,
  566. repositoryDirectory.toURI().toURL().toExternalForm(),
  567. indexDirectory.toURI().toURL().toString(), indexCreators );
  568. context.setSearchable( repository.isScanned() );
  569. }
  570. return context;
  571. }
  572. catch ( MalformedURLException e )
  573. {
  574. throw new RepositoryAdminException( e.getMessage(), e );
  575. }
  576. catch ( IOException e )
  577. {
  578. throw new RepositoryAdminException( e.getMessage(), e );
  579. }
  580. catch ( UnsupportedExistingLuceneIndexException e )
  581. {
  582. throw new RepositoryAdminException( e.getMessage(), e );
  583. }
  584. }
  585. private ManagedRepositoryConfiguration getStageRepoConfig( ManagedRepositoryConfiguration repository )
  586. {
  587. ManagedRepositoryConfiguration stagingRepository = new ManagedRepositoryConfiguration();
  588. stagingRepository.setId( repository.getId() + STAGE_REPO_ID_END );
  589. stagingRepository.setLayout( repository.getLayout() );
  590. stagingRepository.setName( repository.getName() + STAGE_REPO_ID_END );
  591. stagingRepository.setBlockRedeployments( repository.isBlockRedeployments() );
  592. stagingRepository.setDaysOlder( repository.getDaysOlder() );
  593. stagingRepository.setDeleteReleasedSnapshots( repository.isDeleteReleasedSnapshots() );
  594. String path = repository.getLocation();
  595. int lastIndex = path.replace( '\\', '/' ).lastIndexOf( '/' );
  596. stagingRepository.setLocation( path.substring( 0, lastIndex ) + "/" + stagingRepository.getId() );
  597. if ( StringUtils.isNotBlank( repository.getIndexDir() ) )
  598. {
  599. File indexDir = new File( repository.getIndexDir() );
  600. // in case of absolute dir do not use the same
  601. if ( indexDir.isAbsolute() )
  602. {
  603. stagingRepository.setIndexDir( stagingRepository.getLocation() + "/.index" );
  604. }
  605. else
  606. {
  607. stagingRepository.setIndexDir( repository.getIndexDir() );
  608. }
  609. }
  610. stagingRepository.setRefreshCronExpression( repository.getRefreshCronExpression() );
  611. stagingRepository.setReleases( repository.isReleases() );
  612. stagingRepository.setRetentionCount( repository.getRetentionCount() );
  613. stagingRepository.setScanned( repository.isScanned() );
  614. stagingRepository.setSnapshots( repository.isSnapshots() );
  615. stagingRepository.setSkipPackedIndexCreation( repository.isSkipPackedIndexCreation() );
  616. // do not duplicate description
  617. //stagingRepository.getDescription("")
  618. return stagingRepository;
  619. }
  620. public Boolean scanRepository( String repositoryId, boolean fullScan )
  621. {
  622. if ( getRepositoryTaskScheduler().isProcessingRepositoryTask( repositoryId ) )
  623. {
  624. log.info( "scanning of repository with id {} already scheduled", repositoryId );
  625. }
  626. RepositoryTask task = new RepositoryTask();
  627. task.setRepositoryId( repositoryId );
  628. task.setScanAll( fullScan );
  629. try
  630. {
  631. getRepositoryTaskScheduler().queueTask( task );
  632. }
  633. catch ( TaskQueueException e )
  634. {
  635. log.error( "failed to schedule scanning of repo with id {}", repositoryId, e );
  636. return false;
  637. }
  638. return true;
  639. }
  640. private void addRepositoryRoles( String repoId )
  641. throws RoleManagerException
  642. {
  643. // TODO: double check these are configured on start up
  644. // TODO: belongs in the business logic
  645. if ( !getRoleManager().templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId ) )
  646. {
  647. getRoleManager().createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId );
  648. }
  649. if ( !getRoleManager().templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoId ) )
  650. {
  651. getRoleManager().createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoId );
  652. }
  653. }
  654. protected void removeRepositoryRoles( ManagedRepositoryConfiguration existingRepository )
  655. throws RoleManagerException
  656. {
  657. String repoId = existingRepository.getId();
  658. if ( getRoleManager().templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoId ) )
  659. {
  660. getRoleManager().removeTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoId );
  661. }
  662. if ( getRoleManager().templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId ) )
  663. {
  664. getRoleManager().removeTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId );
  665. }
  666. log.debug( "removed user roles associated with repository {}", repoId );
  667. }
  668. //--------------------------
  669. // setters/getters
  670. //--------------------------
  671. public RoleManager getRoleManager()
  672. {
  673. return roleManager;
  674. }
  675. public void setRoleManager( RoleManager roleManager )
  676. {
  677. this.roleManager = roleManager;
  678. }
  679. public RepositoryStatisticsManager getRepositoryStatisticsManager()
  680. {
  681. return repositoryStatisticsManager;
  682. }
  683. public void setRepositoryStatisticsManager( RepositoryStatisticsManager repositoryStatisticsManager )
  684. {
  685. this.repositoryStatisticsManager = repositoryStatisticsManager;
  686. }
  687. public RepositorySessionFactory getRepositorySessionFactory()
  688. {
  689. return repositorySessionFactory;
  690. }
  691. public void setRepositorySessionFactory( RepositorySessionFactory repositorySessionFactory )
  692. {
  693. this.repositorySessionFactory = repositorySessionFactory;
  694. }
  695. public RepositoryArchivaTaskScheduler getRepositoryTaskScheduler()
  696. {
  697. return repositoryTaskScheduler;
  698. }
  699. public void setRepositoryTaskScheduler( RepositoryArchivaTaskScheduler repositoryTaskScheduler )
  700. {
  701. this.repositoryTaskScheduler = repositoryTaskScheduler;
  702. }
  703. public PlexusSisuBridge getPlexusSisuBridge()
  704. {
  705. return plexusSisuBridge;
  706. }
  707. public void setPlexusSisuBridge( PlexusSisuBridge plexusSisuBridge )
  708. {
  709. this.plexusSisuBridge = plexusSisuBridge;
  710. }
  711. public MavenIndexerUtils getMavenIndexerUtils()
  712. {
  713. return mavenIndexerUtils;
  714. }
  715. public void setMavenIndexerUtils( MavenIndexerUtils mavenIndexerUtils )
  716. {
  717. this.mavenIndexerUtils = mavenIndexerUtils;
  718. }
  719. public NexusIndexer getIndexer()
  720. {
  721. return indexer;
  722. }
  723. public void setIndexer( NexusIndexer indexer )
  724. {
  725. this.indexer = indexer;
  726. }
  727. public List<? extends IndexCreator> getIndexCreators()
  728. {
  729. return indexCreators;
  730. }
  731. public void setIndexCreators( List<? extends IndexCreator> indexCreators )
  732. {
  733. this.indexCreators = indexCreators;
  734. }
  735. }