1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.aether.internal.impl.checksum;
20
21 import javax.inject.Inject;
22 import javax.inject.Named;
23 import javax.inject.Singleton;
24
25 import java.util.Collection;
26 import java.util.Collections;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Map;
30
31 import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithmFactory;
32 import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithmFactorySelector;
33
34 import static java.util.Objects.requireNonNull;
35 import static java.util.stream.Collectors.toList;
36
37
38
39
40
41
42 @Singleton
43 @Named
44 public class DefaultChecksumAlgorithmFactorySelector implements ChecksumAlgorithmFactorySelector {
45 private final Map<String, ChecksumAlgorithmFactory> factories;
46
47
48
49
50 @Deprecated
51 public DefaultChecksumAlgorithmFactorySelector() {
52 this.factories = new HashMap<>();
53 this.factories.put(Sha512ChecksumAlgorithmFactory.NAME, new Sha512ChecksumAlgorithmFactory());
54 this.factories.put(Sha256ChecksumAlgorithmFactory.NAME, new Sha256ChecksumAlgorithmFactory());
55 this.factories.put(Sha1ChecksumAlgorithmFactory.NAME, new Sha1ChecksumAlgorithmFactory());
56 this.factories.put(Md5ChecksumAlgorithmFactory.NAME, new Md5ChecksumAlgorithmFactory());
57 }
58
59 @Inject
60 public DefaultChecksumAlgorithmFactorySelector(Map<String, ChecksumAlgorithmFactory> factories) {
61 this.factories = requireNonNull(factories);
62 }
63
64 @Override
65 public ChecksumAlgorithmFactory select(String algorithmName) {
66 requireNonNull(algorithmName, "algorithmMame must not be null");
67 ChecksumAlgorithmFactory factory = factories.get(algorithmName);
68 if (factory == null) {
69 throw new IllegalArgumentException(String.format(
70 "Unsupported checksum algorithm %s, supported ones are %s",
71 algorithmName,
72 getChecksumAlgorithmFactories().stream()
73 .map(ChecksumAlgorithmFactory::getName)
74 .collect(toList())));
75 }
76 return factory;
77 }
78
79 @Override
80 public List<ChecksumAlgorithmFactory> selectList(Collection<String> algorithmNames) {
81 return algorithmNames.stream().map(this::select).collect(toList());
82 }
83
84 @Override
85 public Collection<ChecksumAlgorithmFactory> getChecksumAlgorithmFactories() {
86 return Collections.unmodifiableCollection(factories.values());
87 }
88
89 @Override
90 public boolean isChecksumExtension(String extension) {
91 requireNonNull(extension);
92 if (extension.contains(".")) {
93 return factories.values().stream().anyMatch(a -> extension.endsWith("." + a.getFileExtension()));
94 } else {
95 return factories.values().stream().anyMatch(a -> extension.equals(a.getFileExtension()));
96 }
97 }
98 }