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