1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.repository.legacy;
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.HashMap;
27 import java.util.LinkedHashMap;
28 import java.util.List;
29 import java.util.Map;
30 import org.apache.maven.RepositoryUtils;
31 import org.apache.maven.artifact.Artifact;
32 import org.apache.maven.artifact.InvalidRepositoryException;
33 import org.apache.maven.artifact.factory.ArtifactFactory;
34 import org.apache.maven.artifact.metadata.ArtifactMetadata;
35 import org.apache.maven.artifact.repository.ArtifactRepository;
36 import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
37 import org.apache.maven.artifact.repository.Authentication;
38 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
39 import org.apache.maven.artifact.resolver.ArtifactResolutionRequest;
40 import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
41 import org.apache.maven.artifact.resolver.ArtifactResolver;
42 import org.apache.maven.artifact.resolver.filter.ExcludesArtifactFilter;
43 import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
44 import org.apache.maven.artifact.versioning.VersionRange;
45 import org.apache.maven.model.Dependency;
46 import org.apache.maven.model.Exclusion;
47 import org.apache.maven.model.Plugin;
48 import org.apache.maven.model.Repository;
49 import org.apache.maven.model.RepositoryPolicy;
50 import org.apache.maven.repository.ArtifactDoesNotExistException;
51 import org.apache.maven.repository.ArtifactTransferFailedException;
52 import org.apache.maven.repository.ArtifactTransferListener;
53 import org.apache.maven.repository.DelegatingLocalArtifactRepository;
54 import org.apache.maven.repository.LocalArtifactRepository;
55 import org.apache.maven.repository.MirrorSelector;
56 import org.apache.maven.repository.Proxy;
57 import org.apache.maven.repository.RepositorySystem;
58 import org.apache.maven.repository.legacy.repository.ArtifactRepositoryFactory;
59 import org.apache.maven.settings.Mirror;
60 import org.apache.maven.settings.Server;
61 import org.apache.maven.settings.building.SettingsProblem;
62 import org.apache.maven.settings.crypto.DefaultSettingsDecryptionRequest;
63 import org.apache.maven.settings.crypto.SettingsDecrypter;
64 import org.apache.maven.settings.crypto.SettingsDecryptionRequest;
65 import org.apache.maven.settings.crypto.SettingsDecryptionResult;
66 import org.apache.maven.wagon.proxy.ProxyInfo;
67 import org.apache.maven.wagon.proxy.ProxyUtils;
68 import org.codehaus.plexus.PlexusContainer;
69 import org.codehaus.plexus.component.annotations.Component;
70 import org.codehaus.plexus.component.annotations.Requirement;
71 import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
72 import org.codehaus.plexus.logging.Logger;
73 import org.codehaus.plexus.util.StringUtils;
74 import org.eclipse.aether.RepositorySystemSession;
75 import org.eclipse.aether.repository.AuthenticationContext;
76 import org.eclipse.aether.repository.AuthenticationSelector;
77 import org.eclipse.aether.repository.ProxySelector;
78 import org.eclipse.aether.repository.RemoteRepository;
79
80
81
82
83 @Component(role = RepositorySystem.class, hint = "default")
84 public class LegacyRepositorySystem implements RepositorySystem {
85
86 @Requirement
87 private Logger logger;
88
89 @Requirement
90 private ArtifactFactory artifactFactory;
91
92 @Requirement
93 private ArtifactResolver artifactResolver;
94
95 @Requirement
96 private ArtifactRepositoryFactory artifactRepositoryFactory;
97
98 @Requirement(role = ArtifactRepositoryLayout.class)
99 private Map<String, ArtifactRepositoryLayout> layouts;
100
101 @Requirement
102 private WagonManager wagonManager;
103
104 @Requirement
105 private PlexusContainer plexus;
106
107 @Requirement
108 private MirrorSelector mirrorSelector;
109
110 @Requirement
111 private SettingsDecrypter settingsDecrypter;
112
113 public Artifact createArtifact(String groupId, String artifactId, String version, String scope, String type) {
114 return artifactFactory.createArtifact(groupId, artifactId, version, scope, type);
115 }
116
117 public Artifact createArtifact(String groupId, String artifactId, String version, String packaging) {
118 return artifactFactory.createBuildArtifact(groupId, artifactId, version, packaging);
119 }
120
121 public Artifact createArtifactWithClassifier(
122 String groupId, String artifactId, String version, String type, String classifier) {
123 return artifactFactory.createArtifactWithClassifier(groupId, artifactId, version, type, classifier);
124 }
125
126 public Artifact createProjectArtifact(String groupId, String artifactId, String metaVersionId) {
127 return artifactFactory.createProjectArtifact(groupId, artifactId, metaVersionId);
128 }
129
130 public Artifact createDependencyArtifact(Dependency d) {
131 VersionRange versionRange;
132 try {
133 versionRange = VersionRange.createFromVersionSpec(d.getVersion());
134 } catch (InvalidVersionSpecificationException e) {
135
136 this.logger.error(
137 String.format(
138 "Invalid version specification '%s' creating dependency artifact '%s'.", d.getVersion(), d),
139 e);
140 return null;
141 }
142
143 Artifact artifact = artifactFactory.createDependencyArtifact(
144 d.getGroupId(),
145 d.getArtifactId(),
146 versionRange,
147 d.getType(),
148 d.getClassifier(),
149 d.getScope(),
150 d.isOptional());
151
152 if (Artifact.SCOPE_SYSTEM.equals(d.getScope()) && d.getSystemPath() != null) {
153 artifact.setFile(new File(d.getSystemPath()));
154 }
155
156 if (!d.getExclusions().isEmpty()) {
157 List<String> exclusions = new ArrayList<>();
158
159 for (Exclusion exclusion : d.getExclusions()) {
160 exclusions.add(exclusion.getGroupId() + ':' + exclusion.getArtifactId());
161 }
162
163 artifact.setDependencyFilter(new ExcludesArtifactFilter(exclusions));
164 }
165
166 return artifact;
167 }
168
169 public Artifact createExtensionArtifact(String groupId, String artifactId, String version) {
170 VersionRange versionRange;
171 try {
172 versionRange = VersionRange.createFromVersionSpec(version);
173 } catch (InvalidVersionSpecificationException e) {
174
175 this.logger.error(
176 String.format(
177 "Invalid version specification '%s' creating extension artifact '%s:%s:%s'.",
178 version, groupId, artifactId, version),
179 e);
180
181 return null;
182 }
183
184 return artifactFactory.createExtensionArtifact(groupId, artifactId, versionRange);
185 }
186
187 public Artifact createParentArtifact(String groupId, String artifactId, String version) {
188 return artifactFactory.createParentArtifact(groupId, artifactId, version);
189 }
190
191 public Artifact createPluginArtifact(Plugin plugin) {
192 String version = plugin.getVersion();
193 if (StringUtils.isEmpty(version)) {
194 version = "RELEASE";
195 }
196
197 VersionRange versionRange;
198 try {
199 versionRange = VersionRange.createFromVersionSpec(version);
200 } catch (InvalidVersionSpecificationException e) {
201
202 this.logger.error(
203 String.format("Invalid version specification '%s' creating plugin artifact '%s'.", version, plugin),
204 e);
205
206 return null;
207 }
208
209 return artifactFactory.createPluginArtifact(plugin.getGroupId(), plugin.getArtifactId(), versionRange);
210 }
211
212 public ArtifactRepositoryPolicy buildArtifactRepositoryPolicy(RepositoryPolicy policy) {
213 boolean enabled = true;
214
215 String updatePolicy = null;
216
217 String checksumPolicy = null;
218
219 if (policy != null) {
220 enabled = policy.isEnabled();
221
222 if (policy.getUpdatePolicy() != null) {
223 updatePolicy = policy.getUpdatePolicy();
224 }
225 if (policy.getChecksumPolicy() != null) {
226 checksumPolicy = policy.getChecksumPolicy();
227 }
228 }
229
230 return new ArtifactRepositoryPolicy(enabled, updatePolicy, checksumPolicy);
231 }
232
233 public ArtifactRepository createDefaultLocalRepository() throws InvalidRepositoryException {
234 return createLocalRepository(RepositorySystem.defaultUserLocalRepository);
235 }
236
237 public ArtifactRepository createLocalRepository(File localRepository) throws InvalidRepositoryException {
238 return createRepository(
239 "file://" + localRepository.toURI().getRawPath(),
240 RepositorySystem.DEFAULT_LOCAL_REPO_ID,
241 true,
242 ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS,
243 true,
244 ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS,
245 ArtifactRepositoryPolicy.CHECKSUM_POLICY_IGNORE);
246 }
247
248 public ArtifactRepository createDefaultRemoteRepository() throws InvalidRepositoryException {
249 return createRepository(
250 RepositorySystem.DEFAULT_REMOTE_REPO_URL,
251 RepositorySystem.DEFAULT_REMOTE_REPO_ID,
252 true,
253 ArtifactRepositoryPolicy.UPDATE_POLICY_DAILY,
254 false,
255 ArtifactRepositoryPolicy.UPDATE_POLICY_DAILY,
256 ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN);
257 }
258
259 public ArtifactRepository createLocalRepository(String url, String repositoryId) throws IOException {
260 return createRepository(
261 canonicalFileUrl(url),
262 repositoryId,
263 true,
264 ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS,
265 true,
266 ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS,
267 ArtifactRepositoryPolicy.CHECKSUM_POLICY_IGNORE);
268 }
269
270 private String canonicalFileUrl(String url) throws IOException {
271 if (!url.startsWith("file:")) {
272 url = "file://" + url;
273 } else if (url.startsWith("file:") && !url.startsWith("file://")) {
274 url = "file://" + url.substring("file:".length());
275 }
276
277
278
279
280
281
282
283
284 File localRepository = new File(url.substring("file://".length()));
285
286 if (!localRepository.isAbsolute()) {
287 url = "file://" + localRepository.getCanonicalPath();
288 }
289
290 return url;
291 }
292
293 public ArtifactResolutionResult resolve(ArtifactResolutionRequest request) {
294
295
296
297
298 try {
299 LocalArtifactRepository ideWorkspace =
300 plexus.lookup(LocalArtifactRepository.class, LocalArtifactRepository.IDE_WORKSPACE);
301
302 if (request.getLocalRepository() instanceof DelegatingLocalArtifactRepository) {
303 DelegatingLocalArtifactRepository delegatingLocalRepository =
304 (DelegatingLocalArtifactRepository) request.getLocalRepository();
305
306 LocalArtifactRepository orig = delegatingLocalRepository.getIdeWorkspace();
307
308 delegatingLocalRepository.setIdeWorkspace(ideWorkspace);
309
310 try {
311 return artifactResolver.resolve(request);
312 } finally {
313 delegatingLocalRepository.setIdeWorkspace(orig);
314 }
315 } else {
316 ArtifactRepository localRepository = request.getLocalRepository();
317 DelegatingLocalArtifactRepository delegatingLocalRepository =
318 new DelegatingLocalArtifactRepository(localRepository);
319 delegatingLocalRepository.setIdeWorkspace(ideWorkspace);
320 request.setLocalRepository(delegatingLocalRepository);
321 try {
322 return artifactResolver.resolve(request);
323 } finally {
324 request.setLocalRepository(localRepository);
325 }
326 }
327 } catch (ComponentLookupException e) {
328
329 }
330
331 return artifactResolver.resolve(request);
332 }
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350 public List<ArtifactRepository> getEffectiveRepositories(List<ArtifactRepository> repositories) {
351 if (repositories == null) {
352 return null;
353 }
354
355 Map<String, List<ArtifactRepository>> reposByKey = new LinkedHashMap<>();
356
357 for (ArtifactRepository repository : repositories) {
358 String key = repository.getId();
359
360 List<ArtifactRepository> aliasedRepos = reposByKey.computeIfAbsent(key, k -> new ArrayList<>());
361
362 aliasedRepos.add(repository);
363 }
364
365 List<ArtifactRepository> effectiveRepositories = new ArrayList<>();
366
367 for (List<ArtifactRepository> aliasedRepos : reposByKey.values()) {
368 List<ArtifactRepository> mirroredRepos = new ArrayList<>();
369
370 List<ArtifactRepositoryPolicy> releasePolicies = new ArrayList<>(aliasedRepos.size());
371
372 for (ArtifactRepository aliasedRepo : aliasedRepos) {
373 releasePolicies.add(aliasedRepo.getReleases());
374 mirroredRepos.addAll(aliasedRepo.getMirroredRepositories());
375 }
376
377 ArtifactRepositoryPolicy releasePolicy = getEffectivePolicy(releasePolicies);
378
379 List<ArtifactRepositoryPolicy> snapshotPolicies = new ArrayList<>(aliasedRepos.size());
380
381 for (ArtifactRepository aliasedRepo : aliasedRepos) {
382 snapshotPolicies.add(aliasedRepo.getSnapshots());
383 }
384
385 ArtifactRepositoryPolicy snapshotPolicy = getEffectivePolicy(snapshotPolicies);
386
387 ArtifactRepository aliasedRepo = aliasedRepos.get(0);
388
389 ArtifactRepository effectiveRepository = createArtifactRepository(
390 aliasedRepo.getId(), aliasedRepo.getUrl(), aliasedRepo.getLayout(), snapshotPolicy, releasePolicy);
391
392 effectiveRepository.setAuthentication(aliasedRepo.getAuthentication());
393
394 effectiveRepository.setProxy(aliasedRepo.getProxy());
395
396 effectiveRepository.setMirroredRepositories(mirroredRepos);
397
398 effectiveRepository.setBlocked(aliasedRepo.isBlocked());
399
400 effectiveRepositories.add(effectiveRepository);
401 }
402
403 return effectiveRepositories;
404 }
405
406 private ArtifactRepositoryPolicy getEffectivePolicy(Collection<ArtifactRepositoryPolicy> policies) {
407 ArtifactRepositoryPolicy effectivePolicy = null;
408
409 for (ArtifactRepositoryPolicy policy : policies) {
410 if (effectivePolicy == null) {
411 effectivePolicy = new ArtifactRepositoryPolicy(policy);
412 } else {
413 effectivePolicy.merge(policy);
414 }
415 }
416
417 return effectivePolicy;
418 }
419
420 public Mirror getMirror(ArtifactRepository repository, List<Mirror> mirrors) {
421 return mirrorSelector.getMirror(repository, mirrors);
422 }
423
424 public void injectMirror(List<ArtifactRepository> repositories, List<Mirror> mirrors) {
425 if (repositories != null && mirrors != null) {
426 for (ArtifactRepository repository : repositories) {
427 Mirror mirror = getMirror(repository, mirrors);
428 injectMirror(repository, mirror);
429 }
430 }
431 }
432
433 private Mirror getMirror(RepositorySystemSession session, ArtifactRepository repository) {
434 if (session != null) {
435 org.eclipse.aether.repository.MirrorSelector selector = session.getMirrorSelector();
436 if (selector != null) {
437 RemoteRepository repo = selector.getMirror(RepositoryUtils.toRepo(repository));
438 if (repo != null) {
439 Mirror mirror = new Mirror();
440 mirror.setId(repo.getId());
441 mirror.setUrl(repo.getUrl());
442 mirror.setLayout(repo.getContentType());
443 mirror.setBlocked(repo.isBlocked());
444 return mirror;
445 }
446 }
447 }
448 return null;
449 }
450
451 public void injectMirror(RepositorySystemSession session, List<ArtifactRepository> repositories) {
452 if (repositories != null && session != null) {
453 for (ArtifactRepository repository : repositories) {
454 Mirror mirror = getMirror(session, repository);
455 injectMirror(repository, mirror);
456 }
457 }
458 }
459
460 private void injectMirror(ArtifactRepository repository, Mirror mirror) {
461 if (mirror != null) {
462 ArtifactRepository original = createArtifactRepository(
463 repository.getId(),
464 repository.getUrl(),
465 repository.getLayout(),
466 repository.getSnapshots(),
467 repository.getReleases());
468
469 repository.setMirroredRepositories(Collections.singletonList(original));
470
471 repository.setId(mirror.getId());
472 repository.setUrl(mirror.getUrl());
473
474 if (StringUtils.isNotEmpty(mirror.getLayout())) {
475 repository.setLayout(getLayout(mirror.getLayout()));
476 }
477
478 repository.setBlocked(mirror.isBlocked());
479 }
480 }
481
482 public void injectAuthentication(List<ArtifactRepository> repositories, List<Server> servers) {
483 if (repositories != null) {
484 Map<String, Server> serversById = new HashMap<>();
485
486 if (servers != null) {
487 for (Server server : servers) {
488 if (!serversById.containsKey(server.getId())) {
489 serversById.put(server.getId(), server);
490 }
491 }
492 }
493
494 for (ArtifactRepository repository : repositories) {
495 Server server = serversById.get(repository.getId());
496
497 if (server != null) {
498 SettingsDecryptionRequest request = new DefaultSettingsDecryptionRequest(server);
499 SettingsDecryptionResult result = settingsDecrypter.decrypt(request);
500 server = result.getServer();
501
502 if (logger.isDebugEnabled()) {
503 for (SettingsProblem problem : result.getProblems()) {
504 logger.debug(problem.getMessage(), problem.getException());
505 }
506 }
507
508 Authentication authentication = new Authentication(server.getUsername(), server.getPassword());
509 authentication.setPrivateKey(server.getPrivateKey());
510 authentication.setPassphrase(server.getPassphrase());
511
512 repository.setAuthentication(authentication);
513 } else {
514 repository.setAuthentication(null);
515 }
516 }
517 }
518 }
519
520 private Authentication getAuthentication(RepositorySystemSession session, ArtifactRepository repository) {
521 if (session != null) {
522 AuthenticationSelector selector = session.getAuthenticationSelector();
523 if (selector != null) {
524 RemoteRepository repo = RepositoryUtils.toRepo(repository);
525 org.eclipse.aether.repository.Authentication auth = selector.getAuthentication(repo);
526 if (auth != null) {
527 repo = new RemoteRepository.Builder(repo)
528 .setAuthentication(auth)
529 .build();
530 AuthenticationContext authCtx = AuthenticationContext.forRepository(session, repo);
531 Authentication result = new Authentication(
532 authCtx.get(AuthenticationContext.USERNAME), authCtx.get(AuthenticationContext.PASSWORD));
533 result.setPrivateKey(authCtx.get(AuthenticationContext.PRIVATE_KEY_PATH));
534 result.setPassphrase(authCtx.get(AuthenticationContext.PRIVATE_KEY_PASSPHRASE));
535 authCtx.close();
536 return result;
537 }
538 }
539 }
540 return null;
541 }
542
543 public void injectAuthentication(RepositorySystemSession session, List<ArtifactRepository> repositories) {
544 if (repositories != null && session != null) {
545 for (ArtifactRepository repository : repositories) {
546 repository.setAuthentication(getAuthentication(session, repository));
547 }
548 }
549 }
550
551 private org.apache.maven.settings.Proxy getProxy(
552 ArtifactRepository repository, List<org.apache.maven.settings.Proxy> proxies) {
553 if (proxies != null && repository.getProtocol() != null) {
554 for (org.apache.maven.settings.Proxy proxy : proxies) {
555 if (proxy.isActive() && repository.getProtocol().equalsIgnoreCase(proxy.getProtocol())) {
556 if (StringUtils.isNotEmpty(proxy.getNonProxyHosts())) {
557 ProxyInfo pi = new ProxyInfo();
558 pi.setNonProxyHosts(proxy.getNonProxyHosts());
559
560 org.apache.maven.wagon.repository.Repository repo =
561 new org.apache.maven.wagon.repository.Repository(
562 repository.getId(), repository.getUrl());
563
564 if (!ProxyUtils.validateNonProxyHosts(pi, repo.getHost())) {
565 return proxy;
566 }
567 } else {
568 return proxy;
569 }
570 }
571 }
572 }
573
574 return null;
575 }
576
577 public void injectProxy(List<ArtifactRepository> repositories, List<org.apache.maven.settings.Proxy> proxies) {
578 if (repositories != null) {
579 for (ArtifactRepository repository : repositories) {
580 org.apache.maven.settings.Proxy proxy = getProxy(repository, proxies);
581
582 if (proxy != null) {
583 SettingsDecryptionRequest request = new DefaultSettingsDecryptionRequest(proxy);
584 SettingsDecryptionResult result = settingsDecrypter.decrypt(request);
585 proxy = result.getProxy();
586
587 if (logger.isDebugEnabled()) {
588 for (SettingsProblem problem : result.getProblems()) {
589 logger.debug(problem.getMessage(), problem.getException());
590 }
591 }
592
593 Proxy p = new Proxy();
594 p.setHost(proxy.getHost());
595 p.setProtocol(proxy.getProtocol());
596 p.setPort(proxy.getPort());
597 p.setNonProxyHosts(proxy.getNonProxyHosts());
598 p.setUserName(proxy.getUsername());
599 p.setPassword(proxy.getPassword());
600
601 repository.setProxy(p);
602 } else {
603 repository.setProxy(null);
604 }
605 }
606 }
607 }
608
609 private Proxy getProxy(RepositorySystemSession session, ArtifactRepository repository) {
610 if (session != null) {
611 ProxySelector selector = session.getProxySelector();
612 if (selector != null) {
613 RemoteRepository repo = RepositoryUtils.toRepo(repository);
614 org.eclipse.aether.repository.Proxy proxy = selector.getProxy(repo);
615 if (proxy != null) {
616 Proxy p = new Proxy();
617 p.setHost(proxy.getHost());
618 p.setProtocol(proxy.getType());
619 p.setPort(proxy.getPort());
620 if (proxy.getAuthentication() != null) {
621 repo = new RemoteRepository.Builder(repo)
622 .setProxy(proxy)
623 .build();
624 AuthenticationContext authCtx = AuthenticationContext.forProxy(session, repo);
625 p.setUserName(authCtx.get(AuthenticationContext.USERNAME));
626 p.setPassword(authCtx.get(AuthenticationContext.PASSWORD));
627 p.setNtlmDomain(authCtx.get(AuthenticationContext.NTLM_DOMAIN));
628 p.setNtlmHost(authCtx.get(AuthenticationContext.NTLM_WORKSTATION));
629 authCtx.close();
630 }
631 return p;
632 }
633 }
634 }
635 return null;
636 }
637
638 public void injectProxy(RepositorySystemSession session, List<ArtifactRepository> repositories) {
639 if (repositories != null && session != null) {
640 for (ArtifactRepository repository : repositories) {
641 repository.setProxy(getProxy(session, repository));
642 }
643 }
644 }
645
646 public void retrieve(
647 ArtifactRepository repository,
648 File destination,
649 String remotePath,
650 ArtifactTransferListener transferListener)
651 throws ArtifactTransferFailedException, ArtifactDoesNotExistException {
652 try {
653 wagonManager.getRemoteFile(
654 repository,
655 destination,
656 remotePath,
657 TransferListenerAdapter.newAdapter(transferListener),
658 ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN,
659 true);
660 } catch (org.apache.maven.wagon.TransferFailedException e) {
661 throw new ArtifactTransferFailedException(getMessage(e, "Error transferring artifact."), e);
662 } catch (org.apache.maven.wagon.ResourceDoesNotExistException e) {
663 throw new ArtifactDoesNotExistException(getMessage(e, "Requested artifact does not exist."), e);
664 }
665 }
666
667 public void publish(
668 ArtifactRepository repository, File source, String remotePath, ArtifactTransferListener transferListener)
669 throws ArtifactTransferFailedException {
670 try {
671 wagonManager.putRemoteFile(
672 repository, source, remotePath, TransferListenerAdapter.newAdapter(transferListener));
673 } catch (org.apache.maven.wagon.TransferFailedException e) {
674 throw new ArtifactTransferFailedException(getMessage(e, "Error transferring artifact."), e);
675 }
676 }
677
678
679
680
681 public ArtifactRepository buildArtifactRepository(Repository repo) throws InvalidRepositoryException {
682 if (repo != null) {
683 String id = repo.getId();
684
685 if (StringUtils.isEmpty(id)) {
686 throw new InvalidRepositoryException("Repository identifier missing", "");
687 }
688
689 String url = repo.getUrl();
690
691 if (StringUtils.isEmpty(url)) {
692 throw new InvalidRepositoryException("URL missing for repository " + id, id);
693 }
694
695 ArtifactRepositoryPolicy snapshots = buildArtifactRepositoryPolicy(repo.getSnapshots());
696
697 ArtifactRepositoryPolicy releases = buildArtifactRepositoryPolicy(repo.getReleases());
698
699 return createArtifactRepository(id, url, getLayout(repo.getLayout()), snapshots, releases);
700 } else {
701 return null;
702 }
703 }
704
705 private ArtifactRepository createRepository(
706 String url,
707 String repositoryId,
708 boolean releases,
709 String releaseUpdates,
710 boolean snapshots,
711 String snapshotUpdates,
712 String checksumPolicy) {
713 ArtifactRepositoryPolicy snapshotsPolicy =
714 new ArtifactRepositoryPolicy(snapshots, snapshotUpdates, checksumPolicy);
715
716 ArtifactRepositoryPolicy releasesPolicy =
717 new ArtifactRepositoryPolicy(releases, releaseUpdates, checksumPolicy);
718
719 return createArtifactRepository(repositoryId, url, null, snapshotsPolicy, releasesPolicy);
720 }
721
722 public ArtifactRepository createArtifactRepository(
723 String repositoryId,
724 String url,
725 ArtifactRepositoryLayout repositoryLayout,
726 ArtifactRepositoryPolicy snapshots,
727 ArtifactRepositoryPolicy releases) {
728 if (repositoryLayout == null) {
729 repositoryLayout = layouts.get("default");
730 }
731 return artifactRepositoryFactory.createArtifactRepository(
732 repositoryId, url, repositoryLayout, snapshots, releases);
733 }
734
735 private static String getMessage(Throwable error, String def) {
736 if (error == null) {
737 return def;
738 }
739 String msg = error.getMessage();
740 if (StringUtils.isNotEmpty(msg)) {
741 return msg;
742 }
743 return getMessage(error.getCause(), def);
744 }
745
746 private ArtifactRepositoryLayout getLayout(String id) {
747 ArtifactRepositoryLayout layout = layouts.get(id);
748
749 if (layout == null) {
750 layout = new UnknownRepositoryLayout(id, layouts.get("default"));
751 }
752
753 return layout;
754 }
755
756
757
758
759
760
761
762 static class UnknownRepositoryLayout implements ArtifactRepositoryLayout {
763
764 private final String id;
765
766 private final ArtifactRepositoryLayout fallback;
767
768 UnknownRepositoryLayout(String id, ArtifactRepositoryLayout fallback) {
769 this.id = id;
770 this.fallback = fallback;
771 }
772
773 public String getId() {
774 return id;
775 }
776
777 public String pathOf(Artifact artifact) {
778 return fallback.pathOf(artifact);
779 }
780
781 public String pathOfLocalRepositoryMetadata(ArtifactMetadata metadata, ArtifactRepository repository) {
782 return fallback.pathOfLocalRepositoryMetadata(metadata, repository);
783 }
784
785 public String pathOfRemoteRepositoryMetadata(ArtifactMetadata metadata) {
786 return fallback.pathOfRemoteRepositoryMetadata(metadata);
787 }
788
789 @Override
790 public String toString() {
791 return getId();
792 }
793 }
794 }