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