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