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.Collection;
28  import java.util.HashMap;
29  import java.util.List;
30  import java.util.Map;
31  
32  import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithmFactory;
33  import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithmFactorySelector;
34  
35  import static java.util.Objects.requireNonNull;
36  import static java.util.stream.Collectors.toList;
37  
38  /**
39   * Default implementation.
40   *
41   * @since 1.8.0
42   */
43  @Singleton
44  @Named
45  public class DefaultChecksumAlgorithmFactorySelector
46          implements ChecksumAlgorithmFactorySelector
47  {
48      private final Map<String, ChecksumAlgorithmFactory> factories;
49  
50      /**
51       * Default ctor for SL.
52       */
53      @Deprecated
54      public DefaultChecksumAlgorithmFactorySelector()
55      {
56          this.factories = new HashMap<>();
57          this.factories.put( Sha512ChecksumAlgorithmFactory.NAME, new Sha512ChecksumAlgorithmFactory() );
58          this.factories.put( Sha256ChecksumAlgorithmFactory.NAME, new Sha256ChecksumAlgorithmFactory() );
59          this.factories.put( Sha1ChecksumAlgorithmFactory.NAME, new Sha1ChecksumAlgorithmFactory() );
60          this.factories.put( Md5ChecksumAlgorithmFactory.NAME, new Md5ChecksumAlgorithmFactory() );
61      }
62  
63      @Inject
64      public DefaultChecksumAlgorithmFactorySelector( Map<String, ChecksumAlgorithmFactory> factories )
65      {
66          this.factories = requireNonNull( factories );
67      }
68  
69      @Override
70      public ChecksumAlgorithmFactory select( String algorithmName )
71      {
72          requireNonNull( algorithmName, "algorithmMame must not be null" );
73          ChecksumAlgorithmFactory factory = factories.get( algorithmName );
74          if ( factory == null )
75          {
76              throw new IllegalArgumentException(
77                      String.format( "Unsupported checksum algorithm %s, supported ones are %s",
78                              algorithmName,
79                              getChecksumAlgorithmFactories().stream()
80                                      .map( ChecksumAlgorithmFactory::getName )
81                                      .collect( toList() )
82                      )
83              );
84          }
85          return factory;
86      }
87  
88      @Override
89      public List<ChecksumAlgorithmFactory> selectList( Collection<String> algorithmNames )
90      {
91          return algorithmNames.stream()
92                  .map( this::select )
93                  .collect( toList() );
94      }
95  
96      @Override
97      public List<ChecksumAlgorithmFactory> getChecksumAlgorithmFactories()
98      {
99          return new ArrayList<>( factories.values() );
100     }
101 }