View Javadoc
1   package org.eclipse.aether.internal.impl;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   * 
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import javax.inject.Named;
23  
24  import org.eclipse.aether.RepositorySystemSession;
25  import org.eclipse.aether.repository.RemoteRepository;
26  import org.eclipse.aether.repository.RepositoryPolicy;
27  import org.eclipse.aether.spi.connector.checksum.ChecksumPolicy;
28  import org.eclipse.aether.spi.connector.checksum.ChecksumPolicyProvider;
29  import org.eclipse.aether.transfer.TransferResource;
30  
31  /**
32   */
33  @Named
34  public final class DefaultChecksumPolicyProvider
35      implements ChecksumPolicyProvider
36  {
37  
38      private static final int ORDINAL_IGNORE = 0;
39  
40      private static final int ORDINAL_WARN = 1;
41  
42      private static final int ORDINAL_FAIL = 2;
43  
44      public DefaultChecksumPolicyProvider()
45      {
46          // enables default constructor
47      }
48  
49      public ChecksumPolicy newChecksumPolicy( RepositorySystemSession session, RemoteRepository repository,
50                                               TransferResource resource, String policy )
51      {
52          if ( RepositoryPolicy.CHECKSUM_POLICY_IGNORE.equals( policy ) )
53          {
54              return null;
55          }
56          if ( RepositoryPolicy.CHECKSUM_POLICY_FAIL.equals( policy ) )
57          {
58              return new FailChecksumPolicy( resource );
59          }
60          return new WarnChecksumPolicy( resource );
61      }
62  
63      public String getEffectiveChecksumPolicy( RepositorySystemSession session, String policy1, String policy2 )
64      {
65          if ( policy1 != null && policy1.equals( policy2 ) )
66          {
67              return policy1;
68          }
69          int ordinal1 = ordinalOfPolicy( policy1 );
70          int ordinal2 = ordinalOfPolicy( policy2 );
71          if ( ordinal2 < ordinal1 )
72          {
73              return ( ordinal2 != ORDINAL_WARN ) ? policy2 : RepositoryPolicy.CHECKSUM_POLICY_WARN;
74          }
75          else
76          {
77              return ( ordinal1 != ORDINAL_WARN ) ? policy1 : RepositoryPolicy.CHECKSUM_POLICY_WARN;
78          }
79      }
80  
81      private static int ordinalOfPolicy( String policy )
82      {
83          if ( RepositoryPolicy.CHECKSUM_POLICY_FAIL.equals( policy ) )
84          {
85              return ORDINAL_FAIL;
86          }
87          else if ( RepositoryPolicy.CHECKSUM_POLICY_IGNORE.equals( policy ) )
88          {
89              return ORDINAL_IGNORE;
90          }
91          else
92          {
93              return ORDINAL_WARN;
94          }
95      }
96  
97  }