Coverage Report - org.apache.maven.profiles.activation.FileProfileActivator
 
Classes in this File Line Coverage Branch Coverage Complexity
FileProfileActivator
0%
0/27
0%
0/12
5
 
 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.util.FileUtils;
 32  
 import org.codehaus.plexus.util.StringUtils;
 33  
 
 34  0
 public class FileProfileActivator
 35  
     extends DetectedProfileActivator
 36  
 {
 37  
     protected boolean canDetectActivation( Profile profile )
 38  
     {
 39  0
         return profile.getActivation() != null && profile.getActivation().getFile() != null;
 40  
     }
 41  
 
 42  
     public boolean isActive( Profile profile )
 43  
     {
 44  0
         Activation activation = profile.getActivation();
 45  
 
 46  0
         ActivationFile actFile = activation.getFile();
 47  
 
 48  0
         if ( actFile != null )
 49  
         {
 50  
             // check if the file exists, if it does then the profile will be active
 51  0
             String fileString = actFile.getExists();
 52  
 
 53  0
             RegexBasedInterpolator interpolator = new RegexBasedInterpolator();
 54  
             try
 55  
             {
 56  0
                 interpolator.addValueSource( new EnvarBasedValueSource() );
 57  
             }
 58  0
             catch ( IOException e )
 59  
             {
 60  
                 // ignored
 61  0
             }
 62  0
             interpolator.addValueSource( new MapBasedValueSource( System.getProperties() ) );
 63  
 
 64  0
             if ( StringUtils.isNotEmpty( fileString ) )
 65  
             {
 66  
                 try
 67  
                 {
 68  0
                     fileString = interpolator.interpolate( fileString, "" );
 69  
                 }
 70  0
                 catch ( InterpolationException e )
 71  
                 {
 72  0
                     getLogger().debug( "Failed to interpolate path in file profile activator: " + fileString, e );
 73  0
                 }
 74  
                 
 75  0
                 fileString = StringUtils.replace( fileString, "\\", "/" );
 76  0
                 return FileUtils.fileExists( fileString );
 77  
             }
 78  
 
 79  
             // check if the file is missing, if it is then the profile will be active
 80  0
             fileString = actFile.getMissing();
 81  
 
 82  0
             if ( StringUtils.isNotEmpty( fileString ) )
 83  
             {
 84  
                 try
 85  
                 {
 86  0
                     fileString = interpolator.interpolate( fileString, "" );
 87  
                 }
 88  0
                 catch ( InterpolationException e )
 89  
                 {
 90  0
                     getLogger().debug( "Failed to interpolate path in file profile activator: " + fileString, e );
 91  0
                 }
 92  
                 
 93  0
                 fileString = StringUtils.replace( fileString, "\\", "/" );
 94  0
                 return !FileUtils.fileExists( fileString );
 95  
             }
 96  
         }
 97  
 
 98  0
         return false;
 99  
     }
 100  
 }