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 public Mirror getMirror( ArtifactRepository repository, List<Mirror> mirrors )
45 {
46 String repoId = repository.getId();
47
48 if ( repoId != null && mirrors != null )
49 {
50 for ( Mirror mirror : mirrors )
51 {
52 if ( repoId.equals( mirror.getMirrorOf() ) && matchesLayout( repository, mirror ) )
53 {
54 return mirror;
55 }
56 }
57
58 for ( Mirror mirror : mirrors )
59 {
60 if ( matchPattern( repository, mirror.getMirrorOf() ) && matchesLayout( repository, mirror ) )
61 {
62 return mirror;
63 }
64 }
65 }
66
67 return null;
68 }
69
70
71
72
73
74
75
76
77
78
79 static boolean matchPattern( ArtifactRepository originalRepository, String pattern )
80 {
81 boolean result = false;
82 String originalId = originalRepository.getId();
83
84
85 if ( WILDCARD.equals( pattern ) || pattern.equals( originalId ) )
86 {
87 result = true;
88 }
89 else
90 {
91
92 String[] repos = pattern.split( "," );
93 for ( String repo : repos )
94 {
95 repo = repo.trim();
96
97 if ( repo.length() > 1 && repo.startsWith( "!" ) )
98 {
99 if ( repo.substring( 1 ).equals( originalId ) )
100 {
101
102 result = false;
103 break;
104 }
105 }
106
107 else if ( repo.equals( originalId ) )
108 {
109 result = true;
110 break;
111 }
112
113 else if ( EXTERNAL_WILDCARD.equals( repo ) && isExternalRepo( originalRepository ) )
114 {
115 result = true;
116
117 }
118 else if ( WILDCARD.equals( repo ) )
119 {
120 result = true;
121
122 }
123 }
124 }
125 return result;
126 }
127
128
129
130
131
132
133
134 static boolean isExternalRepo( ArtifactRepository originalRepository )
135 {
136 try
137 {
138 URL url = new URL( originalRepository.getUrl() );
139 return !( url.getHost().equals( "localhost" ) || url.getHost().equals( "127.0.0.1" )
140 || url.getProtocol().equals( "file" ) );
141 }
142 catch ( MalformedURLException e )
143 {
144
145 return false;
146 }
147 }
148
149 static boolean matchesLayout( ArtifactRepository repository, Mirror mirror )
150 {
151 return matchesLayout( RepositoryUtils.getLayout( repository ), mirror.getMirrorOfLayouts() );
152 }
153
154
155
156
157
158
159
160
161
162 static boolean matchesLayout( String repoLayout, String mirrorLayout )
163 {
164 boolean result = false;
165
166
167 if ( StringUtils.isEmpty( mirrorLayout ) || WILDCARD.equals( mirrorLayout ) )
168 {
169 result = true;
170 }
171 else if ( mirrorLayout.equals( repoLayout ) )
172 {
173 result = true;
174 }
175 else
176 {
177
178 String[] layouts = mirrorLayout.split( "," );
179 for ( String layout : layouts )
180 {
181
182 if ( layout.length() > 1 && layout.startsWith( "!" ) )
183 {
184 if ( layout.substring( 1 ).equals( repoLayout ) )
185 {
186
187 result = false;
188 break;
189 }
190 }
191
192 else if ( layout.equals( repoLayout ) )
193 {
194 result = true;
195 break;
196 }
197 else if ( WILDCARD.equals( layout ) )
198 {
199 result = true;
200
201 }
202 }
203 }
204
205 return result;
206 }
207
208 }