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