001    package org.apache.maven.profiles.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 java.io.IOException;
023    
024    import org.apache.maven.model.Activation;
025    import org.apache.maven.model.ActivationFile;
026    import org.apache.maven.model.Profile;
027    import org.codehaus.plexus.interpolation.EnvarBasedValueSource;
028    import org.codehaus.plexus.interpolation.InterpolationException;
029    import org.codehaus.plexus.interpolation.MapBasedValueSource;
030    import org.codehaus.plexus.interpolation.RegexBasedInterpolator;
031    import org.codehaus.plexus.logging.LogEnabled;
032    import org.codehaus.plexus.logging.Logger;
033    import org.codehaus.plexus.util.FileUtils;
034    import org.codehaus.plexus.util.StringUtils;
035    
036    @Deprecated
037    public class FileProfileActivator
038        extends DetectedProfileActivator
039        implements LogEnabled
040    {
041        private Logger logger;
042    
043        protected boolean canDetectActivation( Profile profile )
044        {
045            return profile.getActivation() != null && profile.getActivation().getFile() != null;
046        }
047    
048        public boolean isActive( Profile profile )
049        {
050            Activation activation = profile.getActivation();
051    
052            ActivationFile actFile = activation.getFile();
053    
054            if ( actFile != null )
055            {
056                // check if the file exists, if it does then the profile will be active
057                String fileString = actFile.getExists();
058    
059                RegexBasedInterpolator interpolator = new RegexBasedInterpolator();
060                try
061                {
062                    interpolator.addValueSource( new EnvarBasedValueSource() );
063                }
064                catch ( IOException e )
065                {
066                    // ignored
067                }
068                interpolator.addValueSource( new MapBasedValueSource( System.getProperties() ) );
069    
070                try
071                {
072                    if ( StringUtils.isNotEmpty( fileString ) )
073                    {
074                        fileString = StringUtils.replace( interpolator.interpolate( fileString, "" ), "\\", "/" );
075                        return FileUtils.fileExists( fileString );
076                    }
077    
078                    // check if the file is missing, if it is then the profile will be active
079                    fileString = actFile.getMissing();
080    
081                    if ( StringUtils.isNotEmpty( fileString ) )
082                    {
083                        fileString = StringUtils.replace( interpolator.interpolate( fileString, "" ), "\\", "/" );
084                        return !FileUtils.fileExists( fileString );
085                    }
086                }
087                catch ( InterpolationException e )
088                {
089                    if ( logger.isDebugEnabled() )
090                    {
091                        logger.debug( "Failed to interpolate missing file location for profile activator: " + fileString,
092                                      e );
093                    }
094                    else
095                    {
096                        logger.warn( "Failed to interpolate missing file location for profile activator: " + fileString
097                            + ". Run in debug mode (-X) for more information." );
098                    }
099                }
100            }
101    
102            return false;
103        }
104    
105        public void enableLogging( Logger logger )
106        {
107            this.logger = logger;
108        }
109    }