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