From 6025b6016517c6d898d8957d1d7e03ba71431912 Mon Sep 17 00:00:00 2001 From: tknall Date: Fri, 1 Dec 2006 12:20:24 +0000 Subject: Initial import of release 2.2. git-svn-id: https://joinup.ec.europa.eu/svn/pdf-as/trunk@4 7b5415b0-85f9-ee4d-85bd-d5d0c3b42d1c --- .../knowcenter/wag/egov/egiz/cfg/ConfigLogger.java | 71 +++ .../knowcenter/wag/egov/egiz/cfg/PropertyTree.java | 348 +++++++++++ .../at/knowcenter/wag/egov/egiz/cfg/Settings.java | 57 ++ .../wag/egov/egiz/cfg/SettingsReader.java | 683 +++++++++++++++++++++ 4 files changed, 1159 insertions(+) create mode 100644 src/main/java/at/knowcenter/wag/egov/egiz/cfg/ConfigLogger.java create mode 100644 src/main/java/at/knowcenter/wag/egov/egiz/cfg/PropertyTree.java create mode 100644 src/main/java/at/knowcenter/wag/egov/egiz/cfg/Settings.java create mode 100644 src/main/java/at/knowcenter/wag/egov/egiz/cfg/SettingsReader.java (limited to 'src/main/java/at/knowcenter/wag/egov/egiz/cfg') diff --git a/src/main/java/at/knowcenter/wag/egov/egiz/cfg/ConfigLogger.java b/src/main/java/at/knowcenter/wag/egov/egiz/cfg/ConfigLogger.java new file mode 100644 index 0000000..3b32c22 --- /dev/null +++ b/src/main/java/at/knowcenter/wag/egov/egiz/cfg/ConfigLogger.java @@ -0,0 +1,71 @@ +/* + * + * Copyright (c) 2006 by Know-Center, Graz, Austria + * + * + * This software is the confidential and proprietary information of Know-Center, + * Graz, Austria. You shall not disclose such Confidential Information and shall + * use it only in accordance with the terms of the license agreement you entered + * into with Know-Center. + * + * KNOW-CENTER MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE + * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE + * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, + * OR NON-INFRINGEMENT. KNOW-CENTER SHALL NOT BE LIABLE FOR ANY DAMAGES + * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING + * THIS SOFTWARE OR ITS DERIVATIVES. + * + * $Id: ConfigLogger.java,v 1.3 2006/08/30 13:55:50 wprinz Exp $ + */ +package at.knowcenter.wag.egov.egiz.cfg; + +import java.util.ArrayList; + +import org.apache.log4j.Level; +import org.apache.log4j.Logger; + +/** + * This logger class is the main logger class for the pdf-as project. It holds static logger + * instances with could be configured the level with one method. + */ +public class ConfigLogger { + + /** + * The static logger cache. It holds all used logger instances that could be configured by this + * main class. + */ + private static ArrayList logger_ = new ArrayList(); + /** + * This is the Level to use. Default is INFO. + */ + private static Level level_ = Level.INFO; + + /** + * This method activates a new log4j logger instance and store the instance in the local logger + * store. + * + * @param classRef the caller class to be set + * @return a log4j logger instance + * @see Logger + */ + public static Logger getLogger(Class classRef) { + Logger logger = Logger.getLogger(classRef); + //logger.setLevel(level_); + logger_.add(logger); + return logger; + } + + /** + * This method is to set a new logger level for all stored config logger. + * + * @param level the level to set + */ + public static void setLevel(Level level) { + level_ = level; + for (int log_idx = 0; log_idx < logger_.size(); log_idx++) { + Logger logger = (Logger) logger_.get(log_idx); + logger.setLevel(level_); + logger_.set(log_idx, logger); + } + } +} \ No newline at end of file diff --git a/src/main/java/at/knowcenter/wag/egov/egiz/cfg/PropertyTree.java b/src/main/java/at/knowcenter/wag/egov/egiz/cfg/PropertyTree.java new file mode 100644 index 0000000..fd700fe --- /dev/null +++ b/src/main/java/at/knowcenter/wag/egov/egiz/cfg/PropertyTree.java @@ -0,0 +1,348 @@ +/* + * + * Copyright (c) 2006 by Know-Center, Graz, Austria + * + * + * This software is the confidential and proprietary information of Know-Center, + * Graz, Austria. You shall not disclose such Confidential Information and shall + * use it only in accordance with the terms of the license agreement you entered + * into with Know-Center. + * + * KNOW-CENTER MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE + * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE + * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, + * OR NON-INFRINGEMENT. KNOW-CENTER SHALL NOT BE LIABLE FOR ANY DAMAGES + * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING + * THIS SOFTWARE OR ITS DERIVATIVES. + * + * $Id: PropertyTree.java,v 1.4 2006/10/31 08:06:28 wprinz Exp $ + */ +package at.knowcenter.wag.egov.egiz.cfg; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Vector; + +/** + * This class can be used to store a property config tree. The property key are separated by the + * {@link at.knowcenter.wag.egov.egiz.cfg.PropertyTree#SPLIT_STRING}. Therefore the keys an also + * the values of a configuration is stored in nested hashes. The keys in an area are stored in a + * HashMap. The values of a key are stored in a Vector to overload some keys. The property tree can + * be used to extract sub nodes and sub keys of different tree levels. + * + * @author wlackner + * @see java.util.HashMap + * @see java.util.Vector + */ +public class PropertyTree implements Serializable { + + /** + * SVUID. + */ + private static final long serialVersionUID = -1686170519955886222L; + + /** + * The key split string. A key can be a complex key. Sub keys are separated from each other with + * the split string. This string is used to devide the complex key. + */ + public static final String SPLIT_STRING = "\\."; + /** + * Stores the key references to the sub nodes + */ + private Map keys_ = new HashMap(3); + /** + * Stores all values of a node + */ + private Vector values_ = new Vector(3); + + /** + * The default constructor od the class. + */ + public PropertyTree() { + } + + /** + * This method takes a key value tupel and store them in the property tree. The key splitted into + * different levels (splitted by the string + * {@link at.knowcenter.wag.egov.egiz.cfg.PropertyTree#SPLIT_STRING}). All subnodes not stored in + * the tree will be created. The last part of the key (last splitted element) adds the value to + * there own value data structure (Vector).
+ * Example: setKeyValue("key.1_level.2_level","the value for k_1_2")null if the key is not a subtree referece + */ + private PropertyTree getLastSubTree(String splitKey) { + String[] keys = splitKey.split(SPLIT_STRING); + PropertyTree curr_tree = this; + for (int key_idx = 0; key_idx < keys.length; key_idx++) { + String key = keys[key_idx]; + if (!curr_tree.containsNode(key)) { + return null; + } + curr_tree = (PropertyTree) curr_tree.getSubNode(key); + } + return curr_tree; + } + + /** + * This method return the subtree that corresponds to a particular key. The key does not split. + * Therefore the key must be a children of the current node. Search only in the key map of the + * current node. + * + * @param key the key that has to be a sub node + * @return a sub tree (PropertyTree) or null if the key is not a children of the + * current node + */ + private PropertyTree getSubNode(String key) { + return (PropertyTree) keys_.get(key); + } + + /** + * Returns the last value (keys can be overloaded) of a key. The key are splitted into subnodes + * and the last node of the key is the current value holder. If a key or subnode is not in the sub + * tree the return value is null. + * + * @param key the key that holds the value (can be a nested key like "key.1.2.3") + * @return the value of the key (last node of the key) or null otherwise + */ + public String getLastValue(String key) { + PropertyTree curr_tree = getLastSubTree(key); + String result = null; + if (curr_tree != null && !curr_tree.values_.isEmpty()) { + result = (String) curr_tree.values_.lastElement(); + } +// if (logger_.isDebugEnabled()) { +// logger_.debug("getLastValue:" + key + "=" + result); +// } + return result; + } + + /** + * Returns the first value (keys can be overloaded) of a key. The key are splitted into subnodes + * and the last node of the key is the current value holder. If a key or subnode is not in the sub + * tree the return value is null. + * + * @param key the key that holds the value (can be a nested key like "key.1.2.3") + * @return the value of the key (last node of the key) or null otherwise + */ + public String getFirstValue(String key) { + PropertyTree curr_tree = getLastSubTree(key); + String result = null; + if (curr_tree != null && !curr_tree.values_.isEmpty()) { + result = (String) curr_tree.values_.firstElement(); + } +// if (logger_.isDebugEnabled()) { +// logger_.debug("getFirstValue:" + key + "=" + result); +// } + return result; + } + + /** + * This method return all values of the current node. The values are stored as String values. + * + * @return the values (type String) of the current node + * @see Vector + */ + public Vector getValues() { + return values_; + } + + /** + * This method return all keys (sub tree references) of the current node as a Map. The keys are + * stored as String values. + * + * @return the keys (type String) of the current node + * @see Map + */ + public Map getKeyEntries() { + return keys_; + } + + /** + * This method return all keys (sub tree references) of the current node as an ArrayList. The keys + * are stored as String values. + * + * @return the keys (type String) of the current node + * @see ArrayList + */ + public ArrayList getKeys() { + if (!keys_.isEmpty()) { + Object[] objs = keys_.keySet().toArray(); + ArrayList keys = new ArrayList(objs.length); + for (int idx = 0; idx < objs.length; idx++) { + keys.add((String) objs[idx]); + } + return keys; + } + return null; + } + + /** + * + * This method return all sub tree references of a key as an ArrayList. The keys are stored as + * String values. + * + * @param key (can be a nested key like "key.1.2.3") + * @return the keys (type String) of the current node + * @see ArrayList + */ + public ArrayList getKeys(String key) { + PropertyTree curr_tree = getLastSubTree(key); + if (curr_tree != null) { + return curr_tree.getKeys(); + } + return null; + } + + /** + * This method return all values of a key. The values are stored as String values. + * + * @param key (can be a nested key like "key.1.2.3") + * @return the values (type Vector) of the key or null if the key is not in the sub + * tree of the current node + * @see Vector + */ + public Vector getValues(String key) { + PropertyTree curr_tree = getLastSubTree(key); + if (curr_tree != null) { + return curr_tree.values_; + } + return null; + } + + /** + * Store a sub tree (type PropertyTree) in the current node. The key and it's sub tree are stored + * in a HashMap. + * + * @param key the reference of the sub tree + * @param tree the sub tree of the key + * @see HashMap + */ + private void setSubTree(String key, PropertyTree tree) { + if (tree == null) { + tree = new PropertyTree(); + } + keys_.put(key, tree); + } + + /** + * Extracts a sub tree of a nested key. The Method returns the last sub tree of the nested key. + * Example: if the key is like: key.1.2.3 the sub tree of the last + * node 3 is returned. + * + * @param key the reference of the sub tree + * @return a sub tree of the key or null if the key can not be found + */ + public PropertyTree getSubTree(String key) { + return getLastSubTree(key); + } + + /** + * This method checks if a key is a reference to a sub tree in the current node. + * + * @param key a simple key that is a parent reference of a sub tree + * @return true if the key is found, false otherwise + */ + public boolean containsNode(String key) { + return keys_.containsKey(key); + } + + /** + * The default toString method. It starts with the current node recursively downwards and return + * the String representation of the node. + * + * @return the string representation of the node + */ + public String toString() { + return toString("", this); + } + + /** + * This is a helper function to define the prefix for different levels in the toString method, not + * realy nice ;-). + * It replaces all "." chars with " ". + * + * @param key + * @return a replaces prefix string + */ + private static String getEmptyString(String key) { + return key.replaceAll(".", " "); + } + + /** + * This method concatenates all values of the current node and return them as a combinded string. + * + * @param prefix + * @param tree + * @return the string representation of the node values + */ + private static String printValues(String prefix, PropertyTree tree) { + String os = ""; + Iterator values = tree.getValues().iterator(); + while (values.hasNext()) { + String value = (String) values.next(); + os += prefix + "=" + value; + } + return os; + } + + /** + * The toString method. It starts with a special level prefix, sub tree and recursively adds all + * sub trees. + * + * @param prefix the prefix for this node + * @param tree the current node + * @return the string representation of the node + */ + public static String toString(String prefix, PropertyTree tree) { + String os = ""; + Iterator entries = tree.getKeyEntries().entrySet().iterator(); + while (entries.hasNext()) { + Map.Entry entry = (Map.Entry) entries.next(); + String key = (String) entry.getKey(); + PropertyTree sub = (PropertyTree) entry.getValue(); + String os_key = "\n" + prefix + "." + key; + os += printValues(os_key, sub); + String subs = toString(prefix + getEmptyString(key) + " |", sub); + if (subs.length() > 0) { + os += os_key + "|" + subs; + } + } + return os; + } +} \ No newline at end of file diff --git a/src/main/java/at/knowcenter/wag/egov/egiz/cfg/Settings.java b/src/main/java/at/knowcenter/wag/egov/egiz/cfg/Settings.java new file mode 100644 index 0000000..b8eb35e --- /dev/null +++ b/src/main/java/at/knowcenter/wag/egov/egiz/cfg/Settings.java @@ -0,0 +1,57 @@ +/* + * + * Copyright (c) 2006 by Know-Center, Graz, Austria + * + * + * This software is the confidential and proprietary information of Know-Center, + * Graz, Austria. You shall not disclose such Confidential Information and shall + * use it only in accordance with the terms of the license agreement you entered + * into with Know-Center. + * + * KNOW-CENTER MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE + * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE + * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, + * OR NON-INFRINGEMENT. KNOW-CENTER SHALL NOT BE LIABLE FOR ANY DAMAGES + * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING + * THIS SOFTWARE OR ITS DERIVATIVES. + * + * $Id: Settings.java,v 1.2 2006/08/03 07:43:03 wprinz Exp $ + */ + +package at.knowcenter.wag.egov.egiz.cfg; + +import at.knowcenter.wag.egov.egiz.exceptions.SettingNotFoundException; + +/** + * Defines an interface reading a configuration file. + * + * @author wlackner + */ +public interface Settings { + /** + * Search for a key in the configuration file. + * + * @param key to search for + * @return the corresponding value + * @throws SettingNotFoundException if the key is not found + */ + public String getSetting(String key) throws SettingNotFoundException; + + /** + * Search for a key in the configuration file. + * + * @param key to search for + * @param defaultValue return this value if the key is not found + * @return the corresponding value + */ + public String getSetting(String key, String defaultValue); + + /** + * + * @param primaryKey to search for + * @param defaultKey to search for if the primaryKey is not found + * @param defaultValue return this value if the defaultKey is not found + * @return the corresponding value + */ + public String getSetting(String primaryKey, String defaultKey, String defaultValue); +} \ No newline at end of file diff --git a/src/main/java/at/knowcenter/wag/egov/egiz/cfg/SettingsReader.java b/src/main/java/at/knowcenter/wag/egov/egiz/cfg/SettingsReader.java new file mode 100644 index 0000000..a884140 --- /dev/null +++ b/src/main/java/at/knowcenter/wag/egov/egiz/cfg/SettingsReader.java @@ -0,0 +1,683 @@ +/** + * Copyright (c) 2006 by Know-Center, Graz, Austria + * + * This software is the confidential and proprietary information of Know-Center, + * Graz, Austria. You shall not disclose such Confidential Information and shall + * use it only in accordance with the terms of the license agreement you entered + * into with Know-Center. + * + * KNOW-CENTER MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF + * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE + * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR + * NON-INFRINGEMENT. KNOW-CENTER SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY + * LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS + * DERIVATIVES. + * + * $Id: SettingsReader.java,v 1.6 2006/10/31 08:06:36 wprinz Exp $ + */ + +package at.knowcenter.wag.egov.egiz.cfg; + +import java.io.FileInputStream; +import java.io.File; +import java.io.IOException; +import java.io.Serializable; +import java.util.Vector; +import java.util.ArrayList; +import java.util.Enumeration; +import java.util.Properties; + +import org.apache.log4j.Level; +import org.apache.log4j.Logger; + +import at.knowcenter.wag.egov.egiz.exceptions.SettingNotFoundException; +import at.knowcenter.wag.egov.egiz.exceptions.SettingsException; + +/** + * The SettingsReader reads the settings.txt file. The + * settings.txt is a simple java property file that collects all + * parameters used in different modules. + * + * The SettingsReader provides methods to get the property keys and the + * corresponding values. The keys could be defined as combinations of single + * keys. Therefore it is possible to combine differen classes of keys. An + * example could be: + * + *
+ *     
+ *      
+ *       
+ *        
+ *         
+ *          
+ *           
+ *            
+ *             
+ *              
+ *               
+ *                        #SettingNotFoundException
+ *                        error.code.100=Interner Fehler
+ *                        error.code.101=Die Konfigurationsdatei konnte nicht geladen werden
+ *                        
+ *                        #PDFDocumentException
+ *                        error.code.200=Das Dokument konnte nicht geladen werden
+ *                        
+ *                        #SignatureException
+ *                        error.code.300=Die Signatur ist ungültig
+ *                        
+ *                        #NormalizeException
+ *                        error.code.400=Die angegebene Version ist nicht bekannt
+ *                       
+ *                        normalizer.version=V01
+ *                
+ *               
+ *              
+ *             
+ *            
+ *           
+ *          
+ *         
+ *        
+ *       
+ *      
+ * 
+ * + * The internal representation of the example above is: + * + *
+ *     
+ *      
+ *       
+ *        
+ *         
+ *          
+ *           
+ *            
+ *             
+ *              
+ *               
+ *                        .error|
+ *                              |.code|
+ *                              |     |.200=Das Dokument konnte nicht geladen werden
+ *                              |     |.100=Interner Fehler
+ *                              |     |.400=Die angegebene Version ist nicht bekannt
+ *                              |     |.101=Die Konfigurationsdatei konnte nicht geladen werden
+ *                              |     |.300=Die Signatur ist ungültig
+ *                       .normalizer|
+ *                                  |.version=V01
+ *                
+ *               
+ *              
+ *             
+ *            
+ *           
+ *          
+ *         
+ *        
+ *       
+ *      
+ * 
+ * + * @author wlackner + */ +public class SettingsReader implements Settings, Serializable +{ + + /** + * SVUID. + */ + private static final long serialVersionUID = -8754114172766023454L; + + /** + * The system File separator char + */ + private static final String FILE_SEP = System.getProperty("file.separator"); + + // /** + // * The system temp file path + // */ + // private static final String TEMP_FILE_PATH = + // System.getProperty("java.io.tmpdir"); + + /** + * The current user path + */ + private static final String USER_DIR = System.getProperty("user.dir"); + +// /** +// * The home path of the tomcat webaplication +// */ +// private static final String CATALINA_HOME = System.getProperty("catalina.home"); + +// /** +// * The default application name used in templates, settings, jsp's etc. +// */ +// private static final String APPL_NAME = "pdf-as"; + + // private static final String APPL_NAME = "egiz"; + /** + * The config file path postfix + */ + private static final String CFG = "cfg"; + + /** + * The file path postfix where certificates are stored + */ + private static final String CERT = "certificates"; + +// /** +// * The web application path +// */ +// private static final String WEB_APPL_DIR = "webapps" + FILE_SEP + APPL_NAME + FILE_SEP; + + /** + * The path of the resources repository. + * + *

+ * This usually contains sub directories for the templates, the configuration + * files, etc. + *

+ */ + public static String RESOURCES_PATH = null; + + /** + * The path of the configuration directory. + */ + public static String CONFIG_PATH = null; + + /** + * The path of the certificated directory. + */ + public static String CERT_PATH = null; + + /** + * The name of the directory, where temporary files are stored. + */ + protected static String TEMP_DIR_NAME = "pdfastmp"; + + // /** + // * The application config path for the command line tool + // */ + // public static final String APPL_CONFIG_PATH = USER_DIR + FILE_SEP + CFG + + // FILE_SEP; + // + // /** + // * The application config path for the web application + // */ + // public static final String WEB_CONFIG_PATH = CATALINA_HOME + FILE_SEP + + // WEB_APPL_DIR + CFG + FILE_SEP; + // + // /** + // * The certificates path for the command line tool + // */ + // public static final String APPL_CERT_PATH = USER_DIR + FILE_SEP + CERT + + // FILE_SEP; + // + // /** + // * The certificates path for the cweb application + // */ + // public static final String WEB_CERT_PATH = CATALINA_HOME + FILE_SEP + + // WEB_APPL_DIR + CERT + FILE_SEP; + + /** + * The name of the default configuration file. The definition syntax is the + * java property config syntax. + */ + public static final String CONFIG_FILE_DEFAULT_NAME = "config.properties"; + + /** + * The name of the help text configuration file. The definition syntax is the + * java property config syntax. + */ + public static final String HELP_TEXT_FILE_DEFAULT_NAME = "help_text.properties"; + + /** + * The java properties from the settings file. + */ + private Properties properties_ = null; + + /** + * The settings reader instance. Used to make the class singleton. + */ + private static SettingsReader instance_ = null; + + /** + * The reference to the settings file. + */ + private static String settingsFile_ = null; + + /** + * The reference to the property representation of the settings file. + */ + private PropertyTree pTree_ = new PropertyTree(); + + /** + * The logger definition. + */ + private static final Logger logger_ = ConfigLogger.getLogger(SettingsReader.class); + + /** + * Make this constructor private. Use the method + * {@link SettingsReader#getInstance()}to get an instance from this class. + * The only cause to do this is that the definition file should only be read + * once while getting often this instance. The method throws an IOException if + * the settings file could not be read. + * + * @param settingsFile + * load this file, if the settingsFile == null the + * default settings ({@link SettingsReader#CONFIG_FILE_DEFAULT_NAME}) + * file is used + * @throws SettingsException + * if the settings file could not be read + */ + private SettingsReader(String settingsFile) throws SettingsException + { + try + { + String cfg_path = CONFIG_PATH; + properties_ = new Properties(); + if (settingsFile == null) + { + settingsFile = cfg_path + CONFIG_FILE_DEFAULT_NAME; + } + settingsFile_ = settingsFile; + if (logger_.isInfoEnabled()) + { + File file = new File(settingsFile_); + logger_.info("load Settings:" + file.getAbsolutePath()); + // Properties sys_prop = System.getProperties(); + // Enumeration prop_keys = sys_prop.propertyNames(); + // while (prop_keys.hasMoreElements()) { + // String key = (String) prop_keys.nextElement(); + // String value = sys_prop.getProperty(key); + // logger_.info(key + "=" + value); + // } + } + FileInputStream sfs = new FileInputStream(settingsFile_); + properties_.load(sfs); + + Properties help_prop = new Properties(); + FileInputStream hfs = new FileInputStream(cfg_path + HELP_TEXT_FILE_DEFAULT_NAME); + help_prop.load(hfs); + + // load properties from current package! + // properties_.load(getClass().getResourceAsStream(settingsFile_)); + Enumeration prop_keys = properties_.propertyNames(); + while (prop_keys.hasMoreElements()) + { + String key = (String) prop_keys.nextElement(); + String value = properties_.getProperty(key); + pTree_.setKeyValue(key, value); + } + prop_keys = help_prop.propertyNames(); + while (prop_keys.hasMoreElements()) + { + String key = (String) prop_keys.nextElement(); + String value = help_prop.getProperty(key); + properties_.setProperty(key, value); + pTree_.setKeyValue(key, value); + } + } + catch (IOException e) + { + throw new SettingsException(e); + } + } + + /** + * This method returns an synchronized instance of this class. The settings + * file is read only once using this class. This method returns the instance + * holding the definitions of the default settings file. Default file: + * {@link SettingsReader#CONFIG_FILE_DEFAULT_NAME}: "settings.txt" + * + * @return an instance of the SettingsReader + * @throws SettingsException + * if the default settings file could not be read + */ + public synchronized static SettingsReader getInstance() throws SettingsException + { + return getInstance(null); + } + + /** + * This method returns an synchronized instance of this class. The settings + * file is read only once using this class. This method returns the instance + * holding the definitions of the settingsFile. If the input param + * settingsFile == null the default settings file will be load. + * Default file: {@link SettingsReader#CONFIG_FILE_DEFAULT_NAME}: + * "settings.txt" + * + * If an instance of this class exist, the input param is ignored! The + * SettingsReader is singleton and therefore the first + * {@link SettingsReader#getInstance()}defines the settings file that has to + * be loaded. This means changes between a application lifecyle can not be + * done! + * + * @param settingsFile + * the settings file that should be load. + * @return an instance of the SettingsReader + * @throws SettingsException + * if the settings file could not be read + */ + private synchronized static SettingsReader getInstance(String settingsFile) throws SettingsException + { + if (instance_ == null) + { + instance_ = new SettingsReader(settingsFile); + } + return instance_; + } + + /** + * This method returns a property value to the corresponding key. If the key + * is not found in the property file a SettingNotFoundException is thrown. + * + * @param key + * get the value for that key in the property file + * @return the value of the property key. + * @throws SettingNotFoundException + * ErrorCode: 100 + */ + public String getSetting(String key) throws SettingNotFoundException + { + String result = properties_.getProperty(key); + if (result == null) + { + String log_message = "Configuration key not found: '" + key + "'! Check '" + settingsFile_ + "' file."; + if (logger_.isEnabledFor(Level.WARN)) + { + logger_.warn(log_message); + } + SettingNotFoundException snf = new SettingNotFoundException(100, log_message); + throw snf; + } +// if (logger_.isDebugEnabled()) +// { +// logger_.debug("Get Property:" + key + "=" + result); +// } + return result; + } + + /** + * Relocates the relative file. + * + * @param file + * The relative file. + * @return Returns the usable file. + */ + public static String relocateFile(String file) + { + // if (isWeb()) + // { + // return CATALINA_HOME + FILE_SEP + WEB_APPL_DIR + file; + // } + // + // return file; + return RESOURCES_PATH + file; + } + + /** + * This method returns a property value to the corresponding key. If the key + * is not found in the property file the input param defaultValue is returned. + * + * @param key + * get the value for that key in the property file + * @param defaultValue + * the default value if the key is not found + * @return the value of the property key + */ + public String getSetting(String key, String defaultValue) + { + + String result = properties_.getProperty(key); + if (result == null) + { + result = defaultValue; + } +// if (logger_.isDebugEnabled()) +// { +// logger_.debug("Get Property:" + key + "=" + result); +// } + return result; + } + + /** + * This method returns a property value to the corresponding key. If the key + * is not found in the property file the input param defaultKey is searched. + * If the default key is not found the input param defaultValue is returned. + * + * @param primaryKey + * get the value for that key in the property file + * @param defaultKey + * the default key that should be searched if the primaryKey is not + * found + * @param defaultValue + * the default value if the defaultKey is not found + * @return the value of the property key + */ + public String getSetting(String primaryKey, String defaultKey, + String defaultValue) + { + String key = primaryKey; + String result = properties_.getProperty(key); + if (result == null) + { + key = defaultKey; + result = properties_.getProperty(key); + if (result == null) + { + result = defaultValue; + } + } +// if (logger_.isDebugEnabled()) +// { +// logger_.debug("Get Property:" + key + "=" + result); +// } + return result; + } + + /** + * This method returns an array of keys in the same hierarchy of the + * keyPrefix. The method search all keys in the property file that has the + * keyPrefix as leading substring. The Object[] collects all + * sub keys without the keyPrefix. + * + * @param keyPrefix + * to search for sub keys + * @return alls keys starting with the keyPrefix + */ + public Vector getSettingKeys(String keyPrefix) + { + Vector keys = new Vector(); + Enumeration names = properties_.propertyNames(); + while (names.hasMoreElements()) + { + String full_name = (String) names.nextElement(); + if (full_name.indexOf(keyPrefix) == 0) + { + keys.add(full_name.substring(keyPrefix.length() + 1)); + } + } + return keys; + } + + /** + * If a property value is number (interger) this method extracts the value and + * convert it to an int. If the key ist not found or the conversion fails, the + * defaultValue is returned. + * + * @param key + * get the value for that key in the property file + * @param defaultValue + * the default value if the key is not found + * @return the int value of the property key + */ + public int getIntSetting(String key, int defaultValue) + { + int int_property = defaultValue; + String value = null; + try + { + value = getSetting(key); + int_property = Integer.parseInt(value); + } + catch (NumberFormatException e) + { + if (logger_.isEnabledFor(Level.WARN)) + { + logger_.warn("Can not convert " + value + " to int."); + } + } + catch (SettingNotFoundException e) + { + if (logger_.isEnabledFor(Level.WARN)) + { + logger_.warn("Setting " + key + " not found, return default value:" + defaultValue); + } + } + return int_property; + } + + /** + * This method returns an array of sub keys (children references) of the key. + * The method is a wrapper calling the method + * {@link PropertyTree#getKeys(String key)}. + * + * @param key + * get all sub keys for that key in the property file + * @return an list of sub keys (type String) + * @see PropertyTree + */ + public ArrayList getKeys(String key) + { + return pTree_.getKeys(key); + } + + /** + * This method returns a the first value from a key. This means the method + * search in the PropertyTree representation of the config file. The + * PropertyTree class can overload key value paires. But the config file can + * not overload keys. If a key is defined more than one times the last + * definition is stored it the property list. The method is a wrapper calling + * the method {@link PropertyTree#getFirstValue(String key)}. + * + * @param key + * get the value for that key in the property file + * @return the value of the property key + * @see PropertyTree + */ + public String getValueFromKey(String key) + { + String value = pTree_.getFirstValue(key); +// if (logger_.isDebugEnabled()) +// { +// logger_.debug("getValueFromKey:" + key + "=" + value); +// } + return value; + } + + /** + * This method returns the PropertyTree representation of the configuration + * file. + * + * @return Returns the pTree. + * @see PropertyTree + */ + public PropertyTree getPTree() + { + return pTree_; + } + + // /** + // * This method checks the application context. + // * + // * @return true if the application is running in a webinterface, false + // * otherwise + // */ + // public static boolean isWeb() + // { + // return CATALINA_HOME != null; + // } + + /** + * Assembles the File of the temporary directory without checking if it really + * exists. + */ + protected static File assembleTemporaryDirectoryFile() + { + File temp_dir = new File(RESOURCES_PATH + TEMP_DIR_NAME); + return temp_dir; + } + + /** + * Returns the directory where temporary files should be stored. + * + *

+ * If the directory doesn't exist, it is created. + *

+ * + * @return Returns the directory where temporary files should be stored. + */ + public static File getTemporaryDirectory() + { + File temp_dir = assembleTemporaryDirectoryFile(); + if (!temp_dir.exists()) + { + temp_dir.mkdirs(); + } + return temp_dir; + } + + /** + * Deletes all files in the temporary directory, if it exists. + * + *

+ * This should be used to clear temporary files when the application shuts + * down. + *

+ */ + public static void clearTemporaryDirectory() + { + File temp_dir = assembleTemporaryDirectoryFile(); + logger_.info("Clearing temporary directory: " + temp_dir); + + if (!temp_dir.exists()) + { + return; + } + + File[] files = temp_dir.listFiles(); + for (int i = 0; i < files.length; i++) + { + logger_.info(" Clearing temporary file: " + files[i]); + boolean delete_success = files[i].delete(); + if (!delete_success) + { + logger_.error("Couldn't delete the temporary file: " + files[i]); + } + } + } + + /** + * Initializes the paths of the SettingsReader for web application usage. + * + * @param base_dir The base directory of this web application. E.g. TOMCAT_HOME/webapps/pdf-as + */ + public static void initializeForWeb(String base_dir) + { + RESOURCES_PATH = base_dir + FILE_SEP; //CATALINA_HOME + FILE_SEP + WEB_APPL_DIR; + CONFIG_PATH = RESOURCES_PATH + CFG + FILE_SEP; + CERT_PATH = RESOURCES_PATH + CERT + FILE_SEP; + } + + /** + * Initializes the paths of the SettingsReader for commanline usage. + */ + public static void initializeForCommandLine() + { + RESOURCES_PATH = USER_DIR + FILE_SEP; + CONFIG_PATH = RESOURCES_PATH + CFG + FILE_SEP; + CERT_PATH = RESOURCES_PATH + CERT + FILE_SEP; + } + +} \ No newline at end of file -- cgit v1.2.3