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