1 package com.teamkonzept.lib; 2 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 propertyName; 22 private ResourceBundle bundle; 23 private static Hashtable allBundles = new Hashtable(); 24 25 30 private PropertyManager(String _propertyName) throws TKException 31 { 32 propertyName = _propertyName; 33 loadBundle(); 34 } 35 36 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 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 bundle = new PropertyResourceBundle(url.openStream()); 62 allBundles.put(propertyName, this); 63 } 64 catch (IOException e) 65 { 66 throw new TKConfigurationException("Unable to load Property " + propertyName, NO_PROPERTY_FILE, HIGH_SEVERITY, true, e); 68 } 69 } 70 } 71 72 77 public String getValue (String key) 78 { 79 return bundle.getString(key); 80 } 81 82 91 public String getValue (String key, String defaultValue) 92 { 93 try 94 { 95 return bundle.getString(key); 96 } 97 catch (MissingResourceException mre) 98 { 99 return defaultValue; 100 } 101 } 102 103 106 public Enumeration getKeys() 107 { 108 return bundle.getKeys(); 109 } 110 111 114 public static void doReloadAll() throws TKException 115 { 116 allBundles.clear(); 117 } 118 119 122 public void doReload() throws TKException 123 { 124 loadBundle(); 125 } 126 127 131 public static PropertyManager getPropertyManager(String 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 } 140 | Popular Tags |