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 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
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 }