KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > teamkonzept > lib > DBResourceBundle


1 package com.teamkonzept.lib;
2
3 import java.util.*;
4 import java.io.*;
5 import java.sql.*;
6 import com.teamkonzept.db.*;
7 import com.teamkonzept.lib.db.queries.TKDBPropGroupGetPropsByName;
8
9 /**
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 ConfigurationErrorCodes
16 {
17     /* class variables */
18     Hashtable props=new Hashtable();
19
20     /* instance variables */
21     String JavaDoc groupName;
22
23     /**
24        Constructor, loads properties from the database.
25        @param groupName property group name
26      */

27     public DBResourceBundle(String JavaDoc groupName) throws TKException
28     {
29     this.groupName = groupName;
30     loadProperties();
31     }
32
33     /**
34        reloads the keys and values for this group from the database.
35      */

36     public void doReload() throws TKException
37     {
38     loadProperties();
39     }
40
41     /**
42        Overridden method returns enumeration of all keys.
43      */

44     public Enumeration getKeys()
45     {
46     return props.keys();
47     }
48
49     protected void loadProperties() throws TKException
50     {
51     try {
52         props.clear();
53         TKQuery q = TKDBManager.newQuery(TKDBPropGroupGetPropsByName.class);
54         q.setQueryParams("PROPGROUP_NAME", new String JavaDoc(groupName));
55         q.execute();
56         ResultSet rs = q.fetchResultSet();
57         // TODO: leere Gruppe und nicht vorhandene unterscheiden
58
if (rs != null && rs.next())
59         {
60             do
61             {
62                 String JavaDoc pName = rs.getString("NAME");
63                 String JavaDoc pValue = rs.getString("VALUE");
64                 props.put(pName, pValue);
65             } while (rs.next());
66         }
67         else
68         {
69             throw new TKConfigurationException("Can't find Propertygroup " + groupName, NO_PROPERTY_FILE, HIGH_SEVERITY, true, null);
70         }
71     } catch (Throwable JavaDoc e) {
72         throw DefaultExceptionHandler.getException(e);
73     }
74     
75     }
76
77     /**
78        Overridden method returns an Object from the resource bundle.
79        @param key the search key
80      */

81     protected Object JavaDoc handleGetObject(String JavaDoc key) throws MissingResourceException
82     {
83     String JavaDoc value = (String JavaDoc) props.get(key);
84     if (value == null) {
85         throw new MissingResourceException("The following property was not found:","DBResource: "+groupName, key);
86     }
87     return value;
88     }
89
90     
91 }
92
Popular Tags