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.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
045        implements ChecksumAlgorithmFactorySelector
046{
047    private final Map<String, ChecksumAlgorithmFactory> factories;
048
049    /**
050     * Default ctor for SL.
051     */
052    @Deprecated
053    public DefaultChecksumAlgorithmFactorySelector()
054    {
055        this.factories = new HashMap<>();
056        this.factories.put( Sha512ChecksumAlgorithmFactory.NAME, new Sha512ChecksumAlgorithmFactory() );
057        this.factories.put( Sha256ChecksumAlgorithmFactory.NAME, new Sha256ChecksumAlgorithmFactory() );
058        this.factories.put( Sha1ChecksumAlgorithmFactory.NAME, new Sha1ChecksumAlgorithmFactory() );
059        this.factories.put( Md5ChecksumAlgorithmFactory.NAME, new Md5ChecksumAlgorithmFactory() );
060    }
061
062    @Inject
063    public DefaultChecksumAlgorithmFactorySelector( Map<String, ChecksumAlgorithmFactory> factories )
064    {
065        this.factories = requireNonNull( factories );
066    }
067
068    @Override
069    public ChecksumAlgorithmFactory select( String algorithmName )
070    {
071        requireNonNull( algorithmName, "algorithmMame must not be null" );
072        ChecksumAlgorithmFactory factory = factories.get( algorithmName );
073        if ( factory == null )
074        {
075            throw new IllegalArgumentException(
076                    String.format( "Unsupported checksum algorithm %s, supported ones are %s",
077                            algorithmName,
078                            getChecksumAlgorithmFactories().stream()
079                                    .map( ChecksumAlgorithmFactory::getName )
080                                    .collect( toList() )
081                    )
082            );
083        }
084        return factory;
085    }
086
087    @Override
088    public List<ChecksumAlgorithmFactory> getChecksumAlgorithmFactories()
089    {
090        return new ArrayList<>( factories.values() );
091    }
092}