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.IOException;
21  import java.util.Map;
22  import java.util.Objects;
23  
24  /**
25   * Wraps environment variables.
26   */
27  public class EnvironmentUtils {
28  
29      private static final DefaultProcessingEnvironment ENVIRONMENT;
30  
31      static {
32          ENVIRONMENT = new DefaultProcessingEnvironment();
33      }
34  
35      /**
36       * Adds a key/value pair to the given environment. If the key matches an existing key, the previous setting is replaced.
37       *
38       * @param environment the current environment.
39       * @param keyAndValue the key/value pair.
40       */
41      public static void addVariableToEnvironment(final Map<String, String> environment, final String keyAndValue) {
42          final String[] parsedVariable = parseEnvironmentVariable(keyAndValue);
43          environment.put(parsedVariable[0], parsedVariable[1]);
44      }
45  
46      /**
47       * Gets the list of environment variables for this process. The returned map preserves the casing of a variable's name on all platforms but obeys the casing
48       * rules of the current platform during lookup, e.g. key names will be case-insensitive on Windows platforms.
49       *
50       * @return a map containing the environment variables, may be empty but never {@code null}.
51       * @throws IOException the operation failed.
52       */
53      public static Map<String, String> getProcEnvironment() throws IOException {
54          return ENVIRONMENT.getProcEnvironment();
55      }
56  
57      /**
58       * Parses a key/value pair into a String[]. It is assumed that the ky/value pair contains a '=' character.
59       *
60       * @param keyAndValue the key/value pair.
61       * @return a String[] containing the key and value.
62       */
63      private static String[] parseEnvironmentVariable(final String keyAndValue) {
64          final int index = keyAndValue.indexOf('=');
65          if (index == -1) {
66              throw new IllegalArgumentException("Environment variable for this platform must contain an equals sign ('=')");
67          }
68          final String[] result = new String[2];
69          result[0] = keyAndValue.substring(0, index);
70          result[1] = keyAndValue.substring(index + 1);
71          return result;
72      }
73  
74      private static String toString(final String value) {
75          return Objects.toString(value, "");
76      }
77  
78      /**
79       * Converts a variable map as an array.
80       *
81       * @param environment the environment to use, may be {@code null}.
82       * @return array of key=value assignment strings or {@code null} if and only if the input map was {@code null}.
83       */
84      public static String[] toStrings(final Map<String, String> environment) {
85          if (environment == null) {
86              return null;
87          }
88          return environment.entrySet().stream().map(e -> toString(e.getKey()) + "=" + toString(e.getValue())).toArray(String[]::new);
89      }
90  
91      /**
92       * Hides constructor.
93       */
94      private EnvironmentUtils() {
95          // empty
96      }
97  
98  }