public class Environment extends Object implements Cloneable
The environment class mimics the environment variables available in any shell using a hash map of keys/values, the key being the variables names, excepted here they are called parameters.
In addition, this class provides facilities to:
As in any shell, most of the time, the environment is global and accessible
from any part of the system. Here a singleton instance of this class is
created and accessible from anywhere in the JVM using the
getGlobalEnvironment()
method (indeed the singleton instance is
created at its first access). However, it is still possible to create a
private instance of this class for use in a specific part of a program.
To read a file of parameters, simply call the
readParameterFile(String)
method. In the same way, to write a set of
parameters to a file, call the writeParameterFile(String)
method.
The format of the parameter file is given in the description of these
methods.
To read parameters from he command line, call the
readCommandLine(String[])
or
readCommandLine(String[], Collection)
methods. These methods expect
a format for the command line that is described in there respective
documentations.
It is also possible to setup automatically the fields of an arbitrary object,
provided these fields have name that match parameters in this environment. To
do this call the initializeFieldsOf(Object)
method passing the
object to initialise as argument. The object to setup must provide methods of
the form "setThing(Type)" where "Thing" or "thing" is the name of the field
to set and "Type" is one of "int", "long", "float", "double", "String" and
"boolean". For the boolean type, the accepted values meaning true are "true",
"on", "1", and "yes", all other value are considered as false.
initializeFieldsOf(Object)
?Modifier and Type | Field and Description |
---|---|
protected String |
configFileName
Name of the configuration file.
|
protected boolean |
configFileRead
Has the configuration file been read yet?.
|
static Environment |
GLOBAL_ENV
Global environment for the whole JVM.
|
protected boolean |
locked
When locked the environment parameters value still can be changed but it
is no more possible to add new parameters.
|
protected Hashtable<String,String> |
parameters
Set of parameters.
|
Constructor and Description |
---|
Environment() |
Modifier and Type | Method and Description |
---|---|
Environment |
clone()
Generate a new Environment object with a deep copy of the elements this
object.
|
boolean |
getBooleanParameter(String parameter)
Check a parameter expected to be of boolean type.
|
int |
getBooleanParameteri(String parameter)
Check a parameter expected to be of boolean type.
|
static Environment |
getGlobalEnvironment()
Access to the global shared environment for the whole JVM.
|
double |
getNumberParameter(String parameter)
Get the value of a parameter that is expected to be a number.
|
String |
getParameter(String parameter)
Access to a parameter in the environment.
|
int |
getParameterCount()
Returns the number of parameters found in the configuration file.
|
Set<String> |
getParametersKeySet()
Set of all parameter names.
|
boolean |
hasParameter(String parameter)
True if the given paramter exist.
|
void |
initializeFieldsOf(Object object)
Initialize all the fields of the given object whose name correspond to
parameters of this environment.
|
protected void |
initializeFieldsOf(Object object,
Collection<String> fieldList)
Initialize all the fields of the given object that both appear in the
given field list and whose name correspond to parameters of this
environment.
|
void |
initializeFieldsOf(Object object,
String... fieldList)
Initialize all the fields of the given object that both appear in the
given field list and whose name correspond to parameters of this
environment.
|
protected void |
invokeSetMethod(Object object,
Method method,
Class<?>[] types,
String name,
String value) |
boolean |
isLocked()
Is the environment locked?.
|
void |
lockEnvironment(boolean on)
Disallow the addition of new parameters.
|
void |
printParameters()
Print all parameters the stdout.
|
void |
printParameters(PrintStream out)
Print all parameters to the given stream.
|
void |
readCommandLine(String[] args)
Read the parameters from the given command line array.
|
void |
readCommandLine(String[] args,
Collection<String> trashcan)
Read the parameters from the given command line array.
|
protected void |
readConfigFile(String filename,
Collection<String> trashcan)
Internal method that reads a configuration file.
|
protected void |
readConfigurationFile()
Read the default configuration file.
|
void |
readParameterFile(String fileName)
Read a parameter file.
|
void |
setParameter(String parameter,
String value)
Set the value of a parameter.
|
String |
toString() |
void |
writeParameterFile(String fileName)
Save the curent parameters to a file.
|
protected String configFileName
protected boolean configFileRead
protected Hashtable<String,String> parameters
protected boolean locked
public static Environment GLOBAL_ENV
getGlobalEnvironment()
static method is called if this field
was not yet initialized by any other mean.getGlobalEnvironment()
public Environment()
public static Environment getGlobalEnvironment()
public boolean isLocked()
lockEnvironment(boolean)
public String getParameter(String parameter)
parameter
- The parameter name.public boolean hasParameter(String parameter)
parameter
- The parameter name.public boolean getBooleanParameter(String parameter)
parameter
- The parameter name.getBooleanParameteri(String)
public int getBooleanParameteri(String parameter)
parameter
- The parameter name.getBooleanParameter(String)
public double getNumberParameter(String parameter)
parameter
- The parameter name.public int getParameterCount()
public Set<String> getParametersKeySet()
public Environment clone()
public void setParameter(String parameter, String value)
parameter
- The parameter name.value
- The new parameter value.isLocked()
,
lockEnvironment(boolean)
public void lockEnvironment(boolean on)
on
- If true the environment is locked.public void initializeFieldsOf(Object object)
object
- The object to initialize.initializeFieldsOf(Object, String[])
,
initializeFieldsOf(Object, Collection)
public void initializeFieldsOf(Object object, String... fieldList)
initializeFieldsOf(Object)
method
description.object
- The object to initialize.fieldList
- The name of the fields to initialize in the object.initializeFieldsOf(Object)
,
initializeFieldsOf(Object, Collection)
protected void initializeFieldsOf(Object object, Collection<String> fieldList)
initializeFieldsOf(Object)
method
description.object
- The object to initialize.fieldList
- The name of the fields to initialize in the object.initializeFieldsOf(Object)
,
initializeFieldsOf(Object, String[])
protected void invokeSetMethod(Object object, Method method, Class<?>[] types, String name, String value)
public void printParameters(PrintStream out)
out
- The output stream to use.public void printParameters()
public void readCommandLine(String[] args)
readCommandLine(String[], Collection)
method.args
- The command line.public void readCommandLine(String[] args, Collection<String> trashcan)
trashcan
.args
- The command line.trashcan
- Will be filled by the set of unparsed strings (can be
null if these strings can be ignored).protected void readConfigFile(String filename, Collection<String> trashcan)
public void writeParameterFile(String fileName) throws IOException
fileName
- Name of the file to save the config in.IOException
- For any output error on the given file name.protected void readConfigurationFile()
configFileRead
boolean is set to true.configFileName
public void readParameterFile(String fileName) throws IOException
fileName
- Name of the parameter file to read.IOException
- For any error with the given parameter file name.WebARTS Library Licensed Under the GNU - General Public License. Other Libraries licensed under their respective Open Source Licenses