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 * @see ActivationProperty
038 */
039 @Component( role = ProfileActivator.class, hint = "property" )
040 public class PropertyProfileActivator
041 implements ProfileActivator
042 {
043
044 public boolean isActive( Profile profile, ProfileActivationContext context, ModelProblemCollector problems )
045 {
046 Activation activation = profile.getActivation();
047
048 if ( activation == null )
049 {
050 return false;
051 }
052
053 ActivationProperty property = activation.getProperty();
054
055 if ( property == null )
056 {
057 return false;
058 }
059
060 String name = property.getName();
061 boolean reverseName = false;
062
063 if ( name != null && name.startsWith( "!" ) )
064 {
065 reverseName = true;
066 name = name.substring( 1 );
067 }
068
069 if ( name == null || name.length() <= 0 )
070 {
071 problems.add( new ModelProblemCollectorRequest( Severity.ERROR, Version.BASE )
072 .setMessage( "The property name is required to activate the profile " + profile.getId() )
073 .setLocation( property.getLocation( "" ) ) );
074 return false;
075 }
076
077 String sysValue = context.getUserProperties().get( name );
078 if ( sysValue == null )
079 {
080 sysValue = context.getSystemProperties().get( name );
081 }
082
083 String propValue = property.getValue();
084 if ( StringUtils.isNotEmpty( propValue ) )
085 {
086 boolean reverseValue = false;
087 if ( propValue.startsWith( "!" ) )
088 {
089 reverseValue = true;
090 propValue = propValue.substring( 1 );
091 }
092
093 // we have a value, so it has to match the system value...
094 boolean result = propValue.equals( sysValue );
095
096 return reverseValue ? !result : result;
097 }
098 else
099 {
100 boolean result = StringUtils.isNotEmpty( sysValue );
101
102 return reverseName ? !result : result;
103 }
104 }
105
106 }