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