Coverage Report - org.codehaus.plexus.interpolation.fixed.EnvarBasedValueSource
 
Classes in this File Line Coverage Branch Coverage Complexity
EnvarBasedValueSource
73 %
14/19
40 %
4/10
2,5
 
 1  
 package org.codehaus.plexus.interpolation.fixed;
 2  
 
 3  
 /*
 4  
  * Copyright 2014 The Codehaus Foundation.
 5  
  *
 6  
  * Licensed under the Apache License, Version 2.0 (the "License");
 7  
  * you may not use this file except in compliance with the License.
 8  
  * You may obtain a copy of the License at
 9  
  *
 10  
  *      http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 17  
  */
 18  
 
 19  
 import org.codehaus.plexus.interpolation.os.OperatingSystemUtils;
 20  
 
 21  
 import java.io.IOException;
 22  
 import java.util.Properties;
 23  
 
 24  
 /**
 25  
  * {@link org.codehaus.plexus.interpolation.ValueSource} which resolves expressions against the environment variables
 26  
  * available from the underlying operating system (and possibly, the shell environment
 27  
  * that created the present Java process). If the expression starts with 'env.',
 28  
  * this prefix is trimmed before resolving the rest as an environment variable name.
 29  
  *
 30  
  * @version $Id$
 31  
  */
 32  
 public class EnvarBasedValueSource
 33  
     implements FixedValueSource
 34  
 {
 35  
 
 36  
     private static Properties envarsCaseSensitive;
 37  
     private static Properties envarsCaseInsensitive;
 38  
 
 39  
     private final Properties envars;
 40  
     private final boolean caseSensitive;
 41  
 
 42  
     /**
 43  
      * Create a new value source for interpolation based on shell environment variables. In this
 44  
      * case, envar keys ARE CASE SENSITIVE.
 45  
      *
 46  
      * @throws java.io.IOException
 47  
      */
 48  
     public EnvarBasedValueSource() throws IOException
 49  
     {
 50  0
         this( true );
 51  0
     }
 52  
 
 53  
     /**
 54  
      * Create a new value source for interpolation based on shell environment variables.
 55  
      *
 56  
      * @param caseSensitive Whether the environment variable key should be treated in a
 57  
      *                      case-sensitive manner for lookups
 58  
      * @throws java.io.IOException
 59  
      */
 60  
     public EnvarBasedValueSource( boolean caseSensitive ) throws IOException
 61  1
     {
 62  1
         this.caseSensitive = caseSensitive;
 63  1
         this.envars = getEnvars( caseSensitive );
 64  1
     }
 65  
 
 66  
     private static synchronized Properties getEnvars( boolean caseSensitive )
 67  
         throws IOException
 68  
     {
 69  1
         if ( caseSensitive )
 70  
         {
 71  0
             if ( envarsCaseSensitive == null )
 72  
             {
 73  0
                 envarsCaseSensitive = OperatingSystemUtils.getSystemEnvVars( caseSensitive );
 74  
             }
 75  0
             return envarsCaseSensitive;
 76  
         }
 77  
         else
 78  
         {
 79  1
             if ( envarsCaseInsensitive == null )
 80  
             {
 81  1
                 envarsCaseInsensitive = OperatingSystemUtils.getSystemEnvVars( caseSensitive );
 82  
             }
 83  1
             return envarsCaseInsensitive;
 84  
         }
 85  
     }
 86  
 
 87  
     /**
 88  
      * If the expression starts with 'env.' then trim this prefix. Next, resolve
 89  
      * the (possibly trimmed) expression as an environment variable name against
 90  
      * the collection of environment variables that were read from the operating
 91  
      * system when this {@link org.codehaus.plexus.interpolation.ValueSource} instance was created.
 92  
      *
 93  
      * @param expression envar expression, like 'HOME' or 'env.HOME'
 94  
      * @return the environment variable value for the given expression
 95  
      */
 96  
     public Object getValue( String expression, InterpolationState interpolationState )
 97  
     {
 98  2
         String expr = expression;
 99  
 
 100  2
         if ( expr.startsWith( "env." ) )
 101  
         {
 102  2
             expr = expr.substring( "env.".length() );
 103  
         }
 104  
 
 105  2
         if ( !caseSensitive )
 106  
         {
 107  2
             expr = expr.toUpperCase();
 108  
         }
 109  
 
 110  2
         return envars.getProperty( expr );
 111  
     }
 112  
 
 113  
 }