1 30 31 32 package org.hsqldb.resources; 33 34 import java.lang.reflect.Method ; 35 import java.util.Locale ; 36 import java.util.MissingResourceException ; 37 import java.util.ResourceBundle ; 38 39 import org.hsqldb.lib.HashMap; 40 import org.hsqldb.lib.HsqlArrayList; 41 42 58 public final class BundleHandler { 59 60 61 private static final Object mutex = new Object (); 62 63 64 private static Locale locale = Locale.getDefault(); 65 66 67 private static HashMap bundleHandleMap = new HashMap(); 68 69 70 private static HsqlArrayList bundleList = new HsqlArrayList(); 71 72 76 private static final String prefix = "org/hsqldb/resources/"; 77 78 79 private static final Method newGetBundleMethod = getNewGetBundleMethod(); 80 81 82 private BundleHandler() {} 83 84 89 public static Locale getLocale() { 90 91 synchronized (mutex) { 92 return locale; 93 } 94 } 95 96 102 public static void setLocale(Locale l) throws IllegalArgumentException { 103 104 synchronized (mutex) { 105 if (l == null) { 106 throw new IllegalArgumentException ("null locale"); 107 } 108 109 locale = l; 110 } 111 } 112 113 125 public static int getBundleHandle(String name, ClassLoader cl) { 126 127 Integer bundleHandle; 128 ResourceBundle bundle; 129 String bundleName; 130 String bundleKey; 131 132 bundleName = prefix + name; 133 134 synchronized (mutex) { 135 bundleKey = locale.toString() + bundleName; 136 bundleHandle = (Integer ) bundleHandleMap.get(bundleKey); 137 138 if (bundleHandle == null) { 139 try { 140 bundle = getBundle(bundleName, locale, cl); 141 142 bundleList.add(bundle); 143 144 bundleHandle = new Integer (bundleList.size() - 1); 145 146 bundleHandleMap.put(bundleKey, bundleHandle); 147 } catch (Exception e) { 148 149 } 151 } 152 } 153 154 return bundleHandle == null ? -1 155 : bundleHandle.intValue(); 156 } 157 158 170 public static String getString(int handle, String key) { 171 172 ResourceBundle bundle; 173 String s; 174 175 synchronized (mutex) { 176 if (handle < 0 || handle >= bundleList.size() || key == null) { 177 bundle = null; 178 } else { 179 bundle = (ResourceBundle ) bundleList.get(handle); 180 } 181 } 182 183 if (bundle == null) { 184 s = null; 185 } else { 186 try { 187 s = bundle.getString(key); 188 } catch (Exception e) { 189 s = null; 190 } 191 } 192 193 return s; 194 } 195 196 200 private static Method getNewGetBundleMethod() { 201 202 Class clazz; 203 Class [] args; 204 205 clazz = ResourceBundle .class; 206 args = new Class [] { 207 String .class, Locale .class, ClassLoader .class 208 }; 209 210 try { 211 return clazz.getMethod("getBundle", args); 212 } catch (Exception e) { 213 return null; 214 } 215 } 216 217 231 public static ResourceBundle getBundle(String name, Locale locale, 232 ClassLoader cl) 233 throws NullPointerException , 234 MissingResourceException { 235 236 if (cl == null) { 237 return ResourceBundle.getBundle(name, locale); 238 } else if (newGetBundleMethod == null) { 239 return ResourceBundle.getBundle(name, locale); 240 } else { 241 try { 242 return (ResourceBundle ) newGetBundleMethod.invoke(null, 243 new Object [] { 244 name, locale, cl 245 }); 246 } catch (Exception e) { 247 return ResourceBundle.getBundle(name, locale); 248 } 249 } 250 } 251 } 252 | Popular Tags |