1 package org.eclipse.aether.util.repository;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.List;
25
26 import org.eclipse.aether.repository.MirrorSelector;
27 import org.eclipse.aether.repository.RemoteRepository;
28
29
30
31
32 public final class DefaultMirrorSelector
33 implements MirrorSelector
34 {
35
36 private static final String WILDCARD = "*";
37
38 private static final String EXTERNAL_WILDCARD = "external:*";
39
40 private final List<MirrorDef> mirrors = new ArrayList<MirrorDef>();
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58 public DefaultMirrorSelector add( String id, String url, String type, boolean repositoryManager,
59 String mirrorOfIds, String mirrorOfTypes )
60 {
61 mirrors.add( new MirrorDef( id, url, type, repositoryManager, mirrorOfIds, mirrorOfTypes ) );
62
63 return this;
64 }
65
66 public RemoteRepository getMirror( RemoteRepository repository )
67 {
68 MirrorDef mirror = findMirror( repository );
69
70 if ( mirror == null )
71 {
72 return null;
73 }
74
75 RemoteRepository.Builder builder =
76 new RemoteRepository.Builder( mirror.id, repository.getContentType(), mirror.url );
77
78 builder.setRepositoryManager( mirror.repositoryManager );
79
80 if ( mirror.type != null && mirror.type.length() > 0 )
81 {
82 builder.setContentType( mirror.type );
83 }
84
85 builder.setSnapshotPolicy( repository.getPolicy( true ) );
86 builder.setReleasePolicy( repository.getPolicy( false ) );
87
88 builder.setMirroredRepositories( Collections.singletonList( repository ) );
89
90 return builder.build();
91 }
92
93 private MirrorDef findMirror( RemoteRepository repository )
94 {
95 String repoId = repository.getId();
96
97 if ( repoId != null && !mirrors.isEmpty() )
98 {
99 for ( MirrorDef mirror : mirrors )
100 {
101 if ( repoId.equals( mirror.mirrorOfIds ) && matchesType( repository.getContentType(),
102 mirror.mirrorOfTypes ) )
103 {
104 return mirror;
105 }
106 }
107
108 for ( MirrorDef mirror : mirrors )
109 {
110 if ( matchPattern( repository, mirror.mirrorOfIds ) && matchesType( repository.getContentType(),
111 mirror.mirrorOfTypes ) )
112 {
113 return mirror;
114 }
115 }
116 }
117
118 return null;
119 }
120
121
122
123
124
125
126
127
128
129 static boolean matchPattern( RemoteRepository repository, String pattern )
130 {
131 boolean result = false;
132 String originalId = repository.getId();
133
134
135 if ( WILDCARD.equals( pattern ) || pattern.equals( originalId ) )
136 {
137 result = true;
138 }
139 else
140 {
141
142 String[] repos = pattern.split( "," );
143 for ( String repo : repos )
144 {
145
146 if ( repo.length() > 1 && repo.startsWith( "!" ) )
147 {
148 if ( repo.substring( 1 ).equals( originalId ) )
149 {
150
151 result = false;
152 break;
153 }
154 }
155
156 else if ( repo.equals( originalId ) )
157 {
158 result = true;
159 break;
160 }
161
162 else if ( EXTERNAL_WILDCARD.equals( repo ) && isExternalRepo( repository ) )
163 {
164 result = true;
165
166 }
167 else if ( WILDCARD.equals( repo ) )
168 {
169 result = true;
170
171 }
172 }
173 }
174 return result;
175 }
176
177
178
179
180
181
182
183 static boolean isExternalRepo( RemoteRepository repository )
184 {
185 boolean local =
186 "localhost".equals( repository.getHost() ) || "127.0.0.1".equals( repository.getHost() )
187 || "file".equalsIgnoreCase( repository.getProtocol() );
188 return !local;
189 }
190
191
192
193
194
195
196
197
198
199 static boolean matchesType( String repoType, String mirrorType )
200 {
201 boolean result = false;
202
203
204 if ( mirrorType == null || mirrorType.length() <= 0 || WILDCARD.equals( mirrorType ) )
205 {
206 result = true;
207 }
208 else if ( mirrorType.equals( repoType ) )
209 {
210 result = true;
211 }
212 else
213 {
214
215 String[] layouts = mirrorType.split( "," );
216 for ( String layout : layouts )
217 {
218
219 if ( layout.length() > 1 && layout.startsWith( "!" ) )
220 {
221 if ( layout.substring( 1 ).equals( repoType ) )
222 {
223
224 result = false;
225 break;
226 }
227 }
228
229 else if ( layout.equals( repoType ) )
230 {
231 result = true;
232 break;
233 }
234 else if ( WILDCARD.equals( layout ) )
235 {
236 result = true;
237
238 }
239 }
240 }
241
242 return result;
243 }
244
245 static class MirrorDef
246 {
247
248 final String id;
249
250 final String url;
251
252 final String type;
253
254 final boolean repositoryManager;
255
256 final String mirrorOfIds;
257
258 final String mirrorOfTypes;
259
260 public MirrorDef( String id, String url, String type, boolean repositoryManager, String mirrorOfIds,
261 String mirrorOfTypes )
262 {
263 this.id = id;
264 this.url = url;
265 this.type = type;
266 this.repositoryManager = repositoryManager;
267 this.mirrorOfIds = mirrorOfIds;
268 this.mirrorOfTypes = mirrorOfTypes;
269 }
270
271 }
272
273 }