001: package com.teamkonzept.lib;
002:
003: /**
004: * Title: <p>
005: * Description: Kurzer Mantel um PropertyResourceBundle um ein Reload zu ermöglichen<p>
006: * toDo: event definieren für das reload
007: * Copyright: Copyright (c) <p>
008: * Company: <p>
009: * @author alex
010: * @version 1.0
011: */
012:
013: import org.apache.log4j.Category;
014: import java.io.*;
015: import java.util.*;
016: import java.net.*;
017:
018: public class PropertyManager implements ConfigurationErrorCodes {
019: private final static Category CAT = Category
020: .getInstance(PropertyManager.class);
021:
022: private String propertyName;
023: private ResourceBundle bundle;
024: private static Hashtable allBundles = new Hashtable();
025:
026: /**
027: * Konstruktor
028: * private, only access is via the static method getPropertyManager
029: * @param _filename name of the Propertyfile
030: */
031: private PropertyManager(String _propertyName) throws TKException {
032: propertyName = _propertyName;
033: loadBundle();
034: }
035:
036: /**
037: * loads the bundle using the Classloader
038: * ToDO: What happens after an exception ?
039: */
040: private void loadBundle() throws TKConfigurationException {
041: URL url = getClass().getResource(propertyName);
042: if (url == null) {
043: try {
044: bundle = new DBResourceBundle(propertyName);
045: } catch (Throwable e) {
046: throw new TKConfigurationException(
047: "Can't find Propertygroup " + propertyName,
048: NO_PROPERTY_FILE, HIGH_SEVERITY, true, null);
049: }
050:
051: } else {
052: CAT.debug("PropertyURL: " + url.toExternalForm());
053: try {
054: //URLConnection con = url.openConnection();
055: bundle = new PropertyResourceBundle(url.openStream());
056: allBundles.put(propertyName, this );
057: } catch (IOException e) {
058: // Name der gesuchten Property mit ausgeben !
059: throw new TKConfigurationException(
060: "Unable to load Property " + propertyName,
061: NO_PROPERTY_FILE, HIGH_SEVERITY, true, e);
062: }
063: }
064: }
065:
066: /**
067: * Returns the value associated with the given key.
068: *
069: * @return the value associated with the given key.
070: */
071: public String getValue(String key) {
072: return bundle.getString(key);
073: }
074:
075: /**
076: * Returns the value associated with the given key.
077: * <P>
078: * If there is no such property, the given default value
079: * will be returned.
080: *
081: * @param defaultValue the default value for the given key.
082: * @return the value associated with the given key.
083: */
084: public String getValue(String key, String defaultValue) {
085: try {
086: return bundle.getString(key);
087: } catch (MissingResourceException mre) {
088: return defaultValue;
089: }
090: }
091:
092: /**
093: * @return all keys of the ResourceBundle
094: */
095: public Enumeration getKeys() {
096: return bundle.getKeys();
097: }
098:
099: /**
100: * reloads the bundle
101: */
102: public static void doReloadAll() throws TKException {
103: allBundles.clear();
104: }
105:
106: /**
107: * reloads the bundle
108: */
109: public void doReload() throws TKException {
110: loadBundle();
111: }
112:
113: /**
114: * get the PropertyManager
115: * @param
116: */
117: public static PropertyManager getPropertyManager(String name)
118: throws TKException {
119: PropertyManager man = (PropertyManager) allBundles.get(name);
120: if (man == null)
121: man = new PropertyManager(name);
122: return man;
123: }
124: }//end class
|