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 mirror.setBlocked( repo.isBlocked() );
194 return mirror;
195 }
196 }
197 }
198 return null;
199 }
200
201 public void injectMirror( RepositorySystemSession session, List<ArtifactRepository> repositories )
202 {
203 if ( repositories != null && session != null )
204 {
205 for ( ArtifactRepository repository : repositories )
206 {
207 Mirror mirror = getMirror( session, repository );
208 injectMirror( repository, mirror );
209 }
210 }
211 }
212
213 private void injectMirror( ArtifactRepository repository, Mirror mirror )
214 {
215 if ( mirror != null )
216 {
217 ArtifactRepository original =
218 createArtifactRepository( repository.getId(), repository.getUrl(), repository.getLayout(),
219 repository.getSnapshots(), repository.getReleases() );
220
221 repository.setMirroredRepositories( Collections.singletonList( original ) );
222
223 repository.setId( mirror.getId() );
224 repository.setUrl( mirror.getUrl() );
225
226 if ( StringUtils.isNotEmpty( mirror.getLayout() ) )
227 {
228 repository.setLayout( getLayout( mirror.getLayout() ) );
229 }
230
231 repository.setBlocked( mirror.isBlocked() );
232 }
233 }
234
235 private Authentication getAuthentication( RepositorySystemSession session, ArtifactRepository repository )
236 {
237 if ( session != null )
238 {
239 AuthenticationSelector selector = session.getAuthenticationSelector();
240 if ( selector != null )
241 {
242 RemoteRepository repo = RepositoryUtils.toRepo( repository );
243 org.eclipse.aether.repository.Authentication auth = selector.getAuthentication( repo );
244 if ( auth != null )
245 {
246 repo = new RemoteRepository.Builder( repo ).setAuthentication( auth ).build();
247 AuthenticationContext authCtx = AuthenticationContext.forRepository( session, repo );
248 Authentication result =
249 new Authentication( authCtx.get( AuthenticationContext.USERNAME ),
250 authCtx.get( AuthenticationContext.PASSWORD ) );
251 result.setPrivateKey( authCtx.get( AuthenticationContext.PRIVATE_KEY_PATH ) );
252 result.setPassphrase( authCtx.get( AuthenticationContext.PRIVATE_KEY_PASSPHRASE ) );
253 authCtx.close();
254 return result;
255 }
256 }
257 }
258 return null;
259 }
260
261 public void injectAuthentication( RepositorySystemSession session, List<ArtifactRepository> repositories )
262 {
263 if ( repositories != null && session != null )
264 {
265 for ( ArtifactRepository repository : repositories )
266 {
267 repository.setAuthentication( getAuthentication( session, repository ) );
268 }
269 }
270 }
271
272 private Proxy getProxy( RepositorySystemSession session, ArtifactRepository repository )
273 {
274 if ( session != null )
275 {
276 ProxySelector selector = session.getProxySelector();
277 if ( selector != null )
278 {
279 RemoteRepository repo = RepositoryUtils.toRepo( repository );
280 org.eclipse.aether.repository.Proxy proxy = selector.getProxy( repo );
281 if ( proxy != null )
282 {
283 Proxy p = new Proxy();
284 p.setHost( proxy.getHost() );
285 p.setProtocol( proxy.getType() );
286 p.setPort( proxy.getPort() );
287 if ( proxy.getAuthentication() != null )
288 {
289 repo = new RemoteRepository.Builder( repo ).setProxy( proxy ).build();
290 AuthenticationContext authCtx = AuthenticationContext.forProxy( session, repo );
291 p.setUserName( authCtx.get( AuthenticationContext.USERNAME ) );
292 p.setPassword( authCtx.get( AuthenticationContext.PASSWORD ) );
293 p.setNtlmDomain( authCtx.get( AuthenticationContext.NTLM_DOMAIN ) );
294 p.setNtlmHost( authCtx.get( AuthenticationContext.NTLM_WORKSTATION ) );
295 authCtx.close();
296 }
297 return p;
298 }
299 }
300 }
301 return null;
302 }
303
304 public void injectProxy( RepositorySystemSession session, List<ArtifactRepository> repositories )
305 {
306 if ( repositories != null && session != null )
307 {
308 for ( ArtifactRepository repository : repositories )
309 {
310 repository.setProxy( getProxy( session, repository ) );
311 }
312 }
313 }
314
315 private ArtifactRepositoryLayout getLayout( String id )
316 {
317 ArtifactRepositoryLayout layout = layouts.get( id );
318
319 return layout;
320 }
321
322
323
324
325
326
327 public static org.apache.maven.model.Repository fromSettingsRepository( org.apache.maven.settings.Repository
328 settingsRepository )
329 {
330 org.apache.maven.model.Repository modelRepository = new org.apache.maven.model.Repository();
331 modelRepository.setId( settingsRepository.getId() );
332 modelRepository.setLayout( settingsRepository.getLayout() );
333 modelRepository.setName( settingsRepository.getName() );
334 modelRepository.setUrl( settingsRepository.getUrl() );
335 modelRepository.setReleases( fromSettingsRepositoryPolicy( settingsRepository.getReleases() ) );
336 modelRepository.setSnapshots( fromSettingsRepositoryPolicy( settingsRepository.getSnapshots() ) );
337 return modelRepository;
338 }
339
340 public static org.apache.maven.model.RepositoryPolicy fromSettingsRepositoryPolicy(
341 org.apache.maven.settings.RepositoryPolicy settingsRepositoryPolicy )
342 {
343 org.apache.maven.model.RepositoryPolicy modelRepositoryPolicy = new org.apache.maven.model.RepositoryPolicy();
344 if ( settingsRepositoryPolicy != null )
345 {
346 modelRepositoryPolicy.setEnabled( settingsRepositoryPolicy.isEnabled() );
347 modelRepositoryPolicy.setUpdatePolicy( settingsRepositoryPolicy.getUpdatePolicy() );
348 modelRepositoryPolicy.setChecksumPolicy( settingsRepositoryPolicy.getChecksumPolicy() );
349 }
350 return modelRepositoryPolicy;
351 }
352
353 public static ArtifactRepository buildArtifactRepository( org.apache.maven.settings.Repository repo )
354 throws InvalidRepositoryException
355 {
356 return buildArtifactRepository( fromSettingsRepository( repo ) );
357 }
358
359 public static ArtifactRepository buildArtifactRepository( org.apache.maven.model.Repository repo )
360 throws InvalidRepositoryException
361 {
362 if ( repo != null )
363 {
364 String id = repo.getId();
365
366 if ( StringUtils.isEmpty( id ) )
367 {
368 throw new InvalidRepositoryException( "Repository identifier missing", "" );
369 }
370
371 String url = repo.getUrl();
372
373 if ( StringUtils.isEmpty( url ) )
374 {
375 throw new InvalidRepositoryException( "URL missing for repository " + id, id );
376 }
377
378 ArtifactRepositoryPolicy snapshots = buildArtifactRepositoryPolicy( repo.getSnapshots() );
379
380 ArtifactRepositoryPolicy releases = buildArtifactRepositoryPolicy( repo.getReleases() );
381
382 ArtifactRepositoryLayout layout = new DefaultRepositoryLayout();
383
384 return createArtifactRepository( id, url, layout, snapshots, releases );
385 }
386 else
387 {
388 return null;
389 }
390 }
391
392 public static ArtifactRepositoryPolicy buildArtifactRepositoryPolicy( org.apache.maven.model.RepositoryPolicy
393 policy )
394 {
395 boolean enabled = true;
396
397 String updatePolicy = null;
398
399 String checksumPolicy = null;
400
401 if ( policy != null )
402 {
403 enabled = policy.isEnabled();
404
405 if ( policy.getUpdatePolicy() != null )
406 {
407 updatePolicy = policy.getUpdatePolicy();
408 }
409 if ( policy.getChecksumPolicy() != null )
410 {
411 checksumPolicy = policy.getChecksumPolicy();
412 }
413 }
414
415 return new ArtifactRepositoryPolicy( enabled, updatePolicy, checksumPolicy );
416 }
417
418 public ArtifactRepository createArtifactRepository( String id, String url, String layoutId,
419 ArtifactRepositoryPolicy snapshots,
420 ArtifactRepositoryPolicy releases )
421 throws Exception
422 {
423 ArtifactRepositoryLayout layout = layouts.get( layoutId );
424
425 checkLayout( id, layoutId, layout );
426
427 return createArtifactRepository( id, url, layout, snapshots, releases );
428 }
429
430 private void checkLayout( String repositoryId, String layoutId, ArtifactRepositoryLayout layout )
431 throws Exception
432 {
433 if ( layout == null )
434 {
435 throw new Exception( String.format( "Cannot find ArtifactRepositoryLayout instance for: %s %s", layoutId,
436 repositoryId ) );
437 }
438 }
439
440 public static ArtifactRepository createArtifactRepository( String id, String url,
441 ArtifactRepositoryLayout repositoryLayout,
442 ArtifactRepositoryPolicy snapshots,
443 ArtifactRepositoryPolicy releases )
444 {
445 if ( snapshots == null )
446 {
447 snapshots = new ArtifactRepositoryPolicy();
448 }
449
450 if ( releases == null )
451 {
452 releases = new ArtifactRepositoryPolicy();
453 }
454
455 ArtifactRepository repository;
456 if ( repositoryLayout instanceof ArtifactRepositoryLayout2 )
457 {
458 repository =
459 ( (ArtifactRepositoryLayout2) repositoryLayout ).newMavenArtifactRepository( id, url, snapshots,
460 releases );
461 }
462 else
463 {
464 repository = new MavenArtifactRepository( id, url, repositoryLayout, snapshots, releases );
465 }
466
467 return repository;
468 }
469
470
471 private Artifact createArtifactX( String groupId, String artifactId, String version, String scope, String type )
472 {
473 return createArtifactX( groupId, artifactId, version, scope, type, null, null );
474 }
475
476 private Artifact createDependencyArtifactX( String groupId, String artifactId, VersionRange versionRange,
477 String type, String classifier, String scope, boolean optional )
478 {
479 return createArtifactX( groupId, artifactId, versionRange, type, classifier, scope, null, optional );
480 }
481
482 private Artifact createProjectArtifactX( String groupId, String artifactId, String version )
483 {
484 return createProjectArtifactX( groupId, artifactId, version, null );
485 }
486
487 private Artifact createParentArtifactX( String groupId, String artifactId, String version )
488 {
489 return createProjectArtifactX( groupId, artifactId, version );
490 }
491
492 private Artifact createPluginArtifactX( String groupId, String artifactId, VersionRange versionRange )
493 {
494 return createArtifactX( groupId, artifactId, versionRange, "maven-plugin", null, Artifact.SCOPE_RUNTIME, null );
495 }
496
497 private Artifact createProjectArtifactX( String groupId, String artifactId, String version, String scope )
498 {
499 return createArtifactX( groupId, artifactId, version, scope, "pom" );
500 }
501
502 private Artifact createExtensionArtifactX( String groupId, String artifactId, VersionRange versionRange )
503 {
504 return createArtifactX( groupId, artifactId, versionRange, "jar", null, Artifact.SCOPE_RUNTIME, null );
505 }
506
507 private Artifact createArtifactX( String groupId, String artifactId, String version, String scope, String type,
508 String classifier, String inheritedScope )
509 {
510 VersionRange versionRange = null;
511 if ( version != null )
512 {
513 versionRange = VersionRange.createFromVersion( version );
514 }
515 return createArtifactX( groupId, artifactId, versionRange, type, classifier, scope, inheritedScope );
516 }
517
518 private Artifact createArtifactX( String groupId, String artifactId, VersionRange versionRange, String type,
519 String classifier, String scope, String inheritedScope )
520 {
521 return createArtifactX( groupId, artifactId, versionRange, type, classifier, scope, inheritedScope, false );
522 }
523
524 @SuppressWarnings( "checkstyle:parameternumber" )
525 private Artifact createArtifactX( String groupId, String artifactId, VersionRange versionRange, String type,
526 String classifier, String scope, String inheritedScope, boolean optional )
527 {
528 String desiredScope = Artifact.SCOPE_RUNTIME;
529
530 if ( inheritedScope == null )
531 {
532 desiredScope = scope;
533 }
534 else if ( Artifact.SCOPE_TEST.equals( scope ) || Artifact.SCOPE_PROVIDED.equals( scope ) )
535 {
536 return null;
537 }
538 else if ( Artifact.SCOPE_COMPILE.equals( scope ) && Artifact.SCOPE_COMPILE.equals( inheritedScope ) )
539 {
540
541 desiredScope = Artifact.SCOPE_COMPILE;
542 }
543
544 if ( Artifact.SCOPE_TEST.equals( inheritedScope ) )
545 {
546 desiredScope = Artifact.SCOPE_TEST;
547 }
548
549 if ( Artifact.SCOPE_PROVIDED.equals( inheritedScope ) )
550 {
551 desiredScope = Artifact.SCOPE_PROVIDED;
552 }
553
554 if ( Artifact.SCOPE_SYSTEM.equals( scope ) )
555 {
556
557 desiredScope = Artifact.SCOPE_SYSTEM;
558 }
559
560 ArtifactHandler handler = artifactHandlerManager.getArtifactHandler( type );
561
562 return new DefaultArtifact( groupId, artifactId, versionRange, desiredScope, type, classifier, handler,
563 optional );
564 }
565
566
567
568
569
570 public ArtifactRepository createDefaultRemoteRepository( MavenExecutionRequest request )
571 throws Exception
572 {
573 return createRepository( RepositorySystem.DEFAULT_REMOTE_REPO_URL, RepositorySystem.DEFAULT_REMOTE_REPO_ID,
574 true, ArtifactRepositoryPolicy.UPDATE_POLICY_DAILY, false,
575 ArtifactRepositoryPolicy.UPDATE_POLICY_DAILY,
576 ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN );
577 }
578
579 public ArtifactRepository createRepository( String url, String repositoryId, boolean releases,
580 String releaseUpdates, boolean snapshots, String snapshotUpdates,
581 String checksumPolicy ) throws Exception
582 {
583 ArtifactRepositoryPolicy snapshotsPolicy =
584 new ArtifactRepositoryPolicy( snapshots, snapshotUpdates, checksumPolicy );
585
586 ArtifactRepositoryPolicy releasesPolicy =
587 new ArtifactRepositoryPolicy( releases, releaseUpdates, checksumPolicy );
588
589 return createArtifactRepository( repositoryId, url, "default", snapshotsPolicy, releasesPolicy );
590 }
591
592 public Set<String> getRepoIds( List<ArtifactRepository> repositories )
593 {
594 Set<String> repoIds = new HashSet<>();
595
596 if ( repositories != null )
597 {
598 for ( ArtifactRepository repository : repositories )
599 {
600 repoIds.add( repository.getId() );
601 }
602 }
603
604 return repoIds;
605 }
606
607
608
609
610
611
612
613
614 public List<ArtifactRepository> getEffectiveRepositories( List<ArtifactRepository> repositories )
615 {
616 if ( repositories == null )
617 {
618 return null;
619 }
620
621 Map<String, List<ArtifactRepository>> reposByKey = new LinkedHashMap<>();
622
623 for ( ArtifactRepository repository : repositories )
624 {
625 String key = repository.getId();
626
627 List<ArtifactRepository> aliasedRepos = reposByKey.get( key );
628
629 if ( aliasedRepos == null )
630 {
631 aliasedRepos = new ArrayList<>();
632 reposByKey.put( key, aliasedRepos );
633 }
634
635 aliasedRepos.add( repository );
636 }
637
638 List<ArtifactRepository> effectiveRepositories = new ArrayList<>();
639
640 for ( List<ArtifactRepository> aliasedRepos : reposByKey.values() )
641 {
642 List<ArtifactRepository> mirroredRepos = new ArrayList<>();
643
644 List<ArtifactRepositoryPolicy> releasePolicies =
645 new ArrayList<>( aliasedRepos.size() );
646
647 for ( ArtifactRepository aliasedRepo : aliasedRepos )
648 {
649 releasePolicies.add( aliasedRepo.getReleases() );
650 mirroredRepos.addAll( aliasedRepo.getMirroredRepositories() );
651 }
652
653 ArtifactRepositoryPolicy releasePolicy = getEffectivePolicy( releasePolicies );
654
655 List<ArtifactRepositoryPolicy> snapshotPolicies =
656 new ArrayList<>( aliasedRepos.size() );
657
658 for ( ArtifactRepository aliasedRepo : aliasedRepos )
659 {
660 snapshotPolicies.add( aliasedRepo.getSnapshots() );
661 }
662
663 ArtifactRepositoryPolicy snapshotPolicy = getEffectivePolicy( snapshotPolicies );
664
665 ArtifactRepository aliasedRepo = aliasedRepos.get( 0 );
666
667 ArtifactRepository effectiveRepository =
668 createArtifactRepository( aliasedRepo.getId(), aliasedRepo.getUrl(), aliasedRepo.getLayout(),
669 snapshotPolicy, releasePolicy );
670
671 effectiveRepository.setAuthentication( aliasedRepo.getAuthentication() );
672
673 effectiveRepository.setProxy( aliasedRepo.getProxy() );
674
675 effectiveRepository.setMirroredRepositories( mirroredRepos );
676
677 effectiveRepository.setBlocked( aliasedRepo.isBlocked() );
678
679 effectiveRepositories.add( effectiveRepository );
680 }
681
682 return effectiveRepositories;
683 }
684
685 private ArtifactRepositoryPolicy getEffectivePolicy( Collection<ArtifactRepositoryPolicy> policies )
686 {
687 ArtifactRepositoryPolicy effectivePolicy = null;
688
689 for ( ArtifactRepositoryPolicy policy : policies )
690 {
691 if ( effectivePolicy == null )
692 {
693 effectivePolicy = new ArtifactRepositoryPolicy( policy );
694 }
695 else
696 {
697 effectivePolicy.merge( policy );
698 }
699 }
700
701 return effectivePolicy;
702 }
703
704 public ArtifactRepository createLocalRepository( MavenExecutionRequest request, File localRepository )
705 throws Exception
706 {
707 return createRepository( "file://" + localRepository.toURI().getRawPath(),
708 RepositorySystem.DEFAULT_LOCAL_REPO_ID, true,
709 ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS, true,
710 ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS,
711 ArtifactRepositoryPolicy.CHECKSUM_POLICY_IGNORE );
712 }
713
714 private static final String WILDCARD = "*";
715
716 private static final String EXTERNAL_WILDCARD = "external:*";
717
718 private static final String EXTERNAL_HTTP_WILDCARD = "external:http:*";
719
720 public static Mirror getMirror( ArtifactRepository repository, List<Mirror> mirrors )
721 {
722 String repoId = repository.getId();
723
724 if ( repoId != null && mirrors != null )
725 {
726 for ( Mirror mirror : mirrors )
727 {
728 if ( repoId.equals( mirror.getMirrorOf() ) && matchesLayout( repository, mirror ) )
729 {
730 return mirror;
731 }
732 }
733
734 for ( Mirror mirror : mirrors )
735 {
736 if ( matchPattern( repository, mirror.getMirrorOf() ) && matchesLayout( repository, mirror ) )
737 {
738 return mirror;
739 }
740 }
741 }
742
743 return null;
744 }
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760 static boolean matchPattern( ArtifactRepository originalRepository, String pattern )
761 {
762 boolean result = false;
763 String originalId = originalRepository.getId();
764
765
766 if ( WILDCARD.equals( pattern ) || pattern.equals( originalId ) )
767 {
768 result = true;
769 }
770 else
771 {
772
773 String[] repos = pattern.split( "," );
774 for ( String repo : repos )
775 {
776
777 if ( repo.length() > 1 && repo.startsWith( "!" ) )
778 {
779 if ( repo.substring( 1 ).equals( originalId ) )
780 {
781
782 result = false;
783 break;
784 }
785 }
786
787 else if ( repo.equals( originalId ) )
788 {
789 result = true;
790 break;
791 }
792
793 else if ( EXTERNAL_WILDCARD.equals( repo ) && isExternalRepo( originalRepository ) )
794 {
795 result = true;
796
797 }
798
799 else if ( EXTERNAL_HTTP_WILDCARD.equals( repo ) && isExternalHttpRepo( originalRepository ) )
800 {
801 result = true;
802
803 }
804 else if ( WILDCARD.equals( repo ) )
805 {
806 result = true;
807
808 }
809 }
810 }
811 return result;
812 }
813
814
815
816
817
818
819
820 static boolean isExternalRepo( ArtifactRepository originalRepository )
821 {
822 try
823 {
824 URL url = new URL( originalRepository.getUrl() );
825 return !( isLocal( url.getHost() ) || url.getProtocol().equals( "file" ) );
826 }
827 catch ( MalformedURLException e )
828 {
829
830 return false;
831 }
832 }
833
834 private static boolean isLocal( String host )
835 {
836 return "localhost".equals( host ) || "127.0.0.1".equals( host );
837 }
838
839
840
841
842
843
844
845 static boolean isExternalHttpRepo( ArtifactRepository originalRepository )
846 {
847 try
848 {
849 URL url = new URL( originalRepository.getUrl() );
850 return ( "http".equalsIgnoreCase( url.getProtocol() ) || "dav".equalsIgnoreCase( url.getProtocol() )
851 || "dav:http".equalsIgnoreCase( url.getProtocol() )
852 || "dav+http".equalsIgnoreCase( url.getProtocol() ) ) && !isLocal( url.getHost() );
853 }
854 catch ( MalformedURLException e )
855 {
856
857 return false;
858 }
859 }
860
861 static boolean matchesLayout( ArtifactRepository repository, Mirror mirror )
862 {
863 return matchesLayout( RepositoryUtils.getLayout( repository ), mirror.getMirrorOfLayouts() );
864 }
865
866
867
868
869
870
871
872
873
874 static boolean matchesLayout( String repoLayout, String mirrorLayout )
875 {
876 boolean result = false;
877
878
879 if ( StringUtils.isEmpty( mirrorLayout ) || WILDCARD.equals( mirrorLayout ) )
880 {
881 result = true;
882 }
883 else if ( mirrorLayout.equals( repoLayout ) )
884 {
885 result = true;
886 }
887 else
888 {
889
890 String[] layouts = mirrorLayout.split( "," );
891 for ( String layout : layouts )
892 {
893
894 if ( layout.length() > 1 && layout.startsWith( "!" ) )
895 {
896 if ( layout.substring( 1 ).equals( repoLayout ) )
897 {
898
899 result = false;
900 break;
901 }
902 }
903
904 else if ( layout.equals( repoLayout ) )
905 {
906 result = true;
907 break;
908 }
909 else if ( WILDCARD.equals( layout ) )
910 {
911 result = true;
912
913 }
914 }
915 }
916
917 return result;
918 }
919 }