001    package org.apache.maven.model.profile.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    
022    import org.apache.maven.model.Activation;
023    import org.apache.maven.model.ActivationProperty;
024    import org.apache.maven.model.Profile;
025    import org.apache.maven.model.building.ModelProblemCollector;
026    import org.apache.maven.model.building.ModelProblem.Severity;
027    import org.apache.maven.model.profile.ProfileActivationContext;
028    import org.codehaus.plexus.component.annotations.Component;
029    import org.codehaus.plexus.util.StringUtils;
030    
031    /**
032     * Determines profile activation based on the existence or value of some execution property.
033     * 
034     * @author Benjamin Bentmann
035     */
036    @Component( role = ProfileActivator.class, hint = "property" )
037    public class PropertyProfileActivator
038        implements ProfileActivator
039    {
040    
041        public boolean isActive( Profile profile, ProfileActivationContext context, ModelProblemCollector problems )
042        {
043            boolean active = false;
044    
045            Activation activation = profile.getActivation();
046    
047            if ( activation != null )
048            {
049                ActivationProperty property = activation.getProperty();
050    
051                if ( property != null )
052                {
053                    String name = property.getName();
054                    boolean reverseName = false;
055    
056                    if ( name != null && name.startsWith( "!" ) )
057                    {
058                        reverseName = true;
059                        name = name.substring( 1 );
060                    }
061    
062                    if ( name == null || name.length() <= 0 )
063                    {
064                        problems.add( Severity.ERROR, "The property name is required to activate the profile "
065                            + profile.getId(), property.getLocation( "" ), null );
066                        return false;
067                    }
068    
069                    String sysValue = context.getUserProperties().get( name );
070                    if ( sysValue == null )
071                    {
072                        sysValue = context.getSystemProperties().get( name );
073                    }
074    
075                    String propValue = property.getValue();
076                    if ( StringUtils.isNotEmpty( propValue ) )
077                    {
078                        boolean reverseValue = false;
079                        if ( propValue.startsWith( "!" ) )
080                        {
081                            reverseValue = true;
082                            propValue = propValue.substring( 1 );
083                        }
084    
085                        // we have a value, so it has to match the system value...
086                        boolean result = propValue.equals( sysValue );
087    
088                        if ( reverseValue )
089                        {
090                            active = !result;
091                        }
092                        else
093                        {
094                            active = result;
095                        }
096                    }
097                    else
098                    {
099                        boolean result = StringUtils.isNotEmpty( sysValue );
100    
101                        if ( reverseName )
102                        {
103                            active = !result;
104                        }
105                        else
106                        {
107                            active = result;
108                        }
109                    }
110                }
111            }
112    
113            return active;
114        }
115    
116    }