001package org.apache.maven.profiles.activation;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.util.Properties;
023import org.apache.maven.model.Activation;
024import org.apache.maven.model.ActivationProperty;
025import org.apache.maven.model.Profile;
026import org.codehaus.plexus.context.Context;
027import org.codehaus.plexus.context.ContextException;
028import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
029import org.codehaus.plexus.util.StringUtils;
030
031@Deprecated
032public class SystemPropertyProfileActivator
033    extends DetectedProfileActivator implements Contextualizable
034{
035    private Properties properties;
036
037    public void contextualize( Context context )
038        throws ContextException
039    {
040        properties = (Properties) context.get( "SystemProperties" );
041    }
042
043    protected boolean canDetectActivation( Profile profile )
044    {
045        return profile.getActivation() != null && profile.getActivation().getProperty() != null;
046    }
047
048    public boolean isActive( Profile profile )
049        throws ProfileActivationException
050    {
051        Activation activation = profile.getActivation();
052
053        ActivationProperty property = activation.getProperty();
054
055        if ( property != null )
056        {
057            String name = property.getName();
058            boolean reverseName = false;
059
060            if ( name == null )
061            {
062                throw new ProfileActivationException( "The property name is required to activate the profile '"
063                    + profile.getId() + "'" );
064            }
065
066            if ( name.startsWith( "!" ) )
067            {
068                reverseName = true;
069                name = name.substring( 1 );
070            }
071
072            String sysValue = properties.getProperty( name );
073
074            String propValue = property.getValue();
075            if ( StringUtils.isNotEmpty( propValue ) )
076            {
077                boolean reverseValue = false;
078                if ( propValue.startsWith( "!" ) )
079                {
080                    reverseValue = true;
081                    propValue = propValue.substring( 1 );
082                }
083
084                // we have a value, so it has to match the system value...
085                boolean result = propValue.equals( sysValue );
086
087                if ( reverseValue )
088                {
089                    return !result;
090                }
091                else
092                {
093                    return result;
094                }
095            }
096            else
097            {
098                boolean result = StringUtils.isNotEmpty( sysValue );
099
100                if ( reverseName )
101                {
102                    return !result;
103                }
104                else
105                {
106                    return result;
107                }
108            }
109        }
110
111        return false;
112    }
113
114}