001: /* Licensed to the Apache Software Foundation (ASF) under one or more
002: * contributor license agreements. See the NOTICE file distributed with
003: * this work for additional information regarding copyright ownership.
004: * The ASF licenses this file to You under the Apache License, Version 2.0
005: * (the "License"); you may not use this file except in compliance with
006: * the License. You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.harmony.luni.platform;
018:
019: import java.util.Collections;
020: import java.util.HashMap;
021: import java.util.Map;
022:
023: /**
024: * Environment
025: */
026: public class Environment {
027:
028: private static Map<String, String> envMap = null;
029:
030: /**
031: * Returns a Map of the current environment variables, containing key and
032: * value pairs.
033: *
034: * @return a Map containing the environment variables and their values
035: */
036: public static Map<String, String> getenv() {
037: if (null == envMap) {
038: HashMap<String, String> newEnvMap = new EnvironmentMap();
039: byte[] bytes = getEnvBytes();
040: if (bytes == null) {
041: throw new Error("Failed to get environment variables.");
042: }
043: String[] envStrings = new String(bytes).split("\0");
044: for (int i = 0; i < envStrings.length; i++) {
045: // some "hidden" variable names on Windows start with "="
046: // so we search "=" from the second character
047: int separator = envStrings[i].indexOf("=", 1);
048: newEnvMap.put(envStrings[i].substring(0, separator),
049: envStrings[i].substring(separator + 1));
050: }
051: envMap = Collections.unmodifiableMap(newEnvMap);
052: }
053: return envMap;
054: }
055:
056: /**
057: * Returns a String containing the value of the specified name environment
058: * variable
059: *
060: * @param name -
061: * the environment variable to get the value of
062: *
063: * @return the value of the environment variable specified
064: */
065: public static String getenv(String name) {
066: byte[] env = getEnvByName(name.getBytes());
067: if (null == env) {
068: return null;
069: }
070: return new String(env);
071: }
072:
073: public static class EnvironmentMap extends HashMap<String, String> {
074:
075: private static final long serialVersionUID = 1L;
076:
077: public EnvironmentMap() {
078: super ();
079: }
080:
081: public EnvironmentMap(Map<String, String> map) {
082: super (map);
083: }
084:
085: public boolean containsKey(Object key) {
086: checkParam(key);
087: return super .containsKey(key);
088: }
089:
090: public boolean containsValue(Object value) {
091: checkParam(value);
092: return super .containsValue(value);
093: }
094:
095: public String get(Object key) {
096: checkParam(key);
097: return super .get(key);
098: }
099:
100: public String put(String key, String value) {
101: checkParam(key);
102: checkParam(value);
103: return super .put(key, value);
104: }
105:
106: public String remove(Object key) {
107: checkParam(key);
108: return super .remove(key);
109: }
110:
111: private void checkParam(Object o) {
112: if (null == o) {
113: throw new NullPointerException();
114: }
115: if (!(o instanceof String)) {
116: throw new ClassCastException();
117: }
118: }
119: }
120:
121: private static native byte[] getEnvBytes();
122:
123: private static native byte[] getEnvByName(byte[] name);
124: }
|