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 java.io.File;
023
024import org.apache.maven.model.Activation;
025import org.apache.maven.model.ActivationFile;
026import org.apache.maven.model.Profile;
027import org.apache.maven.model.building.ModelProblemCollector;
028import org.apache.maven.model.building.ModelProblem.Severity;
029import org.apache.maven.model.building.ModelProblem.Version;
030import org.apache.maven.model.building.ModelProblemCollectorRequest;
031import org.apache.maven.model.path.PathTranslator;
032import org.apache.maven.model.profile.ProfileActivationContext;
033import org.codehaus.plexus.component.annotations.Component;
034import org.codehaus.plexus.component.annotations.Requirement;
035import org.codehaus.plexus.interpolation.AbstractValueSource;
036import org.codehaus.plexus.interpolation.MapBasedValueSource;
037import org.codehaus.plexus.interpolation.RegexBasedInterpolator;
038import org.codehaus.plexus.util.StringUtils;
039
040/**
041 * Determines profile activation based on the existence/absence of some file.
042 * File name interpolation support is limited to <code>${basedir}</code> (since Maven 3,
043 * see <a href="http://jira.codehaus.org/browse/MNG-2363">MNG-2363</a>),
044 * System properties and request properties.
045 * <code>${project.basedir}</code> is intentionally not supported as this form would suggest that other
046 * <code>${project.*}</code> expressions can be used, which is however beyond the design.
047 *
048 * @author Benjamin Bentmann
049 * @see ActivationFile
050 * @see org.apache.maven.model.validation.DefaultModelValidator#validateRawModel
051 */
052@Component( role = ProfileActivator.class, hint = "file" )
053public class FileProfileActivator
054    implements ProfileActivator
055{
056
057    @Requirement
058    private PathTranslator pathTranslator;
059
060    public FileProfileActivator setPathTranslator( PathTranslator pathTranslator )
061    {
062        this.pathTranslator = pathTranslator;
063        return this;
064    }
065
066    public boolean isActive( Profile profile, ProfileActivationContext context, ModelProblemCollector problems )
067    {
068        Activation activation = profile.getActivation();
069
070        if ( activation == null )
071        {
072            return false;
073        }
074
075        ActivationFile file = activation.getFile();
076
077        if ( file == null )
078        {
079            return false;
080        }
081
082        String path;
083        boolean missing;
084
085        if ( StringUtils.isNotEmpty( file.getExists() ) )
086        {
087            path = file.getExists();
088            missing = false;
089        }
090        else if ( StringUtils.isNotEmpty( file.getMissing() ) )
091        {
092            path = file.getMissing();
093            missing = true;
094        }
095        else
096        {
097            return false;
098        }
099
100        RegexBasedInterpolator interpolator = new RegexBasedInterpolator();
101
102        final File basedir = context.getProjectDirectory();
103
104        if ( basedir != null )
105        {
106            interpolator.addValueSource( new AbstractValueSource( false )
107            {
108                public Object getValue( String expression )
109                {
110                    /*
111                     * NOTE: We intentionally only support ${basedir} and not ${project.basedir} as the latter form
112                     * would suggest that other project.* expressions can be used which is however beyond the design.
113                     */
114                    if ( "basedir".equals( expression ) )
115                    {
116                        return basedir.getAbsolutePath();
117                    }
118                    return null;
119                }
120            } );
121        }
122        else if ( path.contains( "${basedir}" ) )
123        {
124            return false;
125        }
126
127        interpolator.addValueSource( new MapBasedValueSource( context.getProjectProperties() ) );
128
129        interpolator.addValueSource( new MapBasedValueSource( context.getUserProperties() ) );
130
131        interpolator.addValueSource( new MapBasedValueSource( context.getSystemProperties() ) );
132
133        try
134        {
135            path = interpolator.interpolate( path, "" );
136        }
137        catch ( Exception e )
138        {
139            problems.add( new ModelProblemCollectorRequest( Severity.ERROR, Version.BASE )
140                    .setMessage( "Failed to interpolate file location " + path + " for profile " + profile.getId()
141                                 + ": " + e.getMessage() )
142                    .setLocation( file.getLocation( missing ? "missing" : "exists" ) )
143                    .setException( e ) );
144            return false;
145        }
146
147        path = pathTranslator.alignToBaseDirectory( path, basedir );
148
149        // replace activation value with interpolated value
150        if ( missing )
151        {
152            file.setMissing( path );
153        }
154        else
155        {
156            file.setExists( path );
157        }
158
159        File f = new File( path );
160
161        if ( !f.isAbsolute() )
162        {
163            return false;
164        }
165
166        boolean fileExists = f.exists();
167
168        return missing ? !fileExists : fileExists;
169    }
170
171    @Override
172    public boolean presentInConfig( Profile profile, ProfileActivationContext context, ModelProblemCollector problems )
173    {
174        Activation activation = profile.getActivation();
175
176        if ( activation == null )
177        {
178            return false;
179        }
180
181        ActivationFile file = activation.getFile();
182
183        if ( file == null )
184        {
185            return false;
186        }
187        return true;
188    }
189
190}