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.internal.impl.model.profile;
20  
21  import org.apache.maven.api.di.Named;
22  import org.apache.maven.api.di.Singleton;
23  import org.apache.maven.api.model.Activation;
24  import org.apache.maven.api.model.ActivationProperty;
25  import org.apache.maven.api.model.Profile;
26  import org.apache.maven.api.services.BuilderProblem;
27  import org.apache.maven.api.services.ModelProblem;
28  import org.apache.maven.api.services.ModelProblemCollector;
29  import org.apache.maven.api.services.model.ProfileActivationContext;
30  import org.apache.maven.api.services.model.ProfileActivator;
31  
32  /**
33   * Determines profile activation based on the existence or value of some execution property.
34   *
35   * @see ActivationProperty
36   */
37  @Named("property")
38  @Singleton
39  public class PropertyProfileActivator implements ProfileActivator {
40  
41      @Override
42      public boolean isActive(Profile profile, ProfileActivationContext context, ModelProblemCollector problems) {
43          Activation activation = profile.getActivation();
44  
45          if (activation == null) {
46              return false;
47          }
48  
49          ActivationProperty property = activation.getProperty();
50  
51          if (property == null) {
52              return false;
53          }
54  
55          String name = property.getName();
56          boolean reverseName = false;
57  
58          if (name != null && name.startsWith("!")) {
59              reverseName = true;
60              name = name.substring(1);
61          }
62  
63          if (name == null || name.isEmpty()) {
64              problems.add(
65                      BuilderProblem.Severity.ERROR,
66                      ModelProblem.Version.BASE,
67                      "The property name is required to activate the profile " + profile.getId(),
68                      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 (propValue != null && !propValue.isEmpty()) {
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 != (sysValue != null && !sysValue.isEmpty());
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 }