View Javadoc

1   package org.apache.maven.profiles.activation;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
23  import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
24  import org.apache.maven.artifact.versioning.VersionRange;
25  import org.apache.maven.model.Activation;
26  import org.apache.maven.model.Profile;
27  import org.codehaus.plexus.util.StringUtils;
28  
29  public class JdkPrefixProfileActivator
30      extends DetectedProfileActivator
31  {
32      private static final String JDK_VERSION = System.getProperty( "java.version" );
33  
34      public boolean isActive( Profile profile )
35          throws ProfileActivationException
36      {
37          Activation activation = profile.getActivation();
38  
39          String jdk = activation.getJdk();
40  
41          // null case is covered by canDetermineActivation(), so we can do a straight startsWith() here.
42          if ( jdk.startsWith( "[" ) || jdk.startsWith( "(" ) )
43          {
44              try
45              {
46                  if ( matchJdkVersionRange( jdk ) )
47                  {
48                      return true;
49                  }
50                  else
51                  {
52                      return false;
53                  }
54              }
55              catch ( InvalidVersionSpecificationException e )
56              {
57                  throw new ProfileActivationException( "Invalid JDK version in profile '" + profile.getId() + "': "
58                      + e.getMessage() );
59              }
60          }
61  
62          boolean reverse = false;
63  
64          if ( jdk.startsWith( "!" ) )
65          {
66              reverse = true;
67              jdk = jdk.substring( 1 );
68          }
69  
70          if ( getJdkVersion().startsWith( jdk ) )
71          {
72              return !reverse;
73          }
74          else
75          {
76              return reverse;
77          }
78      }
79  
80      private boolean matchJdkVersionRange( String jdk )
81          throws InvalidVersionSpecificationException
82      {
83          VersionRange jdkVersionRange = VersionRange.createFromVersionSpec( convertJdkToMavenVersion( jdk ) );
84          DefaultArtifactVersion jdkVersion = new DefaultArtifactVersion( convertJdkToMavenVersion( getJdkVersion() ) );
85          return jdkVersionRange.containsVersion( jdkVersion );
86      }
87  
88      private String convertJdkToMavenVersion( String jdk )
89      {
90          return jdk.replaceAll( "_", "-" );
91      }
92  
93      protected String getJdkVersion()
94      {
95          return JDK_VERSION;
96      }
97  
98      protected boolean canDetectActivation( Profile profile )
99      {
100         return profile.getActivation() != null && StringUtils.isNotEmpty( profile.getActivation().getJdk() );
101     }
102 
103 }