1 package org.apache.archiva.repository.maven2;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.archiva.common.filelock.FileLockManager;
23 import org.apache.archiva.configuration.*;
24 import org.apache.archiva.repository.*;
25 import org.apache.archiva.event.Event;
26 import org.apache.archiva.repository.features.ArtifactCleanupFeature;
27 import org.apache.archiva.repository.features.IndexCreationFeature;
28 import org.apache.archiva.repository.features.RemoteIndexFeature;
29 import org.apache.archiva.repository.features.StagingRepositoryFeature;
30 import org.apache.archiva.repository.base.BasicManagedRepository;
31 import org.apache.archiva.repository.base.PasswordCredentials;
32 import org.apache.archiva.repository.storage.FilesystemStorage;
33 import org.apache.commons.lang3.StringUtils;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.stereotype.Service;
38 import javax.inject.Inject;
39 import java.io.IOException;
41 import java.net.URISyntaxException;
42 import java.nio.file.Files;
43 import java.nio.file.Path;
44 import java.nio.file.Paths;
45 import java.time.Duration;
46 import java.time.Period;
47 import java.time.temporal.ChronoUnit;
48 import java.util.HashSet;
50 import java.util.stream.Collectors;
52 import static org.apache.archiva.indexer.ArchivaIndexManager.DEFAULT_INDEX_PATH;
53 import static org.apache.archiva.indexer.ArchivaIndexManager.DEFAULT_PACKED_INDEX_PATH;
56 * Provider for the maven2 repository implementations
58 @Service("mavenRepositoryProvider")
59 public class MavenRepositoryProvider implements RepositoryProvider {
63 private ArchivaConfiguration archivaConfiguration;
66 private FileLockManager fileLockManager;
68 private static final Logger log = LoggerFactory.getLogger(MavenRepositoryProvider.class);
70 static final Set<RepositoryType> TYPES = new HashSet<>();
73 TYPES.add(RepositoryType.MAVEN);
77 public Set<RepositoryType> provides() {
82 public MavenManagedRepository createManagedInstance(String id, String name) {
83 return createManagedInstance(id, name, archivaConfiguration.getRemoteRepositoryBaseDir());
86 public MavenManagedRepository createManagedInstance(String id, String name, Path baseDir) {
87 FilesystemStorage storage = null;
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);
94 return new MavenManagedRepository(id, name, storage);
98 public MavenRemoteRepository createRemoteInstance(String id, String name) {
99 return createRemoteInstance(id, name, archivaConfiguration.getRemoteRepositoryBaseDir());
102 public MavenRemoteRepository createRemoteInstance(String id, String name, Path baseDir) {
103 FilesystemStorage storage = null;
105 storage = new FilesystemStorage(baseDir.resolve(id), fileLockManager);
106 } catch (IOException e) {
107 log.error("Could not initialize fileystem for repository {}", id);
108 throw new RuntimeException(e);
110 return new MavenRemoteRepository(id, name, storage);
114 public EditableRepositoryGroup createRepositoryGroup(String id, String name) {
115 return createRepositoryGroup(id, name, archivaConfiguration.getRepositoryBaseDir());
118 public MavenRepositoryGroup createRepositoryGroup(String id, String name, Path baseDir) {
119 FilesystemStorage storage = null;
121 storage = new FilesystemStorage(baseDir.resolve(id), fileLockManager);
122 } catch (IOException e) {
123 log.error("Could not initialize fileystem for repository {}", id);
124 throw new RuntimeException(e);
126 return new MavenRepositoryGroup(id, name, storage);
129 private URI getURIFromString(String uriStr) throws RepositoryException {
132 if (StringUtils.isEmpty(uriStr)) {
135 if (uriStr.startsWith("/")) {
136 // only absolute paths are prepended with file scheme
137 uri = new URI("file://" + uriStr);
139 uri = new URI(uriStr);
141 if (uri.getScheme() != null && !"file".equals(uri.getScheme())) {
142 log.error("Bad URI scheme found: {}, URI={}", uri.getScheme(), uri);
143 throw new RepositoryException("The uri " + uriStr + " is not valid. Only file:// URI is allowed for maven.");
145 } catch (URISyntaxException e) {
146 String newCfg = "file://" + uriStr;
148 uri = new URI(newCfg);
149 } catch (URISyntaxException e1) {
150 log.error("Could not create URI from {} -> ", uriStr, newCfg);
151 throw new RepositoryException("The config entry " + uriStr + " cannot be converted to URI.");
154 log.debug("Setting location uri: {}", uri);
159 public ManagedRepository createManagedInstance(ManagedRepositoryConfiguration cfg) throws RepositoryException {
160 MavenManagedRepository repo = createManagedInstance(cfg.getId(), cfg.getName(), Paths.get(cfg.getLocation()).getParent());
161 updateManagedInstance(repo, cfg);
166 public void updateManagedInstance(EditableManagedRepository repo, ManagedRepositoryConfiguration cfg) throws RepositoryException {
168 repo.setLocation(getURIFromString(cfg.getLocation()));
169 } catch (UnsupportedURIException e) {
170 throw new RepositoryException("The location entry is not a valid uri: " + cfg.getLocation());
172 setBaseConfig(repo, cfg);
173 Path repoDir = repo.getAsset("").getFilePath();
174 if (!Files.exists(repoDir)) {
175 log.debug("Creating repo directory {}", repoDir);
177 Files.createDirectories(repoDir);
178 } catch (IOException e) {
179 log.error("Could not create directory {} for repository {}", repoDir, repo.getId(), e);
180 throw new RepositoryException("Could not create directory for repository " + repoDir);
183 repo.setSchedulingDefinition(cfg.getRefreshCronExpression());
184 repo.setBlocksRedeployment(cfg.isBlockRedeployments());
185 repo.setScanned(cfg.isScanned());
186 if (cfg.isReleases()) {
187 repo.addActiveReleaseScheme(ReleaseScheme.RELEASE);
189 repo.removeActiveReleaseScheme(ReleaseScheme.RELEASE);
191 if (cfg.isSnapshots()) {
192 repo.addActiveReleaseScheme(ReleaseScheme.SNAPSHOT);
194 repo.removeActiveReleaseScheme(ReleaseScheme.SNAPSHOT);
197 StagingRepositoryFeature stagingRepositoryFeature = repo.getFeature(StagingRepositoryFeature.class).get();
198 stagingRepositoryFeature.setStageRepoNeeded(cfg.isStageRepoNeeded());
200 IndexCreationFeature indexCreationFeature = repo.getFeature(IndexCreationFeature.class).get();
201 indexCreationFeature.setSkipPackedIndexCreation(cfg.isSkipPackedIndexCreation());
202 String indexDir = StringUtils.isEmpty( cfg.getIndexDir() ) ? DEFAULT_INDEX_PATH : cfg.getIndexDir();
203 String packedIndexDir = StringUtils.isEmpty( cfg.getPackedIndexDir() ) ? DEFAULT_PACKED_INDEX_PATH : cfg.getPackedIndexDir();
204 indexCreationFeature.setIndexPath(getURIFromString(indexDir));
205 indexCreationFeature.setPackedIndexPath(getURIFromString(packedIndexDir));
207 ArtifactCleanupFeature artifactCleanupFeature = repo.getFeature(ArtifactCleanupFeature.class).get();
209 artifactCleanupFeature.setDeleteReleasedSnapshots(cfg.isDeleteReleasedSnapshots());
210 artifactCleanupFeature.setRetentionCount(cfg.getRetentionCount());
211 artifactCleanupFeature.setRetentionPeriod(Period.ofDays(cfg.getRetentionPeriod()));
216 public ManagedRepository createStagingInstance(ManagedRepositoryConfiguration baseConfiguration) throws RepositoryException {
217 log.debug("Creating staging instance for {}", baseConfiguration.getId());
218 return createManagedInstance(getStageRepoConfig(baseConfiguration));
223 public RemoteRepository createRemoteInstance(RemoteRepositoryConfiguration cfg) throws RepositoryException {
224 MavenRemoteRepository repo = createRemoteInstance(cfg.getId(), cfg.getName(), archivaConfiguration.getRemoteRepositoryBaseDir());
225 updateRemoteInstance(repo, cfg);
229 private String convertUriToPath(URI uri) {
230 if (uri.getScheme() == null) {
231 return uri.getPath();
232 } else if ("file".equals(uri.getScheme())) {
233 return Paths.get(uri).toString();
235 return uri.toString();
240 public void updateRemoteInstance(EditableRemoteRepository repo, RemoteRepositoryConfiguration cfg) throws RepositoryException {
241 setBaseConfig(repo, cfg);
242 repo.setCheckPath(cfg.getCheckPath());
243 repo.setSchedulingDefinition(cfg.getRefreshCronExpression());
245 repo.setLocation(new URI(cfg.getUrl()));
246 } catch (UnsupportedURIException | URISyntaxException e) {
247 log.error("Could not set remote url " + cfg.getUrl());
248 throw new RepositoryException("The url config is not a valid uri: " + cfg.getUrl());
250 repo.setTimeout(Duration.ofSeconds(cfg.getTimeout()));
251 RemoteIndexFeature remoteIndexFeature = repo.getFeature(RemoteIndexFeature.class).get();
252 remoteIndexFeature.setDownloadRemoteIndex(cfg.isDownloadRemoteIndex());
253 remoteIndexFeature.setDownloadRemoteIndexOnStartup(cfg.isDownloadRemoteIndexOnStartup());
254 remoteIndexFeature.setDownloadTimeout(Duration.ofSeconds(cfg.getRemoteDownloadTimeout()));
255 remoteIndexFeature.setProxyId(cfg.getRemoteDownloadNetworkProxyId());
256 if (cfg.isDownloadRemoteIndex()) {
258 remoteIndexFeature.setIndexUri(new URI(cfg.getRemoteIndexUrl()));
259 } catch (URISyntaxException e) {
260 log.error("Could not set remote index url " + cfg.getRemoteIndexUrl());
261 remoteIndexFeature.setDownloadRemoteIndex(false);
262 remoteIndexFeature.setDownloadRemoteIndexOnStartup(false);
265 for ( Object key : cfg.getExtraHeaders().keySet() ) {
266 repo.addExtraHeader( key.toString(), cfg.getExtraHeaders().get(key).toString() );
268 for ( Object key : cfg.getExtraParameters().keySet() ) {
269 repo.addExtraParameter( key.toString(), cfg.getExtraParameters().get(key).toString() );
271 PasswordCredentials credentials = new PasswordCredentials("", new char[0]);
272 if (cfg.getPassword() != null && cfg.getUsername() != null) {
273 credentials.setPassword(cfg.getPassword().toCharArray());
274 credentials.setUsername(cfg.getUsername());
275 repo.setCredentials(credentials);
277 credentials.setPassword(new char[0]);
279 IndexCreationFeature indexCreationFeature = repo.getFeature(IndexCreationFeature.class).get();
280 if (cfg.getIndexDir() != null) {
281 indexCreationFeature.setIndexPath(getURIFromString(cfg.getIndexDir()));
283 if (cfg.getPackedIndexDir() != null) {
284 indexCreationFeature.setPackedIndexPath(getURIFromString(cfg.getPackedIndexDir()));
286 log.debug("Updated remote instance {}", repo);
290 public RepositoryGroup createRepositoryGroup(RepositoryGroupConfiguration configuration) throws RepositoryException {
291 Path repositoryGroupBase = getArchivaConfiguration().getRepositoryGroupBaseDir();
292 MavenRepositoryGroup newGrp = createRepositoryGroup(configuration.getId(), configuration.getName(),
293 repositoryGroupBase);
294 updateRepositoryGroupInstance(newGrp, configuration);
299 public void updateRepositoryGroupInstance(EditableRepositoryGroup repositoryGroup, RepositoryGroupConfiguration configuration) throws RepositoryException {
300 repositoryGroup.setName(repositoryGroup.getPrimaryLocale(), configuration.getName());
301 repositoryGroup.setMergedIndexTTL(configuration.getMergedIndexTtl());
302 repositoryGroup.setSchedulingDefinition(configuration.getCronExpression());
303 if (repositoryGroup.supportsFeature( IndexCreationFeature.class )) {
304 IndexCreationFeature indexCreationFeature = repositoryGroup.getFeature( IndexCreationFeature.class ).get();
305 indexCreationFeature.setIndexPath( getURIFromString(configuration.getMergedIndexPath()) );
306 Path localPath = Paths.get(configuration.getMergedIndexPath());
307 Path repoGroupPath = repositoryGroup.getAsset("").getFilePath().toAbsolutePath();
308 if (localPath.isAbsolute() && !localPath.startsWith(repoGroupPath)) {
310 FilesystemStorage storage = new FilesystemStorage(localPath.getParent(), fileLockManager);
311 indexCreationFeature.setLocalIndexPath(storage.getAsset(localPath.getFileName().toString()));
312 } catch (IOException e) {
313 throw new RepositoryException("Could not initialize storage for index path "+localPath);
315 } else if (localPath.isAbsolute()) {
316 indexCreationFeature.setLocalIndexPath(repositoryGroup.getAsset(repoGroupPath.relativize(localPath).toString()));
319 indexCreationFeature.setLocalIndexPath(repositoryGroup.getAsset(localPath.toString()));
322 // References to other repositories are set filled by the registry
326 public RemoteRepositoryConfiguration getRemoteConfiguration(RemoteRepository remoteRepository) throws RepositoryException {
327 if (!(remoteRepository instanceof MavenRemoteRepository)) {
328 log.error("Wrong remote repository type " + remoteRepository.getClass().getName());
329 throw new RepositoryException("The given repository type cannot be handled by the maven provider: " + remoteRepository.getClass().getName());
331 RemoteRepositoryConfiguration cfg = new RemoteRepositoryConfiguration();
332 cfg.setType(remoteRepository.getType().toString());
333 cfg.setId(remoteRepository.getId());
334 cfg.setName(remoteRepository.getName());
335 cfg.setDescription(remoteRepository.getDescription());
336 cfg.setUrl(remoteRepository.getLocation().toString());
337 cfg.setTimeout((int) remoteRepository.getTimeout().toMillis() / 1000);
338 cfg.setCheckPath(remoteRepository.getCheckPath());
339 RepositoryCredentials creds = remoteRepository.getLoginCredentials();
341 if (creds instanceof PasswordCredentials) {
342 PasswordCredentials pCreds = (PasswordCredentials) creds;
343 cfg.setPassword(new String(pCreds.getPassword()));
344 cfg.setUsername(pCreds.getUsername());
347 cfg.setLayout(remoteRepository.getLayout());
348 cfg.setExtraParameters(remoteRepository.getExtraParameters());
349 cfg.setExtraHeaders(remoteRepository.getExtraHeaders());
350 cfg.setRefreshCronExpression(remoteRepository.getSchedulingDefinition());
352 IndexCreationFeature indexCreationFeature = remoteRepository.getFeature(IndexCreationFeature.class).get();
353 cfg.setIndexDir(convertUriToPath(indexCreationFeature.getIndexPath()));
354 cfg.setPackedIndexDir(convertUriToPath(indexCreationFeature.getPackedIndexPath()));
356 RemoteIndexFeature remoteIndexFeature = remoteRepository.getFeature(RemoteIndexFeature.class).get();
357 if (remoteIndexFeature.getIndexUri() != null) {
358 cfg.setRemoteIndexUrl(remoteIndexFeature.getIndexUri().toString());
360 cfg.setRemoteDownloadTimeout((int) remoteIndexFeature.getDownloadTimeout().get(ChronoUnit.SECONDS));
361 cfg.setDownloadRemoteIndexOnStartup(remoteIndexFeature.isDownloadRemoteIndexOnStartup());
362 cfg.setDownloadRemoteIndex(remoteIndexFeature.isDownloadRemoteIndex());
363 cfg.setRemoteDownloadNetworkProxyId(remoteIndexFeature.getProxyId());
364 if (!StringUtils.isEmpty(remoteIndexFeature.getProxyId())) {
365 cfg.setRemoteDownloadNetworkProxyId(remoteIndexFeature.getProxyId());
367 cfg.setRemoteDownloadNetworkProxyId("");
378 public ManagedRepositoryConfiguration getManagedConfiguration(ManagedRepository managedRepository) throws RepositoryException {
379 if (!(managedRepository instanceof MavenManagedRepository || managedRepository instanceof BasicManagedRepository )) {
380 log.error("Wrong remote repository type " + managedRepository.getClass().getName());
381 throw new RepositoryException("The given repository type cannot be handled by the maven provider: " + managedRepository.getClass().getName());
383 ManagedRepositoryConfiguration cfg = new ManagedRepositoryConfiguration();
384 cfg.setType(managedRepository.getType().toString());
385 cfg.setId(managedRepository.getId());
386 cfg.setName(managedRepository.getName());
387 cfg.setDescription(managedRepository.getDescription());
388 cfg.setLocation(convertUriToPath(managedRepository.getLocation()));
389 cfg.setLayout(managedRepository.getLayout());
390 cfg.setRefreshCronExpression(managedRepository.getSchedulingDefinition());
391 cfg.setScanned(managedRepository.isScanned());
392 cfg.setBlockRedeployments(managedRepository.blocksRedeployments());
393 StagingRepositoryFeature stagingRepositoryFeature = managedRepository.getFeature(StagingRepositoryFeature.class).get();
394 cfg.setStageRepoNeeded(stagingRepositoryFeature.isStageRepoNeeded());
395 IndexCreationFeature indexCreationFeature = managedRepository.getFeature(IndexCreationFeature.class).get();
396 cfg.setIndexDir(convertUriToPath(indexCreationFeature.getIndexPath()));
397 cfg.setPackedIndexDir(convertUriToPath(indexCreationFeature.getPackedIndexPath()));
398 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());
405 if (managedRepository.getActiveReleaseSchemes().contains(ReleaseScheme.RELEASE)) {
406 cfg.setReleases(true);
408 cfg.setReleases(false);
410 if (managedRepository.getActiveReleaseSchemes().contains(ReleaseScheme.SNAPSHOT)) {
411 cfg.setSnapshots(true);
413 cfg.setSnapshots(false);
420 public RepositoryGroupConfiguration getRepositoryGroupConfiguration(RepositoryGroup repositoryGroup) throws RepositoryException {
421 if (repositoryGroup.getType() != RepositoryType.MAVEN) {
422 throw new RepositoryException("The given repository group is not of MAVEN type");
424 RepositoryGroupConfiguration cfg = new RepositoryGroupConfiguration();
425 cfg.setId(repositoryGroup.getId());
426 cfg.setName(repositoryGroup.getName());
427 if (repositoryGroup.supportsFeature( IndexCreationFeature.class ))
429 IndexCreationFeature indexCreationFeature = repositoryGroup.getFeature( IndexCreationFeature.class ).get();
431 cfg.setMergedIndexPath( indexCreationFeature.getIndexPath().toString() );
433 cfg.setMergedIndexTtl(repositoryGroup.getMergedIndexTTL());
434 cfg.setRepositories(repositoryGroup.getRepositories().stream().map(r -> r.getId()).collect(Collectors.toList()));
435 cfg.setCronExpression(repositoryGroup.getSchedulingDefinition());
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);
449 String path = repository.getLocation();
450 int lastIndex = path.replace('\\', '/').lastIndexOf('/');
451 stagingRepository.setLocation(path.substring(0, lastIndex) + "/" + stagingRepository.getId());
453 if (StringUtils.isNotBlank(repository.getIndexDir())) {
454 Path indexDir = null;
456 indexDir = Paths.get(new URI(repository.getIndexDir().startsWith("file://") ? repository.getIndexDir() : "file://" + repository.getIndexDir()));
457 if (indexDir.isAbsolute()) {
458 Path newDir = indexDir.getParent().resolve(indexDir.getFileName() + StagingRepositoryFeature.STAGING_REPO_POSTFIX);
459 log.debug("Changing index directory {} -> {}", indexDir, newDir);
460 stagingRepository.setIndexDir(newDir.toString());
462 log.debug("Keeping index directory {}", repository.getIndexDir());
463 stagingRepository.setIndexDir(repository.getIndexDir());
465 } catch (URISyntaxException e) {
466 log.error("Could not parse index path as uri {}", repository.getIndexDir());
467 stagingRepository.setIndexDir("");
469 // in case of absolute dir do not use the same
471 if (StringUtils.isNotBlank(repository.getPackedIndexDir())) {
472 Path packedIndexDir = null;
473 packedIndexDir = Paths.get(repository.getPackedIndexDir());
474 if (packedIndexDir.isAbsolute()) {
475 Path newDir = packedIndexDir.getParent().resolve(packedIndexDir.getFileName() + StagingRepositoryFeature.STAGING_REPO_POSTFIX);
476 log.debug("Changing index directory {} -> {}", packedIndexDir, newDir);
477 stagingRepository.setPackedIndexDir(newDir.toString());
479 log.debug("Keeping index directory {}", repository.getPackedIndexDir());
480 stagingRepository.setPackedIndexDir(repository.getPackedIndexDir());
482 // in case of absolute dir do not use the same
484 stagingRepository.setRefreshCronExpression(repository.getRefreshCronExpression());
485 stagingRepository.setReleases(repository.isReleases());
486 stagingRepository.setRetentionCount(repository.getRetentionCount());
487 stagingRepository.setScanned(repository.isScanned());
488 stagingRepository.setSnapshots(repository.isSnapshots());
489 stagingRepository.setSkipPackedIndexCreation(repository.isSkipPackedIndexCreation());
490 // do not duplicate description
491 //stagingRepository.getDescription("")
492 return stagingRepository;
495 private void setBaseConfig(EditableRepository repo, AbstractRepositoryConfiguration cfg) throws RepositoryException {
497 URI baseUri = archivaConfiguration.getRepositoryBaseDir().toUri();
498 repo.setBaseUri(baseUri);
500 repo.setName(repo.getPrimaryLocale(), cfg.getName());
501 repo.setDescription(repo.getPrimaryLocale(), cfg.getDescription());
502 repo.setLayout(cfg.getLayout());
505 public ArchivaConfiguration getArchivaConfiguration() {
506 return archivaConfiguration;
509 public void setArchivaConfiguration(ArchivaConfiguration archivaConfiguration) {
510 this.archivaConfiguration = archivaConfiguration;
514 public void handle(Event event) {