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