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
33
34
35 @Component( role = MirrorSelector.class )
36 public class DefaultMirrorSelector
37 implements MirrorSelector
38 {
39
40 private static final String WILDCARD = "*";
41
42 private static final String EXTERNAL_WILDCARD = "external:*";
43
44 private static final String EXTERNAL_HTTP_WILDCARD = "external:http:*";
45
46 public Mirror getMirror( ArtifactRepository repository, List<Mirror> mirrors )
47 {
48 String repoId = repository.getId();
49
50 if ( repoId != null && mirrors != null )
51 {
52 for ( Mirror mirror : mirrors )
53 {
54 if ( repoId.equals( mirror.getMirrorOf() ) && matchesLayout( repository, mirror ) )
55 {
56 return mirror;
57 }
58 }
59
60 for ( Mirror mirror : mirrors )
61 {
62 if ( matchPattern( repository, mirror.getMirrorOf() ) && matchesLayout( repository, mirror ) )
63 {
64 return mirror;
65 }
66 }
67 }
68
69 return null;
70 }
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86 static boolean matchPattern( ArtifactRepository originalRepository, String pattern )
87 {
88 boolean result = false;
89 String originalId = originalRepository.getId();
90
91
92 if ( WILDCARD.equals( pattern ) || pattern.equals( originalId ) )
93 {
94 result = true;
95 }
96 else
97 {
98
99 String[] repos = pattern.split( "," );
100 for ( String repo : repos )
101 {
102 repo = repo.trim();
103
104 if ( repo.length() > 1 && repo.startsWith( "!" ) )
105 {
106 if ( repo.substring( 1 ).equals( originalId ) )
107 {
108
109 result = false;
110 break;
111 }
112 }
113
114 else if ( repo.equals( originalId ) )
115 {
116 result = true;
117 break;
118 }
119
120 else if ( EXTERNAL_WILDCARD.equals( repo ) && isExternalRepo( originalRepository ) )
121 {
122 result = true;
123
124 }
125
126 else if ( EXTERNAL_HTTP_WILDCARD.equals( repo ) && isExternalHttpRepo( originalRepository ) )
127 {
128 result = true;
129
130 }
131 else if ( WILDCARD.equals( repo ) )
132 {
133 result = true;
134
135 }
136 }
137 }
138 return result;
139 }
140
141
142
143
144
145
146
147 static boolean isExternalRepo( ArtifactRepository originalRepository )
148 {
149 try
150 {
151 URL url = new URL( originalRepository.getUrl() );
152 return !( isLocal( url.getHost() ) || url.getProtocol().equals( "file" ) );
153 }
154 catch ( MalformedURLException e )
155 {
156
157 return false;
158 }
159 }
160
161 private static boolean isLocal( String host )
162 {
163 return "localhost".equals( host ) || "127.0.0.1".equals( host );
164 }
165
166
167
168
169
170
171
172 static boolean isExternalHttpRepo( ArtifactRepository originalRepository )
173 {
174 try
175 {
176 URL url = new URL( originalRepository.getUrl() );
177 return ( "http".equalsIgnoreCase( url.getProtocol() ) || "dav".equalsIgnoreCase( url.getProtocol() )
178 || "dav:http".equalsIgnoreCase( url.getProtocol() )
179 || "dav+http".equalsIgnoreCase( url.getProtocol() ) ) && !isLocal( url.getHost() );
180 }
181 catch ( MalformedURLException e )
182 {
183
184 return false;
185 }
186 }
187
188 static boolean matchesLayout( ArtifactRepository repository, Mirror mirror )
189 {
190 return matchesLayout( RepositoryUtils.getLayout( repository ), mirror.getMirrorOfLayouts() );
191 }
192
193
194
195
196
197
198
199
200
201 static boolean matchesLayout( String repoLayout, String mirrorLayout )
202 {
203 boolean result = false;
204
205
206 if ( StringUtils.isEmpty( mirrorLayout ) || WILDCARD.equals( mirrorLayout ) )
207 {
208 result = true;
209 }
210 else if ( mirrorLayout.equals( repoLayout ) )
211 {
212 result = true;
213 }
214 else
215 {
216
217 String[] layouts = mirrorLayout.split( "," );
218 for ( String layout : layouts )
219 {
220
221 if ( layout.length() > 1 && layout.startsWith( "!" ) )
222 {
223 if ( layout.substring( 1 ).equals( repoLayout ) )
224 {
225
226 result = false;
227 break;
228 }
229 }
230
231 else if ( layout.equals( repoLayout ) )
232 {
233 result = true;
234 break;
235 }
236 else if ( WILDCARD.equals( layout ) )
237 {
238 result = true;
239
240 }
241 }
242 }
243
244 return result;
245 }
246
247 }