Coverage Report - org.apache.maven.profiles.activation.FileProfileActivator
 
Classes in this File Line Coverage Branch Coverage Complexity
FileProfileActivator
58 %
15/26
36 %
5/14
3,667
 
 1  
 package org.apache.maven.profiles.activation;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *  http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import java.io.IOException;
 23  
 
 24  
 import org.apache.maven.model.Activation;
 25  
 import org.apache.maven.model.ActivationFile;
 26  
 import org.apache.maven.model.Profile;
 27  
 import org.codehaus.plexus.interpolation.EnvarBasedValueSource;
 28  
 import org.codehaus.plexus.interpolation.InterpolationException;
 29  
 import org.codehaus.plexus.interpolation.MapBasedValueSource;
 30  
 import org.codehaus.plexus.interpolation.RegexBasedInterpolator;
 31  
 import org.codehaus.plexus.logging.LogEnabled;
 32  
 import org.codehaus.plexus.logging.Logger;
 33  
 import org.codehaus.plexus.util.FileUtils;
 34  
 import org.codehaus.plexus.util.StringUtils;
 35  
 
 36  119
 public class FileProfileActivator
 37  
     extends DetectedProfileActivator
 38  
     implements LogEnabled
 39  
 {
 40  
     private Logger logger;
 41  
 
 42  
     protected boolean canDetectActivation( Profile profile )
 43  
     {
 44  118
         return profile.getActivation() != null && profile.getActivation().getFile() != null;
 45  
     }
 46  
 
 47  
     public boolean isActive( Profile profile )
 48  
     {
 49  1
         Activation activation = profile.getActivation();
 50  
 
 51  1
         ActivationFile actFile = activation.getFile();
 52  
 
 53  1
         if ( actFile != null )
 54  
         {
 55  
             // check if the file exists, if it does then the profile will be active
 56  1
             String fileString = actFile.getExists();
 57  
 
 58  1
             RegexBasedInterpolator interpolator = new RegexBasedInterpolator();
 59  
             try
 60  
             {
 61  1
                 interpolator.addValueSource( new EnvarBasedValueSource() );
 62  
             }
 63  0
             catch ( IOException e )
 64  
             {
 65  
                 // ignored
 66  1
             }
 67  1
             interpolator.addValueSource( new MapBasedValueSource( System.getProperties() ) );
 68  
 
 69  
             try
 70  
             {
 71  1
                 if ( StringUtils.isNotEmpty( fileString ) )
 72  
                 {
 73  1
                     fileString = StringUtils.replace( interpolator.interpolate( fileString, "" ), "\\", "/" );
 74  1
                     return FileUtils.fileExists( fileString );
 75  
                 }
 76  
 
 77  
                 // check if the file is missing, if it is then the profile will be active
 78  0
                 fileString = actFile.getMissing();
 79  
 
 80  0
                 if ( StringUtils.isNotEmpty( fileString ) )
 81  
                 {
 82  0
                     fileString = StringUtils.replace( interpolator.interpolate( fileString, "" ), "\\", "/" );
 83  0
                     return !FileUtils.fileExists( fileString );
 84  
                 }
 85  
             }
 86  0
             catch ( InterpolationException e )
 87  
             {
 88  0
                 if ( logger.isDebugEnabled() )
 89  
                 {
 90  0
                     logger.debug( "Failed to interpolate missing file location for profile activator: " + fileString, e );
 91  
                 }
 92  
                 else
 93  
                 {
 94  0
                     logger.warn( "Failed to interpolate missing file location for profile activator: " + fileString + ". Run in debug mode (-X) for more information." );
 95  
                 }
 96  0
             }
 97  
         }
 98  
 
 99  0
         return false;
 100  
     }
 101  
 
 102  
     public void enableLogging( Logger logger )
 103  
     {
 104  119
         this.logger = logger;
 105  119
     }
 106  
 }