1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * https://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package org.apache.commons.exec.environment; 19 20 import java.io.BufferedReader; 21 import java.io.IOException; 22 import java.util.HashMap; 23 import java.util.Map; 24 import java.util.TreeMap; 25 26 import org.apache.commons.exec.CommandLine; 27 import org.apache.commons.exec.OS; 28 29 /** 30 * Helper class to determine the environment variable for the OS. Depending on the JDK the environment variables can be either retrieved directly from the JVM 31 * or requires starting a process to get them running an OS command line. 32 */ 33 public class DefaultProcessingEnvironment { 34 35 /** The environment variables of the process */ 36 protected Map<String, String> procEnvironment; 37 38 /** 39 * Constructs a new instance. 40 */ 41 public DefaultProcessingEnvironment() { 42 // empty 43 } 44 45 /** 46 * Creates a map that obeys the casing rules of the current platform for key lookup. E.g. on a Windows platform, the map keys will be case-insensitive. 47 * 48 * @return The map for storage of environment variables, never {@code null}. 49 */ 50 private Map<String, String> createEnvironmentMap() { 51 if (OS.isFamilyWindows()) { 52 return new TreeMap<>(String::compareToIgnoreCase); 53 } 54 return new HashMap<>(); 55 } 56 57 /** 58 * Creates the list of environment variables for this process. 59 * 60 * @return a amp containing the environment variables. 61 * @throws IOException the operation failed. 62 */ 63 protected Map<String, String> createProcEnvironment() throws IOException { 64 if (procEnvironment == null) { 65 procEnvironment = createEnvironmentMap(); 66 procEnvironment.putAll(System.getenv()); 67 } 68 return procEnvironment; 69 } 70 71 /** 72 * Determine the OS specific command line to get a list of environment variables. 73 * 74 * @return the command line. 75 * @deprecated No longer needed. 76 */ 77 @Deprecated 78 protected CommandLine getProcEnvCommand() { 79 // String executable; 80 // String[] arguments = null; 81 // if (OS.isFamilyOS2()) { 82 // // OS/2 - use same mechanism as Windows 2000 83 // executable = "cmd"; 84 // 85 // arguments = new String[] {"/c", "set"}; 86 // } else if (OS.isFamilyWindows()) { 87 // // Determine if we're running under XP/2000/NT or 98/95 88 // if (OS.isFamilyWin9x()) { 89 // executable = "command.com"; 90 // // Windows 98/95 91 // } else { 92 // executable = "cmd"; 93 // // Windows XP/2000/NT/2003 94 // } 95 // arguments = new String[] {"/c", "set"}; 96 // } else if (OS.isFamilyZOS() || OS.isFamilyUnix()) { 97 // // On most systems one could use: /bin/sh -c env 98 // 99 // // Some systems have /bin/env, others /usr/bin/env, just try 100 // if (new File("/bin/env").canRead()) { 101 // executable = "/bin/env"; 102 // } else if (new File("/usr/bin/env").canRead()) { 103 // executable = "/usr/bin/env"; 104 // } else { 105 // // rely on PATH 106 // executable = "env"; 107 // } 108 // } else if (OS.isFamilyNetware() || OS.isFamilyOS400()) { 109 // // rely on PATH 110 // executable = "env"; 111 // } else { 112 // // macOS 9 and previous 113 // // TODO: I have no idea how to get it, someone must fix it 114 // executable = null; 115 // } 116 final CommandLine commandLine = null; 117 // if (executable != null) { 118 // commandLine = new CommandLine(executable); 119 // commandLine.addArguments(arguments); 120 // } 121 return commandLine; 122 } 123 124 /** 125 * Gets the list of environment variables for this process. 126 * 127 * @return a map containing the environment variables. 128 * @throws IOException obtaining the environment variables failed. 129 */ 130 public synchronized Map<String, String> getProcEnvironment() throws IOException { 131 if (procEnvironment == null) { 132 procEnvironment = createProcEnvironment(); 133 } 134 // create a copy of the map just in case that 135 // anyone is going to modifiy it, e.g. removing 136 // or setting an evironment variable 137 final Map<String, String> copy = createEnvironmentMap(); 138 copy.putAll(procEnvironment); 139 return copy; 140 } 141 142 /** 143 * Runs a process to list the environment variables. 144 * 145 * @return a reader containing the output of the process. 146 * @throws IOException starting the process failed. 147 * @deprecated No longer needed. 148 */ 149 @Deprecated 150 protected BufferedReader runProcEnvCommand() throws IOException { 151 // final ByteArrayOutputStream out = new ByteArrayOutputStream(); 152 // final Executor exe = DefaultExecutor.builder().get(); 153 // exe.setStreamHandler(new PumpStreamHandler(out)); 154 // // ignore the exit value - Just try to use what we got 155 // exe.execute(getProcEnvCommand()); 156 // return new BufferedReader(new StringReader(toString(out))); 157 return null; 158 } 159 160 }