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