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  
24  import org.apache.maven.model.Activation;
25  import org.apache.maven.model.ActivationProperty;
26  import org.apache.maven.model.Profile;
27  import org.apache.maven.model.building.ModelProblem.Severity;
28  import org.apache.maven.model.building.ModelProblem.Version;
29  import org.apache.maven.model.building.ModelProblemCollector;
30  import org.apache.maven.model.building.ModelProblemCollectorRequest;
31  import org.apache.maven.model.profile.ProfileActivationContext;
32  
33  /**
34   * Determines profile activation based on the existence or value of some execution property.
35   *
36   * @see ActivationProperty
37   */
38  @Named("property")
39  @Singleton
40  public class PropertyProfileActivator implements ProfileActivator {
41  
42      @Override
43      public boolean isActive(Profile profile, ProfileActivationContext context, ModelProblemCollector problems) {
44          Activation activation = profile.getActivation();
45  
46          if (activation == null) {
47              return false;
48          }
49  
50          ActivationProperty property = activation.getProperty();
51  
52          if (property == null) {
53              return false;
54          }
55  
56          String name = property.getName();
57          boolean reverseName = false;
58  
59          if (name != null && name.startsWith("!")) {
60              reverseName = true;
61              name = name.substring(1);
62          }
63  
64          if (name == null || name.isEmpty()) {
65              problems.add(new ModelProblemCollectorRequest(Severity.ERROR, Version.BASE)
66                      .setMessage("The property name is required to activate the profile " + profile.getId())
67                      .setLocation(property.getLocation("")));
68              return false;
69          }
70  
71          String sysValue = context.getUserProperties().get(name);
72          if (sysValue == null) {
73              sysValue = context.getSystemProperties().get(name);
74          }
75  
76          String propValue = property.getValue();
77          if (propValue != null && !propValue.isEmpty()) {
78              boolean reverseValue = false;
79              if (propValue.startsWith("!")) {
80                  reverseValue = true;
81                  propValue = propValue.substring(1);
82              }
83  
84              // we have a value, so it has to match the system value...
85              return reverseValue != propValue.equals(sysValue);
86          } else {
87              return reverseName != (sysValue != null && !sysValue.isEmpty());
88          }
89      }
90  
91      @Override
92      public boolean presentInConfig(Profile profile, ProfileActivationContext context, ModelProblemCollector problems) {
93          Activation activation = profile.getActivation();
94  
95          if (activation == null) {
96              return false;
97          }
98  
99          ActivationProperty property = activation.getProperty();
100 
101         return property != null;
102     }
103 }