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