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.spi.log.LoggerFactory; 033import org.eclipse.aether.spi.log.NullLoggerFactory; 034import org.eclipse.aether.transfer.TransferResource; 035 036/** 037 */ 038@Named 039public final class DefaultChecksumPolicyProvider 040 implements ChecksumPolicyProvider, Service 041{ 042 043 private static final int ORDINAL_IGNORE = 0; 044 045 private static final int ORDINAL_WARN = 1; 046 047 private static final int ORDINAL_FAIL = 2; 048 049 private LoggerFactory loggerFactory = NullLoggerFactory.INSTANCE; 050 051 public DefaultChecksumPolicyProvider() 052 { 053 // enables default constructor 054 } 055 056 @Inject 057 DefaultChecksumPolicyProvider( LoggerFactory loggerFactory ) 058 { 059 setLoggerFactory( loggerFactory ); 060 } 061 062 public void initService( ServiceLocator locator ) 063 { 064 setLoggerFactory( locator.getService( LoggerFactory.class ) ); 065 } 066 067 public DefaultChecksumPolicyProvider setLoggerFactory( LoggerFactory loggerFactory ) 068 { 069 this.loggerFactory = loggerFactory; 070 return this; 071 } 072 073 public ChecksumPolicy newChecksumPolicy( RepositorySystemSession session, RemoteRepository repository, 074 TransferResource resource, String policy ) 075 { 076 if ( RepositoryPolicy.CHECKSUM_POLICY_IGNORE.equals( policy ) ) 077 { 078 return null; 079 } 080 if ( RepositoryPolicy.CHECKSUM_POLICY_FAIL.equals( policy ) ) 081 { 082 return new FailChecksumPolicy( loggerFactory, resource ); 083 } 084 return new WarnChecksumPolicy( loggerFactory, resource ); 085 } 086 087 public String getEffectiveChecksumPolicy( RepositorySystemSession session, String policy1, String policy2 ) 088 { 089 if ( policy1 != null && policy1.equals( policy2 ) ) 090 { 091 return policy1; 092 } 093 int ordinal1 = ordinalOfPolicy( policy1 ); 094 int ordinal2 = ordinalOfPolicy( policy2 ); 095 if ( ordinal2 < ordinal1 ) 096 { 097 return ( ordinal2 != ORDINAL_WARN ) ? policy2 : RepositoryPolicy.CHECKSUM_POLICY_WARN; 098 } 099 else 100 { 101 return ( ordinal1 != ORDINAL_WARN ) ? policy1 : RepositoryPolicy.CHECKSUM_POLICY_WARN; 102 } 103 } 104 105 private static int ordinalOfPolicy( String policy ) 106 { 107 if ( RepositoryPolicy.CHECKSUM_POLICY_FAIL.equals( policy ) ) 108 { 109 return ORDINAL_FAIL; 110 } 111 else if ( RepositoryPolicy.CHECKSUM_POLICY_IGNORE.equals( policy ) ) 112 { 113 return ORDINAL_IGNORE; 114 } 115 else 116 { 117 return ORDINAL_WARN; 118 } 119 } 120 121}