001package org.eclipse.aether.internal.impl;
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.Named;
023
024import org.eclipse.aether.RepositorySystemSession;
025import org.eclipse.aether.repository.RemoteRepository;
026import org.eclipse.aether.repository.RepositoryPolicy;
027import org.eclipse.aether.spi.connector.checksum.ChecksumPolicy;
028import org.eclipse.aether.spi.connector.checksum.ChecksumPolicyProvider;
029import org.eclipse.aether.transfer.TransferResource;
030
031/**
032 */
033@Named
034public final class DefaultChecksumPolicyProvider
035    implements ChecksumPolicyProvider
036{
037
038    private static final int ORDINAL_IGNORE = 0;
039
040    private static final int ORDINAL_WARN = 1;
041
042    private static final int ORDINAL_FAIL = 2;
043
044    public DefaultChecksumPolicyProvider()
045    {
046        // enables default constructor
047    }
048
049    public ChecksumPolicy newChecksumPolicy( RepositorySystemSession session, RemoteRepository repository,
050                                             TransferResource resource, String policy )
051    {
052        if ( RepositoryPolicy.CHECKSUM_POLICY_IGNORE.equals( policy ) )
053        {
054            return null;
055        }
056        if ( RepositoryPolicy.CHECKSUM_POLICY_FAIL.equals( policy ) )
057        {
058            return new FailChecksumPolicy( resource );
059        }
060        return new WarnChecksumPolicy( resource );
061    }
062
063    public String getEffectiveChecksumPolicy( RepositorySystemSession session, String policy1, String policy2 )
064    {
065        if ( policy1 != null && policy1.equals( policy2 ) )
066        {
067            return policy1;
068        }
069        int ordinal1 = ordinalOfPolicy( policy1 );
070        int ordinal2 = ordinalOfPolicy( policy2 );
071        if ( ordinal2 < ordinal1 )
072        {
073            return ( ordinal2 != ORDINAL_WARN ) ? policy2 : RepositoryPolicy.CHECKSUM_POLICY_WARN;
074        }
075        else
076        {
077            return ( ordinal1 != ORDINAL_WARN ) ? policy1 : RepositoryPolicy.CHECKSUM_POLICY_WARN;
078        }
079    }
080
081    private static int ordinalOfPolicy( String policy )
082    {
083        if ( RepositoryPolicy.CHECKSUM_POLICY_FAIL.equals( policy ) )
084        {
085            return ORDINAL_FAIL;
086        }
087        else if ( RepositoryPolicy.CHECKSUM_POLICY_IGNORE.equals( policy ) )
088        {
089            return ORDINAL_IGNORE;
090        }
091        else
092        {
093            return ORDINAL_WARN;
094        }
095    }
096
097}