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