View Javadoc
1   package org.apache.maven.profiles.activation;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.Properties;
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  @Deprecated
32  public class SystemPropertyProfileActivator
33      extends DetectedProfileActivator implements Contextualizable
34  {
35      private Properties properties;
36  
37      public void contextualize( Context context )
38          throws ContextException
39      {
40          properties = (Properties) context.get( "SystemProperties" );
41      }
42  
43      protected boolean canDetectActivation( Profile profile )
44      {
45          return profile.getActivation() != null && profile.getActivation().getProperty() != null;
46      }
47  
48      public boolean isActive( Profile profile )
49          throws ProfileActivationException
50      {
51          Activation activation = profile.getActivation();
52  
53          ActivationProperty property = activation.getProperty();
54  
55          if ( property != null )
56          {
57              String name = property.getName();
58              boolean reverseName = false;
59  
60              if ( name == null )
61              {
62                  throw new ProfileActivationException( "The property name is required to activate the profile '"
63                      + profile.getId() + "'" );
64              }
65  
66              if ( name.startsWith( "!" ) )
67              {
68                  reverseName = true;
69                  name = name.substring( 1 );
70              }
71  
72              String sysValue = properties.getProperty( name );
73  
74              String propValue = property.getValue();
75              if ( StringUtils.isNotEmpty( propValue ) )
76              {
77                  boolean reverseValue = false;
78                  if ( propValue.startsWith( "!" ) )
79                  {
80                      reverseValue = true;
81                      propValue = propValue.substring( 1 );
82                  }
83  
84                  // we have a value, so it has to match the system value...
85                  boolean result = propValue.equals( sysValue );
86  
87                  if ( reverseValue )
88                  {
89                      return !result;
90                  }
91                  else
92                  {
93                      return result;
94                  }
95              }
96              else
97              {
98                  boolean result = StringUtils.isNotEmpty( sysValue );
99  
100                 if ( reverseName )
101                 {
102                     return !result;
103                 }
104                 else
105                 {
106                     return result;
107                 }
108             }
109         }
110 
111         return false;
112     }
113 
114 }