1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.aether.repository;
20
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.Collections;
24 import java.util.List;
25 import java.util.Objects;
26 import java.util.regex.Matcher;
27 import java.util.regex.Pattern;
28
29 import static java.util.Objects.requireNonNull;
30
31
32
33
34 public final class RemoteRepository implements ArtifactRepository {
35
36 private static final Pattern URL_PATTERN =
37 Pattern.compile("([^:/]+(:[^:/]{2,}+(?=://))?):(//([^@/]*@)?([^/:]+))?.*");
38
39 private final String id;
40
41 private final String type;
42
43 private final String url;
44
45 private final String host;
46
47 private final String protocol;
48
49 private final RepositoryPolicy releasePolicy;
50
51 private final RepositoryPolicy snapshotPolicy;
52
53 private final Proxy proxy;
54
55 private final Authentication authentication;
56
57 private final List<RemoteRepository> mirroredRepositories;
58
59 private final boolean repositoryManager;
60
61 private final boolean blocked;
62
63 RemoteRepository(Builder builder) {
64 if (builder.prototype != null) {
65 id = (builder.delta & Builder.ID) != 0 ? builder.id : builder.prototype.id;
66 type = (builder.delta & Builder.TYPE) != 0 ? builder.type : builder.prototype.type;
67 url = (builder.delta & Builder.URL) != 0 ? builder.url : builder.prototype.url;
68 releasePolicy =
69 (builder.delta & Builder.RELEASES) != 0 ? builder.releasePolicy : builder.prototype.releasePolicy;
70 snapshotPolicy = (builder.delta & Builder.SNAPSHOTS) != 0
71 ? builder.snapshotPolicy
72 : builder.prototype.snapshotPolicy;
73 proxy = (builder.delta & Builder.PROXY) != 0 ? builder.proxy : builder.prototype.proxy;
74 authentication =
75 (builder.delta & Builder.AUTH) != 0 ? builder.authentication : builder.prototype.authentication;
76 repositoryManager = (builder.delta & Builder.REPOMAN) != 0
77 ? builder.repositoryManager
78 : builder.prototype.repositoryManager;
79 blocked = (builder.delta & Builder.BLOCKED) != 0 ? builder.blocked : builder.prototype.blocked;
80 mirroredRepositories = (builder.delta & Builder.MIRRORED) != 0
81 ? copy(builder.mirroredRepositories)
82 : builder.prototype.mirroredRepositories;
83 } else {
84 id = builder.id;
85 type = builder.type;
86 url = builder.url;
87 releasePolicy = builder.releasePolicy;
88 snapshotPolicy = builder.snapshotPolicy;
89 proxy = builder.proxy;
90 authentication = builder.authentication;
91 repositoryManager = builder.repositoryManager;
92 blocked = builder.blocked;
93 mirroredRepositories = copy(builder.mirroredRepositories);
94 }
95
96 Matcher m = URL_PATTERN.matcher(url);
97 if (m.matches()) {
98 protocol = m.group(1);
99 String host = m.group(5);
100 this.host = (host != null) ? host : "";
101 } else {
102 protocol = "";
103 host = "";
104 }
105 }
106
107 private static List<RemoteRepository> copy(List<RemoteRepository> repos) {
108 if (repos == null || repos.isEmpty()) {
109 return Collections.emptyList();
110 }
111 return Collections.unmodifiableList(Arrays.asList(repos.toArray(new RemoteRepository[0])));
112 }
113
114 public String getId() {
115 return id;
116 }
117
118 public String getContentType() {
119 return type;
120 }
121
122
123
124
125
126
127 public String getUrl() {
128 return url;
129 }
130
131
132
133
134
135
136
137 public String getProtocol() {
138 return protocol;
139 }
140
141
142
143
144
145
146 public String getHost() {
147 return host;
148 }
149
150
151
152
153
154
155
156 public RepositoryPolicy getPolicy(boolean snapshot) {
157 return snapshot ? snapshotPolicy : releasePolicy;
158 }
159
160
161
162
163
164
165 public Proxy getProxy() {
166 return proxy;
167 }
168
169
170
171
172
173
174 public Authentication getAuthentication() {
175 return authentication;
176 }
177
178
179
180
181
182
183 public List<RemoteRepository> getMirroredRepositories() {
184 return mirroredRepositories;
185 }
186
187
188
189
190
191
192 public boolean isRepositoryManager() {
193 return repositoryManager;
194 }
195
196
197
198
199
200
201
202 public boolean isBlocked() {
203 return blocked;
204 }
205
206 @Override
207 public String toString() {
208 StringBuilder buffer = new StringBuilder(256);
209 buffer.append(getId());
210 buffer.append(" (").append(getUrl());
211 buffer.append(", ").append(getContentType());
212 boolean r = getPolicy(false).isEnabled(), s = getPolicy(true).isEnabled();
213 if (r && s) {
214 buffer.append(", releases+snapshots");
215 } else if (r) {
216 buffer.append(", releases");
217 } else if (s) {
218 buffer.append(", snapshots");
219 } else {
220 buffer.append(", disabled");
221 }
222 if (isRepositoryManager()) {
223 buffer.append(", managed");
224 }
225 if (isBlocked()) {
226 buffer.append(", blocked");
227 }
228 buffer.append(")");
229 return buffer.toString();
230 }
231
232 @Override
233 public boolean equals(Object obj) {
234 if (this == obj) {
235 return true;
236 }
237 if (obj == null || !getClass().equals(obj.getClass())) {
238 return false;
239 }
240
241 RemoteRepository that = (RemoteRepository) obj;
242
243 return Objects.equals(url, that.url)
244 && Objects.equals(type, that.type)
245 && Objects.equals(id, that.id)
246 && Objects.equals(releasePolicy, that.releasePolicy)
247 && Objects.equals(snapshotPolicy, that.snapshotPolicy)
248 && Objects.equals(proxy, that.proxy)
249 && Objects.equals(authentication, that.authentication)
250 && Objects.equals(mirroredRepositories, that.mirroredRepositories)
251 && repositoryManager == that.repositoryManager;
252 }
253
254 @Override
255 public int hashCode() {
256 int hash = 17;
257 hash = hash * 31 + hash(url);
258 hash = hash * 31 + hash(type);
259 hash = hash * 31 + hash(id);
260 hash = hash * 31 + hash(releasePolicy);
261 hash = hash * 31 + hash(snapshotPolicy);
262 hash = hash * 31 + hash(proxy);
263 hash = hash * 31 + hash(authentication);
264 hash = hash * 31 + hash(mirroredRepositories);
265 hash = hash * 31 + (repositoryManager ? 1 : 0);
266 return hash;
267 }
268
269 private static int hash(Object obj) {
270 return obj != null ? obj.hashCode() : 0;
271 }
272
273
274
275
276 public static final class Builder {
277
278 private static final RepositoryPolicy DEFAULT_POLICY = new RepositoryPolicy();
279
280 static final int ID = 0x0001,
281 TYPE = 0x0002,
282 URL = 0x0004,
283 RELEASES = 0x0008,
284 SNAPSHOTS = 0x0010,
285 PROXY = 0x0020,
286 AUTH = 0x0040,
287 MIRRORED = 0x0080,
288 REPOMAN = 0x0100,
289 BLOCKED = 0x0200;
290
291 int delta;
292
293 RemoteRepository prototype;
294
295 String id;
296
297 String type;
298
299 String url;
300
301 RepositoryPolicy releasePolicy = DEFAULT_POLICY;
302
303 RepositoryPolicy snapshotPolicy = DEFAULT_POLICY;
304
305 Proxy proxy;
306
307 Authentication authentication;
308
309 List<RemoteRepository> mirroredRepositories;
310
311 boolean repositoryManager;
312
313 boolean blocked;
314
315
316
317
318
319
320
321
322 public Builder(String id, String type, String url) {
323 this.id = (id != null) ? id : "";
324 this.type = (type != null) ? type : "";
325 this.url = (url != null) ? url : "";
326 }
327
328
329
330
331
332
333
334
335 public Builder(RemoteRepository prototype) {
336 this.prototype = requireNonNull(prototype, "remote repository prototype cannot be null");
337 }
338
339
340
341
342
343
344
345 public RemoteRepository build() {
346 if (prototype != null && delta == 0) {
347 return prototype;
348 }
349 return new RemoteRepository(this);
350 }
351
352 private <T> void delta(int flag, T builder, T prototype) {
353 boolean equal = Objects.equals(builder, prototype);
354 if (equal) {
355 delta &= ~flag;
356 } else {
357 delta |= flag;
358 }
359 }
360
361
362
363
364
365
366
367 public Builder setId(String id) {
368 this.id = (id != null) ? id : "";
369 if (prototype != null) {
370 delta(ID, this.id, prototype.getId());
371 }
372 return this;
373 }
374
375
376
377
378
379
380
381 public Builder setContentType(String type) {
382 this.type = (type != null) ? type : "";
383 if (prototype != null) {
384 delta(TYPE, this.type, prototype.getContentType());
385 }
386 return this;
387 }
388
389
390
391
392
393
394
395 public Builder setUrl(String url) {
396 this.url = (url != null) ? url : "";
397 if (prototype != null) {
398 delta(URL, this.url, prototype.getUrl());
399 }
400 return this;
401 }
402
403
404
405
406
407
408
409 public Builder setPolicy(RepositoryPolicy policy) {
410 this.releasePolicy = (policy != null) ? policy : DEFAULT_POLICY;
411 this.snapshotPolicy = (policy != null) ? policy : DEFAULT_POLICY;
412 if (prototype != null) {
413 delta(RELEASES, this.releasePolicy, prototype.getPolicy(false));
414 delta(SNAPSHOTS, this.snapshotPolicy, prototype.getPolicy(true));
415 }
416 return this;
417 }
418
419
420
421
422
423
424
425 public Builder setReleasePolicy(RepositoryPolicy releasePolicy) {
426 this.releasePolicy = (releasePolicy != null) ? releasePolicy : DEFAULT_POLICY;
427 if (prototype != null) {
428 delta(RELEASES, this.releasePolicy, prototype.getPolicy(false));
429 }
430 return this;
431 }
432
433
434
435
436
437
438
439 public Builder setSnapshotPolicy(RepositoryPolicy snapshotPolicy) {
440 this.snapshotPolicy = (snapshotPolicy != null) ? snapshotPolicy : DEFAULT_POLICY;
441 if (prototype != null) {
442 delta(SNAPSHOTS, this.snapshotPolicy, prototype.getPolicy(true));
443 }
444 return this;
445 }
446
447
448
449
450
451
452
453 public Builder setProxy(Proxy proxy) {
454 this.proxy = proxy;
455 if (prototype != null) {
456 delta(PROXY, this.proxy, prototype.getProxy());
457 }
458 return this;
459 }
460
461
462
463
464
465
466
467 public Builder setAuthentication(Authentication authentication) {
468 this.authentication = authentication;
469 if (prototype != null) {
470 delta(AUTH, this.authentication, prototype.getAuthentication());
471 }
472 return this;
473 }
474
475
476
477
478
479
480
481 public Builder setMirroredRepositories(List<RemoteRepository> mirroredRepositories) {
482 if (this.mirroredRepositories == null) {
483 this.mirroredRepositories = new ArrayList<>();
484 } else {
485 this.mirroredRepositories.clear();
486 }
487 if (mirroredRepositories != null) {
488 this.mirroredRepositories.addAll(mirroredRepositories);
489 }
490 if (prototype != null) {
491 delta(MIRRORED, this.mirroredRepositories, prototype.getMirroredRepositories());
492 }
493 return this;
494 }
495
496
497
498
499
500
501
502
503
504 public Builder addMirroredRepository(RemoteRepository mirroredRepository) {
505 if (mirroredRepository != null) {
506 if (this.mirroredRepositories == null) {
507 this.mirroredRepositories = new ArrayList<>();
508 if (prototype != null) {
509 mirroredRepositories.addAll(prototype.getMirroredRepositories());
510 }
511 }
512 mirroredRepositories.add(mirroredRepository);
513 if (prototype != null) {
514 delta |= MIRRORED;
515 }
516 }
517 return this;
518 }
519
520
521
522
523
524
525
526
527 public Builder setRepositoryManager(boolean repositoryManager) {
528 this.repositoryManager = repositoryManager;
529 if (prototype != null) {
530 delta(REPOMAN, this.repositoryManager, prototype.isRepositoryManager());
531 }
532 return this;
533 }
534
535
536
537
538
539
540
541 public Builder setBlocked(boolean blocked) {
542 this.blocked = blocked;
543 if (prototype != null) {
544 delta(BLOCKED, this.blocked, prototype.isBlocked());
545 }
546 return this;
547 }
548 }
549 }