org.apache.commons.configuration
Class ConfigurationFactory

java.lang.Object
  extended byorg.apache.commons.configuration.ConfigurationFactory
All Implemented Interfaces:
BasePathLoader

public class ConfigurationFactory
extends java.lang.Object
implements BasePathLoader

Factory class to create a CompositeConfiguration from a .xml file using Digester. By default it can handle the Configurations from commons- configuration. If you need to add your own, then you can pass in your own digester rules to use. It is also namespace aware, by providing a digesterRuleNamespaceURI.

Version:
$Id: ConfigurationFactory.java,v 1.2 2003/12/24 14:28:22 epugh Exp $
Author:
Eric Pugh, Henning P. Schmiedehausen, Oliver Heger

Nested Class Summary
static class ConfigurationFactory.AdditionalConfigurationData
          A simple data class that holds all information about a configuration from the <additional> section.
 class ConfigurationFactory.BasePathConfigurationFactory
          A tiny inner class that allows the Configuration Factory to let the digester construct BasePathConfiguration objects that already have the correct base Path set.
static class ConfigurationFactory.ConfigurationBuilder
          An internally used helper class for constructing the composite configuration object.
 class ConfigurationFactory.DigesterConfigurationFactory
          A base class for digester factory classes.
(package private) static class ConfigurationFactory.HierarchicalConfigurationNodeConverter
          A specialized HierarchicalConfigurationConverter class that creates a HierarchicalConfiguration root node from an arbitrary Configuration object.
 class ConfigurationFactory.JNDIConfigurationFactory
          A tiny inner class that allows the Configuration Factory to let the digester construct JNDIPathConfiguration objects.
 
Field Summary
private  java.lang.String basePath
          The basePath to prefix file paths for file based property files.
private  java.lang.String configurationFileName
          The XML file with the details about the configuration to load
private  java.net.URL configurationURL
          The URL to the XML file with the details about the configuration to load.
private static java.lang.String DEF_BASE_PATH
          Constant for the default base path (points to actual directory).
private  java.lang.String digesterRuleNamespaceURI
          The digester namespace to parse
private  java.net.URL digesterRules
          URL for xml digester rules file
private  java.lang.String impliciteBasePath
          The implicit base path for included files.
private static org.apache.commons.logging.Log log
          static logger
private static java.lang.String METH_LOAD
          Constant for the name of the load method.
private static java.lang.String SEC_ADDITIONAL
          Constant for the additional section.
private static java.lang.String SEC_OVERRIDE
          Constant for the override section.
private static java.lang.String SEC_ROOT
          Constant for the root element in the info file.
 
Constructor Summary
ConfigurationFactory()
          C'tor
ConfigurationFactory(java.lang.String configurationFileName)
          C'tor with ConfigurationFile Name passed
 
Method Summary
private  void configureNamespace(org.apache.commons.digester.Digester digester)
          Configure the current digester to be namespace aware and to have a Configuration object to which all of the other configurations should be added
 java.lang.String getBasePath()
          Returns the Base path from which this Configuration Factory operates.
 Configuration getConfiguration()
          Return the configuration provided by this factory.
 java.lang.String getConfigurationFileName()
          Returns the configurationFile.
 java.net.URL getConfigurationURL()
          Returns the URL of the configuration file to be loaded.
 java.lang.String getDigesterRuleNamespaceURI()
          Returns the digesterRuleNamespaceURI.
 java.net.URL getDigesterRules()
          Returns the digesterRules.
protected  void initDefaultDigesterRules(org.apache.commons.digester.Digester digester)
          Initializes the parsing rules for the default digester This allows the Configuration Factory to understand the default types: Properties, XML and JNDI.
protected  void initDigesterSectionRules(org.apache.commons.digester.Digester digester, java.lang.String matchString, boolean additional)
          Sets up digester rules for a specified section of the configuration info file.
 void setBasePath(java.lang.String basePath)
          Sets the basePath for all file references from this Configuration Factory.
 void setConfigurationFileName(java.lang.String configurationFileName)
          Sets the configurationFile.
 void setConfigurationURL(java.net.URL url)
          Sets the URL of the configuration to load.
 void setDigesterRuleNamespaceURI(java.lang.String digesterRuleNamespaceURI)
          Sets the digesterRuleNamespaceURI.
 void setDigesterRules(java.net.URL digesterRules)
          Sets the digesterRules.
protected  void setupDigesterInstance(org.apache.commons.digester.Digester digester, java.lang.String matchString, org.apache.commons.digester.ObjectCreationFactory factory, java.lang.String method, boolean additional)
          Sets up digester rules for a configuration to be loaded.
protected  void setupUnionRules(org.apache.commons.digester.Digester digester, java.lang.String matchString)
          Sets up rules for configurations in the additional section.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SEC_ROOT

private static final java.lang.String SEC_ROOT
Constant for the root element in the info file.

See Also:
Constant Field Values

SEC_OVERRIDE

private static final java.lang.String SEC_OVERRIDE
Constant for the override section.

See Also:
Constant Field Values

SEC_ADDITIONAL

private static final java.lang.String SEC_ADDITIONAL
Constant for the additional section.

See Also:
Constant Field Values

METH_LOAD

private static final java.lang.String METH_LOAD
Constant for the name of the load method.

See Also:
Constant Field Values

DEF_BASE_PATH

