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.Iterator;
023import java.util.Map;
024
025//import org.apache.commons.exec.OS;
026
027/**
028 * Wrapper for environment variables.
029 *
030 * @version $Id: EnvironmentUtils.java 1564205 2014-02-04 05:20:02Z 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</code>
056     * @return array of key=value assignment strings or <code>null</code> if and only if
057     *     the input map was <code>null</code>
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 Iterator<Map.Entry<String, String>> iter = environment.entrySet().iterator(); iter.hasNext();) {
066            final Map.Entry<String, String> entry = iter.next();
067
068            String key  = entry.getKey() == null ? "" : entry.getKey().toString();
069            String value = entry.getValue() == null ? "" : entry.getValue().toString();
070            result[i] = key + "=" + value;
071            i++;
072        }
073        return result;
074    }
075
076    /**
077     * Find the list of environment variables for this process. The returned map preserves
078     * the casing of a variable's name on all platforms but obeys the casing rules of the
079     * current platform during lookup, e.g. key names will be case-insensitive on Windows
080     * platforms.
081     *
082     * @return a map containing the environment variables, may be empty but never <code>null</code>
083     * @throws IOException the operation failed
084     */
085    public static Map<String, String> getProcEnvironment() throws IOException {
086        return PROCESSING_ENVIRONMENT_IMPLEMENTATION.getProcEnvironment();
087    }
088
089    /**
090     * Add a key/value pair to the given environment.
091     * If the key matches an existing key, the previous setting is replaced.
092     *
093     * @param environment the current environment
094     * @param keyAndValue the key/value pair 
095     */
096    public static void addVariableToEnvironment(final Map<String, String> environment, final String keyAndValue) {
097        final String[] parsedVariable = parseEnvironmentVariable(keyAndValue);        
098        environment.put(parsedVariable[0], parsedVariable[1]);
099    }
100    
101    /**
102     * Split a key/value pair into a String[]. It is assumed
103     * that the ky/value pair contains a '=' character.
104     *
105     * @param keyAndValue the key/value pair
106     * @return a String[] containing the key and value
107     */
108    private static String[] parseEnvironmentVariable(final String keyAndValue) {
109        final int index = keyAndValue.indexOf('=');
110        if (index == -1) {
111            throw new IllegalArgumentException(
112                    "Environment variable for this platform "
113                            + "must contain an equals sign ('=')");
114        }
115
116        final String[] result = new String[2];
117        result[0] = keyAndValue.substring(0, index);
118        result[1] = keyAndValue.substring(index + 1);
119
120        return result;
121    }
122    
123}