1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.internal.aether;
20
21 import javax.inject.Named;
22 import javax.inject.Singleton;
23
24 import java.util.Locale;
25 import java.util.StringJoiner;
26
27 import org.eclipse.aether.RepositorySystemSession;
28 import org.eclipse.aether.spi.validator.Validator;
29 import org.eclipse.aether.spi.validator.ValidatorFactory;
30 import org.eclipse.aether.util.ConfigUtils;
31
32 @Named
33 @Singleton
34 public class MavenValidatorFactory implements ValidatorFactory {
35
36
37
38
39
40
41
42
43
44 public static final String MAVEN_RESOLVER_VALIDATION = "maven.resolver.validation";
45
46 public enum ValidationLevel {
47 DEFAULT,
48 MILD,
49 OFF;
50 }
51
52 private final MavenValidator defaultValidator = new MavenValidator(true);
53 private final MavenValidator mildValidator = new MavenValidator(false);
54
55 @Override
56 public Validator newInstance(RepositorySystemSession session) {
57 switch (getValidationLevel(session)) {
58 case DEFAULT:
59 return defaultValidator;
60 case MILD:
61 return mildValidator;
62 case OFF:
63 return NOOP;
64 default:
65 throw new IllegalArgumentException("Unknown validation level");
66 }
67 }
68
69 private static ValidationLevel getValidationLevel(RepositorySystemSession session) {
70 String value = ConfigUtils.getString(session, ValidationLevel.DEFAULT.name(), MAVEN_RESOLVER_VALIDATION);
71 try {
72 return ValidationLevel.valueOf(value.trim().toUpperCase(Locale.ROOT));
73 } catch (IllegalArgumentException e) {
74 StringJoiner supported = new StringJoiner(", ");
75 for (ValidationLevel level : ValidationLevel.values()) {
76 supported.add(level.name().toLowerCase(Locale.ROOT));
77 }
78 throw new IllegalArgumentException(
79 "Invalid value '" + value + "' for " + MAVEN_RESOLVER_VALIDATION + ", supported values are: "
80 + supported,
81 e);
82 }
83 }
84 }