1 package org.eclipse.aether.internal.impl.checksum;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import javax.inject.Inject;
23 import javax.inject.Named;
24 import javax.inject.Singleton;
25
26 import java.util.ArrayList;
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
45 implements ChecksumAlgorithmFactorySelector
46 {
47 private final Map<String, ChecksumAlgorithmFactory> factories;
48
49
50
51
52 @Deprecated
53 public DefaultChecksumAlgorithmFactorySelector()
54 {
55 this.factories = new HashMap<>();
56 this.factories.put( Sha512ChecksumAlgorithmFactory.NAME, new Sha512ChecksumAlgorithmFactory() );
57 this.factories.put( Sha256ChecksumAlgorithmFactory.NAME, new Sha256ChecksumAlgorithmFactory() );
58 this.factories.put( Sha1ChecksumAlgorithmFactory.NAME, new Sha1ChecksumAlgorithmFactory() );
59 this.factories.put( Md5ChecksumAlgorithmFactory.NAME, new Md5ChecksumAlgorithmFactory() );
60 }
61
62 @Inject
63 public DefaultChecksumAlgorithmFactorySelector( Map<String, ChecksumAlgorithmFactory> factories )
64 {
65 this.factories = requireNonNull( factories );
66 }
67
68 @Override
69 public ChecksumAlgorithmFactory select( String algorithmName )
70 {
71 requireNonNull( algorithmName, "algorithmMame must not be null" );
72 ChecksumAlgorithmFactory factory = factories.get( algorithmName );
73 if ( factory == null )
74 {
75 throw new IllegalArgumentException(
76 String.format( "Unsupported checksum algorithm %s, supported ones are %s",
77 algorithmName,
78 getChecksumAlgorithmFactories().stream()
79 .map( ChecksumAlgorithmFactory::getName )
80 .collect( toList() )
81 )
82 );
83 }
84 return factory;
85 }
86
87 @Override
88 public List<ChecksumAlgorithmFactory> getChecksumAlgorithmFactories()
89 {
90 return new ArrayList<>( factories.values() );
91 }
92 }