private static final java.lang.String DEF_BASE_PATH
Constant for the default base path (points to actual directory).

See Also:
Constant Field Values

configurationFileName

private java.lang.String configurationFileName
The XML file with the details about the configuration to load


configurationURL

private java.net.URL configurationURL
The URL to the XML file with the details about the configuration to load.


impliciteBasePath

private java.lang.String impliciteBasePath
The implicit base path for included files. This path is determined by the configuration to load and used unless no other base path was explicitely specified.


basePath

private java.lang.String basePath
The basePath to prefix file paths for file based property files.


log

private static org.apache.commons.logging.Log log
static logger


digesterRules

private java.net.URL digesterRules
URL for xml digester rules file


digesterRuleNamespaceURI

private java.lang.String digesterRuleNamespaceURI
The digester namespace to parse

Constructor Detail

ConfigurationFactory

public ConfigurationFactory()
C'tor


ConfigurationFactory

public ConfigurationFactory(java.lang.String configurationFileName)
C'tor with ConfigurationFile Name passed

Parameters:
configurationFileName - The path to the configuration file
Method Detail

getConfiguration

public Configuration getConfiguration()
                               throws java.lang.Exception
Return the configuration provided by this factory. It loads the configuration file which is a XML description of the actual configurations to load. It can contain various different types of configuration, currently Properties, XML and JNDI.

Returns:
A Configuration object
Throws:
java.lang.Exception - A generic exception that we had trouble during the loading of the configuration data.

getConfigurationFileName

public java.lang.String getConfigurationFileName()
Returns the configurationFile.

Returns:
The name of the configuration file. Can be null.

setConfigurationFileName

public void setConfigurationFileName(java.lang.String configurationFileName)
Sets the configurationFile.

Parameters:
configurationFileName - The name of the configurationFile to use.

getConfigurationURL

public java.net.URL getConfigurationURL()
Returns the URL of the configuration file to be loaded.

Returns:
the URL of the configuration to load

setConfigurationURL

public void setConfigurationURL(java.net.URL url)
Sets the URL of the configuration to load. This configuration can be either specified by a file name or by a URL.

Parameters:
url - the URL of the configuration to load

getDigesterRules

public java.net.URL getDigesterRules()
Returns the digesterRules.

Returns:
URL

setDigesterRules

public void setDigesterRules(java.net.URL digesterRules)
Sets the digesterRules.

Parameters:
digesterRules - The digesterRules to set

initDefaultDigesterRules

protected void initDefaultDigesterRules(org.apache.commons.digester.Digester digester)
Initializes the parsing rules for the default digester This allows the Configuration Factory to understand the default types: Properties, XML and JNDI. Two special sections are introduced: <override> and <additional>.

Parameters:
digester - The digester to configure

initDigesterSectionRules

protected void initDigesterSectionRules(org.apache.commons.digester.Digester digester,
                                        java.lang.String matchString,
                                        boolean additional)
Sets up digester rules for a specified section of the configuration info file.

Parameters:
digester - the current digester instance
matchString - specifies the section
additional - a flag if rules for the additional section are to be added

setupDigesterInstance

protected void setupDigesterInstance(org.apache.commons.digester.Digester digester,
                                     java.lang.String matchString,
                                     org.apache.commons.digester.ObjectCreationFactory factory,
                                     java.lang.String method,
                                     boolean additional)
Sets up digester rules for a configuration to be loaded.

Parameters:
digester - the current digester
matchString - the pattern to match with this rule
factory - an ObjectCreationFactory instance to use for creating new objects
method - the name of a method to be called or null for none
additional - a flag if rules for the additional section are to be added

setupUnionRules

protected void setupUnionRules(org.apache.commons.digester.Digester digester,
                               java.lang.String matchString)
Sets up rules for configurations in the additional section.

Parameters:
digester - the current digester
matchString - the pattern to match with this rule

getDigesterRuleNamespaceURI

public java.lang.String getDigesterRuleNamespaceURI()
Returns the digesterRuleNamespaceURI.

Returns:
A String with the digesterRuleNamespaceURI.

setDigesterRuleNamespaceURI

public void setDigesterRuleNamespaceURI(java.lang.String digesterRuleNamespaceURI)
Sets the digesterRuleNamespaceURI.

Parameters:
digesterRuleNamespaceURI - The new digesterRuleNamespaceURI to use

configureNamespace

private void configureNamespace(org.apache.commons.digester.Digester digester)
Configure the current digester to be namespace aware and to have a Configuration object to which all of the other configurations should be added

Parameters:
digester - The Digester to configure

getBasePath

public java.lang.String getBasePath()
Returns the Base path from which this Configuration Factory operates. This is never null. If you set the BasePath to null, then a base path according to the configuration to load is returned.

Specified by:
getBasePath in interface BasePathLoader
Returns:
The base Path of this configuration factory.

setBasePath

public void setBasePath(java.lang.String basePath)
Sets the basePath for all file references from this Configuration Factory. Normally a base path need not to be set because it is determined by the location of the configuration file to load. All relative pathes in this file are resolved relative to this file. Setting a base path makes sense if such relative pathes should be otherwise resolved, e.g. if the configuration file is loaded from the class path and all sub configurations it refers to are stored in a special config directory.

Specified by:
setBasePath in interface BasePathLoader
Parameters:
basePath - The new basePath to set.