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.profiles.activation;
20  
21  import java.util.Properties;
22  import org.apache.maven.model.Activation;
23  import org.apache.maven.model.ActivationProperty;
24  import org.apache.maven.model.Profile;
25  import org.codehaus.plexus.context.Context;
26  import org.codehaus.plexus.context.ContextException;
27  import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
28  import org.codehaus.plexus.util.StringUtils;
29  
30  /**
31   * SystemPropertyProfileActivator
32   */
33  @Deprecated
34  public class SystemPropertyProfileActivator extends DetectedProfileActivator implements Contextualizable {
35      private Properties properties;
36  
37      public void contextualize(Context context) throws ContextException {
38          properties = (Properties) context.get("SystemProperties");
39      }
40  
41      protected boolean canDetectActivation(Profile profile) {
42          return profile.getActivation() != null && profile.getActivation().getProperty() != null;
43      }
44  
45      public boolean isActive(Profile profile) throws ProfileActivationException {
46          Activation activation = profile.getActivation();
47  
48          ActivationProperty property = activation.getProperty();
49  
50          if (property != null) {
51              String name = property.getName();
52              boolean reverseName = false;
53  
54              if (name == null) {
55                  throw new ProfileActivationException(
56                          "The property name is required to activate the profile '" + profile.getId() + "'");
57              }
58  
59              if (name.startsWith("!")) {
60                  reverseName = true;
61                  name = name.substring(1);
62              }
63  
64              String sysValue = properties.getProperty(name);
65  
66              String propValue = property.getValue();
67              if (StringUtils.isNotEmpty(propValue)) {
68                  boolean reverseValue = false;
69                  if (propValue.startsWith("!")) {
70                      reverseValue = true;
71                      propValue = propValue.substring(1);
72                  }
73  
74                  // we have a value, so it has to match the system value...
75                  boolean result = propValue.equals(sysValue);
76  
77                  if (reverseValue) {
78                      return !result;
79                  } else {
80                      return result;
81                  }
82              } else {
83                  boolean result = StringUtils.isNotEmpty(sysValue);
84  
85                  if (reverseName) {
86                      return !result;
87                  } else {
88                      return result;
89                  }
90              }
91          }
92  
93          return false;
94      }
95  }