01: package com.teamkonzept.lib;
02:
03: import java.util.*;
04: import java.io.*;
05: import java.sql.*;
06: import com.teamkonzept.db.*;
07: import com.teamkonzept.lib.db.queries.TKDBPropGroupGetPropsByName;
08:
09: /**
10: <b>DBResourceBundle</b> maintains those properties, which are
11: stored in the database.
12: * @author $Author: alex $
13: * @version $Revision: 1.4 $
14: */
15: public class DBResourceBundle extends ResourceBundle implements
16: ConfigurationErrorCodes {
17: /* class variables */
18: Hashtable props = new Hashtable();
19:
20: /* instance variables */
21: String groupName;
22:
23: /**
24: Constructor, loads properties from the database.
25: @param groupName property group name
26: */
27: public DBResourceBundle(String groupName) throws TKException {
28: this .groupName = groupName;
29: loadProperties();
30: }
31:
32: /**
33: reloads the keys and values for this group from the database.
34: */
35: public void doReload() throws TKException {
36: loadProperties();
37: }
38:
39: /**
40: Overridden method returns enumeration of all keys.
41: */
42: public Enumeration getKeys() {
43: return props.keys();
44: }
45:
46: protected void loadProperties() throws TKException {
47: try {
48: props.clear();
49: TKQuery q = TKDBManager
50: .newQuery(TKDBPropGroupGetPropsByName.class);
51: q.setQueryParams("PROPGROUP_NAME", new String(groupName));
52: q.execute();
53: ResultSet rs = q.fetchResultSet();
54: // TODO: leere Gruppe und nicht vorhandene unterscheiden
55: if (rs != null && rs.next()) {
56: do {
57: String pName = rs.getString("NAME");
58: String pValue = rs.getString("VALUE");
59: props.put(pName, pValue);
60: } while (rs.next());
61: } else {
62: throw new TKConfigurationException(
63: "Can't find Propertygroup " + groupName,
64: NO_PROPERTY_FILE, HIGH_SEVERITY, true, null);
65: }
66: } catch (Throwable e) {
67: throw DefaultExceptionHandler.getException(e);
68: }
69:
70: }
71:
72: /**
73: Overridden method returns an Object from the resource bundle.
74: @param key the search key
75: */
76: protected Object handleGetObject(String key)
77: throws MissingResourceException {
78: String value = (String) props.get(key);
79: if (value == null) {
80: throw new MissingResourceException(
81: "The following property was not found:",
82: "DBResource: " + groupName, key);
83: }
84: return value;
85: }
86:
87: }
|