Recent articles:
Popular archives:
Java: A platform for platforms
Sun's reorg may seem promising to shareholders but it's also a scramble for position. The question now is whether Sun can,
or wants to, maintain its hold on Java technology. Especially with enterprise leaders like SpringSource and RedHat investing
heavily in Java's future as a platform for platforms
Also see:
Discuss: Java: A platform for platforms?
A quick note about terminology: In Java-speak what other people call environment variables we call properties. Java properties are a bit more flexible than the environment variables available in other systems (see the Resources for more information).
Here's a simple bit of code to extract all of the properties currently in the system:
Properties props = System.getProperties();
Enumeration e = props.propertyNames();
while (e.hasMoreElements())
{
key = (String) e.nextElement();
value = props.getProperty (key);
textArea.appendText ("Key :" + key +
": = :" + value + ":\n");
}
The Properties class is basically just a hashtable of pairs of environment variables and their associated values. We just ask the props object for an enumeration of all of its property names and iterate through that list, requesting the appropriate, associated
value.
Unfortunately, if you run the above code in any "secure" environment -- in an applet, for example -- you are quite likely
to encounter a SecurityException. This little annoyance occurs because security folks think (rightfully so) that it's dangerous to show all of the environment
variables, um, I mean properties, because this allows access to confidential information about the end user. So, as a precaution
they decided to have the security manager throw an exception in a secure environment.
Of course, a better approach would have been to change System.getProperties() to recognize that it's running in a secure environment and return only those properties the security folks think applets
should have access to. But no! Not to fear. All is not completely lost.
For applets, we simply ask for each of the individual properties separately rather than requesting the entire properties list at one time.
Here's code that will extract the standard, "safe" information:
String keys [] =
{
"java.vendor", "java.vendor.url",
"java.version", "java.class.version",
"os.name", "os.arch", "os.version",
"file.separator", "path.separator",
"line.separator", "browser",
"browser.vendor", "browser.version"
};
for (int i = 0; i < keys.length; i++)
{
try
{
key = keys[i];
value = System.getProperty (key);
textArea.appendText ("Key :" + key +
": = :" + value + ":\n");
}
catch (SecurityException see) { ; }
}
This code is similar to what we saw earlier, but instead of trying to get all of the properties from the system, we just hard code the list of known safe properties in an array and iterate through the array.
Here's the example applet in action:
The applet code, PropertyLister.java (see Resources), combines both approaches; it first tries to retrieve all properties and if that fails, tries the individual approach. I'll
leave it as an exercise for you to put that code into a utility method that returns a Properties object regardless of the security situation, allowing the rest of your code to ignore the situation (or not) as it chooses.
java.util.Properties http://www.javasoft.com/products/jdk/1.1/docs/api/java.util.Properties.html