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   * @deprecated use {@link org.apache.maven.api.services.ModelBuilder} instead
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("\\."); // used for split now
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.isEmpty()) {
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          try {
75              return isJavaVersionCompatible(jdk, version);
76          } catch (NumberFormatException e) {
77              problems.add(new ModelProblemCollectorRequest(Severity.WARNING, Version.BASE)
78                      .setMessage("Failed to determine JDK activation for profile " + profile.getId()
79                              + " due invalid JDK version: '" + version + "'")
80                      .setLocation(activation.getLocation("jdk")));
81              return false;
82          }
83      }
84  
85      public static boolean isJavaVersionCompatible(String requiredJdkRange, String currentJavaVersion) {
86          if (requiredJdkRange.startsWith("!")) {
87              return !currentJavaVersion.startsWith(requiredJdkRange.substring(1));
88          } else if (isRange(requiredJdkRange)) {
89              return isInRange(currentJavaVersion, getRange(requiredJdkRange));
90          } else {
91              return currentJavaVersion.startsWith(requiredJdkRange);
92          }
93      }
94  
95      @Override
96      public boolean presentInConfig(Profile profile, ProfileActivationContext context, ModelProblemCollector problems) {
97          Activation activation = profile.getActivation();
98  
99          if (activation == null) {
100             return false;
101         }
102 
103         String jdk = activation.getJdk();
104 
105         return jdk != null;
106     }
107 
108     private static boolean isInRange(String value, List<RangeValue> range) {
109         int leftRelation = getRelationOrder(value, range.get(0), true);
110 
111         if (leftRelation == 0) {
112             return true;
113         }
114 
115         if (leftRelation < 0) {
116             return false;
117         }
118 
119         return getRelationOrder(value, range.get(1), false) <= 0;
120     }
121 
122     private static int getRelationOrder(String value, RangeValue rangeValue, boolean isLeft) {
123         if (rangeValue.value.isEmpty()) {
124             return isLeft ? 1 : -1;
125         }
126 
127         value = FILTER_1.matcher(value).replaceAll("");
128 
129         List<String> valueTokens = new ArrayList<>(Arrays.asList(FILTER_2.split(value)));
130         List<String> rangeValueTokens = new ArrayList<>(Arrays.asList(FILTER_3.split(rangeValue.value)));
131 
132         addZeroTokens(valueTokens, 3);
133         addZeroTokens(rangeValueTokens, 3);
134 
135         for (int i = 0; i < 3; i++) {
136             int x = Integer.parseInt(valueTokens.get(i));
137             int y = Integer.parseInt(rangeValueTokens.get(i));
138             if (x < y) {
139                 return -1;
140             } else if (x > y) {
141                 return 1;
142             }
143         }
144         if (!rangeValue.closed) {
145             return isLeft ? -1 : 1;
146         }
147         return 0;
148     }
149 
150     private static void addZeroTokens(List<String> tokens, int max) {
151         while (tokens.size() < max) {
152             tokens.add("0");
153         }
154     }
155 
156     private static boolean isRange(String value) {
157         return value.startsWith("[") || value.startsWith("(");
158     }
159 
160     private static List<RangeValue> getRange(String range) {
161         List<RangeValue> ranges = new ArrayList<>();
162 
163         for (String token : range.split(",")) {
164             if (token.startsWith("[")) {
165                 ranges.add(new RangeValue(token.replace("[", ""), true));
166             } else if (token.startsWith("(")) {
167                 ranges.add(new RangeValue(token.replace("(", ""), false));
168             } else if (token.endsWith("]")) {
169                 ranges.add(new RangeValue(token.replace("]", ""), true));
170             } else if (token.endsWith(")")) {
171                 ranges.add(new RangeValue(token.replace(")", ""), false));
172             } else if (token.isEmpty()) {
173                 ranges.add(new RangeValue("", false));
174             }
175         }
176         if (ranges.size() < 2) {
177             ranges.add(new RangeValue("99999999", false));
178         }
179         return ranges;
180     }
181 
182     private static class RangeValue {
183         private String value;
184 
185         private boolean closed;
186 
187         RangeValue(String value, boolean closed) {
188             this.value = value.trim();
189             this.closed = closed;
190         }
191 
192         @Override
193         public String toString() {
194             return value;
195         }
196     }
197 }