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    package org.apache.commons.exec.environment;
019    
020    import java.io.BufferedReader;
021    import java.io.IOException;
022    import java.util.HashMap;
023    import java.util.Map;
024    
025    import org.apache.commons.exec.CommandLine;
026    
027    /**
028     * Helper class to determine the environment variable
029     * for VMS.
030     */
031    public class OpenVmsProcessingEnvironment extends DefaultProcessingEnvironment {
032    
033        /**
034         * Find the list of environment variables for this process.
035         *
036         * @return a map containing the environment variables
037         * @throws IOException the operation failed
038         */    
039        protected Map createProcEnvironment() throws IOException {
040            if (procEnvironment == null) {
041                BufferedReader in = runProcEnvCommand();
042                procEnvironment = addVMSenvironmentVariables(new HashMap(), in);
043            }
044    
045            return procEnvironment;
046        }
047    
048        /**
049         * Determine the OS specific command line to get a list of environment
050         * variables.
051         *
052         * @return the command line
053         */    
054        protected CommandLine getProcEnvCommand() {
055            CommandLine commandLine = new CommandLine("show");
056            commandLine.addArgument("symbol/global"); // the parser assumes symbols are global
057            commandLine.addArgument("*");
058            return commandLine;
059        }
060    
061        /**
062         * This method is VMS specific and used by getProcEnvironment(). Parses VMS
063         * symbols from <code>in</code> and adds them to <code>environment</code>.
064         * <code>in</code> is expected to be the output of "SHOW SYMBOL/GLOBAL *".
065         *
066         * @param environment the current environment
067         * @param in the reader from the process to determine VMS env variables
068         * @return the updated environment
069         * @throws IOException operation failed
070         */
071        private Map addVMSenvironmentVariables(final Map environment,
072                final BufferedReader in) throws IOException {
073            String line;
074            while ((line = in.readLine()) != null) {
075                final String SEP = "=="; // global symbol separator
076                int sepidx = line.indexOf(SEP);
077                if (sepidx > 0){
078                    String name = line.substring(0, sepidx).trim();
079                    String value = line.substring(sepidx+SEP.length()).trim();
080                    value = value.substring(1,value.length()-1); // drop enclosing quotes
081                    environment.put(name,value);
082                }
083            }
084            return environment;
085        }
086    }