KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.teamkonzept.lib;
2 /**
3  * Title: <p>
4  * Description: Kurzer Mantel um PropertyResourceBundle um ein Reload zu ermöglichen<p>
5  * toDo: event definieren für das reload
6  * Copyright: Copyright (c) <p>
7  * Company: <p>
8  * @author alex
9  * @version 1.0
10  */

11
12 import org.apache.log4j.Category;
13 import java.io.*;
14 import java.util.*;
15 import java.net.*;
16
17 public class PropertyManager implements ConfigurationErrorCodes
18 {
19     private final static Category CAT = Category.getInstance(PropertyManager.class);
20     
21     private String JavaDoc propertyName;
22     private ResourceBundle bundle;
23     private static Hashtable allBundles = new Hashtable();
24
25     /**
26     * Konstruktor
27     * private, only access is via the static method getPropertyManager
28     * @param _filename name of the Propertyfile
29     */

30     private PropertyManager(String JavaDoc _propertyName) throws TKException
31     {
32         propertyName = _propertyName;
33         loadBundle();
34     }
35
36     /**
37      * loads the bundle using the Classloader
38      * ToDO: What happens after an exception ?
39      */

40     private void loadBundle() throws TKConfigurationException
41     {
42         URL url = getClass().getResource(propertyName);
43         if (url == null)
44         {
45             try
46             {
47                 bundle = new DBResourceBundle(propertyName);
48             }
49             catch (Throwable JavaDoc e)
50             {
51                 throw new TKConfigurationException("Can't find Propertygroup " + propertyName, NO_PROPERTY_FILE, HIGH_SEVERITY, true, null);
52             }
53
54         }
55         else
56         {
57             CAT.debug("PropertyURL: " + url.toExternalForm());
58             try
59             {
60                 //URLConnection con = url.openConnection();
61
bundle = new PropertyResourceBundle(url.openStream());
62                 allBundles.put(propertyName, this);
63             }
64             catch (IOException e)
65             {
66                 // Name der gesuchten Property mit ausgeben !
67
throw new TKConfigurationException("Unable to load Property " + propertyName, NO_PROPERTY_FILE, HIGH_SEVERITY, true, e);
68             }
69         }
70     }
71
72     /**
73      * Returns the value associated with the given key.
74      *
75      * @return the value associated with the given key.
76      */

77     public String JavaDoc getValue (String JavaDoc key)
78     {
79         return bundle.getString(key);
80     }
81
82     /**
83      * Returns the value associated with the given key.
84      * <P>
85      * If there is no such property, the given default value
86      * will be returned.
87      *
88      * @param defaultValue the default value for the given key.
89      * @return the value associated with the given key.
90      */

91     public String JavaDoc getValue (String JavaDoc key, String JavaDoc defaultValue)
92     {
93         try
94         {
95             return bundle.getString(key);
96         }
97         catch (MissingResourceException mre)
98         {
99             return defaultValue;
100         }
101     }
102     
103     /**
104     * @return all keys of the ResourceBundle
105     */

106     public Enumeration getKeys()
107     {
108         return bundle.getKeys();
109     }
110
111     /**
112      * reloads the bundle
113      */

114     public static void doReloadAll() throws TKException
115     {
116         allBundles.clear();
117     }
118
119     /**
120      * reloads the bundle
121      */

122     public void doReload() throws TKException
123     {
124         loadBundle();
125     }
126
127     /**
128      * get the PropertyManager
129      * @param
130     */

131     public static PropertyManager getPropertyManager(String JavaDoc name) throws TKException
132     {
133         PropertyManager man = (PropertyManager)allBundles.get(name);
134         if (man == null)
135             man = new PropertyManager(name);
136         return man;
137     }
138 }//end class
139

140
Popular Tags