View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.model.profile.activation;
20  
21  import javax.inject.Named;
22  import javax.inject.Singleton;
23  import org.apache.maven.model.Activation;
24  import org.apache.maven.model.ActivationProperty;
25  import org.apache.maven.model.Profile;
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.ModelProblemCollector;
29  import org.apache.maven.model.building.ModelProblemCollectorRequest;
30  import org.apache.maven.model.profile.ProfileActivationContext;
31  import org.codehaus.plexus.util.StringUtils;
32  
33  /**
34   * Determines profile activation based on the existence or value of some execution property.
35   *
36   * @author Benjamin Bentmann
37   * @see ActivationProperty
38   */
39  @Named("property")
40  @Singleton
41  public class PropertyProfileActivator implements ProfileActivator {
42  
43      @Override
44      public boolean isActive(Profile profile, ProfileActivationContext context, ModelProblemCollector problems) {
45          Activation activation = profile.getActivation();
46  
47          if (activation == null) {
48              return false;
49          }
50  
51          ActivationProperty property = activation.getProperty();
52  
53          if (property == null) {
54              return false;
55          }
56  
57          String name = property.getName();
58          boolean reverseName = false;
59  
60          if (name != null && name.startsWith("!")) {
61              reverseName = true;
62              name = name.substring(1);
63          }
64  
65          if (name == null || name.length() <= 0) {
66              problems.add(new ModelProblemCollectorRequest(Severity.ERROR, Version.BASE)
67                      .setMessage("The property name is required to activate the profile " + profile.getId())
68                      .setLocation(property.getLocation("")));
69              return false;
70          }
71  
72          String sysValue = context.getUserProperties().get(name);
73          if (sysValue == null) {
74              sysValue = context.getSystemProperties().get(name);
75          }
76  
77          String propValue = property.getValue();
78          if (StringUtils.isNotEmpty(propValue)) {
79              boolean reverseValue = false;
80              if (propValue.startsWith("!")) {
81                  reverseValue = true;
82                  propValue = propValue.substring(1);
83              }
84  
85              // we have a value, so it has to match the system value...
86              return reverseValue != propValue.equals(sysValue);
87          } else {
88              return reverseName != StringUtils.isNotEmpty(sysValue);
89          }
90      }
91  
92      @Override
93      public boolean presentInConfig(Profile profile, ProfileActivationContext context, ModelProblemCollector problems) {
94          Activation activation = profile.getActivation();
95  
96          if (activation == null) {
97              return false;
98          }
99  
100         ActivationProperty property = activation.getProperty();
101 
102         return property != null;
103     }
104 }