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