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 *      http://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 */
018
019package org.apache.commons.exec.environment;
020
021import java.io.IOException;
022import java.util.Map;
023import java.util.Map.Entry;
024
025//import org.apache.commons.exec.OS;
026
027/**
028 * Wrapper for environment variables.
029 *
030 * @version $Id: EnvironmentUtils.java 1636056 2014-11-01 21:12:52Z ggregory $
031 */
032public class EnvironmentUtils
033{
034
035    private static final DefaultProcessingEnvironment PROCESSING_ENVIRONMENT_IMPLEMENTATION;
036    
037    static {
038//        if (OS.isFamilyOpenVms()) {
039//            PROCESSING_ENVIRONMENT_IMPLEMENTATION = new OpenVmsProcessingEnvironment();
040//        } else {
041            PROCESSING_ENVIRONMENT_IMPLEMENTATION = new DefaultProcessingEnvironment();
042//        }
043    }
044    
045    /**
046     * Disable constructor.
047     */
048    private EnvironmentUtils() {
049
050    }
051
052    /**
053     * Get the variable list as an array.
054     *
055     * @param environment the environment to use, may be {@code null}
056     * @return array of key=value assignment strings or {@code null} if and only if
057     *     the input map was {@code null}
058     */
059    public static String[] toStrings(final Map<String, String> environment) {
060        if (environment == null) {
061            return null;
062        }
063        final String[] result = new String[environment.size()];
064        int i = 0;
065        for (final Entry<String, String> entry : environment.entrySet()) {
066            final String key  = entry.getKey() == null ? "" : entry.getKey().toString();
067            final String value = entry.getValue() == null ? "" : entry.getValue().toString();
068            result[i] = key + "=" + value;
069            i++;
070        }
071        return result;
072    }
073
074    /**
075     * Find the list of environment variables for this process. The returned map preserves
076     * the casing of a variable's name on all platforms but obeys the casing rules of the
077     * current platform during lookup, e.g. key names will be case-insensitive on Windows
078     * platforms.
079     *
080     * @return a map containing the environment variables, may be empty but never {@code null}
081     * @throws IOException the operation failed
082     */
083    public static Map<String, String> getProcEnvironment() throws IOException {
084        return PROCESSING_ENVIRONMENT_IMPLEMENTATION.getProcEnvironment();
085    }
086
087    /**
088     * Add a key/value pair to the given environment.
089     * If the key matches an existing key, the previous setting is replaced.
090     *
091     * @param environment the current environment
092     * @param keyAndValue the key/value pair 
093     */
094    public static void addVariableToEnvironment(final Map<String, String> environment, final String keyAndValue) {
095        final String[] parsedVariable = parseEnvironmentVariable(keyAndValue);        
096        environment.put(parsedVariable[0], parsedVariable[1]);
097    }
098    
099    /**
100     * Split a key/value pair into a String[]. It is assumed
101     * that the ky/value pair contains a '=' character.
102     *
103     * @param keyAndValue the key/value pair
104     * @return a String[] containing the key and value
105     */
106    private static String[] parseEnvironmentVariable(final String keyAndValue) {
107        final int index = keyAndValue.indexOf('=');
108        if (index == -1) {
109            throw new IllegalArgumentException(
110                    "Environment variable for this platform "
111                            + "must contain an equals sign ('=')");
112        }
113
114        final String[] result = new String[2];
115        result[0] = keyAndValue.substring(0, index);
116        result[1] = keyAndValue.substring(index + 1);
117
118        return result;
119    }
120    
121}