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.

MavenRepositoryProvider.java 28KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. package org.apache.archiva.maven.repository;
  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. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. import org.apache.archiva.common.filelock.FileLockManager;
  20. import org.apache.archiva.configuration.model.AbstractRepositoryConfiguration;
  21. import org.apache.archiva.configuration.provider.ArchivaConfiguration;
  22. import org.apache.archiva.configuration.model.ManagedRepositoryConfiguration;
  23. import org.apache.archiva.configuration.model.RemoteRepositoryConfiguration;
  24. import org.apache.archiva.configuration.model.RepositoryGroupConfiguration;
  25. import org.apache.archiva.event.Event;
  26. import org.apache.archiva.event.EventHandler;
  27. import org.apache.archiva.repository.EditableManagedRepository;
  28. import org.apache.archiva.repository.EditableRemoteRepository;
  29. import org.apache.archiva.repository.EditableRepository;
  30. import org.apache.archiva.repository.EditableRepositoryGroup;
  31. import org.apache.archiva.repository.ManagedRepository;
  32. import org.apache.archiva.repository.ReleaseScheme;
  33. import org.apache.archiva.repository.RemoteRepository;
  34. import org.apache.archiva.repository.Repository;
  35. import org.apache.archiva.repository.RepositoryCredentials;
  36. import org.apache.archiva.repository.RepositoryException;
  37. import org.apache.archiva.repository.RepositoryGroup;
  38. import org.apache.archiva.repository.RepositoryProvider;
  39. import org.apache.archiva.repository.RepositoryType;
  40. import org.apache.archiva.repository.UnsupportedURIException;
  41. import org.apache.archiva.repository.base.managed.BasicManagedRepository;
  42. import org.apache.archiva.repository.base.PasswordCredentials;
  43. import org.apache.archiva.repository.event.RepositoryEvent;
  44. import org.apache.archiva.repository.features.ArtifactCleanupFeature;
  45. import org.apache.archiva.repository.features.IndexCreationFeature;
  46. import org.apache.archiva.repository.features.RemoteIndexFeature;
  47. import org.apache.archiva.repository.features.StagingRepositoryFeature;
  48. import org.apache.archiva.repository.storage.fs.FilesystemStorage;
  49. import org.apache.commons.lang3.StringUtils;
  50. import org.slf4j.Logger;
  51. import org.slf4j.LoggerFactory;
  52. import org.springframework.stereotype.Service;
  53. import javax.inject.Inject;
  54. import java.io.IOException;
  55. import java.net.URI;
  56. import java.net.URISyntaxException;
  57. import java.nio.file.Files;
  58. import java.nio.file.Path;
  59. import java.nio.file.Paths;
  60. import java.time.Duration;
  61. import java.time.Period;
  62. import java.time.temporal.ChronoUnit;
  63. import java.util.ArrayList;
  64. import java.util.HashSet;
  65. import java.util.List;
  66. import java.util.Set;
  67. import java.util.stream.Collectors;
  68. import static org.apache.archiva.indexer.ArchivaIndexManager.DEFAULT_INDEX_PATH;
  69. import static org.apache.archiva.indexer.ArchivaIndexManager.DEFAULT_PACKED_INDEX_PATH;
  70. /**
  71. * Provider for the maven2 repository implementation
  72. */
  73. @Service("mavenRepositoryProvider")
  74. public class MavenRepositoryProvider implements RepositoryProvider {
  75. @Inject
  76. private ArchivaConfiguration archivaConfiguration;
  77. @Inject
  78. private FileLockManager fileLockManager;
  79. private final List<EventHandler<? super RepositoryEvent>> repositoryEventHandlers = new ArrayList<>( );
  80. private static final Logger log = LoggerFactory.getLogger(MavenRepositoryProvider.class);
  81. static final Set<RepositoryType> TYPES = new HashSet<>();
  82. static {
  83. TYPES.add(RepositoryType.MAVEN);
  84. }
  85. @Override
  86. public Set<RepositoryType> provides() {
  87. return TYPES;
  88. }
  89. @Override
  90. public MavenManagedRepository createManagedInstance(String id, String name) {
  91. return createManagedInstance(id, name, archivaConfiguration.getRemoteRepositoryBaseDir());
  92. }
  93. public MavenManagedRepository createManagedInstance(String id, String name, Path baseDir) {
  94. FilesystemStorage storage;
  95. try {
  96. storage = new FilesystemStorage(baseDir.resolve(id), fileLockManager);
  97. } catch (IOException e) {
  98. log.error("Could not initialize fileystem for repository {}", id);
  99. throw new RuntimeException(e);
  100. }
  101. MavenManagedRepository repo = new MavenManagedRepository( id, name, storage );
  102. registerEventHandler( repo );
  103. return repo;
  104. }
  105. @Override
  106. public MavenRemoteRepository createRemoteInstance(String id, String name) {
  107. return createRemoteInstance(id, name, archivaConfiguration.getRemoteRepositoryBaseDir());
  108. }
  109. public MavenRemoteRepository createRemoteInstance(String id, String name, Path baseDir) {
  110. FilesystemStorage storage;
  111. try {
  112. storage = new FilesystemStorage(baseDir.resolve(id), fileLockManager);
  113. } catch (IOException e) {
  114. log.error("Could not initialize filesystem for repository {}: '{}'", id, e.getMessage());
  115. throw new RuntimeException(e);
  116. }
  117. MavenRemoteRepository repo = new MavenRemoteRepository( id, name, storage );
  118. registerEventHandler( repo );
  119. return repo;
  120. }
  121. @Override
  122. public EditableRepositoryGroup createRepositoryGroup(String id, String name) {
  123. return createRepositoryGroup(id, name, archivaConfiguration.getRepositoryGroupBaseDir().resolve( id ));
  124. }
  125. public MavenRepositoryGroup createRepositoryGroup(String id, String name, Path repositoryPath) {
  126. FilesystemStorage storage;
  127. try {
  128. storage = new FilesystemStorage(repositoryPath, fileLockManager);
  129. } catch (IOException e) {
  130. log.error("Could not initialize filesystem for repository {}: '{}'", id, e.getMessage());
  131. throw new RuntimeException(e);
  132. }
  133. MavenRepositoryGroup group = new MavenRepositoryGroup( id, name, storage );
  134. registerEventHandler( group );
  135. return group;
  136. }
  137. private void registerEventHandler( Repository repo ) {
  138. for (EventHandler<? super RepositoryEvent> eventHandler : repositoryEventHandlers) {
  139. repo.registerEventHandler( RepositoryEvent.ANY, eventHandler );
  140. }
  141. }
  142. private URI getURIFromString(String uriStr) throws RepositoryException {
  143. URI uri;
  144. try {
  145. if (StringUtils.isEmpty(uriStr)) {
  146. return new URI("");
  147. }
  148. if (uriStr.startsWith("/")) {
  149. // only absolute paths are prepended with file scheme
  150. uri = new URI("file://" + uriStr);
  151. } else if (uriStr.contains(":\\")) {
  152. //windows absolute path drive
  153. uri = new URI("file:///" + uriStr.replaceAll("\\\\", "/"));
  154. } else {
  155. uri = new URI(uriStr);
  156. }
  157. if (uri.getScheme() != null && !"file".equals(uri.getScheme())) {
  158. log.error("Bad URI scheme found: {}, URI={}", uri.getScheme(), uri);
  159. throw new RepositoryException("The uri " + uriStr + " is not valid. Only file:// URI is allowed for maven.");
  160. }
  161. } catch (URISyntaxException e) {
  162. String newCfg = "file://" + uriStr;
  163. try {
  164. uri = new URI(newCfg);
  165. } catch (URISyntaxException e1) {
  166. log.error("Could not create URI from {} -> {}", uriStr, newCfg);
  167. throw new RepositoryException("The config entry " + uriStr + " cannot be converted to URI.");
  168. }
  169. }
  170. log.debug("Setting location uri: {}", uri);
  171. return uri;
  172. }
  173. @Override
  174. public ManagedRepository createManagedInstance(ManagedRepositoryConfiguration cfg) throws RepositoryException {
  175. MavenManagedRepository repo = createManagedInstance(cfg.getId(), cfg.getName(), Paths.get(cfg.getLocation()).getParent());
  176. updateManagedInstance(repo, cfg);
  177. return repo;
  178. }
  179. @Override
  180. public void updateManagedInstance(EditableManagedRepository repo, ManagedRepositoryConfiguration cfg) throws RepositoryException {
  181. try {
  182. repo.setLocation(getURIFromString(cfg.getLocation()));
  183. } catch (UnsupportedURIException e) {
  184. throw new RepositoryException("The location entry is not a valid uri: " + cfg.getLocation());
  185. }
  186. setBaseConfig(repo, cfg);
  187. Path repoDir = repo.getRoot().getFilePath();
  188. if (!Files.exists(repoDir)) {
  189. log.debug("Creating repo directory {}", repoDir);
  190. try {
  191. Files.createDirectories(repoDir);
  192. } catch (IOException e) {
  193. log.error("Could not create directory {} for repository {}", repoDir, repo.getId(), e);
  194. throw new RepositoryException("Could not create directory for repository " + repoDir);
  195. }
  196. }
  197. repo.setSchedulingDefinition(cfg.getRefreshCronExpression());
  198. repo.setBlocksRedeployment(cfg.isBlockRedeployments());
  199. repo.setScanned(cfg.isScanned());
  200. if (cfg.isReleases()) {
  201. repo.addActiveReleaseScheme(ReleaseScheme.RELEASE);
  202. } else {
  203. repo.removeActiveReleaseScheme(ReleaseScheme.RELEASE);
  204. }
  205. if (cfg.isSnapshots()) {
  206. repo.addActiveReleaseScheme(ReleaseScheme.SNAPSHOT);
  207. } else {
  208. repo.removeActiveReleaseScheme(ReleaseScheme.SNAPSHOT);
  209. }
  210. StagingRepositoryFeature stagingRepositoryFeature = repo.getFeature( StagingRepositoryFeature.class );
  211. stagingRepositoryFeature.setStageRepoNeeded(cfg.isStageRepoNeeded());
  212. IndexCreationFeature indexCreationFeature = repo.getFeature( IndexCreationFeature.class );
  213. indexCreationFeature.setSkipPackedIndexCreation(cfg.isSkipPackedIndexCreation());
  214. String indexDir = StringUtils.isEmpty( cfg.getIndexDir() ) ? DEFAULT_INDEX_PATH : cfg.getIndexDir();
  215. String packedIndexDir = StringUtils.isEmpty( cfg.getPackedIndexDir() ) ? DEFAULT_PACKED_INDEX_PATH : cfg.getPackedIndexDir();
  216. indexCreationFeature.setIndexPath(getURIFromString(indexDir));
  217. indexCreationFeature.setPackedIndexPath(getURIFromString(packedIndexDir));
  218. ArtifactCleanupFeature artifactCleanupFeature = repo.getFeature( ArtifactCleanupFeature.class );
  219. artifactCleanupFeature.setDeleteReleasedSnapshots(cfg.isDeleteReleasedSnapshots());
  220. artifactCleanupFeature.setRetentionCount(cfg.getRetentionCount());
  221. artifactCleanupFeature.setRetentionPeriod(Period.ofDays(cfg.getRetentionPeriod()));
  222. }
  223. @Override
  224. public ManagedRepository createStagingInstance(ManagedRepositoryConfiguration baseConfiguration) throws RepositoryException {
  225. log.debug("Creating staging instance for {}", baseConfiguration.getId());
  226. return createManagedInstance(getStageRepoConfig(baseConfiguration));
  227. }
  228. @Override
  229. public RemoteRepository createRemoteInstance(RemoteRepositoryConfiguration cfg) throws RepositoryException {
  230. MavenRemoteRepository repo = createRemoteInstance(cfg.getId(), cfg.getName(), archivaConfiguration.getRemoteRepositoryBaseDir());
  231. updateRemoteInstance(repo, cfg);
  232. return repo;
  233. }
  234. private String convertUriToPath(URI uri) {
  235. if (uri.getScheme() == null) {
  236. return uri.getPath();
  237. } else if ("file".equals(uri.getScheme())) {
  238. return Paths.get(uri).toString();
  239. } else {
  240. return uri.toString();
  241. }
  242. }
  243. @Override
  244. public void updateRemoteInstance(EditableRemoteRepository repo, RemoteRepositoryConfiguration cfg) throws RepositoryException {
  245. setBaseConfig(repo, cfg);
  246. repo.setCheckPath(cfg.getCheckPath());
  247. repo.setSchedulingDefinition(cfg.getRefreshCronExpression());
  248. try {
  249. repo.setLocation(new URI(cfg.getUrl()));
  250. } catch (UnsupportedURIException | URISyntaxException e) {
  251. log.error("Could not set remote url " + cfg.getUrl());
  252. throw new RepositoryException("The url config is not a valid uri: " + cfg.getUrl());
  253. }
  254. repo.setTimeout(Duration.ofSeconds(cfg.getTimeout()));
  255. RemoteIndexFeature remoteIndexFeature = repo.getFeature( RemoteIndexFeature.class );
  256. remoteIndexFeature.setDownloadRemoteIndex(cfg.isDownloadRemoteIndex());
  257. remoteIndexFeature.setDownloadRemoteIndexOnStartup(cfg.isDownloadRemoteIndexOnStartup());
  258. remoteIndexFeature.setDownloadTimeout(Duration.ofSeconds(cfg.getRemoteDownloadTimeout()));
  259. remoteIndexFeature.setProxyId(cfg.getRemoteDownloadNetworkProxyId());
  260. if (cfg.isDownloadRemoteIndex()) {
  261. try {
  262. remoteIndexFeature.setIndexUri(new URI(cfg.getRemoteIndexUrl()));
  263. } catch (URISyntaxException e) {
  264. log.error("Could not set remote index url " + cfg.getRemoteIndexUrl());
  265. remoteIndexFeature.setDownloadRemoteIndex(false);
  266. remoteIndexFeature.setDownloadRemoteIndexOnStartup(false);
  267. }
  268. }
  269. for ( Object key : cfg.getExtraHeaders().keySet() ) {
  270. repo.addExtraHeader( key.toString(), cfg.getExtraHeaders().get(key).toString() );
  271. }
  272. for ( Object key : cfg.getExtraParameters().keySet() ) {
  273. repo.addExtraParameter( key.toString(), cfg.getExtraParameters().get(key).toString() );
  274. }
  275. PasswordCredentials credentials = new PasswordCredentials("", new char[0]);
  276. if (cfg.getPassword() != null && cfg.getUsername() != null) {
  277. credentials.setPassword(cfg.getPassword().toCharArray());
  278. credentials.setUsername(cfg.getUsername());
  279. repo.setCredentials(credentials);
  280. } else {
  281. credentials.setPassword(new char[0]);
  282. }
  283. IndexCreationFeature indexCreationFeature = repo.getFeature( IndexCreationFeature.class );
  284. if ( !StringUtils.isEmpty( cfg.getIndexDir( ) ) )
  285. {
  286. indexCreationFeature.setIndexPath( getURIFromString( cfg.getIndexDir( ) ) );
  287. }
  288. if (!StringUtils.isEmpty( cfg.getPackedIndexDir() )) {
  289. indexCreationFeature.setPackedIndexPath(getURIFromString(cfg.getPackedIndexDir()));
  290. }
  291. log.debug("Updated remote instance {}", repo);
  292. }
  293. @Override
  294. public RepositoryGroup createRepositoryGroup(RepositoryGroupConfiguration configuration) throws RepositoryException {
  295. Path repositoryPath = getRepositoryGroupPath( configuration );
  296. MavenRepositoryGroup newGrp = createRepositoryGroup(configuration.getId(), configuration.getName(),
  297. repositoryPath);
  298. updateRepositoryGroupInstance(newGrp, configuration);
  299. return newGrp;
  300. }
  301. private Path getRepositoryGroupPath(RepositoryGroupConfiguration configuration) {
  302. if (StringUtils.isNotEmpty( configuration.getLocation() )) {
  303. return Paths.get( configuration.getLocation( ) );
  304. } else {
  305. return getArchivaConfiguration( ).getRepositoryGroupBaseDir( ).resolve( configuration.getId( ) );
  306. }
  307. }
  308. @Override
  309. public void updateRepositoryGroupInstance(EditableRepositoryGroup repositoryGroup, RepositoryGroupConfiguration configuration) throws RepositoryException {
  310. repositoryGroup.setName(repositoryGroup.getPrimaryLocale(), configuration.getName());
  311. repositoryGroup.setMergedIndexTTL(configuration.getMergedIndexTtl());
  312. repositoryGroup.setSchedulingDefinition(configuration.getCronExpression());
  313. if (repositoryGroup.supportsFeature( IndexCreationFeature.class )) {
  314. IndexCreationFeature indexCreationFeature = repositoryGroup.getFeature( IndexCreationFeature.class );
  315. indexCreationFeature.setIndexPath( getURIFromString(configuration.getMergedIndexPath()) );
  316. Path localPath = Paths.get(configuration.getMergedIndexPath());
  317. Path repoGroupPath = repositoryGroup.getRoot().getFilePath().toAbsolutePath();
  318. if (localPath.isAbsolute() && !localPath.startsWith(repoGroupPath)) {
  319. try {
  320. FilesystemStorage storage = new FilesystemStorage(localPath.getParent(), fileLockManager);
  321. indexCreationFeature.setLocalIndexPath(storage.getAsset(localPath.getFileName().toString()));
  322. } catch (IOException e) {
  323. throw new RepositoryException("Could not initialize storage for index path "+localPath);
  324. }
  325. } else if (localPath.isAbsolute()) {
  326. indexCreationFeature.setLocalIndexPath(repositoryGroup.getAsset(repoGroupPath.relativize(localPath).toString()));
  327. } else
  328. {
  329. indexCreationFeature.setLocalIndexPath(repositoryGroup.getAsset(localPath.toString()));
  330. }
  331. }
  332. // References to other repositories are set filled by the registry
  333. }
  334. @Override
  335. public RemoteRepositoryConfiguration getRemoteConfiguration(RemoteRepository remoteRepository) throws RepositoryException {
  336. if (!(remoteRepository instanceof MavenRemoteRepository)) {
  337. log.error("Wrong remote repository type " + remoteRepository.getClass().getName());
  338. throw new RepositoryException("The given repository type cannot be handled by the maven provider: " + remoteRepository.getClass().getName());
  339. }
  340. RemoteRepositoryConfiguration cfg = new RemoteRepositoryConfiguration();
  341. cfg.setType(remoteRepository.getType().toString());
  342. cfg.setId(remoteRepository.getId());
  343. cfg.setName(remoteRepository.getName());
  344. cfg.setDescription(remoteRepository.getDescription());
  345. cfg.setUrl(remoteRepository.getLocation().toString());
  346. cfg.setTimeout((int) remoteRepository.getTimeout().toMillis() / 1000);
  347. cfg.setCheckPath(remoteRepository.getCheckPath());
  348. RepositoryCredentials creds = remoteRepository.getLoginCredentials();
  349. if (creds != null) {
  350. if (creds instanceof PasswordCredentials) {
  351. PasswordCredentials pCreds = (PasswordCredentials) creds;
  352. cfg.setPassword(new String(pCreds.getPassword()));
  353. cfg.setUsername(pCreds.getUsername());
  354. }
  355. }
  356. cfg.setLayout(remoteRepository.getLayout());
  357. cfg.setExtraParameters(remoteRepository.getExtraParameters());
  358. cfg.setExtraHeaders(remoteRepository.getExtraHeaders());
  359. cfg.setRefreshCronExpression(remoteRepository.getSchedulingDefinition());
  360. IndexCreationFeature indexCreationFeature = remoteRepository.getFeature( IndexCreationFeature.class );
  361. cfg.setIndexDir(convertUriToPath(indexCreationFeature.getIndexPath()));
  362. cfg.setPackedIndexDir(convertUriToPath(indexCreationFeature.getPackedIndexPath()));
  363. RemoteIndexFeature remoteIndexFeature = remoteRepository.getFeature( RemoteIndexFeature.class );
  364. if ( remoteIndexFeature.getIndexUri( ) == null )
  365. {
  366. cfg.setRemoteIndexUrl( "" );
  367. }
  368. else
  369. {
  370. cfg.setRemoteIndexUrl(remoteIndexFeature.getIndexUri().toString());
  371. }
  372. cfg.setRemoteDownloadTimeout((int) remoteIndexFeature.getDownloadTimeout().get(ChronoUnit.SECONDS));
  373. cfg.setDownloadRemoteIndexOnStartup(remoteIndexFeature.isDownloadRemoteIndexOnStartup());
  374. cfg.setDownloadRemoteIndex(remoteIndexFeature.isDownloadRemoteIndex());
  375. cfg.setRemoteDownloadNetworkProxyId(remoteIndexFeature.getProxyId());
  376. if ( StringUtils.isEmpty( remoteIndexFeature.getProxyId( ) ) )
  377. {
  378. cfg.setRemoteDownloadNetworkProxyId("");
  379. }
  380. else
  381. {
  382. cfg.setRemoteDownloadNetworkProxyId(remoteIndexFeature.getProxyId());
  383. }
  384. return cfg;
  385. }
  386. @Override
  387. public ManagedRepositoryConfiguration getManagedConfiguration(ManagedRepository managedRepository) throws RepositoryException {
  388. if (!(managedRepository instanceof MavenManagedRepository || managedRepository instanceof BasicManagedRepository )) {
  389. log.error("Wrong remote repository type " + managedRepository.getClass().getName());
  390. throw new RepositoryException("The given repository type cannot be handled by the maven provider: " + managedRepository.getClass().getName());
  391. }
  392. ManagedRepositoryConfiguration cfg = new ManagedRepositoryConfiguration();
  393. cfg.setType(managedRepository.getType().toString());
  394. cfg.setId(managedRepository.getId());
  395. cfg.setName(managedRepository.getName());
  396. cfg.setDescription(managedRepository.getDescription());
  397. cfg.setLocation(convertUriToPath(managedRepository.getLocation()));
  398. cfg.setLayout(managedRepository.getLayout());
  399. cfg.setRefreshCronExpression(managedRepository.getSchedulingDefinition());
  400. cfg.setScanned(managedRepository.isScanned());
  401. cfg.setBlockRedeployments(managedRepository.blocksRedeployments());
  402. StagingRepositoryFeature stagingRepositoryFeature = managedRepository.getFeature( StagingRepositoryFeature.class );
  403. cfg.setStageRepoNeeded(stagingRepositoryFeature.isStageRepoNeeded());
  404. IndexCreationFeature indexCreationFeature = managedRepository.getFeature( IndexCreationFeature.class );
  405. cfg.setIndexDir(convertUriToPath(indexCreationFeature.getIndexPath()));
  406. cfg.setPackedIndexDir(convertUriToPath(indexCreationFeature.getPackedIndexPath()));
  407. cfg.setSkipPackedIndexCreation(indexCreationFeature.isSkipPackedIndexCreation());
  408. ArtifactCleanupFeature artifactCleanupFeature = managedRepository.getFeature( ArtifactCleanupFeature.class );
  409. cfg.setRetentionCount(artifactCleanupFeature.getRetentionCount());
  410. cfg.setRetentionPeriod(artifactCleanupFeature.getRetentionPeriod().getDays());
  411. cfg.setDeleteReleasedSnapshots(artifactCleanupFeature.isDeleteReleasedSnapshots());
  412. cfg.setReleases( managedRepository.getActiveReleaseSchemes( ).contains( ReleaseScheme.RELEASE ) );
  413. cfg.setSnapshots( managedRepository.getActiveReleaseSchemes( ).contains( ReleaseScheme.SNAPSHOT ) );
  414. return cfg;
  415. }
  416. @Override
  417. public RepositoryGroupConfiguration getRepositoryGroupConfiguration(RepositoryGroup repositoryGroup) throws RepositoryException {
  418. if (repositoryGroup.getType() != RepositoryType.MAVEN) {
  419. throw new RepositoryException("The given repository group is not of MAVEN type");
  420. }
  421. RepositoryGroupConfiguration cfg = new RepositoryGroupConfiguration();
  422. cfg.setId(repositoryGroup.getId());
  423. cfg.setName(repositoryGroup.getName());
  424. if (repositoryGroup.supportsFeature( IndexCreationFeature.class ))
  425. {
  426. IndexCreationFeature indexCreationFeature = repositoryGroup.getFeature( IndexCreationFeature.class );
  427. cfg.setMergedIndexPath( indexCreationFeature.getIndexPath().toString() );
  428. }
  429. cfg.setMergedIndexTtl(repositoryGroup.getMergedIndexTTL());
  430. cfg.setRepositories(repositoryGroup.getRepositories().stream().map( Repository::getId ).collect(Collectors.toList()));
  431. cfg.setCronExpression(repositoryGroup.getSchedulingDefinition());
  432. return cfg;
  433. }
  434. @Override
  435. public void addRepositoryEventHandler( EventHandler<? super RepositoryEvent> eventHandler )
  436. {
  437. this.repositoryEventHandlers.add( eventHandler );
  438. }
  439. private ManagedRepositoryConfiguration getStageRepoConfig(ManagedRepositoryConfiguration repository) {
  440. ManagedRepositoryConfiguration stagingRepository = new ManagedRepositoryConfiguration();
  441. stagingRepository.setId(repository.getId() + StagingRepositoryFeature.STAGING_REPO_POSTFIX);
  442. stagingRepository.setLayout(repository.getLayout());
  443. stagingRepository.setName(repository.getName() + StagingRepositoryFeature.STAGING_REPO_POSTFIX);
  444. stagingRepository.setBlockRedeployments(repository.isBlockRedeployments());
  445. stagingRepository.setRetentionPeriod(repository.getRetentionPeriod());
  446. stagingRepository.setDeleteReleasedSnapshots(repository.isDeleteReleasedSnapshots());
  447. stagingRepository.setStageRepoNeeded(false);
  448. String path = repository.getLocation();
  449. int lastIndex = path.replace('\\', '/').lastIndexOf('/');
  450. stagingRepository.setLocation(path.substring(0, lastIndex) + "/" + stagingRepository.getId());
  451. if (StringUtils.isNotBlank(repository.getIndexDir())) {
  452. Path indexDir;
  453. try {
  454. if (repository.getIndexDir().startsWith( "file:" )) {
  455. indexDir = Paths.get( new URI( repository.getIndexDir( ) ) );
  456. } else
  457. {
  458. indexDir = Paths.get( repository.getIndexDir( ) );
  459. }
  460. if (indexDir.isAbsolute()) {
  461. Path newDir = indexDir.getParent().resolve(indexDir.getFileName() + StagingRepositoryFeature.STAGING_REPO_POSTFIX);
  462. log.debug("Changing index directory {} -> {}", indexDir, newDir);
  463. stagingRepository.setIndexDir(newDir.toString());
  464. } else {
  465. log.debug("Keeping index directory {}", repository.getIndexDir());
  466. stagingRepository.setIndexDir(repository.getIndexDir());
  467. }
  468. } catch (URISyntaxException e) {
  469. log.error("Could not parse index path as uri {}", repository.getIndexDir());
  470. stagingRepository.setIndexDir("");
  471. }
  472. // in case of absolute dir do not use the same
  473. }
  474. if (StringUtils.isNotBlank(repository.getPackedIndexDir())) {
  475. Path packedIndexDir;
  476. packedIndexDir = Paths.get(repository.getPackedIndexDir());
  477. if (packedIndexDir.isAbsolute()) {
  478. Path newDir = packedIndexDir.getParent().resolve(packedIndexDir.getFileName() + StagingRepositoryFeature.STAGING_REPO_POSTFIX);
  479. log.debug("Changing index directory {} -> {}", packedIndexDir, newDir);
  480. stagingRepository.setPackedIndexDir(newDir.toString());
  481. } else {
  482. log.debug("Keeping index directory {}", repository.getPackedIndexDir());
  483. stagingRepository.setPackedIndexDir(repository.getPackedIndexDir());
  484. }
  485. // in case of absolute dir do not use the same
  486. }
  487. stagingRepository.setRefreshCronExpression(repository.getRefreshCronExpression());
  488. stagingRepository.setReleases(repository.isReleases());
  489. stagingRepository.setRetentionCount(repository.getRetentionCount());
  490. stagingRepository.setScanned(repository.isScanned());
  491. stagingRepository.setSnapshots(repository.isSnapshots());
  492. stagingRepository.setSkipPackedIndexCreation(repository.isSkipPackedIndexCreation());
  493. // do not duplicate description
  494. //stagingRepository.getDescription("")
  495. return stagingRepository;
  496. }
  497. private void setBaseConfig( EditableRepository repo, AbstractRepositoryConfiguration cfg)
  498. {
  499. URI baseUri = archivaConfiguration.getRepositoryBaseDir().toUri();
  500. repo.setBaseUri(baseUri);
  501. repo.setName(repo.getPrimaryLocale(), cfg.getName());
  502. repo.setDescription(repo.getPrimaryLocale(), cfg.getDescription());
  503. repo.setLayout(cfg.getLayout());
  504. }
  505. public ArchivaConfiguration getArchivaConfiguration() {
  506. return archivaConfiguration;
  507. }
  508. public void setArchivaConfiguration(ArchivaConfiguration archivaConfiguration) {
  509. this.archivaConfiguration = archivaConfiguration;
  510. }
  511. @Override
  512. public void handle(Event event) {
  513. //
  514. }
  515. }