EnvironmentUtils.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.  *      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. package org.apache.commons.exec.environment;

  18. import java.io.IOException;
  19. import java.util.Map;
  20. import java.util.Objects;

  21. /**
  22.  * Wraps environment variables.
  23.  */
  24. public class EnvironmentUtils {

  25.     private static final DefaultProcessingEnvironment ENVIRONMENT;

  26.     static {
  27.         ENVIRONMENT = new DefaultProcessingEnvironment();
  28.     }

  29.     /**
  30.      * Adds a key/value pair to the given environment. If the key matches an existing key, the previous setting is replaced.
  31.      *
  32.      * @param environment the current environment.
  33.      * @param keyAndValue the key/value pair.
  34.      */
  35.     public static void addVariableToEnvironment(final Map<String, String> environment, final String keyAndValue) {
  36.         final String[] parsedVariable = parseEnvironmentVariable(keyAndValue);
  37.         environment.put(parsedVariable[0], parsedVariable[1]);
  38.     }

  39.     /**
  40.      * 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
  41.      * rules of the current platform during lookup, e.g. key names will be case-insensitive on Windows platforms.
  42.      *
  43.      * @return a map containing the environment variables, may be empty but never {@code null}.
  44.      * @throws IOException the operation failed.
  45.      */
  46.     public static Map<String, String> getProcEnvironment() throws IOException {
  47.         return ENVIRONMENT.getProcEnvironment();
  48.     }

  49.     /**
  50.      * Parses a key/value pair into a String[]. It is assumed that the ky/value pair contains a '=' character.
  51.      *
  52.      * @param keyAndValue the key/value pair.
  53.      * @return a String[] containing the key and value.
  54.      */
  55.     private static String[] parseEnvironmentVariable(final String keyAndValue) {
  56.         final int index = keyAndValue.indexOf('=');
  57.         if (index == -1) {
  58.             throw new IllegalArgumentException("Environment variable for this platform must contain an equals sign ('=')");
  59.         }
  60.         final String[] result = new String[2];
  61.         result[0] = keyAndValue.substring(0, index);
  62.         result[1] = keyAndValue.substring(index + 1);
  63.         return result;
  64.     }

  65.     private static String toString(final String value) {
  66.         return Objects.toString(value, "");
  67.     }

  68.     /**
  69.      * Converts a variable map as an array.
  70.      *
  71.      * @param environment the environment to use, may be {@code null}.
  72.      * @return array of key=value assignment strings or {@code null} if and only if the input map was {@code null}.
  73.      */
  74.     public static String[] toStrings(final Map<String, String> environment) {
  75.         if (environment == null) {
  76.             return null;
  77.         }
  78.         return environment.entrySet().stream().map(e -> toString(e.getKey()) + "=" + toString(e.getValue())).toArray(String[]::new);
  79.     }

  80.     /**
  81.      * Hides constructor.
  82.      */
  83.     private EnvironmentUtils() {
  84.         // empty
  85.     }

  86. }