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