001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018package org.apache.commons.exec.environment; 019 020import java.io.BufferedReader; 021import java.io.IOException; 022import java.util.HashMap; 023import java.util.Map; 024import java.util.TreeMap; 025 026import org.apache.commons.exec.CommandLine; 027import org.apache.commons.exec.OS; 028 029/** 030 * 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 031 * or requires starting a process to get them running an OS command line. 032 */ 033public class DefaultProcessingEnvironment { 034 035 /** The environment variables of the process */ 036 protected Map<String, String> procEnvironment; 037 038 /** 039 * 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. 040 * 041 * @return The map for storage of environment variables, never {@code null}. 042 */ 043 private Map<String, String> createEnvironmentMap() { 044 if (OS.isFamilyWindows()) { 045 return new TreeMap<>(String::compareToIgnoreCase); 046 } 047 return new HashMap<>(); 048 } 049 050 /** 051 * Creates the list of environment variables for this process. 052 * 053 * @return a amp containing the environment variables. 054 * @throws IOException the operation failed. 055 */ 056 protected Map<String, String> createProcEnvironment() throws IOException { 057 if (procEnvironment == null) { 058 procEnvironment = createEnvironmentMap(); 059 procEnvironment.putAll(System.getenv()); 060 } 061 return procEnvironment; 062 } 063 064 /** 065 * Determine the OS specific command line to get a list of environment variables. 066 * 067 * @return the command line. 068 * @deprecated No longer needed. 069 */ 070 @Deprecated 071 protected CommandLine getProcEnvCommand() { 072// String executable; 073// String[] arguments = null; 074// if (OS.isFamilyOS2()) { 075// // OS/2 - use same mechanism as Windows 2000 076// executable = "cmd"; 077// 078// arguments = new String[] {"/c", "set"}; 079// } else if (OS.isFamilyWindows()) { 080// // Determine if we're running under XP/2000/NT or 98/95 081// if (OS.isFamilyWin9x()) { 082// executable = "command.com"; 083// // Windows 98/95 084// } else { 085// executable = "cmd"; 086// // Windows XP/2000/NT/2003 087// } 088// arguments = new String[] {"/c", "set"}; 089// } else if (OS.isFamilyZOS() || OS.isFamilyUnix()) { 090// // On most systems one could use: /bin/sh -c env 091// 092// // Some systems have /bin/env, others /usr/bin/env, just try 093// if (new File("/bin/env").canRead()) { 094// executable = "/bin/env"; 095// } else if (new File("/usr/bin/env").canRead()) { 096// executable = "/usr/bin/env"; 097// } else { 098// // rely on PATH 099// executable = "env"; 100// } 101// } else if (OS.isFamilyNetware() || OS.isFamilyOS400()) { 102// // rely on PATH 103// executable = "env"; 104// } else { 105// // macOS 9 and previous 106// // TODO: I have no idea how to get it, someone must fix it 107// executable = null; 108// } 109 final CommandLine commandLine = null; 110// if (executable != null) { 111// commandLine = new CommandLine(executable); 112// commandLine.addArguments(arguments); 113// } 114 return commandLine; 115 } 116 117 /** 118 * Gets the list of environment variables for this process. 119 * 120 * @return a map containing the environment variables. 121 * @throws IOException obtaining the environment variables failed. 122 */ 123 public synchronized Map<String, String> getProcEnvironment() throws IOException { 124 if (procEnvironment == null) { 125 procEnvironment = this.createProcEnvironment(); 126 } 127 // create a copy of the map just in case that 128 // anyone is going to modifiy it, e.g. removing 129 // or setting an evironment variable 130 final Map<String, String> copy = createEnvironmentMap(); 131 copy.putAll(procEnvironment); 132 return copy; 133 } 134 135 /** 136 * Runs a process to list the environment variables. 137 * 138 * @return a reader containing the output of the process. 139 * @throws IOException starting the process failed. 140 * @deprecated No longer needed. 141 */ 142 @Deprecated 143 protected BufferedReader runProcEnvCommand() throws IOException { 144// final ByteArrayOutputStream out = new ByteArrayOutputStream(); 145// final Executor exe = DefaultExecutor.builder().get(); 146// exe.setStreamHandler(new PumpStreamHandler(out)); 147// // ignore the exit value - Just try to use what we got 148// exe.execute(getProcEnvCommand()); 149// return new BufferedReader(new StringReader(toString(out))); 150 return null; 151 } 152 153}