com.agimatec.commons.config
Class ConfigManager

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

public class ConfigManager
extends Object
implements 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 String C_ProtocolClassPath
           
protected  Map myConfigMap
           
protected  Config myConfigroot
           
protected  String myConfigrootPath
           
protected  String myConfigRootRessouceName
           
protected static ConfigManager singleton
           
 
Constructor Summary
ConfigManager(String aConfigRootRessouceName)
          Creates a new ConfigManager.
 
Method Summary
 void cacheConfig(Config aConfig, String aConfigName)
          put the config into the cache
 void clearCache()
          remove all cached configs from the cache
protected  XMLReader createParser()
          initialize lazy and return the receiver's script instance ready to be used.
 Config getCachedConfig(String aConfigName)
           
 Config getConfig(String configname)
          get or read the config named configname.
 Config getConfig(String configname, boolean allowNullReturn)
           
 Config getConfig(String aConfigname, String aPath)
          return the config with the given name from the cache.
 Config getConfig(String aConfigName, String aPath, boolean isRelativeFlag)
          return the config with the given name from the cache.
 Config getConfigRoot()
           
 String getConfigRootPath()
           
 Config getConfigUncached(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(String aConfigXMLString)
           
protected  void parseResource(XMLReader aParser, String aPath)
          parse either from a file or the classpath
protected  void parseString(XMLReader aParser, String aConfigXMLString)
          parse xml in the given string
 Config readConfig(String aPath)
          open and parse the file name given in path.
 Config readConfig(String aPath, boolean isRelativeFlag)
          open and parse the file name given in path.
protected  String readConfigrootPath()
           
protected  String readConfigrootPath(String aResourceName)
           
static String readRessource(ClassLoader aClassLoader, String aResourceName)
           
 void setConfigRootPath(String aConfigRootPath)
          directly set the config root path.
static URL toURL(String path)
           
static List<URL> toURLs(String path)
           
 void uncacheConfig(Config aConfig)
          remove the config from the cache
 String writeConfig(Config aConfig)
           
 void writeConfig(Config aConfig, 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 String C_ProtocolClassPath
See Also:
Constant Field Values

singleton

protected static ConfigManager singleton

myConfigRootRessouceName

protected final String myConfigRootRessouceName

myConfigroot

protected Config myConfigroot

myConfigrootPath

protected String myConfigrootPath

myConfigMap

protected Map myConfigMap
Constructor Detail

ConfigManager

public ConfigManager(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(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(String aConfigXMLString)

parseString

protected void parseString(XMLReader aParser,
                           String aConfigXMLString)
                    throws IOException,
                           SAXException
parse xml in the given string

Parameters:
aParser - - the parse to use
aConfigXMLString - - the xml to parse
Throws:
IOException
SAXException

parseResource

protected void parseResource(XMLReader aParser,
                             String aPath)
                      throws IOException,
                             SAXException
parse either from a file or the classpath

Throws:
IOException
SAXException

toURL

public static URL toURL(String path)
                 throws MalformedURLException
Throws:
MalformedURLException

toURLs

public static List<URL> toURLs(String path)
                        throws IOException
Throws:
IOException

createParser

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


readConfig

public Config readConfig(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,
                        PrintWriter aPrintWriter)
                 throws IOException
Throws:
IOException

writeConfig

public String writeConfig(Config aConfig)
                   throws IOException
Throws:
IOException

getConfig

public Config getConfig(String aConfigname,
                        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(String aConfigName,
                        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(String aConfigName)
Returns:
the config or null, if there is no cached one

getConfig

public Config getConfig(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:
IllegalArgumentException - if the config is unknown

getConfigUncached

public Config getConfigUncached(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(String configname,
                        boolean allowNullReturn)

getConfigRoot

public Config getConfigRoot()

cacheConfig

public void cacheConfig(Config aConfig,
                        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 String getConfigRootPath()

setConfigRootPath

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


readConfigrootPath

protected String readConfigrootPath()

readConfigrootPath

protected final String readConfigrootPath(String aResourceName)

readRessource

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


Copyright © 2008-2011. All Rights Reserved.