Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

DefaultManagedRepositoryAdmin.java 33KB

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