1 package org.apache.maven.repository;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.net.MalformedURLException;
23 import java.net.URL;
24 import java.util.List;
25
26 import org.apache.maven.RepositoryUtils;
27 import org.apache.maven.artifact.repository.ArtifactRepository;
28 import org.apache.maven.settings.Mirror;
29 import org.codehaus.plexus.component.annotations.Component;
30 import org.codehaus.plexus.util.StringUtils;
31
32 @Component( role = MirrorSelector.class )
33 public class DefaultMirrorSelector
34 implements MirrorSelector
35 {
36
37 private static final String WILDCARD = "*";
38
39 private static final String EXTERNAL_WILDCARD = "external:*";
40
41 public Mirror getMirror( ArtifactRepository repository, List<Mirror> mirrors )
42 {
43 String repoId = repository.getId();
44
45 if ( repoId != null && mirrors != null )
46 {
47 for ( Mirror mirror : mirrors )
48 {
49 if ( repoId.equals( mirror.getMirrorOf() ) && matchesLayout( repository, mirror ) )
50 {
51 return mirror;
52 }
53 }
54
55 for ( Mirror mirror : mirrors )
56 {
57 if ( matchPattern( repository, mirror.getMirrorOf() ) && matchesLayout( repository, mirror ) )
58 {
59 return mirror;
60 }
61 }
62 }
63
64 return null;
65 }
66
67
68
69
70
71
72
73
74
75
76 static boolean matchPattern( ArtifactRepository originalRepository, String pattern )
77 {
78 boolean result = false;
79 String originalId = originalRepository.getId();
80
81
82 if ( WILDCARD.equals( pattern ) || pattern.equals( originalId ) )
83 {
84 result = true;
85 }
86 else
87 {
88
89 String[] repos = pattern.split( "," );
90 for ( String repo : repos )
91 {
92
93 if ( repo.length() > 1 && repo.startsWith( "!" ) )
94 {
95 if ( repo.substring( 1 ).equals( originalId ) )
96 {
97
98 result = false;
99 break;
100 }
101 }
102
103 else if ( repo.equals( originalId ) )
104 {
105 result = true;
106 break;
107 }
108
109 else if ( EXTERNAL_WILDCARD.equals( repo ) && isExternalRepo( originalRepository ) )
110 {
111 result = true;
112
113 }
114 else if ( WILDCARD.equals( repo ) )
115 {
116 result = true;
117
118 }
119 }
120 }
121 return result;
122 }
123
124
125
126
127
128
129
130 static boolean isExternalRepo( ArtifactRepository originalRepository )
131 {
132 try
133 {
134 URL url = new URL( originalRepository.getUrl() );
135 return !( url.getHost().equals( "localhost" ) || url.getHost().equals( "127.0.0.1" )
136 || url.getProtocol().equals( "file" ) );
137 }
138 catch ( MalformedURLException e )
139 {
140
141 return false;
142 }
143 }
144
145 static boolean matchesLayout( ArtifactRepository repository, Mirror mirror )
146 {
147 return matchesLayout( RepositoryUtils.getLayout( repository ), mirror.getMirrorOfLayouts() );
148 }
149
150
151
152
153
154
155
156
157
158 static boolean matchesLayout( String repoLayout, String mirrorLayout )
159 {
160 boolean result = false;
161
162
163 if ( StringUtils.isEmpty( mirrorLayout ) || WILDCARD.equals( mirrorLayout ) )
164 {
165 result = true;
166 }
167 else if ( mirrorLayout.equals( repoLayout ) )
168 {
169 result = true;
170 }
171 else
172 {
173
174 String[] layouts = mirrorLayout.split( "," );
175 for ( String layout : layouts )
176 {
177
178 if ( layout.length() > 1 && layout.startsWith( "!" ) )
179 {
180 if ( layout.substring( 1 ).equals( repoLayout ) )
181 {
182
183 result = false;
184 break;
185 }
186 }
187
188 else if ( layout.equals( repoLayout ) )
189 {
190 result = true;
191 break;
192 }
193 else if ( WILDCARD.equals( layout ) )
194 {
195 result = true;
196
197 }
198 }
199 }
200
201 return result;
202 }
203
204 }