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