001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.eclipse.aether.internal.impl.checksum; 020 021import javax.inject.Inject; 022import javax.inject.Named; 023import javax.inject.Singleton; 024 025import java.util.Collection; 026import java.util.Collections; 027import java.util.HashMap; 028import java.util.List; 029import java.util.Map; 030 031import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithmFactory; 032import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithmFactorySelector; 033 034import static java.util.Objects.requireNonNull; 035import static java.util.stream.Collectors.toList; 036 037/** 038 * Default implementation. 039 * 040 * @since 1.8.0 041 */ 042@Singleton 043@Named 044public class DefaultChecksumAlgorithmFactorySelector implements ChecksumAlgorithmFactorySelector { 045 private final Map<String, ChecksumAlgorithmFactory> factories; 046 047 /** 048 * Default ctor for SL. 049 */ 050 @Deprecated 051 public DefaultChecksumAlgorithmFactorySelector() { 052 this.factories = new HashMap<>(); 053 this.factories.put(Sha512ChecksumAlgorithmFactory.NAME, new Sha512ChecksumAlgorithmFactory()); 054 this.factories.put(Sha256ChecksumAlgorithmFactory.NAME, new Sha256ChecksumAlgorithmFactory()); 055 this.factories.put(Sha1ChecksumAlgorithmFactory.NAME, new Sha1ChecksumAlgorithmFactory()); 056 this.factories.put(Md5ChecksumAlgorithmFactory.NAME, new Md5ChecksumAlgorithmFactory()); 057 } 058 059 @Inject 060 public DefaultChecksumAlgorithmFactorySelector(Map<String, ChecksumAlgorithmFactory> factories) { 061 this.factories = requireNonNull(factories); 062 } 063 064 @Override 065 public ChecksumAlgorithmFactory select(String algorithmName) { 066 requireNonNull(algorithmName, "algorithmMame must not be null"); 067 ChecksumAlgorithmFactory factory = factories.get(algorithmName); 068 if (factory == null) { 069 throw new IllegalArgumentException(String.format( 070 "Unsupported checksum algorithm %s, supported ones are %s", 071 algorithmName, 072 getChecksumAlgorithmFactories().stream() 073 .map(ChecksumAlgorithmFactory::getName) 074 .collect(toList()))); 075 } 076 return factory; 077 } 078 079 @Override 080 public List<ChecksumAlgorithmFactory> selectList(Collection<String> algorithmNames) { 081 return algorithmNames.stream().map(this::select).collect(toList()); 082 } 083 084 @Override 085 public Collection<ChecksumAlgorithmFactory> getChecksumAlgorithmFactories() { 086 return Collections.unmodifiableCollection(factories.values()); 087 } 088 089 @Override 090 public boolean isChecksumExtension(String extension) { 091 requireNonNull(extension); 092 if (extension.contains(".")) { 093 return factories.values().stream().anyMatch(a -> extension.endsWith("." + a.getFileExtension())); 094 } else { 095 return factories.values().stream().anyMatch(a -> extension.equals(a.getFileExtension())); 096 } 097 } 098}