com.agimatec.commons.config
Class ConfigManager

java.lang.Object
  extended by com.agimatec.commons.config.ConfigManager
All Implemented Interfaces:
java.io.Serializable

public class ConfigManager
extends java.lang.Object
implements java.io.Serializable

Provides central access to configuration files. The singleton can be used (getDefault()). A ConfigManger uses a resource name (file) in the classpath that contains the absolute config root path. If the resource cannot be found, the config manager tries to access a system property with the same name. If such a system property exists, its value is used as the config root path. The default configrootressourcename is "configroot.ini".
ConfigManager.getDefault() sucht eine Resource configroot.ini im Klassenpfad. Es koennen andere ConfigManager instanziert werden, die ggf. eine andere Resource verwenden, wenn man mehrere config-roots haben moechte. Die configroot.ini Datei enthaelt den Pfad, in dem die Datei configroot.xml zu finden ist. Wird die Resource nicht gefunden, so ist config-root das aktuelle Verzeichnis. Im Config-root wird als Einstieg in die Konfigurationen die configDatei configroot.xml gesucht (die muss immer so heissen). In dieser Datei sind nur <file> Tags erlaubt, die auf andere ConfigFiles verweisen und zwar entweder relativ zum configroot (als Default) oder mit absolutem Pfad (relative="false"). Alle anderen Configfiles koennen beliebige Datenstrukturen darstellen: - alle primitiv-Typen als Wrapperklasse oder als Node-Klasse - list, map, ArrayList, HashMap - filenodes auf andere Dateien, die ggf. keine config-dateien mehr sind (so kann auf bel. Resourcen verwiesen werden ohne den JavaSource Pfadabh. zu machen)
Author: Roman Stumm

See Also:
Serialized Form

Field Summary
static java.lang.String C_ProtocolClassPath
           
protected  java.util.Map myConfigMap
           
protected  Config myConfigroot
           
protected  java.lang.String myConfigrootPath
           
protected  java.lang.String myConfigRootRessouceName
           
protected static ConfigManager singleton
           
 
Constructor Summary
ConfigManager(java.lang.String aConfigRootRessouceName)
          Creates a new ConfigManager.
 
Method Summary
 void cacheConfig(Config aConfig, java.lang.String aConfigName)
          put the config into the cache
 void clearCache()
          remove all cached configs from the cache
protected  org.xml.sax.XMLReader createParser()
          initialize lazy and return the receiver's script instance ready to be used.
 Config getCachedConfig(java.lang.String aConfigName)
           
 Config getConfig(java.lang.String configname)
          get or read the config named configname.
 Config getConfig(java.lang.String configname, boolean allowNullReturn)
           
 Config getConfig(java.lang.String aConfigname, java.lang.String aPath)
          return the config with the given name from the cache.
 Config getConfig(java.lang.String aConfigName, java.lang.String aPath, boolean isRelativeFlag)
          return the config with the given name from the cache.
 Config getConfigRoot()
           
 java.lang.String getConfigRootPath()
           
 Config getConfigUncached(java.lang.String configname)
          the same as this#getConfig(java.lang.String) but the config is read from file and not cached.
static ConfigManager getDefault()
           
 Config parseConfig(java.lang.String aConfigXMLString)
           
protected  void parseResource(org.xml.sax.XMLReader aParser, java.lang.String aPath)
          parse either from a file or the classpath
protected  void parseString(org.xml.sax.XMLReader aParser, java.lang.String aConfigXMLString)
          parse xml in the given string
 Config readConfig(java.lang.String aPath)
          open and parse the file name given in path.
 Config readConfig(java.lang.String aPath, boolean isRelativeFlag)
          open and parse the file name given in path.
protected  java.lang.String readConfigrootPath()
           
protected  java.lang.String readConfigrootPath(java.lang.String aResourceName)
           
static java.lang.String readRessource(java.lang.ClassLoader aClassLoader, java.lang.String aResourceName)
           
 void setConfigRootPath(java.lang.String aConfigRootPath)
          directly set the config root path.
static java.net.URL toURL(java.lang.String path)
           
static java.util.List<java.net.URL> toURLs(java.lang.String path)
           
 void uncacheConfig(Config aConfig)
          remove the config from the cache
 java.lang.String writeConfig(Config aConfig)
           
 void writeConfig(Config aConfig, java.io.PrintWriter aPrintWriter)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

C_ProtocolClassPath

public static final java.lang.String C_ProtocolClassPath
See Also:
Constant Field Values

singleton

protected static ConfigManager singleton

myConfigRootRessouceName

