1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.model.profile.activation;
20
21 import javax.inject.Named;
22 import javax.inject.Singleton;
23
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.List;
27 import java.util.regex.Pattern;
28
29 import org.apache.maven.model.Activation;
30 import org.apache.maven.model.Profile;
31 import org.apache.maven.model.building.ModelProblem.Severity;
32 import org.apache.maven.model.building.ModelProblem.Version;
33 import org.apache.maven.model.building.ModelProblemCollector;
34 import org.apache.maven.model.building.ModelProblemCollectorRequest;
35 import org.apache.maven.model.profile.ProfileActivationContext;
36
37
38
39
40
41
42
43 @Named("jdk-version")
44 @Singleton
45 @Deprecated(since = "4.0.0")
46 public class JdkVersionProfileActivator implements ProfileActivator {
47
48 private static final Pattern FILTER_1 = Pattern.compile("[^\\d._-]");
49 private static final Pattern FILTER_2 = Pattern.compile("[._-]");
50 private static final Pattern FILTER_3 = Pattern.compile("\\.");
51
52 @Override
53 public boolean isActive(Profile profile, ProfileActivationContext context, ModelProblemCollector problems) {
54 Activation activation = profile.getActivation();
55
56 if (activation == null) {
57 return false;
58 }
59
60 String jdk = activation.getJdk();
61
62 if (jdk == null) {
63 return false;
64 }
65
66 String version = context.getSystemProperties().get("java.version");
67
68 if (version == null || version.length() <= 0) {
69 problems.add(new ModelProblemCollectorRequest(Severity.ERROR, Version.BASE)
70 .setMessage("Failed to determine Java version for profile " + profile.getId())
71 .setLocation(activation.getLocation("jdk")));
72 return false;
73 }
74
75 if (jdk.startsWith("!")) {
76 return !version.startsWith(jdk.substring(1));
77 } else if (isRange(jdk)) {
78 try {
79 return isInRange(version, getRange(jdk));
80 } catch (NumberFormatException e) {
81 problems.add(new ModelProblemCollectorRequest(Severity.WARNING, Version.BASE)
82 .setMessage("Failed to determine JDK activation for profile " + profile.getId()
83 + " due invalid JDK version: '" + version + "'")
84 .setLocation(profile.getLocation(""))
85 .setException(e));
86 return false;
87 }
88 } else {
89 return version.startsWith(jdk);
90 }
91 }
92
93 @Override
94 public boolean presentInConfig(Profile profile, ProfileActivationContext context, ModelProblemCollector problems) {
95 Activation activation = profile.getActivation();
96
97 if (activation == null) {
98 return false;
99 }
100
101 String jdk = activation.getJdk();
102
103 if (jdk == null) {
104 return false;
105 }
106 return true;
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.length() <= 0) {
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.length() <= 0) {
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 }