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