comparison 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
comparison
equal deleted inserted replaced
4067:d5aed044ca0d 4068:21e49e0a2307
1 package de.intevation.db;
2
3 import java.util.Properties;
4
5 import java.io.IOException;
6 import java.io.InputStream;
7
8 import org.apache.log4j.Logger;
9
10 public class Statements
11 {
12 private static Logger log = Logger.getLogger(Statements.class);
13
14 public static final String RESOURCE_PATH = "/sql/";
15 public static final String COMMON_PROPERTIES = "-common.properties";
16
17 protected String type;
18 protected String driver;
19
20 protected Properties properties;
21
22 public Statements(String type, String driver) {
23 this.type = type;
24 this.driver = driver;
25 }
26
27 protected String driverToProperties() {
28 return
29 type + "-" +
30 driver.replace('.', '-').toLowerCase() + ".properties";
31 }
32
33 protected Properties loadCommon() {
34 Properties common = new Properties();
35
36 String path = RESOURCE_PATH + type + COMMON_PROPERTIES;
37
38 InputStream in = Statements.class.getResourceAsStream(path);
39
40 if (in != null) {
41 try {
42 common.load(in);
43 }
44 catch (IOException ioe) {
45 log.error("cannot load defaults: " + path, ioe);
46 }
47 finally {
48 try {
49 in.close();
50 }
51 catch (IOException ioe) {
52 }
53 }
54 }
55 else {
56 log.warn("cannot find: " + path);
57 }
58
59 return common;
60 }
61
62 protected Properties getProperties() {
63
64 if (properties != null) {
65 return properties;
66 }
67
68 Properties common = loadCommon();
69
70 properties = new Properties(common);
71
72 String path = RESOURCE_PATH + driverToProperties();
73
74 InputStream in = Statements.class.getResourceAsStream(path);
75
76 if (in != null) {
77 try {
78 properties.load(in);
79 }
80 catch (IOException ioe) {
81 log.error("cannot load statements: " + path, ioe);
82 }
83 finally {
84 try {
85 in.close();
86 }
87 catch (IOException ioe) {
88 }
89 }
90 }
91 else {
92 log.warn("cannot find: " + path);
93 }
94
95 return properties;
96 }
97 }
98 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org