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