Mercurial > dive4elements > river
comparison flys-aft/src/main/java/org/dive4elements/river/etl/db/Statements.java @ 5825:f529495f901d
moved directories to org.dive4elements.river.etl
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Thu, 25 Apr 2013 11:42:54 +0200 |
parents | flys-aft/src/main/java/org/dive4elements/etl/db/Statements.java@06643e440d1e |
children | 9438e9259213 |
comparison
equal
deleted
inserted
replaced
5824:06643e440d1e | 5825:f529495f901d |
---|---|
1 package de.intevation.db; | |
2 | |
3 import java.io.IOException; | |
4 import java.io.InputStream; | |
5 | |
6 import java.util.Enumeration; | |
7 import java.util.HashMap; | |
8 import java.util.Map; | |
9 import java.util.Properties; | |
10 | |
11 import org.apache.log4j.Logger; | |
12 | |
13 public class Statements | |
14 { | |
15 private static Logger log = Logger.getLogger(Statements.class); | |
16 | |
17 public static final String RESOURCE_PATH = "/sql/"; | |
18 public static final String COMMON_PROPERTIES = "-common.properties"; | |
19 | |
20 protected String type; | |
21 protected String driver; | |
22 | |
23 protected Map<String, SymbolicStatement> statements; | |
24 | |
25 public Statements(String type, String driver) { | |
26 this.type = type; | |
27 this.driver = driver; | |
28 } | |
29 | |
30 public SymbolicStatement getStatement(String key) { | |
31 return getStatements().get(key); | |
32 } | |
33 | |
34 public Map<String, SymbolicStatement> getStatements() { | |
35 if (statements == null) { | |
36 statements = loadStatements(); | |
37 } | |
38 return statements; | |
39 } | |
40 | |
41 protected Map<String, SymbolicStatement> loadStatements() { | |
42 Map<String, SymbolicStatement> statements = | |
43 new HashMap<String, SymbolicStatement>(); | |
44 | |
45 Properties properties = loadProperties(); | |
46 | |
47 for (Enumeration<?> e = properties.propertyNames(); e.hasMoreElements();) { | |
48 String key = (String)e.nextElement(); | |
49 String value = properties.getProperty(key); | |
50 SymbolicStatement symbolic = new SymbolicStatement(value); | |
51 statements.put(key, symbolic); | |
52 } | |
53 | |
54 return statements; | |
55 } | |
56 | |
57 protected String driverToProperties() { | |
58 return | |
59 type + "-" + | |
60 driver.replace('.', '-').toLowerCase() + ".properties"; | |
61 } | |
62 | |
63 protected Properties loadCommon() { | |
64 Properties common = new Properties(); | |
65 | |
66 String path = RESOURCE_PATH + type + COMMON_PROPERTIES; | |
67 | |
68 InputStream in = Statements.class.getResourceAsStream(path); | |
69 | |
70 if (in != null) { | |
71 try { | |
72 common.load(in); | |
73 } | |
74 catch (IOException ioe) { | |
75 log.error("cannot load defaults: " + path, ioe); | |
76 } | |
77 finally { | |
78 try { | |
79 in.close(); | |
80 } | |
81 catch (IOException ioe) { | |
82 } | |
83 } | |
84 } | |
85 else { | |
86 log.warn("cannot find: " + path); | |
87 } | |
88 | |
89 return common; | |
90 } | |
91 | |
92 protected Properties loadProperties() { | |
93 | |
94 Properties common = loadCommon(); | |
95 | |
96 Properties properties = new Properties(common); | |
97 | |
98 String path = RESOURCE_PATH + driverToProperties(); | |
99 | |
100 InputStream in = Statements.class.getResourceAsStream(path); | |
101 | |
102 if (in != null) { | |
103 try { | |
104 properties.load(in); | |
105 } | |
106 catch (IOException ioe) { | |
107 log.error("cannot load statements: " + path, ioe); | |
108 } | |
109 finally { | |
110 try { | |
111 in.close(); | |
112 } | |
113 catch (IOException ioe) { | |
114 } | |
115 } | |
116 } | |
117 else { | |
118 log.warn("cannot find: " + path); | |
119 } | |
120 | |
121 return properties; | |
122 } | |
123 } | |
124 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |