View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * Determines profile activation based on the version of the current Java runtime.
39   *
40   * @see Activation#getJdk()
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("\\."); // used for split now
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.isEmpty()) {
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          try {
73              return isJavaVersionCompatible(jdk, version);
74          } catch (NumberFormatException e) {
75              problems.add(new ModelProblemCollectorRequest(Severity.WARNING, Version.BASE)
76                      .setMessage("Failed to determine JDK activation for profile " + profile.getId()
77                              + " due invalid JDK version: '" + version + "'")
78                      .setLocation(activation.getLocation("jdk")));
79              return false;
80          }
81      }
82  
83      public static boolean isJavaVersionCompatible(String requiredJdkRange, String currentJavaVersion) {
84          if (requiredJdkRange.startsWith("!")) {
85              return !currentJavaVersion.startsWith(requiredJdkRange.substring(1));
86          } else if (isRange(requiredJdkRange)) {
87              return isInRange(currentJavaVersion, getRange(requiredJdkRange));
88          } else {
89              return currentJavaVersion.startsWith(requiredJdkRange);
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         return jdk != null;
104     }
105 
106     private static boolean isInRange(String value, List<RangeValue> range) {
107         int leftRelation = getRelationOrder(value, range.get(0), true);
108 
109         if (leftRelation == 0) {
110             return true;
111         }
112 
113         if (leftRelation < 0) {
114             return false;
115         }
116 
117         return getRelationOrder(value, range.get(1), false) <= 0;
118     }
119 
120     private static int getRelationOrder(String value, RangeValue rangeValue, boolean isLeft) {
121         if (rangeValue.value.isEmpty()) {
122             return isLeft ? 1 : -1;
123         }
124 
125         value = FILTER_1.matcher(value).replaceAll("");
126 
127         List<String> valueTokens = new ArrayList<>(Arrays.asList(FILTER_2.split(value)));
128         List<String> rangeValueTokens = new ArrayList<>(Arrays.asList(FILTER_3.split(rangeValue.value)));
129 
130         addZeroTokens(valueTokens, 3);
131         addZeroTokens(rangeValueTokens, 3);
132 
133         for (int i = 0; i < 3; i++) {
134             int x = Integer.parseInt(valueTokens.get(i));
135             int y = Integer.parseInt(rangeValueTokens.get(i));
136             if (x < y) {
137                 return -1;
138             } else if (x > y) {
139                 return 1;
140             }
141         }
142         if (!rangeValue.closed) {
143             return isLeft ? -1 : 1;
144         }
145         return 0;
146     }
147 
148     private static void addZeroTokens(List<String> tokens, int max) {
149         while (tokens.size() < max) {
150             tokens.add("0");
151         }
152     }
153 
154     private static boolean isRange(String value) {
155         return value.startsWith("[") || value.startsWith("(");
156     }
157 
158     private static List<RangeValue> getRange(String range) {
159         List<RangeValue> ranges = new ArrayList<>();
160 
161         for (String token : range.split(",")) {
162             if (token.startsWith("[")) {
163                 ranges.add(new RangeValue(token.replace("[", ""), true));
164             } else if (token.startsWith("(")) {
165                 ranges.add(new RangeValue(token.replace("(", ""), false));
166             } else if (token.endsWith("]")) {
167                 ranges.add(new RangeValue(token.replace("]", ""), true));
168             } else if (token.endsWith(")")) {
169                 ranges.add(new RangeValue(token.replace(")", ""), false));
170             } else if (token.isEmpty()) {
171                 ranges.add(new RangeValue("", false));
172             }
173         }
174         if (ranges.size() < 2) {
175             ranges.add(new RangeValue("99999999", false));
176         }
177         return ranges;
178     }
179 
180     private static class RangeValue {
181         private String value;
182 
183         private boolean closed;
184 
185         RangeValue(String value, boolean closed) {
186             this.value = value.trim();
187             this.closed = closed;
188         }
189 
190         @Override
191         public String toString() {
192             return value;
193         }
194     }
195 }