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  /**
32   * SystemPropertyProfileActivator
33   */
34  @Deprecated
35  public class SystemPropertyProfileActivator
36      extends DetectedProfileActivator implements Contextualizable
37  {
38      private Properties properties;
39  
40      public void contextualize( Context context )
41          throws ContextException
42      {
43          properties = (Properties) context.get( "SystemProperties" );
44      }
45  
46      protected boolean canDetectActivation( Profile profile )
47      {
48          return profile.getActivation() != null && profile.getActivation().getProperty() != null;
49      }
50  
51      public boolean isActive( Profile profile )
52          throws ProfileActivationException
53      {
54          Activation activation = profile.getActivation();
55  
56          ActivationProperty property = activation.getProperty();
57  
58          if ( property != null )
59          {
60              String name = property.getName();
61              boolean reverseName = false;
62  
63              if ( name == null )
64              {
65                  throw new ProfileActivationException( "The property name is required to activate the profile '"
66                      + profile.getId() + "'" );
67              }
68  
69              if ( name.startsWith( "!" ) )
70              {
71                  reverseName = true;
72                  name = name.substring( 1 );
73              }
74  
75              String sysValue = properties.getProperty( name );
76  
77              String propValue = property.getValue();
78              if ( StringUtils.isNotEmpty( propValue ) )
79              {
80                  boolean reverseValue = false;
81                  if ( propValue.startsWith( "!" ) )
82                  {
83                      reverseValue = true;
84                      propValue = propValue.substring( 1 );
85                  }
86  
87                  // we have a value, so it has to match the system value...
88                  boolean result = propValue.equals( sysValue );
89  
90                  if ( reverseValue )
91                  {
92                      return !result;
93                  }
94                  else
95                  {
96                      return result;
97                  }
98              }
99              else
100             {
101                 boolean result = StringUtils.isNotEmpty( sysValue );
102 
103                 if ( reverseName )
104                 {
105                     return !result;
106                 }
107                 else
108                 {
109                     return result;
110                 }
111             }
112         }
113 
114         return false;
115     }
116 
117 }