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

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