diff flys-aft/src/main/java/de/intevation/db/Statements.java @ 4068:21e49e0a2307

Add infrastructure to load SQL statements for databases. flys-aft/trunk@3389 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 12 Dec 2011 16:57:58 +0000
parents
children a4e79e8e0aa0
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-aft/src/main/java/de/intevation/db/Statements.java	Mon Dec 12 16:57:58 2011 +0000
@@ -0,0 +1,98 @@
+package de.intevation.db;
+
+import java.util.Properties;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import org.apache.log4j.Logger;
+
+public class Statements
+{
+    private static Logger log = Logger.getLogger(Statements.class);
+
+    public static final String RESOURCE_PATH = "/sql/";
+    public static final String COMMON_PROPERTIES = "-common.properties";
+
+    protected String type;
+    protected String driver;
+
+    protected Properties properties;
+
+    public Statements(String type, String driver) {
+        this.type   = type;
+        this.driver = driver;
+    }
+
+    protected String driverToProperties() {
+        return
+            type + "-" + 
+            driver.replace('.', '-').toLowerCase() + ".properties";
+    }
+
+    protected Properties loadCommon() {
+        Properties common = new Properties();
+
+        String path = RESOURCE_PATH + type + COMMON_PROPERTIES;
+
+        InputStream in = Statements.class.getResourceAsStream(path);
+
+        if (in != null) {
+            try {
+                common.load(in);
+            }
+            catch (IOException ioe) {
+                log.error("cannot load defaults: " + path, ioe);
+            }
+            finally {
+                try {
+                    in.close();
+                }
+                catch (IOException ioe) {
+                }
+            }
+        }
+        else {
+            log.warn("cannot find: " + path);
+        }
+
+        return common;
+    }
+
+    protected Properties getProperties() {
+
+        if (properties != null) {
+            return properties;
+        }
+
+        Properties common = loadCommon();
+
+        properties = new Properties(common);
+
+        String path = RESOURCE_PATH + driverToProperties();
+
+        InputStream in = Statements.class.getResourceAsStream(path);
+
+        if (in != null) {
+            try {
+                properties.load(in);
+            }
+            catch (IOException ioe) {
+                log.error("cannot load statements: " + path, ioe);
+            }
+            finally {
+                try {
+                    in.close();
+                }
+                catch (IOException ioe) {
+                }
+            }
+        }
+        else {
+            log.warn("cannot find: " + path);
+        }
+
+        return properties;
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org