|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectorg.mycore.common.MCRConfiguration
public class MCRConfiguration
Provides methods to manage and read all configuration properties from the MyCoRe configuration files. The class is implemented using the singleton pattern. Using this class is very easy, here is an example:
// Get a configuration property as a String: String driver = MCRConfiguration.instance().getString( "MCR.JDBC.Driver" ); // Get a configuration property as an int, use 500 as default if not set: int max = MCRConfiguration.instance().getInt( "MCR.Cache.Size", 500 );As you see, the class provides methods to get configuration properties as different data types and allows you to specify defaults. All MyCoRe configuration properties should start with "
MCR." When
instance() is called the first time, the file
mycore.properties is read. It can be located somewhere in the
CLASSPATH, even in a jar or zip file. The properties file may
have a property called MCR.Configuration.Include that
contains a comma-separated list of other configuration files to read
subsequently. The class also reads any Java system properties that
start with "MCR." and that are set when the application
starts. System properties will override properties read from the
configuration files. Furthermore, the name of the main configuration file can
be altered by specifying the system property
MCR.Configuration.File . Here is an example:
java -DMCR.Configuration.File=some_other.properties -DMCR.foo=bar MyCoReSampleProperty values may include the values of other properties, recursively, by referencing the other property. Example:
MCR.Foo1=FooValue MCR.Foo2=Some %MCR.Foo1% more informationThe class also provides methods for listing or saving all properties to an
OutputStream and for reloading all configuration
properties at runtime. This allows servlets that run a long time to re-read
the configuration files when client code tells them to do so, for example.
Using the set methods allows client code to set new
configuration properties or overwrite existing ones with new values. Finally,
applications could also subclass MCRConfiguration to
change or add behavior and use an instance of the subclass instead of this
class. This is transparent for client code, they would still use
MCRConfiguration.instance() to get the subclass instance. To use a
subclass instead of MCRConfiguration itself, specify the
system property MCR.Configuration.Class, e. g.
java -DMCR.Configuration.Class=MCRConfigurationSubclass MyCoReSample
loadFromFile(java.lang.String),
reload(boolean),
list(PrintStream),
store(java.io.OutputStream, java.lang.String)| Field Summary | |
|---|---|
protected Properties |
depr
List of deprecated properties with their new name |
(package private) static File |
lastModifiedFile
|
protected Properties |
properties
The properties instance that stores the values that have been read from every configuration file |
protected static MCRConfiguration |
singleton
The single instance of this class that will be used at runtime |
| Constructor Summary | |
|---|---|
protected |
MCRConfiguration()
Protected constructor to create the singleton instance |
| Method Summary | |
|---|---|
void |
configureLogging()
Configures Log4J based on the log4j properties |
protected static void |
createSingleton()
Instantiates the singleton by calling the protected constructor. |
boolean |
getBoolean(String name)
Returns the configuration property with the specified name as a
boolean value. |
boolean |
getBoolean(String name,
boolean defaultValue)
Returns the configuration property with the specified name as a
boolean value, or returns a given default value if the property
is not set. |
double |
getDouble(String name)
Returns the configuration property with the specified name as a
double value. |
double |
getDouble(String name,
double defaultValue)
Returns the configuration property with the specified name as a
double value, or returns a given default value if the property is
not set. |
float |
getFloat(String name)
Returns the configuration property with the specified name as a
float value. |
float |
getFloat(String name,
float defaultValue)
Returns the configuration property with the specified name as a
float value, or returns a given default value if the property is
not set. |
Object |
getInstanceOf(String name)
Returns a new instance of the class specified in the configuration property with the given name. |
Object |
getInstanceOf(String name,
String defaultname)
Returns a new instance of the class specified in the configuration property with the given name. |
int |
getInt(String name)
Returns the configuration property with the specified name as an
int value. |
int |
getInt(String name,
int defaultValue)
Returns the configuration property with the specified name as an
int value, or returns a given default value if the property is
not set. |
long |
getLong(String name)
Returns the configuration property with the specified name as a
long value. |
long |
getLong(String name,
long defaultValue)
Returns the configuration property with the specified name as a
long value, or returns a given default value if the property is
not set. |
Properties |
getProperties()
Returns all the properties |
Properties |
getProperties(String startsWith)
Returns all the properties beginning with the specified string |
Object |
getSingleInstanceOf(String name)
Returns a instance of the class specified in the configuration property with the given name. |
Object |
getSingleInstanceOf(String name,
String defaultname)
Returns a instance of the class specified in the configuration property with the given name. |
String |
getString(String name)
Returns the configuration property with the specified name as a String. |
String |
getString(String name,
String defaultValue)
Returns the configuration property with the specified name as a String, or returns a given default value if the property is not set. |
long |
getSystemLastModified()
returns the last point in time when the MyCoRe system was last modified. |
static MCRConfiguration |
instance()
Returns the single instance of this class that can be used to read and manage the configuration properties. |
void |
list(PrintStream out)
Lists all configuration properties currently set to a PrintStream. |
void |
list(PrintWriter out)
Lists all configuration properties currently set to a PrintWriter. |
void |
reload(boolean clear)
Reloads all properties from the configuration files. |
void |
set(String name,
boolean value)
Sets the configuration property with the specified name to a new
boolean value. |
void |
set(String name,
double value)
Sets the configuration property with the specified name to a new
double value. |
void |
set(String name,
float value)
Sets the configuration property with the specified name to a new
float value. |
void |
set(String name,
int value)
Sets the configuration property with the specified name to a new
int value. |
void |
set(String name,
long value)
Sets the configuration property with the specified name to a new
long value. |
void |
set(String name,
String value)
Sets the configuration property with the specified name to a new
String value. |
void |
store(OutputStream out,
String header)
Stores all configuration properties currently set to an OutputStream. |
void |
systemModified()
signalize that the system state has changed. |
String |
toString()
Returns a String containing the configuration properties currently set. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Field Detail |
|---|
protected static MCRConfiguration singleton
static File lastModifiedFile
protected Properties properties
protected Properties depr
| Constructor Detail |
|---|
protected MCRConfiguration()
| Method Detail |
|---|
public static MCRConfiguration instance()
MCRConfiguration to be usedprotected static void createSingleton()
MCR.Configuration.Class is set when the
system starts, the class specified in that property will be instantiated
instead. This allows for subclassing MCRConfiguration to
change behaviour and use the subclass instead of MCRConfiguration
.
public final long getSystemLastModified()
System.currentTimeMillis()public final void systemModified()
public void reload(boolean clear)
MCR.Configuration.File is set, the file specified
in this property will be used as main configuration file, otherwise the
default file mycore.properties will be read. If the
parameter clear is true, all properties
currently set will be deleted first, otherwise the properties read from
the configuration files will be added to the properties currently set and
they will overwrite existing properties with the same name.
clear - if true, properties currently set will be deleted first
MCRConfigurationException - if the config files can not be loadedpublic Properties getProperties()
public Properties getProperties(String startsWith)
startsWith - the string all the returned properties start with
public void configureLogging()
public Object getInstanceOf(String name,
String defaultname)
throws MCRConfigurationException
name - the non-null and non-empty name of the configuration property
MCRConfigurationException - if the property is not set or the class can not be loaded or
instantiated
public Object getInstanceOf(String name)
throws MCRConfigurationException
name - the non-null and non-empty name of the configuration property
MCRConfigurationException - if the property is not set or the class can not be loaded or
instantiated
public Object getSingleInstanceOf(String name,
String defaultname)
throws MCRConfigurationException
name - the non-null and non-empty name of the configuration property
MCRConfigurationException - if the property is not set or the class can not be loaded or
instantiatedpublic Object getSingleInstanceOf(String name)
name - non-null and non-empty name of the configuration property
MCRConfigurationException - if the property is not set or the class can not be loaded or
instantiatedpublic String getString(String name)
name - the non-null and non-empty name of the configuration property
MCRConfigurationException - if the property with this name is not set
public String getString(String name,
String defaultValue)
name - the non-null and non-empty name of the configuration propertydefaultValue - the value to return if the configuration property is not set
public int getInt(String name)
throws NumberFormatException
int value.
name - the non-null and non-empty name of the configuration property
int
value
NumberFormatException - if the configuration property is not an int
value
MCRConfigurationException - if the property with this name is not set
public int getInt(String name,
int defaultValue)
throws NumberFormatException
int value, or returns a given default value if the property is
not set.
name - the non-null and non-empty name of the configuration property
/** Returns the configuration property with the specified name
as an int value, or returns a given default
value if the property is not set.defaultValue - the value to return if the configuration property is not set
int
value
NumberFormatException - if the configuration property is set but is not an int
value
public long getLong(String name)
throws NumberFormatException
long value.
name - the non-null and non-empty name of the configuration property
long
value
NumberFormatException - if the configuration property is not a long
value
MCRConfigurationException - if the property with this name is not set
public long getLong(String name,
long defaultValue)
throws NumberFormatException
long value, or returns a given default value if the property is
not set.
name - the non-null and non-empty name of the configuration propertydefaultValue - the value to return if the configuration property is not set
long
value
NumberFormatException - if the configuration property is set but is not a long
value
public float getFloat(String name)
throws NumberFormatException
float value.
name - the non-null and non-empty name of the configuration property
float
value
NumberFormatException - if the configuration property is not a float
value
MCRConfigurationException - if the property with this name is not set
public float getFloat(String name,
float defaultValue)
throws NumberFormatException
float value, or returns a given default value if the property is
not set.
name - the non-null and non-empty name of the configuration propertydefaultValue - the value to return if the configuration property is not set
float
value
NumberFormatException - if the configuration property is set but is not a
float value
public double getDouble(String name)
throws NumberFormatException
double value.
name - the non-null and non-empty name of the configuration property
double
value
NumberFormatException - if the configuration property is not a double
value
MCRConfigurationException - if the property with this name is not set
public double getDouble(String name,
double defaultValue)
throws NumberFormatException
double value, or returns a given default value if the property is
not set.
name - the non-null and non-empty name of the configuration propertydefaultValue - the value to return if the configuration property is not set
double
value
NumberFormatException - if the configuration property is set but is not a
double valuepublic boolean getBoolean(String name)
boolean value.
name - the non-null and non-empty name of the configuration property
true, if and only if the specified property has
the value true
MCRConfigurationException - if the property with this name is not set
public boolean getBoolean(String name,
boolean defaultValue)
boolean value, or returns a given default value if the property
is not set. If the property is set and its value is not true
, then false is returned.
name - the non-null and non-empty name of the configuration propertydefaultValue - the value to return if the configuration property is not set
boolean
value
public void set(String name,
String value)
String value. If the parameter value is
null, the property will be deleted.
name - the non-null and non-empty name of the configuration propertyvalue - the new value of the configuration property, possibly
null
public void set(String name,
int value)
int value.
name - the non-null and non-empty name of the configuration propertyvalue - the new value of the configuration property
public void set(String name,
long value)
long value.
name - the non-null and non-empty name of the configuration propertyvalue - the new value of the configuration property
public void set(String name,
float value)
float value.
name - the non-null and non-empty name of the configuration propertyvalue - the new value of the configuration property
public void set(String name,
double value)
double value.
name - the non-null and non-empty name of the configuration propertyvalue - the new value of the configuration property
public void set(String name,
boolean value)
boolean value.
name - the non-null and non-empty name of the configuration propertyvalue - the new value of the configuration propertypublic void list(PrintStream out)
MCRConfiguration.instance().list( System.out );
out - the PrintStream to list the configuration properties onProperties.list( PrintStream )public void list(PrintWriter out)
out - the PrintWriter to list the configuration properties onProperties.list( PrintWriter )
public void store(OutputStream out,
String header)
throws IOException
out - the OutputStream to write the configuration properties toheader - the header to prepend before writing the list of properties
IOException - if writing to the OutputStream throws an IOException
Properties.store(java.io.OutputStream, java.lang.String)public String toString()
System.out.println( MCRConfiguration.instance() );
toString in class Object
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||