1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package org.apache.commons.resources.impl;
25
26 import java.io.FileNotFoundException;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.net.URL;
30 import java.sql.Connection;
31 import java.sql.DriverManager;
32 import java.sql.PreparedStatement;
33 import java.sql.ResultSet;
34 import java.sql.SQLException;
35 import java.util.Locale;
36 import java.util.Map;
37 import java.util.Properties;
38
39 import org.apache.commons.logging.Log;
40 import org.apache.commons.logging.LogFactory;
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79 public class JDBCResources extends CollectionResourcesBase {
80
81
82
83
84 private transient Log log = LogFactory.getLog(JDBCResources.class);
85
86
87
88
89
90
91
92
93
94
95
96 public JDBCResources(String name, String base) {
97 super(name, base);
98 }
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127 protected Map getLocaleMap(String baseUrl, Locale locale) {
128
129 if (getLog().isDebugEnabled()) {
130 getLog().debug("Loading database configuration'" + locale + "' resources from base '" +
131 baseUrl + "'");
132 }
133
134 Properties props = new Properties();
135
136 String name = baseUrl + ".properties";
137 InputStream stream = null;
138
139 try {
140
141
142 if (getLog().isTraceEnabled()) {
143 getLog().trace("Absolute URL is '" + name + "'");
144 }
145 URL url = new URL(name);
146 stream = url.openStream();
147
148
149 if (getLog().isTraceEnabled()) {
150 getLog().trace("Parsing input resource");
151 }
152 props.load(stream);
153
154 } catch (FileNotFoundException e) {
155
156
157 if (getLog().isDebugEnabled()) {
158 getLog().debug("No resources for locale '" + locale +
159 "' from base '" + baseUrl + "'");
160 }
161 props.clear();
162
163 } catch (IOException e) {
164
165 getLog().warn("IOException loading locale '" + locale +
166 "' from base '" + baseUrl + "'", e);
167 props.clear();
168
169 } finally {
170
171
172 if (stream != null) {
173 try {
174 stream.close();
175 } catch (IOException e) {
176 getLog().error("Error closing stream.", e);
177 }
178 stream = null;
179 }
180
181 }
182
183
184 Properties properties = new Properties();
185 try {
186 properties = loadData(locale, props);
187
188 } catch (InstantiationException e) {
189 getLog().warn("InstantiationException: locale= '" + locale +
190 "' base= '" + baseUrl + "'", e);
191 } catch (IllegalAccessException e) {
192 getLog().warn("IllegalAccessException: locale= '" + locale +
193 "' base= '" + baseUrl + "'", e);
194 } catch (ClassNotFoundException e) {
195 getLog().warn("Specified Driver not found, make sure it is on " +
196 "the classpath: locale= '" + locale +
197 "' base= '" + baseUrl + "'", e);
198 } catch (SQLException e) {
199 getLog().warn("SQLException: locale= '" + locale +
200 "' base= '" + baseUrl + "'", e);
201 }
202 return properties;
203
204
205
206 }
207
208
209
210
211
212
213
214 private Properties loadData(Locale locale, Properties connectionProps)
215 throws InstantiationException, IllegalAccessException,
216 ClassNotFoundException, SQLException {
217
218 String driver = connectionProps.getProperty("jdbc.connect.driver");
219 String url = connectionProps.getProperty("jdbc.connect.url");
220 String user = connectionProps.getProperty("jdbc.connect.login");
221 String pass = connectionProps.getProperty("jdbc.connect.password");
222
223 String table = connectionProps.getProperty("jdbc.sql.table");
224 String localeColumn = connectionProps.getProperty("jdbc.sql.locale.column");
225 String keyColumn = connectionProps.getProperty("jdbc.sql.key.column");
226 String valColumn = connectionProps.getProperty("jdbc.sql.val.column");
227 Properties pairs = new Properties();
228
229 Connection con = null;
230 PreparedStatement stmt = null;
231 ResultSet rs = null;
232 try {
233 Class.forName(driver).newInstance();
234 con = DriverManager.getConnection(url, user, pass);
235
236 String query = "SELECT " + keyColumn + ", " + valColumn +
237 " FROM " + table + " where " + localeColumn + "= '" + locale + "'";
238 stmt = con.prepareStatement(query);
239 rs = stmt.executeQuery();
240
241 while (rs.next()) {
242 pairs.put(rs.getString(keyColumn), rs.getString(valColumn));
243 }
244 } finally {
245 try {
246 if (rs != null) {
247 rs.close();
248 }
249 } catch(Exception e) {
250 getLog().warn("Error closing ResultSet: " + e);
251 }
252 try {
253 if (stmt != null) {
254 stmt.close();
255 }
256 } catch(Exception e) {
257 getLog().warn("Error closing Statement: " + e);
258 }
259 try {
260 if (con != null) {
261 con.close();
262 }
263 } catch(Exception e) {
264 getLog().warn("Error closing Connection: " + e);
265 }
266 }
267
268 return pairs;
269 }
270
271
272
273
274
275
276
277
278
279
280
281 private Log getLog() {
282 if (log == null) {
283 log = LogFactory.getLog(JDBCResources.class);
284 }
285 return log;
286 }
287 }