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