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