1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 public DefaultChecksumPolicyProvider() {
46
47 }
48
49 public ChecksumPolicy newChecksumPolicy(
50 RepositorySystemSession session, RemoteRepository repository, TransferResource resource, String policy) {
51 requireNonNull(session, "session cannot be null");
52 requireNonNull(repository, "repository cannot be null");
53 requireNonNull(resource, "resource cannot be null");
54 validatePolicy("policy", policy);
55
56 switch (policy) {
57 case RepositoryPolicy.CHECKSUM_POLICY_IGNORE:
58 return null;
59 case RepositoryPolicy.CHECKSUM_POLICY_FAIL:
60 return new FailChecksumPolicy(resource);
61 case RepositoryPolicy.CHECKSUM_POLICY_WARN:
62 return new WarnChecksumPolicy(resource);
63 default:
64 throw new IllegalArgumentException("Unsupported policy: " + policy);
65 }
66 }
67
68 public String getEffectiveChecksumPolicy(RepositorySystemSession session, String policy1, String policy2) {
69 requireNonNull(session, "session cannot be null");
70 validatePolicy("policy1", policy1);
71 validatePolicy("policy2", policy2);
72
73 if (policy1.equals(policy2)) {
74 return policy1;
75 }
76 int ordinal1 = ordinalOfPolicy(policy1);
77 int ordinal2 = ordinalOfPolicy(policy2);
78 if (ordinal2 < ordinal1) {
79 return (ordinal2 != ORDINAL_WARN) ? policy2 : RepositoryPolicy.CHECKSUM_POLICY_WARN;
80 } else {
81 return (ordinal1 != ORDINAL_WARN) ? policy1 : RepositoryPolicy.CHECKSUM_POLICY_WARN;
82 }
83 }
84
85 private static int ordinalOfPolicy(String policy) {
86 switch (policy) {
87 case RepositoryPolicy.CHECKSUM_POLICY_IGNORE:
88 return ORDINAL_IGNORE;
89 case RepositoryPolicy.CHECKSUM_POLICY_FAIL:
90 return ORDINAL_FAIL;
91 case RepositoryPolicy.CHECKSUM_POLICY_WARN:
92 return ORDINAL_WARN;
93 default:
94 throw new IllegalArgumentException("Unsupported policy: " + policy);
95 }
96 }
97
98 private static void validatePolicy(String paramName, String policy) {
99 requireNonNull(policy, paramName + "cannot be null");
100
101 switch (policy) {
102 case RepositoryPolicy.CHECKSUM_POLICY_IGNORE:
103 case RepositoryPolicy.CHECKSUM_POLICY_FAIL:
104 case RepositoryPolicy.CHECKSUM_POLICY_WARN:
105 break;
106 default:
107 throw new IllegalArgumentException("Unsupported policy: " + policy);
108 }
109 }
110 }