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 @Named("jdk-version")
43 @Singleton
44 public class JdkVersionProfileActivator implements ProfileActivator {
45
46 private static final Pattern FILTER_1 = Pattern.compile("[^\\d._-]");
47 private static final Pattern FILTER_2 = Pattern.compile("[._-]");
48 private static final Pattern FILTER_3 = Pattern.compile("\\.");
49
50 @Override
51 public boolean isActive(Profile profile, ProfileActivationContext context, ModelProblemCollector problems) {
52 Activation activation = profile.getActivation();
53
54 if (activation == null) {
55 return false;
56 }
57
58 String jdk = activation.getJdk();
59
60 if (jdk == null) {
61 return false;
62 }
63
64 String version = context.getSystemProperties().get("java.version");
65
66 if (version == null || version.length() <= 0) {
67 problems.add(new ModelProblemCollectorRequest(Severity.ERROR, Version.BASE)
68 .setMessage("Failed to determine Java version for profile " + profile.getId())
69 .setLocation(activation.getLocation("jdk")));
70 return false;
71 }
72 return isJavaVersionCompatible(jdk, version);
73 }
74
75 public static boolean isJavaVersionCompatible(String requiredJdkRange, String currentJavaVersion) {
76 if (requiredJdkRange.startsWith("!")) {
77 return !currentJavaVersion.startsWith(requiredJdkRange.substring(1));
78 } else if (isRange(requiredJdkRange)) {
79 return isInRange(currentJavaVersion, getRange(requiredJdkRange));
80 } else {
81 return currentJavaVersion.startsWith(requiredJdkRange);
82 }
83 }
84
85 @Override
86 public boolean presentInConfig(Profile profile, ProfileActivationContext context, ModelProblemCollector problems) {
87 Activation activation = profile.getActivation();
88
89 if (activation == null) {
90 return false;
91 }
92
93 String jdk = activation.getJdk();
94
95 return jdk != null;
96 }
97
98 private static boolean isInRange(String value, List<RangeValue> range) {
99 int leftRelation = getRelationOrder(value, range.get(0), true);
100
101 if (leftRelation == 0) {
102 return true;
103 }
104
105 if (leftRelation < 0) {
106 return false;
107 }
108
109 return getRelationOrder(value, range.get(1), false) <= 0;
110 }
111
112 private static int getRelationOrder(String value, RangeValue rangeValue, boolean isLeft) {
113 if (rangeValue.value.length() <= 0) {
114 return isLeft ? 1 : -1;
115 }
116
117 value = FILTER_1.matcher(value).replaceAll("");
118
119 List<String> valueTokens = new ArrayList<>(Arrays.asList(FILTER_2.split(value)));
120 List<String> rangeValueTokens = new ArrayList<>(Arrays.asList(FILTER_3.split(rangeValue.value)));
121
122 addZeroTokens(valueTokens, 3);
123 addZeroTokens(rangeValueTokens, 3);
124
125 for (int i = 0; i < 3; i++) {
126 int x = Integer.parseInt(valueTokens.get(i));
127 int y = Integer.parseInt(rangeValueTokens.get(i));
128 if (x < y) {
129 return -1;
130 } else if (x > y) {
131 return 1;
132 }
133 }
134 if (!rangeValue.closed) {
135 return isLeft ? -1 : 1;
136 }
137 return 0;
138 }
139
140 private static void addZeroTokens(List<String> tokens, int max) {
141 while (tokens.size() < max) {
142 tokens.add("0");
143 }
144 }
145
146 private static boolean isRange(String value) {
147 return value.startsWith("[") || value.startsWith("(");
148 }
149
150 private static List<RangeValue> getRange(String range) {
151 List<RangeValue> ranges = new ArrayList<>();
152
153 for (String token : range.split(",")) {
154 if (token.startsWith("[")) {
155 ranges.add(new RangeValue(token.replace("[", ""), true));
156 } else if (token.startsWith("(")) {
157 ranges.add(new RangeValue(token.replace("(", ""), false));
158 } else if (token.endsWith("]")) {
159 ranges.add(new RangeValue(token.replace("]", ""), true));
160 } else if (token.endsWith(")")) {
161 ranges.add(new RangeValue(token.replace(")", ""), false));
162 } else if (token.length() <= 0) {
163 ranges.add(new RangeValue("", false));
164 }
165 }
166 if (ranges.size() < 2) {
167 ranges.add(new RangeValue("99999999", false));
168 }
169 return ranges;
170 }
171
172 private static class RangeValue {
173 private String value;
174
175 private boolean closed;
176
177 RangeValue(String value, boolean closed) {
178 this.value = value.trim();
179 this.closed = closed;
180 }
181
182 @Override
183 public String toString() {
184 return value;
185 }
186 }
187 }