View Javadoc
1   package org.apache.maven.model.profile.activation;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Determines profile activation based on the existence or value of some execution property.
37   *
38   * @author Benjamin Bentmann
39   * @see ActivationProperty
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              // we have a value, so it has to match the system value...
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 }