View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.bridge;
20  
21  import javax.inject.Inject;
22  import javax.inject.Named;
23  import javax.inject.Singleton;
24  
25  import java.io.File;
26  import java.net.MalformedURLException;
27  import java.net.URL;
28  import java.util.ArrayList;
29  import java.util.Collection;
30  import java.util.Collections;
31  import java.util.HashSet;
32  import java.util.LinkedHashMap;
33  import java.util.List;
34  import java.util.Map;
35  import java.util.Set;
36  
37  import org.apache.maven.RepositoryUtils;
38  import org.apache.maven.artifact.Artifact;
39  import org.apache.maven.artifact.DefaultArtifact;
40  import org.apache.maven.artifact.InvalidRepositoryException;
41  import org.apache.maven.artifact.handler.ArtifactHandler;
42  import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
43  import org.apache.maven.artifact.repository.ArtifactRepository;
44  import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
45  import org.apache.maven.artifact.repository.Authentication;
46  import org.apache.maven.artifact.repository.MavenArtifactRepository;
47  import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
48  import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout2;
49  import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
50  import org.apache.maven.artifact.resolver.filter.ExclusionArtifactFilter;
51  import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
52  import org.apache.maven.artifact.versioning.VersionRange;
53  import org.apache.maven.execution.MavenExecutionRequest;
54  import org.apache.maven.model.Dependency;
55  import org.apache.maven.model.Plugin;
56  import org.apache.maven.repository.Proxy;
57  import org.apache.maven.settings.Mirror;
58  import org.eclipse.aether.RepositorySystemSession;
59  import org.eclipse.aether.repository.AuthenticationContext;
60  import org.eclipse.aether.repository.AuthenticationSelector;
61  import org.eclipse.aether.repository.ProxySelector;
62  import org.eclipse.aether.repository.RemoteRepository;
63  
64  /**
65   */
66  @Named("default")
67  @Singleton
68  public class MavenRepositorySystem {
69  
70      public static final String DEFAULT_REMOTE_REPO_ID = "central";
71  
72      public static final String DEFAULT_REMOTE_REPO_URL = "https://repo.maven.apache.org/maven2";
73  
74      public static final String DEFAULT_LOCAL_REPO_ID = "local";
75  
76      private final ArtifactHandlerManager artifactHandlerManager;
77  
78      private final Map<String, ArtifactRepositoryLayout> layouts;
79  
80      @Inject
81      public MavenRepositorySystem(
82              ArtifactHandlerManager artifactHandlerManager, Map<String, ArtifactRepositoryLayout> layouts) {
83          this.artifactHandlerManager = artifactHandlerManager;
84          this.layouts = layouts;
85      }
86  
87      // DefaultProjectBuilder
88      public Artifact createArtifact(String groupId, String artifactId, String version, String scope, String type) {
89          return createArtifactX(groupId, artifactId, version, scope, type);
90      }
91  
92      // DefaultProjectBuilder
93      public Artifact createProjectArtifact(String groupId, String artifactId, String metaVersionId) {
94          return createProjectArtifactX(groupId, artifactId, metaVersionId);
95      }
96  
97      // DefaultProjectBuilder
98      public Artifact createDependencyArtifact(Dependency d) {
99          if (d.getVersion() == null) {
100             return null;
101         }
102 
103         VersionRange versionRange;
104         try {
105             versionRange = VersionRange.createFromVersionSpec(d.getVersion());
106         } catch (InvalidVersionSpecificationException e) {
107             return null;
108         }
109 
110         Artifact artifact = createDependencyArtifactX(
111                 d.getGroupId(),
112                 d.getArtifactId(),
113                 versionRange,
114                 d.getType(),
115                 d.getClassifier(),
116                 d.getScope(),
117                 d.isOptional());
118 
119         if (Artifact.SCOPE_SYSTEM.equals(d.getScope()) && d.getSystemPath() != null) {
120             artifact.setFile(new File(d.getSystemPath()));
121         }
122 
123         if (!d.getExclusions().isEmpty()) {
124             artifact.setDependencyFilter(new ExclusionArtifactFilter(d.getExclusions()));
125         }
126 
127         return artifact;
128     }
129 
130     // DefaultProjectBuilder
131     public Artifact createExtensionArtifact(String groupId, String artifactId, String version) {
132         VersionRange versionRange;
133         try {
134             versionRange = VersionRange.createFromVersionSpec(version);
135         } catch (InvalidVersionSpecificationException e) {
136             return null;
137         }
138 
139         return createExtensionArtifactX(groupId, artifactId, versionRange);
140     }
141 
142     // DefaultProjectBuilder
143     public Artifact createParentArtifact(String groupId, String artifactId, String version) {
144         return createParentArtifactX(groupId, artifactId, version);
145     }
146 
147     // DefaultProjectBuilder
148     public Artifact createPluginArtifact(Plugin plugin) {
149         VersionRange versionRange;
150         try {
151             String version = plugin.getVersion();
152             if (version == null || version.isEmpty()) {
153                 version = "RELEASE";
154             }
155             versionRange = VersionRange.createFromVersionSpec(version);
156         } catch (InvalidVersionSpecificationException e) {
157             return null;
158         }
159 
160         return createPluginArtifactX(plugin.getGroupId(), plugin.getArtifactId(), versionRange);
161     }
162 
163     public void injectMirror(List<ArtifactRepository> repositories, List<Mirror> mirrors) {
164         if (repositories != null && mirrors != null) {
165             for (ArtifactRepository repository : repositories) {
166                 Mirror mirror = getMirror(repository, mirrors);
167                 injectMirror(repository, mirror);
168             }
169         }
170     }
171 
172     private Mirror getMirror(RepositorySystemSession session, ArtifactRepository repository) {
173         if (session != null) {
174             org.eclipse.aether.repository.MirrorSelector selector = session.getMirrorSelector();
175             if (selector != null) {
176                 RemoteRepository repo = selector.getMirror(RepositoryUtils.toRepo(repository));
177                 if (repo != null) {
178                     Mirror mirror = new Mirror();
179                     mirror.setId(repo.getId());
180                     mirror.setUrl(repo.getUrl());
181                     mirror.setLayout(repo.getContentType());
182                     mirror.setBlocked(repo.isBlocked());
183                     return mirror;
184                 }
185             }
186         }
187         return null;
188     }
189 
190     public void injectMirror(RepositorySystemSession session, List<ArtifactRepository> repositories) {
191         if (repositories != null && session != null) {
192             for (ArtifactRepository repository : repositories) {
193                 Mirror mirror = getMirror(session, repository);
194                 injectMirror(repository, mirror);
195             }
196         }
197     }
198 
199     private void injectMirror(ArtifactRepository repository, Mirror mirror) {
200         if (mirror != null) {
201             ArtifactRepository original = createArtifactRepository(
202                     repository.getId(),
203                     repository.getUrl(),
204                     repository.getLayout(),
205                     repository.getSnapshots(),
206                     repository.getReleases());
207 
208             repository.setMirroredRepositories(Collections.singletonList(original));
209 
210             repository.setId(mirror.getId());
211             repository.setUrl(mirror.getUrl());
212 
213             if (mirror.getLayout() != null && !mirror.getLayout().isEmpty()) {
214                 repository.setLayout(getLayout(mirror.getLayout()));
215             }
216 
217             repository.setBlocked(mirror.isBlocked());
218         }
219     }
220 
221     private Authentication getAuthentication(RepositorySystemSession session, ArtifactRepository repository) {
222         if (session != null) {
223             AuthenticationSelector selector = session.getAuthenticationSelector();
224             if (selector != null) {
225                 RemoteRepository repo = RepositoryUtils.toRepo(repository);
226                 org.eclipse.aether.repository.Authentication auth = selector.getAuthentication(repo);
227                 if (auth != null) {
228                     repo = new RemoteRepository.Builder(repo)
229                             .setAuthentication(auth)
230                             .build();
231                     AuthenticationContext authCtx = AuthenticationContext.forRepository(session, repo);
232                     Authentication result = new Authentication(
233                             authCtx.get(AuthenticationContext.USERNAME), authCtx.get(AuthenticationContext.PASSWORD));
234                     result.setPrivateKey(authCtx.get(AuthenticationContext.PRIVATE_KEY_PATH));
235                     result.setPassphrase(authCtx.get(AuthenticationContext.PRIVATE_KEY_PASSPHRASE));
236                     authCtx.close();
237                     return result;
238                 }
239             }
240         }
241         return null;
242     }
243 
244     public void injectAuthentication(RepositorySystemSession session, List<ArtifactRepository> repositories) {
245         if (repositories != null && session != null) {
246             for (ArtifactRepository repository : repositories) {
247                 repository.setAuthentication(getAuthentication(session, repository));
248             }
249         }
250     }
251 
252     private Proxy getProxy(RepositorySystemSession session, ArtifactRepository repository) {
253         if (session != null) {
254             ProxySelector selector = session.getProxySelector();
255             if (selector != null) {
256                 RemoteRepository repo = RepositoryUtils.toRepo(repository);
257                 org.eclipse.aether.repository.Proxy proxy = selector.getProxy(repo);
258                 if (proxy != null) {
259                     Proxy p = new Proxy();
260                     p.setHost(proxy.getHost());
261                     p.setProtocol(proxy.getType());
262                     p.setPort(proxy.getPort());
263                     if (proxy.getAuthentication() != null) {
264                         repo = new RemoteRepository.Builder(repo)
265                                 .setProxy(proxy)
266                                 .build();
267                         AuthenticationContext authCtx = AuthenticationContext.forProxy(session, repo);
268                         p.setUserName(authCtx.get(AuthenticationContext.USERNAME));
269                         p.setPassword(authCtx.get(AuthenticationContext.PASSWORD));
270                         p.setNtlmDomain(authCtx.get(AuthenticationContext.NTLM_DOMAIN));
271                         p.setNtlmHost(authCtx.get(AuthenticationContext.NTLM_WORKSTATION));
272                         authCtx.close();
273                     }
274                     return p;
275                 }
276             }
277         }
278         return null;
279     }
280 
281     public void injectProxy(RepositorySystemSession session, List<ArtifactRepository> repositories) {
282         if (repositories != null && session != null) {
283             for (ArtifactRepository repository : repositories) {
284                 repository.setProxy(getProxy(session, repository));
285             }
286         }
287     }
288 
289     private ArtifactRepositoryLayout getLayout(String id) {
290         return layouts.get(id);
291     }
292 
293     //
294     // Taken from LegacyRepositorySystem
295     //
296 
297     public static org.apache.maven.model.Repository fromSettingsRepository(
298             org.apache.maven.settings.Repository settingsRepository) {
299         org.apache.maven.model.Repository modelRepository = new org.apache.maven.model.Repository();
300         modelRepository.setId(settingsRepository.getId());
301         modelRepository.setLayout(settingsRepository.getLayout());
302         modelRepository.setName(settingsRepository.getName());
303         modelRepository.setUrl(settingsRepository.getUrl());
304         modelRepository.setReleases(fromSettingsRepositoryPolicy(settingsRepository.getReleases()));
305         modelRepository.setSnapshots(fromSettingsRepositoryPolicy(settingsRepository.getSnapshots()));
306         return modelRepository;
307     }
308 
309     public static org.apache.maven.model.RepositoryPolicy fromSettingsRepositoryPolicy(
310             org.apache.maven.settings.RepositoryPolicy settingsRepositoryPolicy) {
311         org.apache.maven.model.RepositoryPolicy modelRepositoryPolicy = new org.apache.maven.model.RepositoryPolicy();
312         if (settingsRepositoryPolicy != null) {
313             modelRepositoryPolicy.setEnabled(settingsRepositoryPolicy.isEnabled());
314             modelRepositoryPolicy.setUpdatePolicy(settingsRepositoryPolicy.getUpdatePolicy());
315             modelRepositoryPolicy.setChecksumPolicy(settingsRepositoryPolicy.getChecksumPolicy());
316         }
317         return modelRepositoryPolicy;
318     }
319 
320     public static ArtifactRepository buildArtifactRepository(org.apache.maven.settings.Repository repo)
321             throws InvalidRepositoryException {
322         return buildArtifactRepository(fromSettingsRepository(repo));
323     }
324 
325     public static ArtifactRepository buildArtifactRepository(org.apache.maven.model.Repository repo)
326             throws InvalidRepositoryException {
327         if (repo != null) {
328             String id = repo.getId();
329 
330             if (id == null || id.isEmpty()) {
331                 throw new InvalidRepositoryException("Repository identifier missing", "");
332             }
333 
334             String url = repo.getUrl();
335 
336             if (url == null || url.isEmpty()) {
337                 throw new InvalidRepositoryException("URL missing for repository " + id, id);
338             }
339 
340             ArtifactRepositoryPolicy snapshots = buildArtifactRepositoryPolicy(repo.getSnapshots());
341 
342             ArtifactRepositoryPolicy releases = buildArtifactRepositoryPolicy(repo.getReleases());
343 
344             ArtifactRepositoryLayout layout = new DefaultRepositoryLayout();
345 
346             return createArtifactRepository(id, url, layout, snapshots, releases);
347         } else {
348             return null;
349         }
350     }
351 
352     public static ArtifactRepositoryPolicy buildArtifactRepositoryPolicy(
353             org.apache.maven.model.RepositoryPolicy policy) {
354         boolean enabled = true;
355 
356         String updatePolicy = null;
357 
358         String checksumPolicy = null;
359 
360         if (policy != null) {
361             enabled = policy.isEnabled();
362 
363             if (policy.getUpdatePolicy() != null) {
364                 updatePolicy = policy.getUpdatePolicy();
365             }
366             if (policy.getChecksumPolicy() != null) {
367                 checksumPolicy = policy.getChecksumPolicy();
368             }
369         }
370 
371         return new ArtifactRepositoryPolicy(enabled, updatePolicy, checksumPolicy);
372     }
373 
374     public ArtifactRepository createArtifactRepository(
375             String id,
376             String url,
377             String layoutId,
378             ArtifactRepositoryPolicy snapshots,
379             ArtifactRepositoryPolicy releases)
380             throws InvalidRepositoryException {
381         ArtifactRepositoryLayout layout = layouts.get(layoutId);
382 
383         checkLayout(id, layoutId, layout);
384 
385         return createArtifactRepository(id, url, layout, snapshots, releases);
386     }
387 
388     private void checkLayout(String repositoryId, String layoutId, ArtifactRepositoryLayout layout)
389             throws InvalidRepositoryException {
390         if (layout == null) {
391             throw new InvalidRepositoryException(
392                     String.format("Cannot find ArtifactRepositoryLayout instance for: %s %s", layoutId, repositoryId),
393                     repositoryId);
394         }
395     }
396 
397     public static ArtifactRepository createArtifactRepository(
398             String id,
399             String url,
400             ArtifactRepositoryLayout repositoryLayout,
401             ArtifactRepositoryPolicy snapshots,
402             ArtifactRepositoryPolicy releases) {
403         if (snapshots == null) {
404             snapshots = new ArtifactRepositoryPolicy();
405         }
406 
407         if (releases == null) {
408             releases = new ArtifactRepositoryPolicy();
409         }
410 
411         ArtifactRepository repository;
412         if (repositoryLayout instanceof ArtifactRepositoryLayout2) {
413             repository = ((ArtifactRepositoryLayout2) repositoryLayout)
414                     .newMavenArtifactRepository(id, url, snapshots, releases);
415         } else {
416             repository = new MavenArtifactRepository(id, url, repositoryLayout, snapshots, releases);
417         }
418 
419         return repository;
420     }
421 
422     // ArtifactFactory
423     private Artifact createArtifactX(String groupId, String artifactId, String version, String scope, String type) {
424         return createArtifactX(groupId, artifactId, version, scope, type, null, null);
425     }
426 
427     private Artifact createDependencyArtifactX(
428             String groupId,
429             String artifactId,
430             VersionRange versionRange,
431             String type,
432             String classifier,
433             String scope,
434             boolean optional) {
435         return createArtifactX(groupId, artifactId, versionRange, type, classifier, scope, null, optional);
436     }
437 
438     private Artifact createProjectArtifactX(String groupId, String artifactId, String version) {
439         return createProjectArtifactX(groupId, artifactId, version, null);
440     }
441 
442     private Artifact createParentArtifactX(String groupId, String artifactId, String version) {
443         return createProjectArtifactX(groupId, artifactId, version);
444     }
445 
446     private Artifact createPluginArtifactX(String groupId, String artifactId, VersionRange versionRange) {
447         return createArtifactX(groupId, artifactId, versionRange, "maven-plugin", null, Artifact.SCOPE_RUNTIME, null);
448     }
449 
450     private Artifact createProjectArtifactX(String groupId, String artifactId, String version, String scope) {
451         return createArtifactX(groupId, artifactId, version, scope, "pom");
452     }
453 
454     private Artifact createExtensionArtifactX(String groupId, String artifactId, VersionRange versionRange) {
455         return createArtifactX(groupId, artifactId, versionRange, "jar", null, Artifact.SCOPE_RUNTIME, null);
456     }
457 
458     private Artifact createArtifactX(
459             String groupId,
460             String artifactId,
461             String version,
462             String scope,
463             String type,
464             String classifier,
465             String inheritedScope) {
466         VersionRange versionRange = null;
467         if (version != null) {
468             versionRange = VersionRange.createFromVersion(version);
469         }
470         return createArtifactX(groupId, artifactId, versionRange, type, classifier, scope, inheritedScope);
471     }
472 
473     private Artifact createArtifactX(
474             String groupId,
475             String artifactId,
476             VersionRange versionRange,
477             String type,
478             String classifier,
479             String scope,
480             String inheritedScope) {
481         return createArtifactX(groupId, artifactId, versionRange, type, classifier, scope, inheritedScope, false);
482     }
483 
484     @SuppressWarnings("checkstyle:parameternumber")
485     private Artifact createArtifactX(
486             String groupId,
487             String artifactId,
488             VersionRange versionRange,
489             String type,
490             String classifier,
491             String scope,
492             String inheritedScope,
493             boolean optional) {
494         String desiredScope = Artifact.SCOPE_RUNTIME;
495 
496         if (inheritedScope == null) {
497             desiredScope = scope;
498         } else if (Artifact.SCOPE_TEST.equals(scope) || Artifact.SCOPE_PROVIDED.equals(scope)) {
499             return null;
500         } else if (Artifact.SCOPE_COMPILE.equals(scope) && Artifact.SCOPE_COMPILE.equals(inheritedScope)) {
501             // added to retain compile artifactScope. Remove if you want compile inherited as runtime
502             desiredScope = Artifact.SCOPE_COMPILE;
503         }
504 
505         if (Artifact.SCOPE_TEST.equals(inheritedScope)) {
506             desiredScope = Artifact.SCOPE_TEST;
507         }
508 
509         if (Artifact.SCOPE_PROVIDED.equals(inheritedScope)) {
510             desiredScope = Artifact.SCOPE_PROVIDED;
511         }
512 
513         if (Artifact.SCOPE_SYSTEM.equals(scope)) {
514             // system scopes come through unchanged...
515             desiredScope = Artifact.SCOPE_SYSTEM;
516         }
517 
518         ArtifactHandler handler = artifactHandlerManager.getArtifactHandler(type);
519 
520         return new DefaultArtifact(
521                 groupId, artifactId, versionRange, desiredScope, type, classifier, handler, optional);
522     }
523 
524     //
525     // Code taken from LegacyRepositorySystem
526     //
527     public ArtifactRepository createDefaultRemoteRepository() throws Exception {
528         return createDefaultRemoteRepository(null);
529     }
530 
531     public ArtifactRepository createDefaultRemoteRepository(MavenExecutionRequest request) throws Exception {
532         return createRepository(
533                 MavenRepositorySystem.DEFAULT_REMOTE_REPO_URL,
534                 MavenRepositorySystem.DEFAULT_REMOTE_REPO_ID,
535                 true,
536                 ArtifactRepositoryPolicy.UPDATE_POLICY_DAILY,
537                 false,
538                 ArtifactRepositoryPolicy.UPDATE_POLICY_DAILY,
539                 ArtifactRepositoryPolicy.DEFAULT_CHECKSUM_POLICY);
540     }
541 
542     public ArtifactRepository createRepository(
543             String url,
544             String repositoryId,
545             boolean releases,
546             String releaseUpdates,
547             boolean snapshots,
548             String snapshotUpdates,
549             String checksumPolicy)
550             throws InvalidRepositoryException {
551         ArtifactRepositoryPolicy snapshotsPolicy =
552                 new ArtifactRepositoryPolicy(snapshots, snapshotUpdates, checksumPolicy);
553 
554         ArtifactRepositoryPolicy releasesPolicy =
555                 new ArtifactRepositoryPolicy(releases, releaseUpdates, checksumPolicy);
556 
557         return createArtifactRepository(repositoryId, url, "default", snapshotsPolicy, releasesPolicy);
558     }
559 
560     public Set<String> getRepoIds(List<ArtifactRepository> repositories) {
561         Set<String> repoIds = new HashSet<>();
562 
563         if (repositories != null) {
564             for (ArtifactRepository repository : repositories) {
565                 repoIds.add(repository.getId());
566             }
567         }
568 
569         return repoIds;
570     }
571 
572     /**
573      * Source from org.apache.maven.repository.legacy.LegacyRepositorySystem#getEffectiveRepositories
574      *
575      * @param repositories a list of repositories
576      * @return corresponding effective repositories
577      * @since 3.6.1
578      */
579     public List<ArtifactRepository> getEffectiveRepositories(List<ArtifactRepository> repositories) {
580         if (repositories == null) {
581             return null;
582         }
583 
584         Map<String, List<ArtifactRepository>> reposByKey = new LinkedHashMap<>();
585 
586         for (ArtifactRepository repository : repositories) {
587             String key = repository.getId();
588 
589             List<ArtifactRepository> aliasedRepos = reposByKey.computeIfAbsent(key, k -> new ArrayList<>());
590 
591             aliasedRepos.add(repository);
592         }
593 
594         List<ArtifactRepository> effectiveRepositories = new ArrayList<>();
595 
596         for (List<ArtifactRepository> aliasedRepos : reposByKey.values()) {
597             List<ArtifactRepository> mirroredRepos = new ArrayList<>();
598 
599             List<ArtifactRepositoryPolicy> releasePolicies = new ArrayList<>(aliasedRepos.size());
600 
601             for (ArtifactRepository aliasedRepo : aliasedRepos) {
602                 releasePolicies.add(aliasedRepo.getReleases());
603                 mirroredRepos.addAll(aliasedRepo.getMirroredRepositories());
604             }
605 
606             ArtifactRepositoryPolicy releasePolicy = getEffectivePolicy(releasePolicies);
607 
608             List<ArtifactRepositoryPolicy> snapshotPolicies = new ArrayList<>(aliasedRepos.size());
609 
610             for (ArtifactRepository aliasedRepo : aliasedRepos) {
611                 snapshotPolicies.add(aliasedRepo.getSnapshots());
612             }
613 
614             ArtifactRepositoryPolicy snapshotPolicy = getEffectivePolicy(snapshotPolicies);
615 
616             ArtifactRepository aliasedRepo = aliasedRepos.get(0);
617 
618             ArtifactRepository effectiveRepository = createArtifactRepository(
619                     aliasedRepo.getId(), aliasedRepo.getUrl(), aliasedRepo.getLayout(), snapshotPolicy, releasePolicy);
620 
621             effectiveRepository.setAuthentication(aliasedRepo.getAuthentication());
622 
623             effectiveRepository.setProxy(aliasedRepo.getProxy());
624 
625             effectiveRepository.setMirroredRepositories(mirroredRepos);
626 
627             effectiveRepository.setBlocked(aliasedRepo.isBlocked());
628 
629             effectiveRepositories.add(effectiveRepository);
630         }
631 
632         return effectiveRepositories;
633     }
634 
635     private ArtifactRepositoryPolicy getEffectivePolicy(Collection<ArtifactRepositoryPolicy> policies) {
636         ArtifactRepositoryPolicy effectivePolicy = null;
637 
638         for (ArtifactRepositoryPolicy policy : policies) {
639             if (effectivePolicy == null) {
640                 effectivePolicy = new ArtifactRepositoryPolicy(policy);
641             } else {
642                 effectivePolicy.merge(policy);
643             }
644         }
645 
646         return effectivePolicy;
647     }
648 
649     public ArtifactRepository createLocalRepository(MavenExecutionRequest request, File localRepository)
650             throws InvalidRepositoryException {
651         return createLocalRepository(localRepository);
652     }
653 
654     public ArtifactRepository createLocalRepository(File localRepository) throws InvalidRepositoryException {
655         return createRepository(
656                 "file://" + localRepository.toURI().getRawPath(),
657                 MavenRepositorySystem.DEFAULT_LOCAL_REPO_ID,
658                 true,
659                 ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS,
660                 true,
661                 ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS,
662                 ArtifactRepositoryPolicy.CHECKSUM_POLICY_IGNORE);
663     }
664 
665     private static final String WILDCARD = "*";
666 
667     private static final String EXTERNAL_WILDCARD = "external:*";
668 
669     private static final String EXTERNAL_HTTP_WILDCARD = "external:http:*";
670 
671     public static Mirror getMirror(ArtifactRepository repository, List<Mirror> mirrors) {
672         String repoId = repository.getId();
673 
674         if (repoId != null && mirrors != null) {
675             for (Mirror mirror : mirrors) {
676                 if (repoId.equals(mirror.getMirrorOf()) && matchesLayout(repository, mirror)) {
677                     return mirror;
678                 }
679             }
680 
681             for (Mirror mirror : mirrors) {
682                 if (matchPattern(repository, mirror.getMirrorOf()) && matchesLayout(repository, mirror)) {
683                     return mirror;
684                 }
685             }
686         }
687 
688         return null;
689     }
690 
691     /**
692      * This method checks if the pattern matches the originalRepository. Valid patterns:
693      * <ul>
694      * <li>{@code *} = everything,</li>
695      * <li>{@code external:*} = everything not on the localhost and not file based,</li>
696      * <li>{@code external:http:*} = any repository not on the localhost using HTTP,</li>
697      * <li>{@code repo,repo1} = {@code repo} or {@code repo1},</li>
698      * <li>{@code *,!repo1} = everything except {@code repo1}.</li>
699      * </ul>
700      *
701      * @param originalRepository to compare for a match.
702      * @param pattern used for match.
703      * @return true if the repository is a match to this pattern.
704      */
705     static boolean matchPattern(ArtifactRepository originalRepository, String pattern) {
706         boolean result = false;
707         String originalId = originalRepository.getId();
708 
709         // simple checks first to short circuit processing below.
710         if (WILDCARD.equals(pattern) || pattern.equals(originalId)) {
711             result = true;
712         } else {
713             // process the list
714             String[] repos = pattern.split(",");
715             for (String repo : repos) {
716                 // see if this is a negative match
717                 if (repo.length() > 1 && repo.startsWith("!")) {
718                     if (repo.substring(1).equals(originalId)) {
719                         // explicitly exclude. Set result and stop processing.
720                         result = false;
721                         break;
722                     }
723                 }
724                 // check for exact match
725                 else if (repo.equals(originalId)) {
726                     result = true;
727                     break;
728                 }
729                 // check for external:*
730                 else if (EXTERNAL_WILDCARD.equals(repo) && isExternalRepo(originalRepository)) {
731                     result = true;
732                     // don't stop processing in case a future segment explicitly excludes this repo
733                 }
734                 // check for external:http:*
735                 else if (EXTERNAL_HTTP_WILDCARD.equals(repo) && isExternalHttpRepo(originalRepository)) {
736                     result = true;
737                     // don't stop processing in case a future segment explicitly excludes this repo
738                 } else if (WILDCARD.equals(repo)) {
739                     result = true;
740                     // don't stop processing in case a future segment explicitly excludes this repo
741                 }
742             }
743         }
744         return result;
745     }
746 
747     /**
748      * Checks the URL to see if this repository refers to an external repository
749      *
750      * @param originalRepository
751      * @return true if external.
752      */
753     static boolean isExternalRepo(ArtifactRepository originalRepository) {
754         try {
755             URL url = new URL(originalRepository.getUrl());
756             return !(isLocal(url.getHost()) || url.getProtocol().equals("file"));
757         } catch (MalformedURLException e) {
758             // bad url just skip it here. It should have been validated already, but the wagon lookup will deal with it
759             return false;
760         }
761     }
762 
763     private static boolean isLocal(String host) {
764         return "localhost".equals(host) || "127.0.0.1".equals(host);
765     }
766 
767     /**
768      * Checks the URL to see if this repository refers to a non-localhost repository using HTTP.
769      *
770      * @param originalRepository
771      * @return true if external.
772      */
773     static boolean isExternalHttpRepo(ArtifactRepository originalRepository) {
774         try {
775             URL url = new URL(originalRepository.getUrl());
776             return ("http".equalsIgnoreCase(url.getProtocol())
777                             || "dav".equalsIgnoreCase(url.getProtocol())
778                             || "dav:http".equalsIgnoreCase(url.getProtocol())
779                             || "dav+http".equalsIgnoreCase(url.getProtocol()))
780                     && !isLocal(url.getHost());
781         } catch (MalformedURLException e) {
782             // bad url just skip it here. It should have been validated already, but the wagon lookup will deal with it
783             return false;
784         }
785     }
786 
787     static boolean matchesLayout(ArtifactRepository repository, Mirror mirror) {
788         return matchesLayout(RepositoryUtils.getLayout(repository), mirror.getMirrorOfLayouts());
789     }
790 
791     /**
792      * Checks whether the layouts configured for a mirror match with the layout of the repository.
793      *
794      * @param repoLayout The layout of the repository, may be {@code null}.
795      * @param mirrorLayout The layouts supported by the mirror, may be {@code null}.
796      * @return {@code true} if the layouts associated with the mirror match the layout of the original repository,
797      *         {@code false} otherwise.
798      */
799     static boolean matchesLayout(String repoLayout, String mirrorLayout) {
800         boolean result = false;
801 
802         // simple checks first to short circuit processing below.
803         if ((mirrorLayout == null || mirrorLayout.isEmpty()) || WILDCARD.equals(mirrorLayout)) {
804             result = true;
805         } else if (mirrorLayout.equals(repoLayout)) {
806             result = true;
807         } else {
808             // process the list
809             String[] layouts = mirrorLayout.split(",");
810             for (String layout : layouts) {
811                 // see if this is a negative match
812                 if (layout.length() > 1 && layout.startsWith("!")) {
813                     if (layout.substring(1).equals(repoLayout)) {
814                         // explicitly exclude. Set result and stop processing.
815                         result = false;
816                         break;
817                     }
818                 }
819                 // check for exact match
820                 else if (layout.equals(repoLayout)) {
821                     result = true;
822                     break;
823                 } else if (WILDCARD.equals(layout)) {
824                     result = true;
825                     // don't stop processing in case a future segment explicitly excludes this repo
826                 }
827             }
828         }
829 
830         return result;
831     }
832 }