📄 environment.java
字号:
/**
* The contents of this file are subject to the OAA Community Research
* 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.ai.sri.com/~oaa/. Software distributed under the License
* is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
* express or implied. See the License for the specific language governing
* rights and limitations under the License. Portions of the software are
* Copyright (c) SRI International, 1999-2003. All rights reserved.
* "OAA" is a registered trademark, and "Open Agent Architecture" is a
* trademark, of SRI International, a California nonprofit public benefit
* corporation.
*/
package com.sri.oaa2.agt.startit;
import java.util.*;
import java.io.*;
public class Environment {
// HostTypes
public static final int UNKNOWN = 1301;
public static final int UNIX = 1302;
public static final int WINDOWS_9x = 1303;
public static final int WINDOWS_ME = 1304;
public static final int WINDOWS_NT = 1305;
public static final int MAC_OS_X = 1306;
static private Properties env = new Properties();
static private int hostType = UNKNOWN;
static private boolean envCached = false;
static private boolean hostCached = false;
// see: http://www.tolstoy.com/samizdat/sysprops.html
// for some dude's list of possible os.name values
public static int getHostType() {
if (hostCached) return hostType;
String OS = System.getProperty("os.name").toLowerCase();
String MAC_ID = "";
if (OS.matches("windows me")) {
hostType = WINDOWS_ME;
}
else if (OS.matches("windows 9.*")) {
hostType = WINDOWS_9x;
}
else if (OS.matches("mac os x")) {
hostType = MAC_OS_X;
}
else if (OS.matches("windows ?(nt|2000|xp|2003).*")) {
hostType = WINDOWS_NT;
}
else if (OS.matches("sunos|solaris|(.*[iu]x)")) { // linux, aix, irix ...
hostType = UNIX;
}
else {
hostType = UNKNOWN;
}
hostCached = true;
return hostType;
}
public static boolean isOSX() {
return isOSX(getHostType());
}
public static boolean isOSX(int type) {
return (type == MAC_OS_X);
}
public static boolean isUnix() {
return isUnix(getHostType());
}
public static boolean isUnix(int type) {
return (type == UNIX);
}
public static boolean isWindows() {
return isWindows(getHostType());
}
public static boolean isWindows(int type) {
return (type == WINDOWS_ME || type == WINDOWS_9x || type == WINDOWS_NT);
}
public static String getUserName() {
if (isUnix() || isOSX()) {
return get("USER");
}
else {
return get("USERNAME");
}
}
public static String get(String key) {
return (String) get().get(key);
}
public static Properties get() {
if (envCached) return env;
//(see http://www.rgagnon.com/javadetails/java-0150.html)
Process p = null;
Runtime r = Runtime.getRuntime();
String line;
try {
switch (getHostType()) {
case WINDOWS_ME:
// Don't try to support Windows Me! It crashes the whole machine HARD!
break;
case WINDOWS_9x:
p = r.exec( "command.com /c set" );
break;
case WINDOWS_NT:
p = r.exec( "cmd.exe /c set" );
break;
case UNIX:
p = r.exec( "env" );
break;
case MAC_OS_X:
p = r.exec( "env" );
break;
default:
// leave p NULL, to get caught in catch clause below
break;
}
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
while((line = br.readLine()) != null) {
String[] keyval = line.split("=", 2);
if (keyval.length >= 2) {
env.put(keyval[0], keyval[1]);
}
}
}
catch (IOException e) {
System.err.println("WARNING: error getting environment variables into java");
System.err.println(" OS = " + System.getProperty("os.name"));
System.err.println(" " + e);
}
catch (NullPointerException e) {
System.err.println("WARNING: environment variables not supported for OS: " + System.getProperty("os.name"));
}
envCached = true;
return env;
}
public static void merge(Properties props, boolean override) {
get(); // ensure we cache system environment
for (Iterator i = props.keySet().iterator(); i.hasNext(); ) {
String key = (String) i.next();
if (override || !env.containsKey(key)) {
env.put(key, props.get(key));
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -