Coverage Report - org.codehaus.plexus.interpolation.fixed.EnvarBasedValueSource
 
Classes in this File Line Coverage Branch Coverage Complexity
EnvarBasedValueSource
100%
22/22
80%
8/10
2.2
 
 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  
  */
 31  
 public class EnvarBasedValueSource
 32  
     implements FixedValueSource
 33  
 {
 34  
 
 35  
     private static Properties envarsCaseSensitive;
 36  
     private static Properties envarsCaseInsensitive;
 37  
 
 38  
     private final Properties envars;
 39  
     private final boolean caseSensitive;
 40  
 
 41  
     /**
 42  
      * Create a new value source for interpolation based on shell environment variables. In this
 43  
      * case, envar keys ARE CASE SENSITIVE.
 44  
      *
 45  
      * @throws java.io.IOException in case of an error.
 46  
      */
 47  
     public EnvarBasedValueSource() throws IOException
 48  
     {
 49  2
         this( true );
 50  2
     }
 51  
 
 52  
     /**
 53  
      * Create a new value source for interpolation based on shell environment variables.
 54  
      *
 55  
      * @param caseSensitive Whether the environment variable key should be treated in a
 56  
      *                      case-sensitive manner for lookups
 57  
      * @throws java.io.IOException in case of an error.
 58  
      */
 59  
     public EnvarBasedValueSource( boolean caseSensitive ) throws IOException
 60  4
     {
 61  4
         this.caseSensitive = caseSensitive;
 62  4
         this.envars = getEnvars( caseSensitive );
 63  4
     }
 64  
 
 65  
     private static synchronized Properties getEnvars( boolean caseSensitive )
 66  
         throws IOException
 67  
     {
 68  4
         if ( caseSensitive )
 69  
         {
 70  2
             if ( envarsCaseSensitive == null )
 71  
             {
 72  2
                 envarsCaseSensitive = OperatingSystemUtils.getSystemEnvVars( caseSensitive );
 73  
             }
 74  2
             return envarsCaseSensitive;
 75  
         }
 76  
         else
 77  
         {
 78  2
             if ( envarsCaseInsensitive == null )
 79  
             {
 80  2
                 envarsCaseInsensitive = OperatingSystemUtils.getSystemEnvVars( caseSensitive );
 81  
             }
 82  2
             return envarsCaseInsensitive;
 83  
         }
 84  
     }
 85  
 
 86  
     /**
 87  
      * If the expression starts with 'env.' then trim this prefix. Next, resolve
 88  
      * the (possibly trimmed) expression as an environment variable name against
 89  
      * the collection of environment variables that were read from the operating
 90  
      * system when this {@link org.codehaus.plexus.interpolation.ValueSource} instance was created.
 91  
      *
 92  
      * @param expression envar expression, like 'HOME' or 'env.HOME'
 93  
      * @return the environment variable value for the given expression
 94  
      */
 95  
     public Object getValue( String expression, InterpolationState interpolationState )
 96  
     {
 97  11
         String expr = expression;
 98  
 
 99  11
         if ( expr.startsWith( "env." ) )
 100  
         {
 101  6
             expr = expr.substring( "env.".length() );
 102  
         }
 103  
 
 104  11
         if ( !caseSensitive )
 105  
         {
 106  6
             expr = expr.toUpperCase();
 107  
         }
 108  
 
 109  11
         return envars.getProperty( expr );
 110  
     }
 111  
 
 112  
     /**
 113  
      * reset static variables acting as a cache for testing purposes only
 114  
      */
 115  
     static void resetStatics()
 116  
     {
 117  33
         envarsCaseSensitive = null;
 118  33
         envarsCaseInsensitive = null;
 119  33
     }
 120  
 
 121  
 }