1   package org.apache.maven.profiles.activation;
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
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          
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  }