changeset 3:d7b4a77ba892

Infrastructur for Providing Applicationsettings gnv/trunk@72 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Tim Englich <tim.englich@intevation.de>
date Fri, 11 Sep 2009 15:19:52 +0000
parents 5e94403971af
children 72951b955f00
files gnv/Changelog gnv/src/main/java/de/intevation/gnv/propertiesreader/MapPropertiesReader.java gnv/src/main/java/de/intevation/gnv/propertiesreader/PropertiesReader.java gnv/src/main/java/de/intevation/gnv/propertiesreader/PropertiesReaderFactory.java gnv/src/main/java/de/intevation/gnv/propertiesreader/ServletPropertiesReader.java
diffstat 5 files changed, 195 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/gnv/Changelog	Fri Sep 11 15:17:38 2009 +0000
+++ b/gnv/Changelog	Fri Sep 11 15:19:52 2009 +0000
@@ -1,3 +1,12 @@
+2009-09-11  Tim Englich  <tim.englich@intevation.de>
+
+    * src/main/java/de/intevation/gnv/propertiesreader/ServletPropertiesReader.java Added, 
+      src/main/java/de/intevation/gnv/propertiesreader/PropertiesReaderFactory.java Added, 
+      src/main/java/de/intevation/gnv/propertiesreader/PropertiesReader.java Added, 
+      src/main/java/de/intevation/gnv/propertiesreader/MapPropertiesReader.java Added:
+      Infrastructur for Reading Properties from a Map or SercletConfig an provide them
+      to the whole Application.
+
 2009-09-11  Tim Englich  <tim.englich@intevation.de>
 
     * src/main/java/de/intevation/gnv/artifactdatabase/objects/ArtifactObject.java Added, 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gnv/src/main/java/de/intevation/gnv/propertiesreader/MapPropertiesReader.java	Fri Sep 11 15:19:52 2009 +0000
@@ -0,0 +1,40 @@
+/**
+ *
+ */
+package de.intevation.gnv.propertiesreader;
+
+import java.util.Map;
+
+import org.apache.log4j.Logger;
+
+/**
+ * @author Tim Englich <tim.englich@intevation.de>
+ *
+ */
+public class MapPropertiesReader implements PropertiesReader {
+    /**
+     * the logger, used to log exceptions and additonaly information
+     */
+    private static Logger log = Logger.getLogger(MapPropertiesReader.class);
+
+    private Map<String,String> properties = null;
+    /**
+     * Constructor
+     */
+    public MapPropertiesReader(Map<String, String> properties) {
+        super();
+        this.properties = properties;
+    }
+
+    /**
+     * @see de.intevation.gnv.propertiesreader.PropertiesReader#getPropertieValue(java.lang.String, java.lang.String)
+     */
+    public String getPropertieValue(String key, String defaultValue) {
+        String value = this.properties.get(key);
+        if (value == null){
+            value = defaultValue;
+        }
+        return value;
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gnv/src/main/java/de/intevation/gnv/propertiesreader/PropertiesReader.java	Fri Sep 11 15:19:52 2009 +0000
@@ -0,0 +1,13 @@
+/**
+ *
+ */
+package de.intevation.gnv.propertiesreader;
+/**
+ * @author Tim Englich <tim.englich@intevation.de>
+ *
+ */
+public interface PropertiesReader {
+    
+    public String getPropertieValue(String key, String defaultValue);
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gnv/src/main/java/de/intevation/gnv/propertiesreader/PropertiesReaderFactory.java	Fri Sep 11 15:19:52 2009 +0000
@@ -0,0 +1,77 @@
+/**
+ *
+ */
+package de.intevation.gnv.propertiesreader;
+
+import java.util.Map;
+
+import javax.servlet.ServletConfig;
+
+import org.apache.log4j.Logger;
+
+/**
+ * @author Tim Englich <tim.englich@intevation.de>
+ *
+ */
+public class PropertiesReaderFactory {
+
+
+    
+    /**
+     * the logger, used to log exceptions and additonaly information
+     */
+    private static Logger log = Logger.getLogger(PropertiesReaderFactory.class);
+
+    
+    /**
+     * The singleton Instance of this Factory.
+     */
+    private static PropertiesReaderFactory instance = null;
+
+    /**
+     * The ConnectionPool providing the Connections to the DatabaseBackends
+     */
+    private PropertiesReader propertiesReader = null;
+    /**
+     * Constructor
+     */
+    private PropertiesReaderFactory() {
+        super();
+    }
+
+    /**
+     * This Method provides an singleton Instance of this Class.
+     * @return an singleton Instance of this Class
+     */
+    public static PropertiesReaderFactory getInstance(){
+        if (instance == null){
+            instance = null;
+        }
+        return instance;
+    }
+    
+    
+    /**
+     * Getting the ConnectionPool
+     * @return the ConnectionPool
+     */
+    public PropertiesReader getPropertiesReader(){
+        return this.propertiesReader;
+    }
+    
+    /**
+     * Getting the ConnectionPool
+     * @param config The Config that should be read
+     */
+    public void initPropertiesReader(Object config){
+        if (config instanceof ServletConfig){
+            this.propertiesReader = new ServletPropertiesReader((ServletConfig)config);
+        }else if (config instanceof Map){
+            this.propertiesReader = new MapPropertiesReader((Map<String, String>)config);
+        }else{
+            log.error("No PropertiesReader for Instance "+ config.getClass().getName());
+        }
+        
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gnv/src/main/java/de/intevation/gnv/propertiesreader/ServletPropertiesReader.java	Fri Sep 11 15:19:52 2009 +0000
@@ -0,0 +1,56 @@
+/**
+ *
+ */
+package de.intevation.gnv.propertiesreader;
+
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.servlet.ServletConfig;
+
+import org.apache.log4j.Logger;
+
+/**
+ * @author Tim Englich <tim.englich@intevation.de>
+ *
+ */
+public class ServletPropertiesReader implements PropertiesReader {
+
+    /**
+     * the logger, used to log exceptions and additonaly information
+     */
+    private static Logger log = Logger.getLogger(ServletPropertiesReader.class);
+
+    private Map<String,String> properties = null;
+    /**
+     * Constructor
+     */
+    public ServletPropertiesReader(ServletConfig config) {
+        super();
+        log.info("ServletPropertiesReader will be initialized");
+        if (config != null){
+            
+            Enumeration<String> keys = config.getInitParameterNames();
+            properties = new HashMap<String, String>();
+            while(keys.hasMoreElements()){
+                String key = keys.nextElement();
+                String value = config.getInitParameter(key);
+                log.info("New ConbfigurationValue; "+ key + " ==> "+ value);
+                this.properties.put(key, value);
+            }
+        }
+    }
+
+    /**
+     * @see de.intevation.gnv.propertiesreader.PropertiesReader#getPropertieValue(java.lang.String, java.lang.String)
+     */
+    public String getPropertieValue(String key, String defaultValue) {
+        String value = this.properties.get(key);
+        if (value == null){
+            value = defaultValue;
+        }
+        return value;
+    }
+
+}

http://dive4elements.wald.intevation.org