1 package org.eclipse.aether.internal.impl;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import javax.inject.Named;
23 import javax.inject.Singleton;
24
25 import java.util.Objects;
26
27 import org.eclipse.aether.RepositorySystemSession;
28 import org.eclipse.aether.repository.RemoteRepository;
29 import org.eclipse.aether.repository.RepositoryPolicy;
30 import org.eclipse.aether.spi.connector.checksum.ChecksumPolicy;
31 import org.eclipse.aether.spi.connector.checksum.ChecksumPolicyProvider;
32 import org.eclipse.aether.transfer.TransferResource;
33
34
35
36 @Singleton
37 @Named
38 public final class DefaultChecksumPolicyProvider
39 implements ChecksumPolicyProvider
40 {
41
42 private static final int ORDINAL_IGNORE = 0;
43
44 private static final int ORDINAL_WARN = 1;
45
46 private static final int ORDINAL_FAIL = 2;
47
48 public DefaultChecksumPolicyProvider()
49 {
50
51 }
52
53 public ChecksumPolicy newChecksumPolicy( RepositorySystemSession session, RemoteRepository repository,
54 TransferResource resource, String policy )
55 {
56 Objects.requireNonNull( session, "session cannot be null" );
57 Objects.requireNonNull( repository, "repository cannot be null" );
58 Objects.requireNonNull( resource, "resource cannot be null" );
59 validatePolicy( "policy", policy );
60
61 switch ( policy )
62 {
63 case RepositoryPolicy.CHECKSUM_POLICY_IGNORE:
64 return null;
65 case RepositoryPolicy.CHECKSUM_POLICY_FAIL:
66 return new FailChecksumPolicy( resource );
67 case RepositoryPolicy.CHECKSUM_POLICY_WARN:
68 return new WarnChecksumPolicy( resource );
69 default:
70 throw new IllegalArgumentException( "Unsupported policy: " + policy );
71 }
72 }
73
74 public String getEffectiveChecksumPolicy( RepositorySystemSession session, String policy1, String policy2 )
75 {
76 Objects.requireNonNull( session, "session cannot be null" );
77 validatePolicy( "policy1", policy1 );
78 validatePolicy( "policy2", policy2 );
79
80 if ( policy1.equals( policy2 ) )
81 {
82 return policy1;
83 }
84 int ordinal1 = ordinalOfPolicy( policy1 );
85 int ordinal2 = ordinalOfPolicy( policy2 );
86 if ( ordinal2 < ordinal1 )
87 {
88 return ( ordinal2 != ORDINAL_WARN ) ? policy2 : RepositoryPolicy.CHECKSUM_POLICY_WARN;
89 }
90 else
91 {
92 return ( ordinal1 != ORDINAL_WARN ) ? policy1 : RepositoryPolicy.CHECKSUM_POLICY_WARN;
93 }
94 }
95
96 private static int ordinalOfPolicy( String policy )
97 {
98 switch ( policy )
99 {
100 case RepositoryPolicy.CHECKSUM_POLICY_IGNORE:
101 return ORDINAL_IGNORE;
102 case RepositoryPolicy.CHECKSUM_POLICY_FAIL:
103 return ORDINAL_FAIL;
104 case RepositoryPolicy.CHECKSUM_POLICY_WARN:
105 return ORDINAL_WARN;
106 default:
107 throw new IllegalArgumentException( "Unsupported policy: " + policy );
108 }
109 }
110
111 private static void validatePolicy( String paramName, String policy )
112 {
113 Objects.requireNonNull( policy, paramName + "cannot be null" );
114
115 switch ( policy )
116 {
117 case RepositoryPolicy.CHECKSUM_POLICY_IGNORE:
118 case RepositoryPolicy.CHECKSUM_POLICY_FAIL:
119 case RepositoryPolicy.CHECKSUM_POLICY_WARN:
120 break;
121 default:
122 throw new IllegalArgumentException( "Unsupported policy: " + policy );
123 }
124 }
125
126 }