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