All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.ymate.platform.commons.util.SystemEnvUtils Maven / Gradle / Ivy

There is a newer version: 1.1
Show newest version
/*
 * Copyright 2007-2107 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.ymate.platform.commons.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.SystemUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 

* SystemEnvUtils *

*

* 系统环境变量工具类 *

* * @author 刘镇([email protected]) * @version 0.0.0 * * * * * * * * * * * *
版本号动作修改人修改时间
0.0.0创建类刘镇2010-8-2上午10:11:43
*/ public class SystemEnvUtils { private static final Log _LOG = LogFactory.getLog(SystemEnvUtils.class); /** * 系统环境变量映射 */ private static final Map SYSTEM_ENV_MAP = new HashMap(); static { initSystemEnvs(); } /** * 私有构造器, 防止被实例化 */ private SystemEnvUtils() { } /** * 初始化系统环境,获取当前系统环境变量 */ public static void initSystemEnvs() { Process p = null; try { if (SystemUtils.IS_OS_WINDOWS) { p = Runtime.getRuntime().exec("cmd /c set"); } else if (SystemUtils.IS_OS_UNIX) { p = Runtime.getRuntime().exec("/bin/sh -c set"); } else { _LOG.warn("Unknown os.name=" + SystemUtils.OS_NAME); SYSTEM_ENV_MAP.clear(); } if (p != null) { BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream())); String line; while ((line = br.readLine()) != null) { int i = line.indexOf("="); if (i > -1) { String key = line.substring(0, i); String value = line.substring(i + 1); SYSTEM_ENV_MAP.put(key, value); } } } } catch (IOException e) { _LOG.warn(RuntimeUtils.unwrapThrow(e)); } } /** * 获取系统运行时,可以进行缓存 * * @return 环境变量对应表 */ public static Map getSystemEnvs() { if (SYSTEM_ENV_MAP.isEmpty()) { initSystemEnvs(); } return SYSTEM_ENV_MAP; } /** * 获取指定名称的环境值 * * @param envName 环境名,如果为空,返回null * @return 当指定名称为空或者对应名称环境变量不存在时返回空 */ public static String getSystemEnv(String envName) { if (StringUtils.isNotBlank(envName)) { if (SYSTEM_ENV_MAP.isEmpty()) { initSystemEnvs(); } return SYSTEM_ENV_MAP.get(envName); } return null; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy