com.knowgate.misc
Class Environment

java.lang.Object
  extended by com.knowgate.misc.Environment

public class Environment
extends java.lang.Object

Reads and keeps in memory properties from .cnf initialization files.


Field Summary
static java.lang.String DEFAULT_PROFILES_DIR
           
 
Method Summary
static java.lang.String getEnvVar(java.lang.String sVarName)
          Get value for an environment variable.
static java.lang.String getEnvVar(java.lang.String sVarName, java.lang.String sDefault)
          Get value for an environment variable.
static java.util.Properties getProfile(java.lang.String sProfile)
          Get a Properties collection from a .CNF file Property files must be in the directory pointed by a operating system environment variable names KNOWGATE_PROFILES.
static boolean getProfileBool(java.lang.String sProfile, java.lang.String sVarName, boolean bDefault)
          Get the value of a property that represents a boolean type.
static java.lang.String getProfilePath(java.lang.String sProfile, java.lang.String sVarName)
          Get a property from a .CNF file representing a file path.
static java.util.Set getProfilesSet()
          Get a Set with all loaded profiles names
static java.lang.String getProfileVar(java.lang.String sProfile, java.lang.String sVarName)
          Get a single property from a .CNF file.
static java.lang.String getProfileVar(java.lang.String sProfile, java.lang.String sVarName, java.lang.String sDefault)
          Get a single property from a .CNF file.
static java.util.Set getProfileVarSet(java.lang.String sProfile)
          Get names of all properties in a profile
static java.lang.String getTempDir()
          Get temporary directory
static java.util.Properties loadProfile(java.lang.String sProfile)
          Load a Profile from a resource bundle The loaded Profile is cached in memory and will be returned in future calls to getProfile() If profile had been already loaded, then it is overwritten.
static java.util.Properties loadProfile(java.lang.String sProfile, java.lang.String sPath)
          Load a Profile from a Properties file The loaded Profile is cached in memory and will be returned in future calls to getProfile() If profile had been already loaded, then it is overwritten.
static void refresh()
          Refresh in-memory cached properties by re-reading then from disk files.
static java.lang.String resolveEnvironmentVariables(java.lang.String sInput)
          Replace values of environment variables at given string Change all substrings of the form %[A-Z]% to the corresponding environment variables values
For example, in Windows %ProgramFiles% typically maps to "C:\Program Files"
Variable matching is case sensitive
static void setProfileVar(java.lang.String sProfile, java.lang.String sVarName, java.lang.String sVarValue)
          Set value for a profile property Value is change in memory cache but not saved to disk.
static void updateSystemTime(long lTime)
          Update system time This is an alpha testing method, do not use in production environments.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_PROFILES_DIR

public static java.lang.String DEFAULT_PROFILES_DIR
Method Detail

getEnvVar

public static java.lang.String getEnvVar(java.lang.String sVarName)
                                  throws java.lang.IllegalArgumentException

Get value for an environment variable.

This is not a Pure Java method since it uses the Runtime obeject for calling OS specific shell commands.

Parameters:
sVarName - Name of the variable to be readed.
Returns:
Value of variable or null if no environment variable with such name was found.
Throws:
java.lang.IllegalArgumentException - If there is a Malformed \\uxxxx encoding or any other type of intrinsic error at the environment variables values

getEnvVar

public static java.lang.String getEnvVar(java.lang.String sVarName,
                                         java.lang.String sDefault)
                                  throws java.lang.IllegalArgumentException

Get value for an environment variable.

This is not a Pure Java method since it uses the Runtime object for calling OS specific shell commands.

Parameters:
sVarName - Name of the variable to be readed.
Returns:
Value of variable or sDefault if no environment variable with such name was found.
Throws:
java.lang.IllegalArgumentException

getTempDir

public static java.lang.String getTempDir()
                                   throws java.lang.IllegalArgumentException

Get temporary directory

Returns:
For UNIX Sytems this function always return "/tmp/".
For Windows Systems getTempDir() returns the value set at the environment variable "TEMP" or C:\\%WINDIR%\\TEMP\\ if TEMP variable is not set.
Throws:
java.lang.IllegalArgumentException

getProfile

public static java.util.Properties getProfile(java.lang.String sProfile)

Get a Properties collection from a .CNF file

Property files must be in the directory pointed by a operating system environment variable names KNOWGATE_PROFILES. If KNOWGATE_PROFILES environment variable is not found, the files will be seeked by default on C:\WINNT\ or C:\WINDOWS\ on Window Systems and /etc/ on UNIX systems.

