|
1 |
| package org.jconfig.handler; |
|
2 |
| |
|
3 |
| import java.io.IOException; |
|
4 |
| import java.io.InputStream; |
|
5 |
| import java.sql.Connection; |
|
6 |
| import java.sql.DriverManager; |
|
7 |
| import java.sql.PreparedStatement; |
|
8 |
| import java.sql.ResultSet; |
|
9 |
| import java.sql.SQLException; |
|
10 |
| import java.util.Enumeration; |
|
11 |
| import java.util.Hashtable; |
|
12 |
| import java.util.Map; |
|
13 |
| import java.util.Properties; |
|
14 |
| import java.util.Set; |
|
15 |
| |
|
16 |
| import org.jconfig.Category; |
|
17 |
| import org.jconfig.Configuration; |
|
18 |
| import org.jconfig.ConfigurationManagerException; |
|
19 |
| import org.jconfig.DefaultConfiguration; |
|
20 |
| import org.jconfig.error.ErrorReporter; |
|
21 |
| import org.jconfig.utils.ResourceLocator; |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
| |
|
40 |
| |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
| public class JDBCHandler implements ConfigurationHandler { |
|
55 |
| |
|
56 |
| private String driver; |
|
57 |
| |
|
58 |
| private String jdbcurl; |
|
59 |
| |
|
60 |
| private String user; |
|
61 |
| |
|
62 |
| private String pwd; |
|
63 |
| |
|
64 |
| private Connection con; |
|
65 |
| |
|
66 |
| |
|
67 |
| |
|
68 |
| |
|
69 |
12
| public Configuration load(String configurationName) throws ConfigurationManagerException {
|
|
70 |
12
| if(configurationName == null) {
|
|
71 |
1
| ErrorReporter.getErrorHandler().reportError("Configuration name cannot be <null>");
|
|
72 |
1
| throw new ConfigurationManagerException("Configuration name cannot be <null>");
|
|
73 |
| } |
|
74 |
11
| if(configurationName.length() == 0) {
|
|
75 |
1
| ErrorReporter.getErrorHandler().reportError("Configuration name not valid. Empty String not allowed");
|
|
76 |
1
| throw new ConfigurationManagerException("Configuration name not valid. Empty String not allowed");
|
|
77 |
| } |
|
78 |
10
| setupJDBC();
|
|
79 |
| |
|
80 |
10
| return loadConfiguration(configurationName);
|
|
81 |
| } |
|
82 |
| |
|
83 |
| |
|
84 |
13
| private void setupJDBC() throws ConfigurationManagerException {
|
|
85 |
13
| loadJDBCProperties();
|
|
86 |
13
| loadJDBCDriver();
|
|
87 |
13
| con = JDBCLogin();
|
|
88 |
| } |
|
89 |
| |
|
90 |
| |
|
91 |
| |
|
92 |
| |
|
93 |
| |
|
94 |
10
| private Configuration loadConfiguration(String configurationName) throws ConfigurationManagerException {
|
|
95 |
10
| Configuration config = new DefaultConfiguration(configurationName);
|
|
96 |
10
| try {
|
|
97 |
10
| PreparedStatement pstmt = con.prepareStatement("SELECT OID FROM T_CONFIGURATION WHERE NAME = ?");
|
|
98 |
10
| pstmt.setString(1, configurationName);
|
|
99 |
10
| ResultSet result = pstmt.executeQuery();
|
|
100 |
10
| while(result.next()) {
|
|
101 |
10
| long oid = result.getLong("OID");
|
|
102 |
10
| loadCategories(config, oid);
|
|
103 |
10
| loadVariables(config, oid);
|
|
104 |
| } |
|
105 |
10
| result.close();
|
|
106 |
10
| pstmt.close();
|
|
107 |
10
| con.close();
|
|
108 |
| } catch (SQLException e) { |
|
109 |
0
| ErrorReporter.getErrorHandler().reportError("Problem retrieving Configuration from database",e);
|
|
110 |
0
| throw new ConfigurationManagerException("Problem retrieving Configuration from database. " + e.getMessage());
|
|
111 |
| } |
|
112 |
10
| return config;
|
|
113 |
| } |
|
114 |
| |
|
115 |
| |
|
116 |
| |
|
117 |
| |
|
118 |
10
| private void loadCategories(Configuration config, long oid) throws ConfigurationManagerException {
|
|
119 |
10
| try {
|
|
120 |
10
| PreparedStatement pstmt = con.prepareStatement("SELECT OID, NAME FROM T_CATEGORY WHERE CONFIGURATION_OID = ?");
|
|
121 |
10
| pstmt.setLong(1, oid);
|
|
122 |
10
| ResultSet result = pstmt.executeQuery();
|
|
123 |
10
| while(result.next()) {
|
|
124 |
21
| long catoid = result.getLong("OID");
|
|
125 |
21
| String categoryName = result.getString("NAME");
|
|
126 |
21
| Category cat = config.getCategory(categoryName);
|
|
127 |
21
| loadProperties(cat, catoid);
|
|
128 |
| } |
|
129 |
10
| result.close();
|
|
130 |
10
| pstmt.close();
|
|
131 |
| } catch (SQLException e) { |
|
132 |
0
| ErrorReporter.getErrorHandler().reportError("Problem retrieving categories",e);
|
|
133 |
0
| throw new ConfigurationManagerException("Problem retrieving categories. " + e.getMessage());
|
|
134 |
| } |
|
135 |
| |
|
136 |
| } |
|
137 |
| |
|
138 |
| |
|
139 |
| |
|
140 |
3
| private Hashtable readOldCategories(long oid) throws ConfigurationManagerException {
|
|
141 |
3
| Hashtable oldCategories = new Hashtable();
|
|
142 |
3
| try {
|
|
143 |
3
| PreparedStatement pstmt = con.prepareStatement("SELECT OID, NAME FROM T_CATEGORY WHERE CONFIGURATION_OID = ?");
|
|
144 |
3
| pstmt.setLong(1, oid);
|
|
145 |
3
| ResultSet result = pstmt.executeQuery();
|
|
146 |
3
| while(result.next()) {
|
|
147 |
6
| Long catoid = new Long(result.getLong("OID"));
|
|
148 |
6
| String categoryName = result.getString("NAME");
|
|
149 |
6
| oldCategories.put(catoid,categoryName);
|
|
150 |
| } |
|
151 |
3
| result.close();
|
|
152 |
3
| pstmt.close();
|
|
153 |
| } catch (SQLException e) { |
|
154 |
0
| ErrorReporter.getErrorHandler().reportError("Problem retrieving categories",e);
|
|
155 |
0
| throw new ConfigurationManagerException("Problem retrieving categories. " + e.getMessage());
|
|
156 |
| } |
|
157 |
3
| return oldCategories;
|
|
158 |
| |
|
159 |
| } |
|
160 |
| |
|
161 |
| |
|
162 |
| |
|
163 |
| |
|
164 |
10
| private void loadVariables(Configuration config, long oid) throws ConfigurationManagerException {
|
|
165 |
10
| try {
|
|
166 |
10
| PreparedStatement pstmt = con.prepareStatement("SELECT OID, NAME, VALUE FROM T_VARIABLE WHERE CONFIGURATION_OID = ?");
|
|
167 |
10
| pstmt.setLong(1, oid);
|
|
168 |
10
| ResultSet result = pstmt.executeQuery();
|
|
169 |
10
| while(result.next()) {
|
|
170 |
39
| long catoid = result.getLong("OID");
|
|
171 |
39
| String variableName = result.getString("NAME");
|
|
172 |
39
| String variableValue = result.getString("VALUE");
|
|
173 |
39
| config.setVariable(variableName, variableValue);
|
|
174 |
| } |
|
175 |
10
| result.close();
|
|
176 |
10
| pstmt.close();
|
|
177 |
| } catch (SQLException e) { |
|
178 |
0
| ErrorReporter.getErrorHandler().reportError("Problem retrieving variables",e);
|
|
179 |
0
| throw new ConfigurationManagerException("Problem retrieving variables. " + e.getMessage());
|
|
180 |
| } |
|
181 |
| } |
|
182 |
| |
|
183 |
| |
|
184 |
| |
|
185 |
| |
|
186 |
| |
|
187 |
| |
|
188 |
3
| private Hashtable readOldVariables(long oid) throws ConfigurationManagerException {
|
|
189 |
3
| Hashtable oldVariables = new Hashtable();
|
|
190 |
3
| try {
|
|
191 |
3
| PreparedStatement pstmt = con.prepareStatement("SELECT OID, NAME, VALUE FROM T_VARIABLE WHERE CONFIGURATION_OID = ?");
|
|
192 |
3
| pstmt.setLong(1, oid);
|
|
193 |
3
| ResultSet result = pstmt.executeQuery();
|
|
194 |
3
| while(result.next()) {
|
|
195 |
12
| long catoid = result.getLong("OID");
|
|
196 |
12
| String variableName = result.getString("NAME");
|
|
197 |
12
| oldVariables.put(new Long(catoid),variableName);
|
|
198 |
| } |
|
199 |
3
| result.close();
|
|
200 |
3
| pstmt.close();
|
|
201 |
| } catch (SQLException e) { |
|
202 |
0
| ErrorReporter.getErrorHandler().reportError("Problem retrieving old variables",e);
|
|
203 |
0
| throw new ConfigurationManagerException("Problem retrieving old variables. " + e.getMessage());
|
|
204 |
| } |
|
205 |
3
| return oldVariables;
|
|
206 |
| } |
|
207 |
| |
|
208 |
| |
|
209 |
| |
|
210 |
| |
|
211 |
7
| private Hashtable readOldProperties(long catoid) throws ConfigurationManagerException {
|
|
212 |
7
| Hashtable oldProperties = new Hashtable();
|
|
213 |
7
| try {
|
|
214 |
7
| PreparedStatement pstmt = con.prepareStatement("SELECT OID, NAME, VALUE FROM T_PROPERTY WHERE CATEGORY_OID = ? ");
|
|
215 |
7
| pstmt.setLong(1, catoid);
|
|
216 |
7
| ResultSet result = pstmt.executeQuery();
|
|
217 |
7
| while(result.next()) {
|
|
218 |
42
| long propoid = result.getLong("OID");
|
|
219 |
42
| String propertyName = result.getString("NAME");
|
|
220 |
42
| oldProperties.put(new Long(propoid),propertyName);
|
|
221 |
| } |
|
222 |
7
| result.close();
|
|
223 |
7
| pstmt.close();
|
|
224 |
| } catch (SQLException e) { |
|
225 |
0
| ErrorReporter.getErrorHandler().reportError("Problem retrieving properties",e);
|
|
226 |
0
| throw new ConfigurationManagerException("Problem retrieving properties from database. " + e.getMessage());
|
|
227 |
| } |
|
228 |
7
| return oldProperties;
|
|
229 |
| } |
|
230 |
| |
|
231 |
| |
|
232 |
| |
|
233 |
| |
|
234 |
| |
|
235 |
21
| private void loadProperties(Category cat, long catoid) throws ConfigurationManagerException {
|
|
236 |
21
| try {
|
|
237 |
21
| PreparedStatement pstmt = con.prepareStatement("SELECT OID, NAME, VALUE FROM T_PROPERTY WHERE CATEGORY_OID = ? ");
|
|
238 |
21
| pstmt.setLong(1, catoid);
|
|
239 |
21
| ResultSet result = pstmt.executeQuery();
|
|
240 |
21
| while(result.next()) {
|
|
241 |
140
| long propoid = result.getLong("OID");
|
|
242 |
140
| String propertyName = result.getString("NAME");
|
|
243 |
140
| String propertyValue = result.getString("VALUE");
|
|
244 |
140
| cat.setProperty(propertyName, propertyValue);
|
|
245 |
| } |
|
246 |
21
| result.close();
|
|
247 |
21
| pstmt.close();
|
|
248 |
| } catch (SQLException e) { |
|
249 |
0
| ErrorReporter.getErrorHandler().reportError("Problem retrieving properties",e);
|
|
250 |
0
| throw new ConfigurationManagerException("Problem retrieving properties from database. " + e.getMessage());
|
|
251 |
| } |
|
252 |
| } |
|
253 |
| |
|
254 |
13
| private void loadJDBCDriver() throws ConfigurationManagerException {
|
|
255 |
13
| if(driver == null)
|
|
256 |
0
| throw new ConfigurationManagerException("JDBC Driver name is <null>. Check jconfig.properties for org.jconfig.jdbcdriver.");
|
|
257 |
13
| try {
|
|
258 |
13
| Class.forName(driver);
|
|
259 |
| } catch (ClassNotFoundException e) { |
|
260 |
0
| ErrorReporter.getErrorHandler().reportError("JDBC Driver " + e.getMessage() + " was not found.");
|
|
261 |
0
| throw new ConfigurationManagerException("JDBC Driver " + e.getMessage() + " was not found.");
|
|
262 |
| } |
|
263 |
| } |
|
264 |
| |
|
265 |
13
| private void loadJDBCProperties() throws ConfigurationManagerException {
|
|
266 |
13
| try {
|
|
267 |
13
| ResourceLocator locator = new ResourceLocator("jconfig.properties");
|
|
268 |
13
| InputStream is = locator.getInputStream();
|
|
269 |
| |
|
270 |
13
| if ( is != null ) {
|
|
271 |
13
| Properties jcfProperties = new Properties();
|
|
272 |
13
| jcfProperties.load( is );
|
|
273 |
13
| driver = jcfProperties.getProperty("org.jconfig.jdbc.driver");
|
|
274 |
13
| jdbcurl = jcfProperties.getProperty("org.jconfig.jdbc.url");
|
|
275 |
13
| user = jcfProperties.getProperty("org.jconfig.jdbc.user");
|
|
276 |
13
| pwd = jcfProperties.getProperty("org.jconfig.jdbc.pwd");
|
|
277 |
| |
|
278 |
| } |
|
279 |
| else { |
|
280 |
0
| ErrorReporter.getErrorHandler().reportError("Problem locating jconfig.properties.");
|
|
281 |
0
| throw new ConfigurationManagerException("Problem locating jconfig.properties.");
|
|
282 |
| } |
|
283 |
| |
|
284 |
| } catch (IOException e) { |
|
285 |
0
| ErrorReporter.getErrorHandler().reportError("Problem locating jconfig.properties.",e);
|
|
286 |
0
| throw new ConfigurationManagerException("Problem locating jconfig.properties. " + e.getMessage());
|
|
287 |
| } |
|
288 |
| } |
|
289 |
| |
|
290 |
0
| protected Connection JDBCLogin() throws ConfigurationManagerException {
|
|
291 |
0
| if(jdbcurl == null) {
|
|
292 |
0
| ErrorReporter.getErrorHandler().reportError("JDBC URL is <null>. Check jconfig.properties for org.jconfig.jdbc.url.");
|
|
293 |
0
| throw new ConfigurationManagerException("JDBC URL is <null>. Check jconfig.properties for org.jconfig.jdbc.url.");
|
|
294 |
| } |
|
295 |
0
| if(user == null) {
|
|
296 |
0
| ErrorReporter.getErrorHandler().reportError("JDBC USER is <null>. Check jconfig.properties for org.jconfig.jdbc.user.");
|
|
297 |
0
| throw new ConfigurationManagerException("JDBC USER is <null>. Check jconfig.properties for org.jconfig.jdbc.user.");
|
|
298 |
| } |
|
299 |
0
| if(pwd == null) {
|
|
300 |
0
| ErrorReporter.getErrorHandler().reportError("JDBC PASSWORD is <null>. Check jconfig.properties for org.jconfig.jdbc.pwd");
|
|
301 |
0
| throw new ConfigurationManagerException("JDBC PASSWORD is <null>. Check jconfig.properties for org.jconfig.jdbc.pwd.");
|
|
302 |
| } |
|
303 |
0
| try {
|
|
304 |
0
| return DriverManager.getConnection(jdbcurl, user, pwd);
|
|
305 |
| } catch (SQLException e) { |
|
306 |
0
| ErrorReporter.getErrorHandler().reportError("Problem getting JDBC Connection",e);
|
|
307 |
0
| throw new ConfigurationManagerException("Problem getting JDBC Connection: " + e.getMessage());
|
|
308 |
| } |
|
309 |
| } |
|
310 |
| |
|
311 |
| |
|
312 |
| |
|
313 |
3
| public void store(Configuration configuration)
|
|
314 |
| throws ConfigurationManagerException { |
|
315 |
3
| setupJDBC();
|
|
316 |
3
| long confoid = storeConfiguration(configuration.getConfigName());
|
|
317 |
3
| Hashtable oldCategories = readOldCategories(confoid);
|
|
318 |
3
| String [] categoryNameArray = configuration.getCategoryNames();
|
|
319 |
3
| for (int i = 0; i < categoryNameArray.length; i++) {
|
|
320 |
7
| String categoryName = categoryNameArray[i];
|
|
321 |
7
| long catid = storeCategory(confoid, categoryName);
|
|
322 |
7
| oldCategories.remove(new Long(catid));
|
|
323 |
7
| storeProperties(catid, configuration.getCategory(categoryName));
|
|
324 |
| } |
|
325 |
| |
|
326 |
3
| Enumeration enumCat = oldCategories.keys();
|
|
327 |
3
| while (enumCat.hasMoreElements()) {
|
|
328 |
0
| Long catid = (Long)enumCat.nextElement();
|
|
329 |
0
| deleteCategory(catid.longValue(),oldCategories.get(catid).toString());
|
|
330 |
| } |
|
331 |
| |
|
332 |
| |
|
333 |
3
| String [] variableNameArray = getStringArray(configuration.getVariables());
|
|
334 |
3
| Hashtable oldVariables = readOldVariables(confoid);
|
|
335 |
3
| for (int i = 0; i < variableNameArray.length; i++) {
|
|
336 |
11
| String variableName = variableNameArray[i];
|
|
337 |
11
| long varoid= storeVariable(confoid, variableName, configuration.getVariable(variableName));
|
|
338 |
11
| oldVariables.remove(new Long(varoid));
|
|
339 |
| } |
|
340 |
3
| Enumeration enumVar = oldVariables.keys();
|
|
341 |
3
| while (enumVar.hasMoreElements()) {
|
|
342 |
1
| Long varoid = (Long)enumVar.nextElement();
|
|
343 |
1
| deleteVariable(varoid.longValue(),oldVariables.get(varoid).toString());
|
|
344 |
| } |
|
345 |
| |
|
346 |
3
| closeConnection();
|
|
347 |
| } |
|
348 |
| |
|
349 |
| |
|
350 |
3
| private void closeConnection() throws ConfigurationManagerException {
|
|
351 |
3
| try {
|
|
352 |
3
| con.close();
|
|
353 |
| } catch (SQLException e) { |
|
354 |
0
| ErrorReporter.getErrorHandler().reportError("Problem closing database connection.",e);
|
|
355 |
0
| throw new ConfigurationManagerException("Problem closing database connection. " + e.getMessage());
|
|
356 |
| } |
|
357 |
| } |
|
358 |
| |
|
359 |
| |
|
360 |
| |
|
361 |
| |
|
362 |
| |
|
363 |
| |
|
364 |
7
| private void storeProperties(long catid, Category category) throws ConfigurationManagerException {
|
|
365 |
7
| Enumeration enm = category.getProperties().keys();
|
|
366 |
7
| Hashtable oldProperties = readOldProperties(catid);
|
|
367 |
7
| while(enm.hasMoreElements()) {
|
|
368 |
42
| String propertyName = (String)enm.nextElement();
|
|
369 |
42
| long propoid = storeProperty(catid, propertyName, category.getProperty(propertyName));
|
|
370 |
42
| oldProperties.remove(new Long(propoid));
|
|
371 |
| } |
|
372 |
7
| Enumeration enum = oldProperties.keys();
|
|
373 |
| |
|
374 |
7
| while (enum.hasMoreElements()) {
|
|
375 |
1
| Long propoid = (Long)enum.nextElement();
|
|
376 |
1
| deleteProperty(propoid.longValue(),oldProperties.get(propoid).toString());
|
|
377 |
| } |
|
378 |
| |
|
379 |
| } |
|
380 |
| |
|
381 |
| |
|
382 |
| |
|
383 |
| |
|
384 |
| |
|
385 |
| |
|
386 |
43
| private long storeProperty(long catoid, String propertyName, String propertyValue) throws ConfigurationManagerException {
|
|
387 |
43
| long propoid = Long.MIN_VALUE;
|
|
388 |
43
| try {
|
|
389 |
43
| PreparedStatement pstmt = con.prepareStatement("SELECT OID FROM T_PROPERTY WHERE CATEGORY_OID = ? AND NAME= ?");
|
|
390 |
43
| pstmt.setLong(1, catoid);
|
|
391 |
43
| pstmt.setString(2, propertyName);
|
|
392 |
43
| ResultSet result = pstmt.executeQuery();
|
|
393 |
43
| while(result.next()) {
|
|
394 |
42
| propoid = result.getLong("OID");
|
|
395 |
42
| updateProperty(propoid, propertyValue);
|
|
396 |
| } |
|
397 |
43
| result.close();
|
|
398 |
43
| pstmt.close();
|
|
399 |
| } catch (SQLException e) { |
|
400 |
0
| ErrorReporter.getErrorHandler().reportError("Problem storing configuration in database.",e);
|
|
401 |
0
| throw new ConfigurationManagerException("Problem storing configuration in database. " + e.getMessage());
|
|
402 |
| } |
|
403 |
43
| if(propoid == Long.MIN_VALUE) {
|
|
404 |
1
| createNewProperty(catoid, propertyName, propertyValue);
|
|
405 |
1
| return storeProperty(catoid, propertyName, propertyValue);
|
|
406 |
| } |
|
407 |
42
| return propoid;
|
|
408 |
| } |
|
409 |
| |
|
410 |
| |
|
411 |
| |
|
412 |
| |
|
413 |
| |
|
414 |
| |
|
415 |
42
| private void updateProperty(long propoid, String propertyValue) throws ConfigurationManagerException {
|
|
416 |
42
| try {
|
|
417 |
42
| PreparedStatement pstmt = con.prepareStatement("UPDATE T_PROPERTY SET VALUE=? WHERE OID=?");
|
|
418 |
42
| pstmt.setString(1, propertyValue);
|
|
419 |
42
| pstmt.setLong(2, propoid);
|
|
420 |
42
| pstmt.execute();
|
|
421 |
42
| pstmt |