001    /*
002     * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003     *
004     * Copyright (c) 1997-2010 Oracle and/or its affiliates. All rights reserved.
005     *
006     * The contents of this file are subject to the terms of either the GNU
007     * General Public License Version 2 only ("GPL") or the Common Development
008     * and Distribution License("CDDL") (collectively, the "License").  You
009     * may not use this file except in compliance with the License.  You can
010     * obtain a copy of the License at
011     * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
012     * or packager/legal/LICENSE.txt.  See the License for the specific
013     * language governing permissions and limitations under the License.
014     *
015     * When distributing the software, include this License Header Notice in each
016     * file and include the License file at packager/legal/LICENSE.txt.
017     *
018     * GPL Classpath Exception:
019     * Oracle designates this particular file as subject to the "Classpath"
020     * exception as provided by Oracle in the GPL Version 2 section of the License
021     * file that accompanied this code.
022     *
023     * Modifications:
024     * If applicable, add the following below the License Header, with the fields
025     * enclosed by brackets [] replaced by your own identifying information:
026     * "Portions Copyright [year] [name of copyright owner]"
027     *
028     * Contributor(s):
029     * If you wish your version of this file to be governed by only the CDDL or
030     * only the GPL Version 2, indicate your decision by adding "[Contributor]
031     * elects to include this software in this distribution under the [CDDL or GPL
032     * Version 2] license."  If you don't indicate a single choice of license, a
033     * recipient has the option to distribute your version of this file under
034     * either the CDDL, the GPL Version 2 or to extend the choice of license to
035     * its licensees as provided above.  However, if you add GPL Version 2 code
036     * and therefore, elected the GPL Version 2 license, then the option applies
037     * only if the new code is made subject to such option by the copyright
038     * holder.
039     */
040    
041    package com.sun.enterprise.admin.cli;
042    
043    import java.util.*;
044    
045    /**
046     * The environment variables for CLI commands.  An instance of this class
047     * is passed to each command to give it access to environment variables.
048     * Command implementations should access environment information from
049     * this class rather than using System.getenv.  In multimode, the export
050     * command may change environment variables in the instance of this class
051     * that is shared by all commands.
052     *
053     * @author Bill Shannon
054     */
055    public final class Environment {
056        // XXX - should Environment just extend HashMap?
057    
058        public static final String AS_ADMIN_ENV_PREFIX = "AS_ADMIN_";
059    
060        private Map<String, String> env = new HashMap<String, String>();
061    
062        /**
063         * Initialize the enviroment with all relevant system environment entries.
064         */
065        public Environment() {
066            this(false);
067        }
068    
069        /**
070         * Constructor that ignores the system environment,
071         * mostly used to enable repeatable tests.
072         */
073        public Environment(boolean ignoreEnvironment) {
074            if (ignoreEnvironment)
075                return;
076            // initialize it with all relevant system environment entries
077            for (Map.Entry<String, String> e : System.getenv().entrySet()) {
078                if (e.getKey().startsWith(AS_ADMIN_ENV_PREFIX)) {
079                    env.put(e.getKey().toUpperCase(Locale.ENGLISH), e.getValue());
080                }
081            }
082        }
083    
084        /**
085         * Return the value of the environment entry corresponding
086         * to the named option.
087         *
088         * @param name the option name
089         * @return the value of the corresponding environment entry
090         */
091        public boolean getBooleanOption(String name) {
092            return Boolean.parseBoolean(env.get(optionToEnv(name)));
093        }
094    
095        /**
096         * Return the value of the environment entry corresponding
097         * to the named option.
098         *
099         * @param name the option name
100         * @return the value of the corresponding environment entry
101         */
102        public String getStringOption(String name) {
103            return env.get(optionToEnv(name));
104        }
105    
106        /**
107         * Is there an environment entry corresponding to the named option?
108         *
109         * @param name the option name
110         * @return true if there's a corresponding environment entry
111         */
112        public boolean hasOption(String name) {
113            return env.containsKey(optionToEnv(name));
114        }
115    
116        /**
117         * Get the named environment entry.
118         *
119         * @param name the name of the environment entry
120         * @return the value of the entry, or null if no such entry
121         */
122        public String get(String name) {
123            return env.get(name);
124        }
125    
126        /**
127         * Set the named environment entry to the specified value.
128         *
129         * @param name the environment entry name
130         * @param value the value
131         * @return the previous value of the entry
132         */
133        public String put(String name, String value) {
134            return env.put(name, value);
135        }
136    
137        /**
138         * Remove the name environment entry.
139         *
140         * @param name the environment entry name
141         */
142        public void remove(String name) {
143            env.remove(name);
144        }
145    
146        /**
147         * Set the environment entry corresponding to the named option
148         * to the specified value.
149         *
150         * @param name the option name
151         * @param value the value
152         * @return the previous value of the entry
153         */
154        public String putOption(String name, String value) {
155            return env.put(optionToEnv(name), value);
156        }
157    
158        /**
159         * Return a set of all the entries, just like a Map does.
160         */
161        public Set<Map.Entry<String, String>> entrySet() {
162            return env.entrySet();
163        }
164    
165        /**
166         * Convert an option name (e.g., "host")
167         * to an environment variable name (e.g., AS_ADMIN_HOST).
168         *
169         * @param name the option name
170         * @return the environment variable name
171         */
172        private String optionToEnv(String name) {
173            return AS_ADMIN_ENV_PREFIX +
174                name.replace('-', '_').toUpperCase(Locale.ENGLISH);
175        }
176    }