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