org.ops4j.util.environment
Class Environment

java.lang.Object
  extended by java.util.Dictionary<K,V>
      extended by java.util.Hashtable<java.lang.Object,java.lang.Object>
          extended by java.util.Properties
              extended by org.ops4j.util.environment.Environment
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, java.util.Map<java.lang.Object,java.lang.Object>

public class Environment
extends java.util.Properties

Encapsulates operating system and shell specific access to environment variables.

Version:
$Id: Environment.java 10301 2008-01-26 07:01:32Z adreghiciu@gmail.com $
Author:
Open Particpation Software for Java
See Also:
Serialized Form

Field Summary
static java.lang.String OSNAME
          os.name System property
static java.lang.String USERNAME
          user.name System property
 
Fields inherited from class java.util.Properties
defaults
 
Constructor Summary
Environment()
          Creates a snapshot of the current shell environment variables for a user.
 
Method Summary
static java.io.File getDataDirectory(java.lang.String applicationname)
          Returns the Data directory of the application for the logged in user.
static java.lang.String getEnvVariable(java.lang.String name)
          Gets the value of a shell environment variable.
static java.util.Properties getEnvVariables()
          Gets all environment variables within a Properties instance where the key is the environment variable name and value is the value of the property.
static java.io.File getSystemDirectory(java.lang.String applicationname)
          Returns the System directory of the application for the logged in user.
static java.lang.String getUserShell()
          Gets the user's shell executable.
static boolean isMacOsX()
          Checks to see if the operating system is a UNIX variant.
static boolean isNetWare()
          Checks to see if the operating system is NetWare.
static boolean isOpenVMS()
          Checks to see if the operating system is OpenVMS.
static boolean isUnix()
          Checks to see if the operating system is a UNIX variant.
static boolean isWindows()
          Checks to see if the operating system is a Windows variant.
 
Methods inherited from class java.util.Properties
getProperty, getProperty, list, list, load, loadFromXML, propertyNames, save, setProperty, store, storeToXML, storeToXML
 
Methods inherited from class java.util.Hashtable
clear, clone, contains, containsKey, containsValue, elements, entrySet, equals, get, hashCode, isEmpty, keys, keySet, put, putAll, rehash, remove, size, toString, values
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

OSNAME

public static final java.lang.String OSNAME
os.name System property


USERNAME

public static final java.lang.String USERNAME
user.name System property

Constructor Detail

Environment

public Environment()
            throws EnvironmentException
Creates a snapshot of the current shell environment variables for a user.

Throws:
EnvironmentException - if there is an error accessing the environment
Method Detail

getDataDirectory

public static java.io.File getDataDirectory(java.lang.String applicationname)
Returns the Data directory of the application for the logged in user. On Unix systems, the $HOME/.applicationname (all lower-case) directory will be returned. On Windows systems, $APPDATA/applicationname (with uppercase, first letter) will be returned.

Parameters:
applicationname - The name of the application.
Returns:
The directory where the application can store data.

getSystemDirectory

public static java.io.File getSystemDirectory(java.lang.String applicationname)
                                       throws EnvironmentException
Returns the System directory of the application for the logged in user. This method returns the directory where the application should be, or has been installed, on the system, for the current user. On Unix systems, the following rules are applied;
  1. applicationname is made into lowercase.
  2. application is looked for in the $HOME/applicationname
  3. application is looked for in the /usr/share/applicationname
  4. if the $USER == 'root', then use /usr/share/applicationname
  5. use $HOME/applicationname
On Windows, the PROGRAMFILES environment variable is looked up, if not found, then use "C:\Program Files\" and then append the applicationname with an initial uppercase character.

Parameters:
applicationname - The name of the application.
Returns:
The directory where the application is either installed or can be installed.
Throws:
EnvironmentException - if the current system in not Windows or Unix/Linux.

getEnvVariable

public static java.lang.String getEnvVariable(java.lang.String name)
                                       throws EnvironmentException
Gets the value of a shell environment variable.

Parameters:
name - the name of variable
Returns:
the String representation of an environment variable value
Throws:
EnvironmentException - if there is a problem accessing the environment

isUnix

public static boolean isUnix()
Checks to see if the operating system is a UNIX variant.

Returns:
true of the OS is a UNIX variant, false otherwise

isMacOsX

public static boolean isMacOsX()
Checks to see if the operating system is a UNIX variant.

Returns:
true of the OS is a UNIX variant, false otherwise

isWindows

public static boolean isWindows()
Checks to see if the operating system is a Windows variant.

Returns:
true of the OS is a Windows variant, false otherwise

isNetWare

public static boolean isNetWare()
Checks to see if the operating system is NetWare.

Returns:
true of the OS is NetWare, false otherwise

isOpenVMS

public static boolean isOpenVMS()
Checks to see if the operating system is OpenVMS.

Returns:
true of the OS is a NetWare variant, false otherwise

getEnvVariables

public static java.util.Properties getEnvVariables()
                                            throws EnvironmentException
Gets all environment variables within a Properties instance where the key is the environment variable name and value is the value of the property.

Returns:
the environment variables and values as Properties
Throws:
EnvironmentException - if os is not recognized

getUserShell

public static java.lang.String getUserShell()
                                     throws EnvironmentException
Gets the user's shell executable.

Returns:
the shell executable for the user
Throws:
EnvironmentException - the there is a problem accessing shell information


Copyright © 2006-2009 OPS4J - Open Participation Software for Java. All Rights Reserved.