protected final java.lang.String myConfigRootRessouceName

myConfigroot

protected Config myConfigroot

myConfigrootPath

protected java.lang.String myConfigrootPath

myConfigMap

protected java.util.Map myConfigMap
Constructor Detail

ConfigManager

public ConfigManager(java.lang.String aConfigRootRessouceName)
Creates a new ConfigManager.

Parameters:
aConfigRootRessouceName - the resource name (file) in the classpath that contains the absolute config root path. If the resource cannot be found, the config manager tries to access a system property with the same name. If such a system property exists, its value is used as the config root path. The default configrootressourcename is "configroot.ini".
Method Detail

getDefault

public static ConfigManager getDefault()
Returns:
the singleton default instance of this class

readConfig

public Config readConfig(java.lang.String aPath,
                         boolean isRelativeFlag)
open and parse the file name given in path. the file is excepted to be an xml file that conforms to the config.dtd


parseConfig

public Config parseConfig(java.lang.String aConfigXMLString)

parseString

protected void parseString(org.xml.sax.XMLReader aParser,
                           java.lang.String aConfigXMLString)
                    throws java.io.IOException,
                           org.xml.sax.SAXException
parse xml in the given string

Parameters:
aParser - - the parse to use
aConfigXMLString - - the xml to parse
Throws:
java.io.IOException
org.xml.sax.SAXException

parseResource

protected void parseResource(org.xml.sax.XMLReader aParser,
                             java.lang.String aPath)
                      throws java.io.IOException,
                             org.xml.sax.SAXException
parse either from a file or the classpath

Throws:
java.io.IOException
org.xml.sax.SAXException

toURL

public static java.net.URL toURL(java.lang.String path)
                          throws java.net.MalformedURLException
Throws:
java.net.MalformedURLException

toURLs

public static java.util.List<java.net.URL> toURLs(java.lang.String path)
                                           throws java.io.IOException
Throws:
java.io.IOException

createParser

protected org.xml.sax.XMLReader createParser()
initialize lazy and return the receiver's script instance ready to be used.


readConfig

public Config readConfig(java.lang.String aPath)
open and parse the file name given in path. the file is excepted to be an xml file that conforms to the config.dtd


writeConfig

public void writeConfig(Config aConfig,
                        java.io.PrintWriter aPrintWriter)
                 throws java.io.IOException
Throws:
java.io.IOException

writeConfig

public java.lang.String writeConfig(Config aConfig)
                             throws java.io.IOException
Throws:
java.io.IOException

getConfig

public Config getConfig(java.lang.String aConfigname,
                        java.lang.String aPath)
return the config with the given name from the cache. if there is no such config read it and cache it.


getConfig

public Config getConfig(java.lang.String aConfigName,
                        java.lang.String aPath,
                        boolean isRelativeFlag)
return the config with the given name from the cache. if there is no such config read it and cache it.

Parameters:
isRelativeFlag - - is true when the path is relative to configroot, so that the absolute path will be created. - is false, when the path should not be modified.

getCachedConfig

public Config getCachedConfig(java.lang.String aConfigName)
Returns:
the config or null, if there is no cached one

getConfig

public Config getConfig(java.lang.String configname)
get or read the config named configname. if the config is not cached yet, read it with the help of configroot.xml and cache afterwards.

Throws:
java.lang.IllegalArgumentException - if the config is unknown

getConfigUncached

public Config getConfigUncached(java.lang.String configname)
the same as this#getConfig(java.lang.String) but the config is read from file and not cached.

Parameters:
configname -
Returns:

getConfig

public Config getConfig(java.lang.String configname,
                        boolean allowNullReturn)

getConfigRoot

public Config getConfigRoot()

cacheConfig

public void cacheConfig(Config aConfig,
                        java.lang.String aConfigName)
put the config into the cache


uncacheConfig

public void uncacheConfig(Config aConfig)
remove the config from the cache


clearCache

public void clearCache()
remove all cached configs from the cache


getConfigRootPath

public java.lang.String getConfigRootPath()

setConfigRootPath

public void setConfigRootPath(java.lang.String aConfigRootPath)
directly set the config root path.


readConfigrootPath

protected java.lang.String readConfigrootPath()

readConfigrootPath

protected final java.lang.String readConfigrootPath(java.lang.String aResourceName)

readRessource

public static java.lang.String readRessource(java.lang.ClassLoader aClassLoader,
                                             java.lang.String aResourceName)
Returns:
the contents of the resource with the given resourcename loaded with the given classloader or null, if not found.


Copyright © 2008-2012. All Rights Reserved.