001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   https://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019
020package org.apache.commons.exec.environment;
021
022import java.io.IOException;
023import java.util.Map;
024import java.util.Objects;
025
026/**
027 * Wraps environment variables.
028 */
029public class EnvironmentUtils {
030
031    /**
032     * Default environment.
033     */
034    private static final DefaultProcessingEnvironment ENVIRONMENT;
035
036    static {
037        ENVIRONMENT = new DefaultProcessingEnvironment();
038    }
039
040    /**
041     * Adds a key/value pair to the given environment. If the key matches an existing key, the previous setting is replaced.
042     *
043     * @param environment the current environment.
044     * @param keyAndValue the key/value pair.
045     */
046    public static void addVariableToEnvironment(final Map<String, String> environment, final String keyAndValue) {
047        final String[] parsedVariable = parseEnvironmentVariable(keyAndValue);
048        environment.put(parsedVariable[0], parsedVariable[1]);
049    }
050
051    /**
052     * 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
053     * rules of the current platform during lookup, e.g. key names will be case-insensitive on Windows platforms.
054     *
055     * @return a map containing the environment variables, may be empty but never {@code null}.
056     * @throws IOException the operation failed.
057     */
058    public static Map<String, String> getProcEnvironment() throws IOException {
059        return ENVIRONMENT.getProcEnvironment();
060    }
061
062    /**
063     * Parses a key/value pair into a String[]. It is assumed that the ky/value pair contains a '=' character.
064     *
065     * @param keyAndValue the key/value pair.
066     * @return a String[] containing the key and value.
067     */
068    private static String[] parseEnvironmentVariable(final String keyAndValue) {
069        final int index = keyAndValue.indexOf('=');
070        if (index == -1) {
071            throw new IllegalArgumentException("Environment variable for this platform must contain an equals sign ('=')");
072        }
073        final String[] result = new String[2];
074        result[0] = keyAndValue.substring(0, index);
075        result[1] = keyAndValue.substring(index + 1);
076        return result;
077    }
078
079    private static String toString(final String value) {
080        return Objects.toString(value, "");
081    }
082
083    /**
084     * Converts a variable map as an array.
085     *
086     * @param environment the environment to use, may be {@code null}.
087     * @return array of key=value assignment strings or {@code null} if and only if the input map was {@code null}.
088     */
089    public static String[] toStrings(final Map<String, String> environment) {
090        if (environment == null) {
091            return null;
092        }
093        return environment.entrySet().stream().map(e -> toString(e.getKey()) + "=" + toString(e.getValue())).toArray(String[]::new);
094    }
095
096    /**
097     * Hides constructor.
098     */
099    private EnvironmentUtils() {
100        // empty
101    }
102
103}