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.profiles.activation;
20  
21  import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
22  import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
23  import org.apache.maven.artifact.versioning.VersionRange;
24  import org.apache.maven.model.Activation;
25  import org.apache.maven.model.Profile;
26  
27  /**
28   * JdkPrefixProfileActivator
29   */
30  @Deprecated
31  public class JdkPrefixProfileActivator extends DetectedProfileActivator {
32      private static final String JDK_VERSION = System.getProperty("java.version");
33  
34      public boolean isActive(Profile profile) throws ProfileActivationException {
35          Activation activation = profile.getActivation();
36  
37          String jdk = activation.getJdk();
38  
39          // null case is covered by canDetermineActivation(), so we can do a straight startsWith() here.
40          if (jdk.startsWith("[") || jdk.startsWith("(")) {
41              try {
42                  return matchJdkVersionRange(jdk);
43              } catch (InvalidVersionSpecificationException e) {
44                  throw new ProfileActivationException(
45                          "Invalid JDK version in profile '" + profile.getId() + "': " + e.getMessage());
46              }
47          }
48  
49          boolean reverse = false;
50  
51          if (jdk.startsWith("!")) {
52              reverse = true;
53              jdk = jdk.substring(1);
54          }
55  
56          if (getJdkVersion().startsWith(jdk)) {
57              return !reverse;
58          } else {
59              return reverse;
60          }
61      }
62  
63      private boolean matchJdkVersionRange(String jdk) throws InvalidVersionSpecificationException {
64          VersionRange jdkVersionRange = VersionRange.createFromVersionSpec(convertJdkToMavenVersion(jdk));
65          DefaultArtifactVersion jdkVersion = new DefaultArtifactVersion(convertJdkToMavenVersion(getJdkVersion()));
66          return jdkVersionRange.containsVersion(jdkVersion);
67      }
68  
69      private String convertJdkToMavenVersion(String jdk) {
70          return jdk.replace("_", "-");
71      }
72  
73      protected String getJdkVersion() {
74          return JDK_VERSION;
75      }
76  
77      protected boolean canDetectActivation(Profile profile) {
78          return profile.getActivation() != null
79                  && profile.getActivation().getJdk() != null
80                  && !profile.getActivation().getJdk().isEmpty();
81      }
82  }