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