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