1 29 30 package com.caucho.jsp; 31 32 import com.caucho.loader.Environment; 33 import com.caucho.loader.EnvironmentLocal; 34 import com.caucho.log.Log; 35 import com.caucho.util.L10N; 36 import com.caucho.util.TimedCache; 37 import com.caucho.vfs.Depend; 38 import com.caucho.vfs.Path; 39 import com.caucho.vfs.ReadStream; 40 41 import javax.servlet.jsp.jstl.fmt.LocalizationContext; 42 import java.io.InputStream ; 43 import java.util.Enumeration ; 44 import java.util.Locale ; 45 import java.util.PropertyResourceBundle ; 46 import java.util.ResourceBundle ; 47 import java.util.logging.Logger ; 48 49 52 public class BundleManager { 53 private static final L10N L = new L10N(BundleManager.class); 54 private static final Logger log = Log.open(BundleManager.class); 55 56 static LocalizationContext NULL_BUNDLE = 57 new LocalizationContext(); 58 59 private static EnvironmentLocal<BundleManager> _envBundle = 60 new EnvironmentLocal<BundleManager>(); 61 62 private TimedCache<String ,LocalizationContext> _bundleCache; 63 64 private BundleManager() 65 { 66 long updateInterval = Environment.getDependencyCheckInterval(); 67 68 _bundleCache = new TimedCache(256, updateInterval); 69 } 70 71 74 public static BundleManager create() 75 { 76 BundleManager manager; 77 78 synchronized (_envBundle) { 79 manager = _envBundle.get(); 80 if (manager == null) { 81 manager = new BundleManager(); 82 _envBundle.set(manager); 83 } 84 } 85 86 return manager; 87 } 88 91 public LocalizationContext getBundle(String name, String cacheKey, 92 Enumeration <Locale > locales) 93 { 94 LocalizationContext cachedValue = _bundleCache.get(cacheKey); 95 96 if (cachedValue != null) 97 return cachedValue == NULL_BUNDLE ? null : cachedValue; 98 99 while (locales.hasMoreElements()) { 100 Locale locale = locales.nextElement(); 101 102 LocalizationContext bundle = getBundle(name, locale); 103 104 if (bundle != null) { 105 _bundleCache.put(cacheKey, bundle); 106 return bundle; 107 } 108 } 109 110 _bundleCache.put(cacheKey, NULL_BUNDLE); 111 112 return null; 113 } 114 115 118 public LocalizationContext getBundle(String name, Locale locale) 119 { 120 String cacheName = (name + '_' + 121 locale.getLanguage() + '_' + 122 locale.getCountry() + '_' + 123 locale.getVariant()); 124 125 LocalizationContext bundle; 126 127 bundle = _bundleCache.get(cacheName); 128 129 if (bundle != null) 130 return bundle != NULL_BUNDLE ? bundle : null; 131 132 String fullName = cacheName; 133 134 ResourceBundle resourceBundle; 135 resourceBundle = getBaseBundle(fullName); 136 if (resourceBundle != null) { 137 bundle = new LocalizationContext(resourceBundle, locale); 138 _bundleCache.put(cacheName, bundle); 139 return bundle; 140 } 141 142 fullName = name + '_' + locale.getLanguage() + '_' + locale.getCountry(); 143 resourceBundle = getBaseBundle(fullName); 144 if (resourceBundle != null) { 145 bundle = new LocalizationContext(resourceBundle, locale); 146 _bundleCache.put(cacheName, bundle); 147 return bundle; 148 } 149 150 fullName = name + '_' + locale.getLanguage(); 151 resourceBundle = getBaseBundle(fullName); 152 if (resourceBundle != null) { 153 bundle = new LocalizationContext(resourceBundle, locale); 154 _bundleCache.put(cacheName, bundle); 155 return bundle; 156 } 157 158 _bundleCache.put(cacheName, NULL_BUNDLE); 159 160 return null; 161 } 162 163 164 167 public LocalizationContext getBundle(String name) 168 { 169 LocalizationContext bundle = _bundleCache.get(name); 170 if (bundle != null) 171 return bundle != NULL_BUNDLE ? bundle : null; 172 173 ResourceBundle resourceBundle = getBaseBundle(name); 174 if (resourceBundle != null) { 175 bundle = new LocalizationContext(resourceBundle); 176 _bundleCache.put(name, bundle); 177 return bundle; 178 } 179 180 _bundleCache.put(name, NULL_BUNDLE); 181 182 return null; 183 } 184 185 188 private ResourceBundle getBaseBundle(String name) 189 { 190 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 191 192 try { 193 Class cl = Class.forName(name, false, loader); 194 195 if (cl != null) { 196 ResourceBundle rb = (ResourceBundle ) cl.newInstance(); 197 198 if (rb != null) 199 return rb; 200 } 201 } catch (Throwable e) { 202 } 203 204 try { 205 InputStream is = loader.getResourceAsStream(name.replace('.', '/') + ".properties"); 206 207 if (is instanceof ReadStream) { 208 Path path = ((ReadStream) is).getPath(); 209 Environment.addDependency(new Depend(path)); 210 } 211 212 ResourceBundle bundle = new PropertyResourceBundle (is); 213 214 is.close(); 215 216 return bundle; 217 } catch (Exception e) { 218 } 219 220 return null; 221 } 222 } 223 | Popular Tags |