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