View Javadoc
1   package org.eclipse.aether.internal.impl.checksum;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Default implementation.
39   *
40   * @since 1.8.0
41   */
42  @Singleton
43  @Named
44  public class DefaultChecksumAlgorithmFactorySelector
45          implements ChecksumAlgorithmFactorySelector
46  {
47      private final Map<String, ChecksumAlgorithmFactory> factories;
48  
49      /**
50       * Default ctor for SL.
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  }