com.sibvisions.rad.server.config
Class UpToDateConfigFile

java.lang.Object
  extended by com.sibvisions.rad.server.config.UpToDateConfigFile
All Implemented Interfaces:
java.lang.Cloneable
Direct Known Subclasses:
Zone

public class UpToDateConfigFile
extends java.lang.Object
implements java.lang.Cloneable

The UpToDateConfigFile encapsulates the access to a xml configuration file. It's guaranteed that the access to the properties of the file is up-to-date. That means that changes will be detected automatically.


Constructor Summary
UpToDateConfigFile(java.io.File pConfig)
          Creates a new instance of UpToDateConfigFile for a xml configuration file.
UpToDateConfigFile(java.io.File pDirectory, java.lang.String pConfig)
          Creates a new instance of UpToDateConfigFile for a xml configuration file.
 
Method Summary
 java.lang.Object clone()
          
protected  XmlWorker createXmlWorker()
          Creates the default XmlWorker for reading the file.
 java.io.File getFile()
          Gets the configuration file.
 XmlNode getNode(java.lang.String pName)
          Gets the value for a property as xml representation.
 java.util.List<XmlNode> getNodes(java.lang.String pName)
          Gets the value for a property as xml representation.
 java.util.List<java.lang.String> getProperties(java.lang.String pName)
          Gets a list of values for a property which exists more than once.
 java.lang.String getProperty(java.lang.String pName)
          Gets the value of a property from the application configuration file.
 java.lang.String getProperty(java.lang.String pName, java.lang.String pDefault)
          Gets the value of a property from the application configuration file.
 boolean isCloneNodes()
          Gets whether xml nodes should be cloned before returning.
 boolean isFile()
          Gets whether the config is a physical file.
 boolean isSaveImmediate()
          Gets it immediate save of changes is enabled.
 boolean isStream()
          Gets whether the config was read from an external stream.
 boolean isUpdateEnabled()
          Gets whether the up-to-date option is enabled.
 boolean isValid()
          Checks if the configuration file exists.
 boolean isVirtual()
          Gets whether the config is virtual.
protected  void prepareIncludeNodes(XmlNode pNode)
          Prepares the include nodes <include>...
 void reload()
          Discards all changes and loads the configuration file.
protected  java.lang.String replacePlaceholder(java.lang.String pValue, java.util.Properties pImportedProperties, java.util.Properties pSystemProperties, java.util.Map<java.lang.String,java.lang.String> pEnvironmentProperties)
          Replaces all placeholder with the desired properties.
protected  boolean replacePlaceholder(XmlNode pNode, java.util.Properties pImportedProperties, java.util.Properties pSystemProperties, java.util.Map<java.lang.String,java.lang.String> pEnvironmentProperties)
          Replaces all property placeholder ${x.y.z} with the corresponding value in the properties, recursively.
 void save()
          Saves the changes to the file.
 void setCloneNodes(boolean pClone)
          Sets whether xml nodes should be cloned before returning.
protected  void setContent(XmlNode pNode)
          Sets the internal data node.
 void setNode(java.lang.String pName, XmlNode pNode)
          Sets a specific node to the application configuration file.
 void setProperty(java.lang.String pName, java.lang.String pValue)
          Sets the value of a property to the application configuration file.
 void setSaveImmediate(boolean pEnabled)
          Sets the save immediate option.
 void setUpdateEnabled(boolean pEnabled)
          Sets the up-to-date option of the configuration.
protected  void update()
          Loads the configuration from the filesystem.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

UpToDateConfigFile

public UpToDateConfigFile(java.io.File pDirectory,
                          java.lang.String pConfig)
                   throws java.lang.Exception
Creates a new instance of UpToDateConfigFile for a xml configuration file. The configuration will read immediately.

Parameters:
pDirectory - the directory where to find the configuration file
pConfig - the configuration file name
Throws:
java.lang.Exception - if it is not possible to read the configuration from the filesystem or the xml content is not valid

UpToDateConfigFile

public UpToDateConfigFile(java.io.File pConfig)
                   throws java.lang.Exception
Creates a new instance of UpToDateConfigFile for a xml configuration file. The configuration will read immediately.

Parameters:
pConfig - the configuration file
Throws:
java.lang.Exception - if it is not possible to read the configuration from the filesystem or the xml content is not valid
Method Detail

clone

public java.lang.Object clone()
                       throws java.lang.CloneNotSupportedException

Overrides:
clone in class java.lang.Object
Throws:
java.lang.CloneNotSupportedException

getFile

public java.io.File getFile()
Gets the configuration file.

Returns:
the configuration file

isValid

public boolean isValid()
Checks if the configuration file exists.

Returns:
true if the configuration file exists otherwise false

update

protected void update()
               throws java.lang.Exception
Loads the configuration from the filesystem.

Throws:
java.lang.Exception - if it is not possible to read the configuration from the filesystem or the xml content is not valid

setProperty

public void setProperty(java.lang.String pName,
                        java.lang.String pValue)
                 throws java.lang.Exception
Sets the value of a property to the application configuration file.

Parameters:
pName - the property name (e.g. /application/securitymanager/class)
pValue - the value
Throws:
java.lang.Exception - if the configuration is invalid

setNode

public void setNode(java.lang.String pName,
                    XmlNode pNode)
             throws java.lang.Exception
Sets a specific node to the application configuration file.

