1 23 package com.sun.enterprise.tools.jsfext.resource; 24 25 import com.sun.enterprise.tools.jsfext.util.Util; 26 27 import java.util.HashMap ; 28 import java.util.Locale ; 29 import java.util.Map ; 30 import java.util.ResourceBundle ; 31 32 33 38 public class ResourceBundleManager { 39 40 41 44 protected ResourceBundleManager() { 45 } 46 47 48 51 public static ResourceBundleManager getInstance() { 52 if (_instance == null) { 53 _instance = new ResourceBundleManager(); 54 } 55 return _instance; 56 } 57 58 68 protected ResourceBundle getCachedBundle(String baseName, Locale locale) { 69 return (ResourceBundle ) _cache.get(getCacheKey(baseName, locale)); 70 } 71 72 77 protected String getCacheKey(String baseName, Locale locale) { 78 return baseName + "__" + locale.toString(); 79 } 80 81 84 protected void addCachedBundle(String baseName, Locale locale, ResourceBundle bundle) { 85 Map map = new HashMap (_cache); 88 89 map.put(getCacheKey(baseName, locale), bundle); 91 92 _cache = map; 94 } 95 96 104 public ResourceBundle getBundle(String baseName, Locale locale) { 105 ResourceBundle bundle = getCachedBundle(baseName, locale); 106 if (bundle == null) { 107 bundle = ResourceBundle.getBundle(baseName, locale, 108 Util.getClassLoader(this)); 109 if (bundle != null) { 110 addCachedBundle(baseName, locale, bundle); 111 } 112 } 113 return bundle; 114 } 115 116 124 public ResourceBundle getBundle(String baseName, Locale locale, ClassLoader loader) { 125 ResourceBundle bundle = getCachedBundle(baseName, locale); 126 if (bundle == null) { 127 bundle = ResourceBundle.getBundle(baseName, locale, loader); 128 if (bundle != null) { 129 addCachedBundle(baseName, locale, bundle); 130 } 131 } 132 return bundle; 133 } 134 135 138 private static ResourceBundleManager _instance = 139 new ResourceBundleManager(); 140 141 144 private Map _cache = new HashMap (); 145 } 146 | Popular Tags |