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