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