1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.internal.impl.model.profile;
20
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.List;
24 import java.util.regex.Pattern;
25
26 import org.apache.maven.api.di.Named;
27 import org.apache.maven.api.di.Singleton;
28 import org.apache.maven.api.model.Activation;
29 import org.apache.maven.api.model.Profile;
30 import org.apache.maven.api.services.BuilderProblem;
31 import org.apache.maven.api.services.ModelProblem;
32 import org.apache.maven.api.services.ModelProblemCollector;
33 import org.apache.maven.api.services.model.ProfileActivationContext;
34 import org.apache.maven.api.services.model.ProfileActivator;
35
36
37
38
39
40
41 @Named("jdk-version")
42 @Singleton
43 public class JdkVersionProfileActivator implements ProfileActivator {
44
45 private static final Pattern FILTER_1 = Pattern.compile("[^\\d._-]");
46 private static final Pattern FILTER_2 = Pattern.compile("[._-]");
47 private static final Pattern FILTER_3 = Pattern.compile("\\.");
48
49 @Override
50 public boolean isActive(Profile profile, ProfileActivationContext context, ModelProblemCollector problems) {
51 Activation activation = profile.getActivation();
52
53 if (activation == null) {
54 return false;
55 }
56
57 String jdk = activation.getJdk();
58
59 if (jdk == null) {
60 return false;
61 }
62
63 String version = context.getSystemProperties().get("java.version");
64
65 if (version == null || version.isEmpty()) {
66 problems.add(
67 BuilderProblem.Severity.ERROR,
68 ModelProblem.Version.BASE,
69 "Failed to determine Java version for profile " + profile.getId(),
70 activation.getLocation("jdk"));
71 return false;
72 }
73 try {
74 return isJavaVersionCompatible(jdk, version);
75 } catch (NumberFormatException e) {
76 problems.add(
77 BuilderProblem.Severity.WARNING,
78 ModelProblem.Version.BASE,
79 "Failed to determine JDK activation for profile " + profile.getId() + " due invalid JDK version: '"
80 + version + "'",
81 profile.getLocation("jdk"));
82 return false;
83 }
84 }
85
86 public static boolean isJavaVersionCompatible(String requiredJdkRange, String currentJavaVersion) {
87 if (requiredJdkRange.startsWith("!")) {
88 return !currentJavaVersion.startsWith(requiredJdkRange.substring(1));
89 } else if (isRange(requiredJdkRange)) {
90 return isInRange(currentJavaVersion, getRange(requiredJdkRange));
91 } else {
92 return currentJavaVersion.startsWith(requiredJdkRange);
93 }
94 }
95
96 @Override
97 public boolean presentInConfig(Profile profile, ProfileActivationContext context, ModelProblemCollector problems) {
98 Activation activation = profile.getActivation();
99
100 if (activation == null) {
101 return false;
102 }
103
104 String jdk = activation.getJdk();
105
106 return jdk != null;
107 }
108
109 private static boolean isInRange(String value, List<RangeValue> range) {
110 int leftRelation = getRelationOrder(value, range.get(0), true);
111
112 if (leftRelation == 0) {
113 return true;
114 }
115
116 if (leftRelation < 0) {
117 return false;
118 }
119
120 return getRelationOrder(value, range.get(1), false) <= 0;
121 }
122
123 private static int getRelationOrder(String value, RangeValue rangeValue, boolean isLeft) {
124 if (rangeValue.value.isEmpty()) {
125 return isLeft ? 1 : -1;
126 }
127
128 value = FILTER_1.matcher(value).replaceAll("");
129
130 List<String> valueTokens = new ArrayList<>(Arrays.asList(FILTER_2.split(value)));
131 List<String> rangeValueTokens = new ArrayList<>(Arrays.asList(FILTER_3.split(rangeValue.value)));
132
133 addZeroTokens(valueTokens, 3);
134 addZeroTokens(rangeValueTokens, 3);
135
136 for (int i = 0; i < 3; i++) {
137 int x = Integer.parseInt(valueTokens.get(i));
138 int y = Integer.parseInt(rangeValueTokens.get(i));
139 if (x < y) {
140 return -1;
141 } else if (x > y) {
142 return 1;
143 }
144 }
145 if (!rangeValue.closed) {
146 return isLeft ? -1 : 1;
147 }
148 return 0;
149 }
150
151 private static void addZeroTokens(List<String> tokens, int max) {
152 while (tokens.size() < max) {
153 tokens.add("0");
154 }
155 }
156
157 private static boolean isRange(String value) {
158 return value.startsWith("[") || value.startsWith("(");
159 }
160
161 private static List<RangeValue> getRange(String range) {
162 List<RangeValue> ranges = new ArrayList<>();
163
164 for (String token : range.split(",")) {
165 if (token.startsWith("[")) {
166 ranges.add(new RangeValue(token.replace("[", ""), true));
167 } else if (token.startsWith("(")) {
168 ranges.add(new RangeValue(token.replace("(", ""), false));
169 } else if (token.endsWith("]")) {
170 ranges.add(new RangeValue(token.replace("]", ""), true));
171 } else if (token.endsWith(")")) {
172 ranges.add(new RangeValue(token.replace(")", ""), false));
173 } else if (token.isEmpty()) {
174 ranges.add(new RangeValue("", false));
175 }
176 }
177 if (ranges.size() < 2) {
178 ranges.add(new RangeValue("99999999", false));
179 }
180 return ranges;
181 }
182
183 private static class RangeValue {
184 private String value;
185
186 private boolean closed;
187
188 RangeValue(String value, boolean closed) {
189 this.value = value.trim();
190 this.closed = closed;
191 }
192
193 @Override
194 public String toString() {
195 return value;
196 }
197 }
198 }