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.Inject;
23  import javax.inject.Named;
24  
25  import org.eclipse.aether.RepositorySystemSession;
26  import org.eclipse.aether.repository.RemoteRepository;
27  import org.eclipse.aether.repository.RepositoryPolicy;
28  import org.eclipse.aether.spi.connector.checksum.ChecksumPolicy;
29  import org.eclipse.aether.spi.connector.checksum.ChecksumPolicyProvider;
30  import org.eclipse.aether.spi.locator.Service;
31  import org.eclipse.aether.spi.locator.ServiceLocator;
32  import org.eclipse.aether.transfer.TransferResource;
33  
34  /**
35   */
36  @Named
37  public final class DefaultChecksumPolicyProvider
38      implements ChecksumPolicyProvider
39  {
40  
41      private static final int ORDINAL_IGNORE = 0;
42  
43      private static final int ORDINAL_WARN = 1;
44  
45      private static final int ORDINAL_FAIL = 2;
46  
47      public DefaultChecksumPolicyProvider()
48      {
49          // enables default constructor
50      }
51  
52      public ChecksumPolicy newChecksumPolicy( RepositorySystemSession session, RemoteRepository repository,
53                                               TransferResource resource, String policy )
54      {
55          if ( RepositoryPolicy.CHECKSUM_POLICY_IGNORE.equals( policy ) )
56          {
57              return null;
58          }
59          if ( RepositoryPolicy.CHECKSUM_POLICY_FAIL.equals( policy ) )
60          {
61              return new FailChecksumPolicy( resource );
62          }
63          return new WarnChecksumPolicy( resource );
64      }
65  
66      public String getEffectiveChecksumPolicy( RepositorySystemSession session, String policy1, String policy2 )
67      {
68          if ( policy1 != null && policy1.equals( policy2 ) )
69          {
70              return policy1;
71          }
72          int ordinal1 = ordinalOfPolicy( policy1 );
73          int ordinal2 = ordinalOfPolicy( policy2 );
74          if ( ordinal2 < ordinal1 )
75          {
76              return ( ordinal2 != ORDINAL_WARN ) ? policy2 : RepositoryPolicy.CHECKSUM_POLICY_WARN;
77          }
78          else
79          {
80              return ( ordinal1 != ORDINAL_WARN ) ? policy1 : RepositoryPolicy.CHECKSUM_POLICY_WARN;
81          }
82      }
83  
84      private static int ordinalOfPolicy( String policy )
85      {
86          if ( RepositoryPolicy.CHECKSUM_POLICY_FAIL.equals( policy ) )
87          {
88              return ORDINAL_FAIL;
89          }
90          else if ( RepositoryPolicy.CHECKSUM_POLICY_IGNORE.equals( policy ) )
91          {
92              return ORDINAL_IGNORE;
93          }
94          else
95          {
96              return ORDINAL_WARN;
97          }
98      }
99  
100 }