1 package org.apache.maven.model.profile.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.model.Activation;
23 import org.apache.maven.model.ActivationProperty;
24 import org.apache.maven.model.Profile;
25 import org.apache.maven.model.building.ModelProblemCollector;
26 import org.apache.maven.model.building.ModelProblem.Severity;
27 import org.apache.maven.model.building.ModelProblem.Version;
28 import org.apache.maven.model.building.ModelProblemCollectorRequest;
29 import org.apache.maven.model.profile.ProfileActivationContext;
30 import org.codehaus.plexus.component.annotations.Component;
31 import org.codehaus.plexus.util.StringUtils;
32
33
34
35
36
37
38 @Component( role = ProfileActivator.class, hint = "property" )
39 public class PropertyProfileActivator
40 implements ProfileActivator
41 {
42
43 public boolean isActive( Profile profile, ProfileActivationContext context, ModelProblemCollector problems )
44 {
45 boolean active = false;
46
47 Activation activation = profile.getActivation();
48
49 if ( activation != null )
50 {
51 ActivationProperty property = activation.getProperty();
52
53 if ( property != null )
54 {
55 String name = property.getName();
56 boolean reverseName = false;
57
58 if ( name != null && name.startsWith( "!" ) )
59 {
60 reverseName = true;
61 name = name.substring( 1 );
62 }
63
64 if ( name == null || name.length() <= 0 )
65 {
66 problems.add( new ModelProblemCollectorRequest( Severity.ERROR, Version.BASE)
67 .setMessage( "The property name is required to activate the profile " + profile.getId())
68 .setLocation( property.getLocation( "" )));
69 return false;
70 }
71
72 String sysValue = context.getUserProperties().get( name );
73 if ( sysValue == null )
74 {
75 sysValue = context.getSystemProperties().get( name );
76 }
77
78 String propValue = property.getValue();
79 if ( StringUtils.isNotEmpty( propValue ) )
80 {
81 boolean reverseValue = false;
82 if ( propValue.startsWith( "!" ) )
83 {
84 reverseValue = true;
85 propValue = propValue.substring( 1 );
86 }
87
88
89 boolean result = propValue.equals( sysValue );
90
91 if ( reverseValue )
92 {
93 active = !result;
94 }
95 else
96 {
97 active = result;
98 }
99 }
100 else
101 {
102 boolean result = StringUtils.isNotEmpty( sysValue );
103
104 if ( reverseName )
105 {
106 active = !result;
107 }
108 else
109 {
110 active = result;
111 }
112 }
113 }
114 }
115
116 return active;
117 }
118
119 }