1 7 8 package com.ibm.icu.util; 9 10 import java.lang.ref.SoftReference ; 11 import java.util.HashMap ; 12 import java.util.Locale ; 13 import java.util.Map ; 14 import java.util.MissingResourceException ; 15 import java.util.ResourceBundle ; 16 17 import com.ibm.icu.impl.ICUResourceBundle; 18 import com.ibm.icu.impl.ICUResourceBundleReader; 19 import com.ibm.icu.impl.ResourceBundleWrapper; 20 import com.ibm.icu.util.ULocale; 21 22 76 public abstract class UResourceBundle extends ResourceBundle { 77 78 79 89 public static UResourceBundle getBundleInstance(String baseName, String localeName){ 90 return getBundleInstance(baseName, localeName, ICUResourceBundle.ICU_DATA_CLASS_LOADER, false); 91 } 92 93 104 public static UResourceBundle getBundleInstance(String baseName, String localeName, ClassLoader root){ 105 return getBundleInstance(baseName, localeName, root, false); 106 } 107 108 122 protected static UResourceBundle getBundleInstance(String baseName, String localeName, ClassLoader root, boolean disableFallback){ 123 return instantiateBundle(baseName, localeName, root, disableFallback); 124 } 125 126 132 public UResourceBundle() { 133 } 134 135 143 public static UResourceBundle getBundleInstance(ULocale locale){ 144 if(locale==null){ 145 locale = ULocale.getDefault(); 146 } 147 return getBundleInstance( ICUResourceBundle.ICU_BASE_NAME, locale.toString(), ICUResourceBundle.ICU_DATA_CLASS_LOADER ); 148 } 149 157 public static UResourceBundle getBundleInstance(String baseName){ 158 return getBundleInstance( baseName, ULocale.getDefault().toString(), ICUResourceBundle.ICU_DATA_CLASS_LOADER ); 159 } 160 170 171 public static UResourceBundle getBundleInstance(String baseName, Locale locale){ 172 return getBundleInstance(baseName, ULocale.forLocale(locale)); 173 } 174 175 185 public static UResourceBundle getBundleInstance(String baseName, ULocale locale){ 186 return getBundleInstance(baseName, locale.toString(),ICUResourceBundle.ICU_DATA_CLASS_LOADER); 187 } 188 189 201 public static UResourceBundle getBundleInstance(String baseName, Locale locale, ClassLoader loader){ 202 return getBundleInstance(baseName, ULocale.forLocale(locale), loader); 203 } 204 205 217 public static UResourceBundle getBundleInstance(String baseName, ULocale locale, ClassLoader loader){ 218 return getBundleInstance(baseName, locale.toString(),loader); 219 } 220 221 230 public abstract ULocale getULocale(); 231 232 237 protected abstract String getLocaleID(); 238 243 protected abstract String getBaseName(); 244 249 protected abstract UResourceBundle getParent(); 250 251 252 257 public Locale getLocale(){ 258 return getULocale().toLocale(); 259 } 260 261 private static SoftReference BUNDLE_CACHE; 263 264 private static void addToCache(ResourceCacheKey key, UResourceBundle b) { 265 Map m = null; 266 if (BUNDLE_CACHE != null) { 267 m = (Map )BUNDLE_CACHE.get(); 268 } 269 if (m == null) { 270 m = new HashMap (); 271 BUNDLE_CACHE = new SoftReference (m); 272 } 273 m.put(key, b); 274 } 275 276 280 protected static void addToCache(ClassLoader cl, String fullName, ULocale defaultLocale, UResourceBundle b){ 281 synchronized(cacheKey){ 282 cacheKey.setKeyValues(cl, fullName, defaultLocale); 283 addToCache((ResourceCacheKey)cacheKey.clone(), b); 284 } 285 } 286 290 protected static UResourceBundle loadFromCache(ClassLoader cl, String fullName, ULocale defaultLocale){ 291 synchronized(cacheKey){ 292 cacheKey.setKeyValues(cl, fullName, defaultLocale); 293 return loadFromCache(cacheKey); 294 } 295 } 296 private static UResourceBundle loadFromCache(ResourceCacheKey key) { 297 if (BUNDLE_CACHE != null) { 298 Map m = (Map )BUNDLE_CACHE.get(); 299 if (m != null) { 300 return (UResourceBundle)m.get(key); 301 } 302 } 303 return null; 304 } 305 306 316 private static final class ResourceCacheKey implements Cloneable { 317 private SoftReference loaderRef; 318 private String searchName; 319 private ULocale defaultLocale; 320 private int hashCodeCache; 321 public boolean equals(Object other) { 323 if (this == other) { 324 return true; 325 } 326 try { 327 final ResourceCacheKey otherEntry = (ResourceCacheKey) other; 328 if (hashCodeCache != otherEntry.hashCodeCache) { 330 return false; 331 } 332 if (!searchName.equals(otherEntry.searchName)) { 334 return false; 335 } 336 if (defaultLocale == null) { 338 if (otherEntry.defaultLocale != null) { 339 return false; 340 } 341 } else { 342 if (!defaultLocale.equals(otherEntry.defaultLocale)) { 343 return false; 344 } 345 } 346 if (loaderRef == null) { 348 return otherEntry.loaderRef == null; 349 } else { 350 return (otherEntry.loaderRef != null) 351 && (loaderRef.get() == otherEntry.loaderRef.get()); 352 } 353 } catch (NullPointerException e) { 354 return false; 355 } catch (ClassCastException e) { 356 return false; 357 } 358 } 359 public int hashCode() { 360 return hashCodeCache; 361 } 362 public Object clone() { 363 try { 364 return super.clone(); 365 } catch (CloneNotSupportedException e) { 366 throw new IllegalStateException (); 368 } 369 } 370 private synchronized void setKeyValues(ClassLoader root, String searchName, ULocale defaultLocale) { 372 this.searchName = searchName; 373 hashCodeCache = searchName.hashCode(); 374 this.defaultLocale = defaultLocale; 375 if (defaultLocale != null) { 376 hashCodeCache ^= defaultLocale.hashCode(); 377 } 378 if (root == null) { 379 this.loaderRef = null; 380 } else { 381 loaderRef = new SoftReference (root); 382 hashCodeCache ^= root.hashCode(); 383 } 384 } 385 388 } 389 390 private static final ResourceCacheKey cacheKey = new ResourceCacheKey(); 391 392 private static final int ROOT_MISSING = 0; 393 private static final int ROOT_ICU = 1; 394 private static final int ROOT_JAVA = 2; 395 396 private static SoftReference ROOT_CACHE; 397 398 private static int getRootType(String baseName, ClassLoader root) 399 { 400 Map m = null; 401 Integer rootType; 402 403 if (ROOT_CACHE != null) { 404 m = (Map ) ROOT_CACHE.get(); 405 } 406 407 if (m == null) { 408 m = new HashMap (); 409 ROOT_CACHE = new SoftReference (m); 410 } 411 412 rootType = (Integer ) m.get(baseName); 413 414 if (rootType == null) { 415 String rootLocale = (baseName.indexOf('.')==-1) ? "root" : ""; 416 int rt = ROOT_MISSING; try{ 418 ICUResourceBundle.getBundleInstance(baseName, rootLocale, root, true); 419 rt = ROOT_ICU; 420 }catch(MissingResourceException ex){ 421 try{ 422 ResourceBundleWrapper.getBundleInstance(baseName, rootLocale, root, true); 423 rt = ROOT_JAVA; 424 }catch(MissingResourceException e){ 425 } 427 } 428 429 rootType = new Integer (rt); 430 m.put(baseName, rootType); 431 } 432 433 return rootType.intValue(); 434 } 435 436 private static void setRootType(String baseName, int rootType) 437 { 438 Integer rt = new Integer (rootType); 439 Map m = null; 440 441 if (ROOT_CACHE != null) { 442 m = (Map ) ROOT_CACHE.get(); 443 } else { 444 m = new HashMap (); 445 ROOT_CACHE = new SoftReference (m); 446 } 447 448 m.put(baseName, rt); 449 } 450 451 463 protected static UResourceBundle instantiateBundle(String baseName, String localeName, 464 ClassLoader root, boolean disableFallback){ 465 UResourceBundle b = null; 466 int rootType = getRootType(baseName, root); 467 468 ULocale defaultLocale = ULocale.getDefault(); 469 470 switch (rootType) 471 { 472 case ROOT_ICU: 473 if(disableFallback) { 474 String fullName = ICUResourceBundleReader.getFullName(baseName, localeName); 475 synchronized(cacheKey){ 476 cacheKey.setKeyValues(root, fullName, defaultLocale); 477 b = loadFromCache(cacheKey); 478 } 479 480 if (b == null) { 481 b = ICUResourceBundle.getBundleInstance(baseName, localeName, root, disableFallback); 482 addToCache(cacheKey, b); 484 } 485 } else { 486 b = ICUResourceBundle.getBundleInstance(baseName, localeName, root, disableFallback); 487 } 488 489 return b; 490 491 case ROOT_JAVA: 492 return ResourceBundleWrapper.getBundleInstance(baseName, localeName, root, disableFallback); 493 494 default: 495 try{ 496 b = ICUResourceBundle.getBundleInstance(baseName, localeName, root, disableFallback); 497 setRootType(baseName, ROOT_ICU); 498 }catch(MissingResourceException ex){ 499 b = ResourceBundleWrapper.getBundleInstance(baseName, localeName, root, disableFallback); 500 setRootType(baseName, ROOT_JAVA); 501 } 502 return b; 503 } 504 } 505 506 510 protected abstract void setLoadingStatus(int newStatus); 511 } 512 | Popular Tags |