View Javadoc
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    *      http://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       * 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.
40       *
41       * @return The map for storage of environment variables, never {@code null}.
42       */
43      private Map<String, String> createEnvironmentMap() {
44          if (OS.isFamilyWindows()) {
45              return new TreeMap<>(String::compareToIgnoreCase);
46          }
47          return new HashMap<>();
48      }
49  
50      /**
51       * Creates the list of environment variables for this process.
52       *
53       * @return a amp containing the environment variables.
54       * @throws IOException the operation failed.
55       */
56      protected Map<String, String> createProcEnvironment() throws IOException {
57          if (procEnvironment == null) {
58              procEnvironment = createEnvironmentMap();
59              procEnvironment.putAll(System.getenv());
60          }
61          return procEnvironment;
62      }
63  
64      /**
65       * Determine the OS specific command line to get a list of environment variables.
66       *
67       * @return the command line.
68       * @deprecated No longer needed.
69       */
70      @Deprecated
71      protected CommandLine getProcEnvCommand() {
72  //        String executable;
73  //        String[] arguments = null;
74  //        if (OS.isFamilyOS2()) {
75  //            // OS/2 - use same mechanism as Windows 2000
76  //            executable = "cmd";
77  //
78  //            arguments = new String[] {"/c", "set"};
79  //        } else if (OS.isFamilyWindows()) {
80  //            // Determine if we're running under XP/2000/NT or 98/95
81  //            if (OS.isFamilyWin9x()) {
82  //                executable = "command.com";
83  //                // Windows 98/95
84  //            } else {
85  //                executable = "cmd";
86  //                // Windows XP/2000/NT/2003
87  //            }
88  //            arguments = new String[] {"/c", "set"};
89  //        } else if (OS.isFamilyZOS() || OS.isFamilyUnix()) {
90  //            // On most systems one could use: /bin/sh -c env
91  //
92  //            // Some systems have /bin/env, others /usr/bin/env, just try
93  //            if (new File("/bin/env").canRead()) {
94  //                executable = "/bin/env";
95  //            } else if (new File("/usr/bin/env").canRead()) {
96  //                executable = "/usr/bin/env";
97  //            } else {
98  //                // rely on PATH
99  //                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 }