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