📄 environmentvariables.java
字号:
/**
* Copyright (c) 2003-2005 Craig Setera
* All Rights Reserved.
* Licensed under the Eclipse Public License - v 1.0
* For more information see http://www.eclipse.org/legal/epl-v10.html
*/
package eclipseme.core.internal.utils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import org.eclipse.core.runtime.Platform;
/**
* Wrapper around the environment variables queried from
* the underlying operating system.
* <p />
* Copyright (c) 2003-2005 Craig Setera<br>
* All Rights Reserved.<br>
* Licensed under the Eclipse Public License - v 1.0<p/>
* <br>
* $Revision: 1.5 $
* <br>
* $Date: 2005/10/02 22:37:47 $
* <br>
* @author Craig Setera
*/
public class EnvironmentVariables {
// The properties instance used to track the values
private Properties variables;
// The map of upper-cased property name to property name as found
private Map nameMap;
/**
* Construct a new instance.
*
* @throws IOException
*/
public EnvironmentVariables()
throws IOException
{
super();
variables = new Properties();
nameMap = new HashMap();
retrieveEnvironmentVariables();
}
/**
* Return a boolean indicating whether or not the environment
* variables contains a variable of the specified name.
*
* @param name
* @return
*/
public boolean containsVariable(String name) {
return getVariable(name) != null;
}
/**
* Convert the environment variables into an array of Strings.
*
* @return
*/
public String[] convertToStrings()
{
String[] strings = new String[variables.size()];
int index = 0;
Iterator entries = variables.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry entry = (Map.Entry) entries.next();
strings[index++] = entry.getKey() + "=" + entry.getValue();
}
return strings;
}
/**
* Return the variable value or <code>null</code> searching in
* a case-insensitive way.
*
* @param name
* @return
*/
public String getVariable(String name) {
// Handle things in a case insensitive manner...
String caseSpecificName = (String) nameMap.get(name.toUpperCase());
return (caseSpecificName == null) ? null : (String) variables.get(caseSpecificName);
}
/**
* Set the specified environment variable to the specified
* value. This does not set the variable into the operating
* system, only into this holder class.
*
* @param name
* @param value
*/
public void setVariable(String name, String value) {
// Handle things in a case insensitive manner...
String caseSpecificName = (String) nameMap.get(name.toUpperCase());
if (caseSpecificName == null) {
caseSpecificName = name;
nameMap.put(caseSpecificName, caseSpecificName);
}
variables.setProperty(caseSpecificName, value);
}
/**
* Retrieve the environment variables.
*
* @return
* @throws IOException
*/
private void retrieveEnvironmentVariables()
throws IOException
{
// Launch a command to gather the environment variables
String envVarsCommand = getEnvironmentVariablesCommand();
Process process = Runtime.getRuntime().exec(envVarsCommand);
InputStreamReader isr = new InputStreamReader(process.getInputStream());
BufferedReader br = new BufferedReader(isr);
// Parse the resulting properties
String line = null;
while ((line = br.readLine()) != null) {
int equalsIndex = line.indexOf('=');
if (equalsIndex != -1) {
String key = line.substring(0, equalsIndex);
String value = line.substring(equalsIndex + 1);
variables.setProperty(key, value);
nameMap.put(key.toUpperCase(), key);
}
}
}
/**
* Get the command to be executed to retrieve the enviroment
* variables.
*
* @return
*/
private String getEnvironmentVariablesCommand() {
String command = null;
// NOTE: I'm assuming that we don't support Win95 or Win98 anymore...
// if (os.indexOf("windows 9") > -1) {
// command = "command.com /c set";
if (Platform.getOS().equals(Platform.OS_WIN32)) {
command = "cmd.exe /c set";
} else {
command = "env";
}
return command;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -