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