Parameters:
pName - the property name
pNode - the new node or null to remove the property
Throws:
java.lang.Exception - if the configuration is invalid

getProperty

public java.lang.String getProperty(java.lang.String pName)
                             throws java.lang.Exception
Gets the value of a property from the application configuration file.

Parameters:
pName - the property name (e.g /application/securitymanager/class)
Returns:
the value for the property or null if the property is not available
Throws:
java.lang.Exception - if the configuration is invalid

getProperty

public java.lang.String getProperty(java.lang.String pName,
                                    java.lang.String pDefault)
                             throws java.lang.Exception
Gets the value of a property from the application configuration file.

Parameters:
pName - the property name (e.g /application/securitymanager/class)
pDefault - the default value if the property is not available
Returns:
the value for the property or pDefault if the property is not available
Throws:
java.lang.Exception - if the configuration is invalid

getProperties

public java.util.List<java.lang.String> getProperties(java.lang.String pName)
                                               throws java.lang.Exception
Gets a list of values for a property which exists more than once.

Parameters:
pName - the property name
Returns:
the list of values or null if the property is not available
Throws:
java.lang.Exception - if the configuration is invalid

getNodes

public java.util.List<XmlNode> getNodes(java.lang.String pName)
                                 throws java.lang.Exception
Gets the value for a property as xml representation.

Parameters:
pName - the property name
Returns:
the available xml node(s) or null if the property is not available
Throws:
java.lang.Exception - if the configuration is invalid

getNode

public XmlNode getNode(java.lang.String pName)
                throws java.lang.Exception
Gets the value for a property as xml representation.

Parameters:
pName - the property name
Returns:
the available xml node or null is the property is not available
Throws:
java.lang.Exception - if the configuration is invalid

setUpdateEnabled

public void setUpdateEnabled(boolean pEnabled)
Sets the up-to-date option of the configuration.

Parameters:
pEnabled - true doesn't update the configuration when next accessing; false always keeps the configuration up-to-date

isUpdateEnabled

public boolean isUpdateEnabled()
Gets whether the up-to-date option is enabled.

Returns:
true if the current configuration will be read before accessing a property, false if the configuration won't be read again

setSaveImmediate

public void setSaveImmediate(boolean pEnabled)
Sets the save immediate option. If save immediate is enabled, then changes will be written automatically.

Parameters:
pEnabled - true to enable immediate save changes, false otherwise

isSaveImmediate

public boolean isSaveImmediate()
Gets it immediate save of changes is enabled.

Returns:
true if changes will saved immediate, false otherwise

save

public void save()
          throws java.io.IOException
Saves the changes to the file.

Throws:
java.io.IOException - if the file access failed or the content was modified by <include> declarations

reload

public void reload()
            throws java.lang.Exception
Discards all changes and loads the configuration file.

Throws:
java.lang.Exception - if an exception occurs during loading

createXmlWorker

protected XmlWorker createXmlWorker()
Creates the default XmlWorker for reading the file.

Returns:
the xml worker

setContent

protected void setContent(XmlNode pNode)
Sets the internal data node.

Parameters:
pNode - the data node

isFile

public final boolean isFile()
Gets whether the config is a physical file.

Returns:
true if config is a physical file

isVirtual

public final boolean isVirtual()
Gets whether the config is virtual.

Returns:
true if config is virtual

isStream

public final boolean isStream()
Gets whether the config was read from an external stream.

Returns:
true if config was read from stream

setCloneNodes

public void setCloneNodes(boolean pClone)
Sets whether xml nodes should be cloned before returning.

Parameters:
pClone - true to clone nodes, false otherwise

isCloneNodes

public boolean isCloneNodes()
Gets whether xml nodes should be cloned before returning.

Returns:
true if nodes will be cloned, false otherwise

prepareIncludeNodes

protected void prepareIncludeNodes(XmlNode pNode)
                            throws java.io.IOException
Prepares the include nodes <include>...</include>. The value of the include node is used as file path to load the resource over the classpath or filesystem if it is available. All loaded properties are inserted under the first founded root node. In addition, all property place holders ${x.y.z} are replaced with the loaded values of the properties.

Parameters:
pNode - the node to prepare
Throws:
java.io.IOException - if an error occurred when reading from the property file input stream.

replacePlaceholder

protected boolean replacePlaceholder(XmlNode pNode,
                                     java.util.Properties pImportedProperties,
                                     java.util.Properties pSystemProperties,
                                     java.util.Map<java.lang.String,java.lang.String> pEnvironmentProperties)
Replaces all property placeholder ${x.y.z} with the corresponding value in the properties, recursively.

Parameters:
pNode - the node to replace the property place holders
pImportedProperties - the import properties
pSystemProperties - the system properties
pEnvironmentProperties - the environment properties
Returns:
true if one node value was modified

replacePlaceholder

protected java.lang.String replacePlaceholder(java.lang.String pValue,
                                              java.util.Properties pImportedProperties,
                                              java.util.Properties pSystemProperties,
                                              java.util.Map<java.lang.String,java.lang.String> pEnvironmentProperties)
Replaces all placeholder with the desired properties.

Parameters:
pValue - the value of an node
pImportedProperties - the import properties
pSystemProperties - the system properties
pEnvironmentProperties - the environment properties
Returns:
the value with replaced placeholder(s)


Copyright © 2009 SIB Visions GmbH. All Rights Reserved.