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