1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.aether;
20
21 import java.util.Collections;
22 import java.util.HashMap;
23 import java.util.Map;
24
25 import org.eclipse.aether.artifact.ArtifactType;
26 import org.eclipse.aether.artifact.ArtifactTypeRegistry;
27 import org.eclipse.aether.collection.DependencyGraphTransformer;
28 import org.eclipse.aether.collection.DependencyManager;
29 import org.eclipse.aether.collection.DependencySelector;
30 import org.eclipse.aether.collection.DependencyTraverser;
31 import org.eclipse.aether.collection.VersionFilter;
32 import org.eclipse.aether.repository.Authentication;
33 import org.eclipse.aether.repository.AuthenticationSelector;
34 import org.eclipse.aether.repository.LocalRepository;
35 import org.eclipse.aether.repository.LocalRepositoryManager;
36 import org.eclipse.aether.repository.MirrorSelector;
37 import org.eclipse.aether.repository.Proxy;
38 import org.eclipse.aether.repository.ProxySelector;
39 import org.eclipse.aether.repository.RemoteRepository;
40 import org.eclipse.aether.repository.RepositoryPolicy;
41 import org.eclipse.aether.repository.WorkspaceReader;
42 import org.eclipse.aether.resolution.ArtifactDescriptorPolicy;
43 import org.eclipse.aether.resolution.ResolutionErrorPolicy;
44 import org.eclipse.aether.transfer.TransferListener;
45
46 import static java.util.Objects.requireNonNull;
47
48
49
50
51
52
53
54
55
56 public final class DefaultRepositorySystemSession implements RepositorySystemSession {
57
58 private boolean readOnly;
59
60 private boolean offline;
61
62 private boolean ignoreArtifactDescriptorRepositories;
63
64 private ResolutionErrorPolicy resolutionErrorPolicy;
65
66 private ArtifactDescriptorPolicy artifactDescriptorPolicy;
67
68 private String checksumPolicy;
69
70 private String artifactUpdatePolicy;
71
72 private String metadataUpdatePolicy;
73
74 private LocalRepositoryManager localRepositoryManager;
75
76 private WorkspaceReader workspaceReader;
77
78 private RepositoryListener repositoryListener;
79
80 private TransferListener transferListener;
81
82 private Map<String, String> systemProperties;
83
84 private Map<String, String> systemPropertiesView;
85
86 private Map<String, String> userProperties;
87
88 private Map<String, String> userPropertiesView;
89
90 private Map<String, Object> configProperties;
91
92 private Map<String, Object> configPropertiesView;
93
94 private MirrorSelector mirrorSelector;
95
96 private ProxySelector proxySelector;
97
98 private AuthenticationSelector authenticationSelector;
99
100 private ArtifactTypeRegistry artifactTypeRegistry;
101
102 private DependencyTraverser dependencyTraverser;
103
104 private DependencyManager dependencyManager;
105
106 private DependencySelector dependencySelector;
107
108 private VersionFilter versionFilter;
109
110 private DependencyGraphTransformer dependencyGraphTransformer;
111
112 private SessionData data;
113
114 private RepositoryCache cache;
115
116
117
118
119
120
121 public DefaultRepositorySystemSession() {
122 systemProperties = new HashMap<>();
123 systemPropertiesView = Collections.unmodifiableMap(systemProperties);
124 userProperties = new HashMap<>();
125 userPropertiesView = Collections.unmodifiableMap(userProperties);
126 configProperties = new HashMap<>();
127 configPropertiesView = Collections.unmodifiableMap(configProperties);
128 mirrorSelector = NullMirrorSelector.INSTANCE;
129 proxySelector = NullProxySelector.INSTANCE;
130 authenticationSelector = NullAuthenticationSelector.INSTANCE;
131 artifactTypeRegistry = NullArtifactTypeRegistry.INSTANCE;
132 data = new DefaultSessionData();
133 }
134
135
136
137
138
139
140
141
142
143 public DefaultRepositorySystemSession(RepositorySystemSession session) {
144 requireNonNull(session, "repository system session cannot be null");
145
146 setOffline(session.isOffline());
147 setIgnoreArtifactDescriptorRepositories(session.isIgnoreArtifactDescriptorRepositories());
148 setResolutionErrorPolicy(session.getResolutionErrorPolicy());
149 setArtifactDescriptorPolicy(session.getArtifactDescriptorPolicy());
150 setChecksumPolicy(session.getChecksumPolicy());
151 setUpdatePolicy(session.getUpdatePolicy());
152 setMetadataUpdatePolicy(session.getMetadataUpdatePolicy());
153 setLocalRepositoryManager(session.getLocalRepositoryManager());
154 setWorkspaceReader(session.getWorkspaceReader());
155 setRepositoryListener(session.getRepositoryListener());
156 setTransferListener(session.getTransferListener());
157 setSystemProperties(session.getSystemProperties());
158 setUserProperties(session.getUserProperties());
159 setConfigProperties(session.getConfigProperties());
160 setMirrorSelector(session.getMirrorSelector());
161 setProxySelector(session.getProxySelector());
162 setAuthenticationSelector(session.getAuthenticationSelector());
163 setArtifactTypeRegistry(session.getArtifactTypeRegistry());
164 setDependencyTraverser(session.getDependencyTraverser());
165 setDependencyManager(session.getDependencyManager());
166 setDependencySelector(session.getDependencySelector());
167 setVersionFilter(session.getVersionFilter());
168 setDependencyGraphTransformer(session.getDependencyGraphTransformer());
169 setData(session.getData());
170 setCache(session.getCache());
171 }
172
173 @Override
174 public boolean isOffline() {
175 return offline;
176 }
177
178
179
180
181
182
183
184
185 public DefaultRepositorySystemSession setOffline(boolean offline) {
186 verifyStateForMutation();
187 this.offline = offline;
188 return this;
189 }
190
191 @Override
192 public boolean isIgnoreArtifactDescriptorRepositories() {
193 return ignoreArtifactDescriptorRepositories;
194 }
195
196
197
198
199
200
201
202
203
204
205 public DefaultRepositorySystemSession setIgnoreArtifactDescriptorRepositories(
206 boolean ignoreArtifactDescriptorRepositories) {
207 verifyStateForMutation();
208 this.ignoreArtifactDescriptorRepositories = ignoreArtifactDescriptorRepositories;
209 return this;
210 }
211
212 @Override
213 public ResolutionErrorPolicy getResolutionErrorPolicy() {
214 return resolutionErrorPolicy;
215 }
216
217
218
219
220
221
222
223
224 public DefaultRepositorySystemSession setResolutionErrorPolicy(ResolutionErrorPolicy resolutionErrorPolicy) {
225 verifyStateForMutation();
226 this.resolutionErrorPolicy = resolutionErrorPolicy;
227 return this;
228 }
229
230 @Override
231 public ArtifactDescriptorPolicy getArtifactDescriptorPolicy() {
232 return artifactDescriptorPolicy;
233 }
234
235
236
237
238
239
240
241
242 public DefaultRepositorySystemSession setArtifactDescriptorPolicy(
243 ArtifactDescriptorPolicy artifactDescriptorPolicy) {
244 verifyStateForMutation();
245 this.artifactDescriptorPolicy = artifactDescriptorPolicy;
246 return this;
247 }
248
249 @Override
250 public String getChecksumPolicy() {
251 return checksumPolicy;
252 }
253
254
255
256
257
258
259
260
261
262
263
264 public DefaultRepositorySystemSession setChecksumPolicy(String checksumPolicy) {
265 verifyStateForMutation();
266 this.checksumPolicy = checksumPolicy;
267 return this;
268 }
269
270 @Override
271 public String getUpdatePolicy() {
272 return getArtifactUpdatePolicy();
273 }
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292 public DefaultRepositorySystemSession setUpdatePolicy(String updatePolicy) {
293 verifyStateForMutation();
294 setArtifactUpdatePolicy(updatePolicy);
295 setMetadataUpdatePolicy(updatePolicy);
296 return this;
297 }
298
299 @Override
300 public String getArtifactUpdatePolicy() {
301 return artifactUpdatePolicy;
302 }
303
304
305
306
307
308
309
310
311
312
313
314
315 public DefaultRepositorySystemSession setArtifactUpdatePolicy(String artifactUpdatePolicy) {
316 verifyStateForMutation();
317 this.artifactUpdatePolicy = artifactUpdatePolicy;
318 return this;
319 }
320
321 @Override
322 public String getMetadataUpdatePolicy() {
323 return metadataUpdatePolicy;
324 }
325
326
327
328
329
330
331
332
333
334
335
336
337 public DefaultRepositorySystemSession setMetadataUpdatePolicy(String metadataUpdatePolicy) {
338 verifyStateForMutation();
339 this.metadataUpdatePolicy = metadataUpdatePolicy;
340 return this;
341 }
342
343 @Override
344 public LocalRepository getLocalRepository() {
345 LocalRepositoryManager lrm = getLocalRepositoryManager();
346 return (lrm != null) ? lrm.getRepository() : null;
347 }
348
349 public LocalRepositoryManager getLocalRepositoryManager() {
350 return localRepositoryManager;
351 }
352
353
354
355
356
357
358
359
360 public DefaultRepositorySystemSession setLocalRepositoryManager(LocalRepositoryManager localRepositoryManager) {
361 verifyStateForMutation();
362 this.localRepositoryManager = localRepositoryManager;
363 return this;
364 }
365
366 @Override
367 public WorkspaceReader getWorkspaceReader() {
368 return workspaceReader;
369 }
370
371
372
373
374
375
376
377
378 public DefaultRepositorySystemSession setWorkspaceReader(WorkspaceReader workspaceReader) {
379 verifyStateForMutation();
380 this.workspaceReader = workspaceReader;
381 return this;
382 }
383
384 @Override
385 public RepositoryListener getRepositoryListener() {
386 return repositoryListener;
387 }
388
389
390
391
392
393
394
395 public DefaultRepositorySystemSession setRepositoryListener(RepositoryListener repositoryListener) {
396 verifyStateForMutation();
397 this.repositoryListener = repositoryListener;
398 return this;
399 }
400
401 @Override
402 public TransferListener getTransferListener() {
403 return transferListener;
404 }
405
406
407
408
409
410
411
412 public DefaultRepositorySystemSession setTransferListener(TransferListener transferListener) {
413 verifyStateForMutation();
414 this.transferListener = transferListener;
415 return this;
416 }
417
418 @SuppressWarnings("checkstyle:magicnumber")
419 private <T> Map<String, T> copySafe(Map<?, ?> table, Class<T> valueType) {
420 Map<String, T> map;
421 if (table == null || table.isEmpty()) {
422 map = new HashMap<>();
423 } else {
424 map = new HashMap<>((int) (table.size() / 0.75f) + 1);
425 for (Map.Entry<?, ?> entry : table.entrySet()) {
426 Object key = entry.getKey();
427 if (key instanceof String) {
428 Object value = entry.getValue();
429 if (valueType.isInstance(value)) {
430 map.put(key.toString(), valueType.cast(value));
431 }
432 }
433 }
434 }
435 return map;
436 }
437
438 @Override
439 public Map<String, String> getSystemProperties() {
440 return systemPropertiesView;
441 }
442
443
444
445
446
447
448
449
450
451
452
453 public DefaultRepositorySystemSession setSystemProperties(Map<?, ?> systemProperties) {
454 verifyStateForMutation();
455 this.systemProperties = copySafe(systemProperties, String.class);
456 systemPropertiesView = Collections.unmodifiableMap(this.systemProperties);
457 return this;
458 }
459
460
461
462
463
464
465
466
467 public DefaultRepositorySystemSession setSystemProperty(String key, String value) {
468 verifyStateForMutation();
469 if (value != null) {
470 systemProperties.put(key, value);
471 } else {
472 systemProperties.remove(key);
473 }
474 return this;
475 }
476
477 @Override
478 public Map<String, String> getUserProperties() {
479 return userPropertiesView;
480 }
481
482
483
484
485
486
487
488
489
490
491
492
493 public DefaultRepositorySystemSession setUserProperties(Map<?, ?> userProperties) {
494 verifyStateForMutation();
495 this.userProperties = copySafe(userProperties, String.class);
496 userPropertiesView = Collections.unmodifiableMap(this.userProperties);
497 return this;
498 }
499
500
501
502
503
504
505
506
507 public DefaultRepositorySystemSession setUserProperty(String key, String value) {
508 verifyStateForMutation();
509 if (value != null) {
510 userProperties.put(key, value);
511 } else {
512 userProperties.remove(key);
513 }
514 return this;
515 }
516
517 @Override
518 public Map<String, Object> getConfigProperties() {
519 return configPropertiesView;
520 }
521
522
523
524
525
526
527
528
529
530
531
532 public DefaultRepositorySystemSession setConfigProperties(Map<?, ?> configProperties) {
533 verifyStateForMutation();
534 this.configProperties = copySafe(configProperties, Object.class);
535 configPropertiesView = Collections.unmodifiableMap(this.configProperties);
536 return this;
537 }
538
539
540
541
542
543
544
545
546 public DefaultRepositorySystemSession setConfigProperty(String key, Object value) {
547 verifyStateForMutation();
548 if (value != null) {
549 configProperties.put(key, value);
550 } else {
551 configProperties.remove(key);
552 }
553 return this;
554 }
555
556 @Override
557 public MirrorSelector getMirrorSelector() {
558 return mirrorSelector;
559 }
560
561
562
563
564
565
566
567
568
569 public DefaultRepositorySystemSession setMirrorSelector(MirrorSelector mirrorSelector) {
570 verifyStateForMutation();
571 this.mirrorSelector = mirrorSelector;
572 if (this.mirrorSelector == null) {
573 this.mirrorSelector = NullMirrorSelector.INSTANCE;
574 }
575 return this;
576 }
577
578 @Override
579 public ProxySelector getProxySelector() {
580 return proxySelector;
581 }
582
583
584
585
586
587
588
589
590
591
592 public DefaultRepositorySystemSession setProxySelector(ProxySelector proxySelector) {
593 verifyStateForMutation();
594 this.proxySelector = proxySelector;
595 if (this.proxySelector == null) {
596 this.proxySelector = NullProxySelector.INSTANCE;
597 }
598 return this;
599 }
600
601 @Override
602 public AuthenticationSelector getAuthenticationSelector() {
603 return authenticationSelector;
604 }
605
606
607
608
609
610
611
612
613
614
615 public DefaultRepositorySystemSession setAuthenticationSelector(AuthenticationSelector authenticationSelector) {
616 verifyStateForMutation();
617 this.authenticationSelector = authenticationSelector;
618 if (this.authenticationSelector == null) {
619 this.authenticationSelector = NullAuthenticationSelector.INSTANCE;
620 }
621 return this;
622 }
623
624 @Override
625 public ArtifactTypeRegistry getArtifactTypeRegistry() {
626 return artifactTypeRegistry;
627 }
628
629
630
631
632
633
634
635 public DefaultRepositorySystemSession setArtifactTypeRegistry(ArtifactTypeRegistry artifactTypeRegistry) {
636 verifyStateForMutation();
637 this.artifactTypeRegistry = artifactTypeRegistry;
638 if (this.artifactTypeRegistry == null) {
639 this.artifactTypeRegistry = NullArtifactTypeRegistry.INSTANCE;
640 }
641 return this;
642 }
643
644 @Override
645 public DependencyTraverser getDependencyTraverser() {
646 return dependencyTraverser;
647 }
648
649
650
651
652
653
654
655 public DefaultRepositorySystemSession setDependencyTraverser(DependencyTraverser dependencyTraverser) {
656 verifyStateForMutation();
657 this.dependencyTraverser = dependencyTraverser;
658 return this;
659 }
660
661 @Override
662 public DependencyManager getDependencyManager() {
663 return dependencyManager;
664 }
665
666
667
668
669
670
671
672 public DefaultRepositorySystemSession setDependencyManager(DependencyManager dependencyManager) {
673 verifyStateForMutation();
674 this.dependencyManager = dependencyManager;
675 return this;
676 }
677
678 @Override
679 public DependencySelector getDependencySelector() {
680 return dependencySelector;
681 }
682
683
684
685
686
687
688
689 public DefaultRepositorySystemSession setDependencySelector(DependencySelector dependencySelector) {
690 verifyStateForMutation();
691 this.dependencySelector = dependencySelector;
692 return this;
693 }
694
695 @Override
696 public VersionFilter getVersionFilter() {
697 return versionFilter;
698 }
699
700
701
702
703
704
705
706
707 public DefaultRepositorySystemSession setVersionFilter(VersionFilter versionFilter) {
708 verifyStateForMutation();
709 this.versionFilter = versionFilter;
710 return this;
711 }
712
713 @Override
714 public DependencyGraphTransformer getDependencyGraphTransformer() {
715 return dependencyGraphTransformer;
716 }
717
718
719
720
721
722
723
724
725 public DefaultRepositorySystemSession setDependencyGraphTransformer(
726 DependencyGraphTransformer dependencyGraphTransformer) {
727 verifyStateForMutation();
728 this.dependencyGraphTransformer = dependencyGraphTransformer;
729 return this;
730 }
731
732 @Override
733 public SessionData getData() {
734 return data;
735 }
736
737
738
739
740
741
742
743 public DefaultRepositorySystemSession setData(SessionData data) {
744 verifyStateForMutation();
745 this.data = data;
746 if (this.data == null) {
747 this.data = new DefaultSessionData();
748 }
749 return this;
750 }
751
752 @Override
753 public RepositoryCache getCache() {
754 return cache;
755 }
756
757
758
759
760
761
762
763 public DefaultRepositorySystemSession setCache(RepositoryCache cache) {
764 verifyStateForMutation();
765 this.cache = cache;
766 return this;
767 }
768
769
770
771
772
773
774 public void setReadOnly() {
775 readOnly = true;
776 }
777
778
779
780
781 private void verifyStateForMutation() {
782 if (readOnly) {
783 throw new IllegalStateException("repository system session is read-only");
784 }
785 }
786
787 static class NullProxySelector implements ProxySelector {
788
789 public static final ProxySelector INSTANCE = new NullProxySelector();
790
791 public Proxy getProxy(RemoteRepository repository) {
792 requireNonNull(repository, "repository cannot be null");
793 return repository.getProxy();
794 }
795 }
796
797 static class NullMirrorSelector implements MirrorSelector {
798
799 public static final MirrorSelector INSTANCE = new NullMirrorSelector();
800
801 public RemoteRepository getMirror(RemoteRepository repository) {
802 requireNonNull(repository, "repository cannot be null");
803 return null;
804 }
805 }
806
807 static class NullAuthenticationSelector implements AuthenticationSelector {
808
809 public static final AuthenticationSelector INSTANCE = new NullAuthenticationSelector();
810
811 public Authentication getAuthentication(RemoteRepository repository) {
812 requireNonNull(repository, "repository cannot be null");
813 return repository.getAuthentication();
814 }
815 }
816
817 static final class NullArtifactTypeRegistry implements ArtifactTypeRegistry {
818
819 public static final ArtifactTypeRegistry INSTANCE = new NullArtifactTypeRegistry();
820
821 public ArtifactType get(String typeId) {
822 return null;
823 }
824 }
825 }