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.  *      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. 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.      * Constructs a new instance.
  34.      */
  35.     public DefaultProcessingEnvironment() {
  36.         // empty
  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.      * Creates the list of environment variables for this process.
  51.      *
  52.      * @return a amp containing the environment variables.
  53.      * @throws IOException the operation failed.
  54.      */
  55.     protected Map<String, String> createProcEnvironment() throws IOException {
  56.         if (procEnvironment == null) {
  57.             procEnvironment = createEnvironmentMap();
  58.             procEnvironment.putAll(System.getenv());
  59.         }
  60.         return procEnvironment;
  61.     }

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

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

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

  148. }