DefaultProcessingEnvironment.java

  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. package org.apache.commons.exec.environment;

  18. import java.io.BufferedReader;
  19. import java.io.IOException;
  20. import java.util.HashMap;
  21. import java.util.Map;
  22. import java.util.TreeMap;

  23. import org.apache.commons.exec.CommandLine;
  24. import org.apache.commons.exec.OS;

  25. /**
  26.  * 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
  27.  * or requires starting a process to get them running an OS command line.
  28.  */
  29. public class DefaultProcessingEnvironment {

  30.     /** The environment variables of the process */
  31.     protected Map<String, String> procEnvironment;

  32.     /**
  33.      * 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.
  34.      *
  35.      * @return The map for storage of environment variables, never {@code null}.
  36.      */
  37.     private Map<String, String> createEnvironmentMap() {
  38.         if (OS.isFamilyWindows()) {
  39.             return new TreeMap<>(String::compareToIgnoreCase);
  40.         }
  41.         return new HashMap<>();
  42.     }

  43.     /**
  44.      * Creates the list of environment variables for this process.
  45.      *
  46.      * @return a amp containing the environment variables.
  47.      * @throws IOException the operation failed.
  48.      */
  49.     protected Map<String, String> createProcEnvironment() throws IOException {
  50.         if (procEnvironment == null) {
  51.             procEnvironment = createEnvironmentMap();
  52.             procEnvironment.putAll(System.getenv());
  53.         }
  54.         return procEnvironment;
  55.     }

  56.     /**
  57.      * Determine the OS specific command line to get a list of environment variables.
  58.      *
  59.      * @return the command line.
  60.      * @deprecated No longer needed.
  61.      */
  62.     @Deprecated
  63.     protected CommandLine getProcEnvCommand() {
  64. //        String executable;
  65. //        String[] arguments = null;
  66. //        if (OS.isFamilyOS2()) {
  67. //            // OS/2 - use same mechanism as Windows 2000
  68. //            executable = "cmd";
  69. //
  70. //            arguments = new String[] {"/c", "set"};
  71. //        } else if (OS.isFamilyWindows()) {
  72. //            // Determine if we're running under XP/2000/NT or 98/95
  73. //            if (OS.isFamilyWin9x()) {
  74. //                executable = "command.com";
  75. //                // Windows 98/95
  76. //            } else {
  77. //                executable = "cmd";
  78. //                // Windows XP/2000/NT/2003
  79. //            }
  80. //            arguments = new String[] {"/c", "set"};
  81. //        } else if (OS.isFamilyZOS() || OS.isFamilyUnix()) {
  82. //            // On most systems one could use: /bin/sh -c env
  83. //
  84. //            // Some systems have /bin/env, others /usr/bin/env, just try
  85. //            if (new File("/bin/env").canRead()) {
  86. //                executable = "/bin/env";
  87. //            } else if (new File("/usr/bin/env").canRead()) {
  88. //                executable = "/usr/bin/env";
  89. //            } else {
  90. //                // rely on PATH
  91. //                executable = "env";
  92. //            }
  93. //        } else if (OS.isFamilyNetware() || OS.isFamilyOS400()) {
  94. //            // rely on PATH
  95. //            executable = "env";
  96. //        } else {
  97. //            // macOS 9 and previous
  98. //            // TODO: I have no idea how to get it, someone must fix it
  99. //            executable = null;
  100. //        }
  101.         final CommandLine commandLine = null;
  102. //        if (executable != null) {
  103. //            commandLine = new CommandLine(executable);
  104. //            commandLine.addArguments(arguments);
  105. //        }
  106.         return commandLine;
  107.     }

  108.     /**
  109.      * Gets the list of environment variables for this process.
  110.      *
  111.      * @return a map containing the environment variables.
  112.      * @throws IOException obtaining the environment variables failed.
  113.      */
  114.     public synchronized Map<String, String> getProcEnvironment() throws IOException {
  115.         if (procEnvironment == null) {
  116.             procEnvironment = this.createProcEnvironment();
  117.         }
  118.         // create a copy of the map just in case that
  119.         // anyone is going to modifiy it, e.g. removing
  120.         // or setting an evironment variable
  121.         final Map<String, String> copy = createEnvironmentMap();
  122.         copy.putAll(procEnvironment);
  123.         return copy;
  124.     }

  125.     /**
  126.      * Runs a process to list the environment variables.
  127.      *
  128.      * @return a reader containing the output of the process.
  129.      * @throws IOException starting the process failed.
  130.      * @deprecated No longer needed.
  131.      */
  132.     @Deprecated
  133.     protected BufferedReader runProcEnvCommand() throws IOException {
  134. //        final ByteArrayOutputStream out = new ByteArrayOutputStream();
  135. //        final Executor exe = DefaultExecutor.builder().get();
  136. //        exe.setStreamHandler(new PumpStreamHandler(out));
  137. //        // ignore the exit value - Just try to use what we got
  138. //        exe.execute(getProcEnvCommand());
  139. //        return new BufferedReader(new StringReader(toString(out)));
  140.         return null;
  141.     }

  142. }