WORK AROUND
Name: dbT83986 Date: 12/21/98
Write code to get the current OS, Guess the Current OS command to display the environment into a dataStream, then parse out all the values.
example (work in progress):
/**
$Workfile: OSEnvironment.java $ $Revision: 1.0 $ $Modtime: Sep 01 1998 13:31:46 $
$Log: U:/archives/VCS/VMT/COMM/src/OSEnvironment.java.v $
//
// Rev 1.0 Sep 01 1998 13:31:22 opr5581
// Initial version of new class to replace System.getenv() call.
// This revision works specifically for the AIX environment with the "printenv" commnad located at /usr/bin.
//
Since getenv has been removed...
we need to get our own values from the OS for all existing Environment Variables.
*/
import java.lang.*;
import java.io.*;
import java.util.*;
public class OSEnvironment {
static String[][] envData;
static int envDataSize = 0;
String wholeEnv = "";
OSEnvironment(){
/** TODO: The following command is set for the AIX environment.
In order to make this portable, an OS check and alternate
command will need to be added.
*/
// need to know what type off system we are on an know the 'set' path/command
String command = "/usr/bin/printenv";
//Get our environment into a single string:
try {
Process p = Runtime.getRuntime().exec(command);
DataInputStream commandResult = new DataInputStream(new BufferedInputStream(p.getInputStream()));
String line = null;
try {
while ((line = commandResult.readLine()) != null)
wholeEnv = wholeEnv +"," + line;
if (p.exitValue() != 0) {
wholeEnv="Error=Cannot complete OSEnvironment.class: " + command + ":" + p.exitValue();
}
} catch (Exception e) {
}
} catch (Exception e) {
wholeEnv = "Error Cannot Use OSEnvironment.class: " + command + ":" + e;
}
envData=parseEnv();
}
String[][] parseEnv(){
StringTokenizer envTokenizer = new StringTokenizer(wholeEnv,",",false);
envDataSize=envTokenizer.countTokens();
String singlePair;
String[][] parsedEnv = new String [envDataSize][2];
for (int i=0;i<envDataSize;i++) {
if (envTokenizer.hasMoreTokens()) {
singlePair=envTokenizer.nextToken();
parsedEnv[i][0]=singlePair.substring(0,singlePair.indexOf("="));
parsedEnv[i][1]=singlePair.substring(singlePair.indexOf("=")+1);
}
}
return parsedEnv;
}
String getEnvValue(String envName,String defaultValue){
String valueFound = defaultValue;
for (int i=0;i<envDataSize;i++){
if (envData[i][0].equals(envName)) {
valueFound=envData[i][1];
}
}
return valueFound;
}
}
======================================================================
|
EVALUATION
Many operating systems support the notion of a process environment that maps
identifiers to string values, but no two systems define the contents of this
environment in the same way. Even amongst Unixes there are significant
differences; some define USER as the user's login name, while some define
LOGNAME to have this value, and some (e.g., Linux) define both.
So while we could easily reinstate the getenv() method to provide access to the
process environment, any code that invokes this method would inherently be
dependent upon the operating system(s) upon which it is written and tested.
One of the primary goals of the Java platform is to provide a set of APIs that
work in essentially the same way across a wide variety of operating systems, so
that developers can write portable programs. Providing access to information
that is inherently OS-specific is not consistent with this goal.
-- mr@eng 2001/7/25
Due to overwhelming demand we are going to reconsider this position for
the 1.5 release.
-- ###@###.### 2002/4/30
|