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

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