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