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