Parameters:
sProfile - Properties file to read (for example "hipergate.cnf")
Since:
v2.2 The behaviour of this function has changed: it first tries to get KNOWGATE_PROFILES from Java environment variables as set on startup "java -DKNOWGATE_PROFILES=..." if there is no Java property named KNOWGATE_PROFILES then operating system environment variabled are scanned and last if neither is found the default C:\WINNT\ C:\WINDOWS\ or /etc/ is returned

loadProfile

public static java.util.Properties loadProfile(java.lang.String sProfile,
                                               java.lang.String sPath)

Load a Profile from a Properties file

The loaded Profile is cached in memory and will be returned in future calls to getProfile()

If profile had been already loaded, then it is overwritten.

Parameters:
sProfile - Profile name, for example "hipergate"
sPath - Full path to properties file, fo example "/etc/knowgate/hipergate.cnf"

loadProfile

public static java.util.Properties loadProfile(java.lang.String sProfile)

Load a Profile from a resource bundle

The loaded Profile is cached in memory and will be returned in future calls to getProfile()

If profile had been already loaded, then it is overwritten.

Parameters:
sProfile - Profile name, for example "hipergate"
Since:
5.0

getProfilesSet

public static java.util.Set getProfilesSet()
Get a Set with all loaded profiles names

Returns:
Set of profile names

getProfileVar

public static java.lang.String getProfileVar(java.lang.String sProfile,
                                             java.lang.String sVarName)

Get a single property from a .CNF file.

Properties are readed once from disk and then cached in memory. If .CNF file is changed, refresh() method must be called for refreshing in-memory cached values.

Parameters:
sProfile - .CNF file name
sVarName - Property Name
Returns:
Value of property or null if no property with such name was found.

getProfileVarSet

public static java.util.Set getProfileVarSet(java.lang.String sProfile)
Get names of all properties in a profile

Parameters:
sProfile - Profile Name
Returns:
Set of property names

getProfilePath

public static java.lang.String getProfilePath(java.lang.String sProfile,
                                              java.lang.String sVarName)

Get a property from a .CNF file representing a file path.

This method is equivalent to getProfileVar except that a file separator is always appended to the end of the readed value.

Parameters:
sProfile - .CNF file name
sVarName - Property Name
Returns:
Value terminated with a file separator or null if no property with such name was found.

getProfileBool

public static boolean getProfileBool(java.lang.String sProfile,
                                     java.lang.String sVarName,
                                     boolean bDefault)

Get the value of a property that represents a boolean type.

Parameters:
sProfile - .CNF file name
sVarName - Property Name
bDefault - Default Value
Returns:
If no property named sVarName is found at sProfile then bDefault value is returned. If sVarName is one of {true , yes, on, 1} then return value is true. If sVarName is one of {false, no, off, 0} then return value is false. If sVarName is any other value then then return value is bDefault
Since:
4.0

getProfileVar

public static java.lang.String getProfileVar(java.lang.String sProfile,
                                             java.lang.String sVarName,
                                             java.lang.String sDefault)

Get a single property from a .CNF file.

Parameters:
sProfile - .CNF file name
sVarName - Property Name
sDefault - Default Value
Returns:
Value of property or sDefault if no property with such name was found.

setProfileVar

public static void setProfileVar(java.lang.String sProfile,
                                 java.lang.String sVarName,
                                 java.lang.String sVarValue)

Set value for a profile property

Value is change in memory cache but not saved to disk.

Parameters:
sProfile - Profile Name
sVarName - Property Name
sVarValue - Prioperty Value

refresh

public static void refresh()

Refresh in-memory cached properties by re-reading then from disk files.


resolveEnvironmentVariables

public static java.lang.String resolveEnvironmentVariables(java.lang.String sInput)
                                                    throws java.lang.IllegalArgumentException

Replace values of environment variables at given string

Change all substrings of the form %[A-Z]% to the corresponding environment variables values
For example, in Windows %ProgramFiles% typically maps to "C:\Program Files"
Variable matching is case sensitive

Parameters:
sInput - String
Returns:
String
Throws:
java.lang.IllegalArgumentException - If there is a Malformed \\uxxxx encoding or any other type of intrinsic error at the environment variables values
Since:
3.1

updateSystemTime

public static void updateSystemTime(long lTime)

Update system time

This is an alpha testing method, do not use in production environments.

Parameters:
lTime - New System Date