1 18 package org.apache.beehive.netui.script.common; 19 20 import org.apache.beehive.netui.util.internal.InternalStringBuilder; 21 import org.apache.beehive.netui.util.internal.ServletUtils; 22 23 import java.util.ArrayList ; 24 import java.util.Set ; 25 import java.util.Iterator ; 26 import java.util.Locale ; 27 import java.util.Enumeration ; 28 import java.util.HashMap ; 29 import java.util.ResourceBundle ; 30 import javax.servlet.http.HttpServletRequest ; 31 import javax.servlet.http.HttpSession ; 32 import javax.servlet.ServletContext ; 33 34 import org.apache.struts.Globals; 35 import org.apache.struts.config.MessageResourcesConfig; 36 import org.apache.struts.config.ModuleConfig; 37 import org.apache.struts.util.MessageResources; 38 39 import org.apache.beehive.netui.script.common.bundle.BundleNode; 40 import org.apache.beehive.netui.script.common.bundle.BundleNodeFactory; 41 import org.apache.beehive.netui.util.logging.Logger; 42 import org.apache.beehive.netui.pageflow.internal.InternalUtils; 43 44 53 public class BundleMap 54 extends AbstractScriptableMap { 55 56 public static final String DEFAULT_STRUTS_BUNDLE_NAME = "default"; 57 58 private static final Logger LOGGER = Logger.getInstance(BundleMap.class); 59 60 private HashMap _registeredBundles = null; 61 62 private HttpServletRequest _servletRequest = null; 63 private HttpSession _httpSession = null; 64 private ServletContext _servletContext = null; 65 66 74 public BundleMap(HttpServletRequest servletRequest, ServletContext servletContext) { 75 assert servletRequest != null; 76 assert servletContext != null; 77 78 _servletRequest = servletRequest; 79 _httpSession = servletRequest.getSession(false); 80 _servletContext = servletContext; 81 82 _registeredBundles = new HashMap (); 83 } 84 85 public void registerResourceBundle(String name, String resourcePath, Locale forcedLocale) { 86 if(_registeredBundles == null) 87 _registeredBundles = new HashMap (); 88 89 if(LOGGER.isInfoEnabled() && _registeredBundles.containsKey(name)) 90 LOGGER.info("The bundle map already contains a key \"" + name + "\" overwriting the previous value."); 91 92 Locale locale = forcedLocale != null ? forcedLocale : InternalUtils.lookupLocale(_servletRequest); 93 ResourceBundle resourceBundle = ResourceBundle.getBundle(resourcePath, locale); 94 BundleNode bundle = BundleNodeFactory.getInstance().getResourceBundleNode(name, resourceBundle, locale); 95 _registeredBundles.put(name, bundle); 96 } 97 98 public Object get(Object key) { 99 if(key == null) 100 throw new NullPointerException ("Binding to a resource bundle does not accept a null key"); 101 102 BundleNodeMap map = lookupScriptableBundle(key.toString()); 103 if(map == null) { 104 105 handleBundleNotFound(key.toString()); 106 return null; 107 } 108 else return map; 109 } 110 111 121 public boolean containsKey(Object key) { 122 if(key == null) 123 throw new NullPointerException ("Binding to a resource bundle does not accept a null key"); 124 125 BundleNodeMap map = lookupScriptableBundle(key.toString()); 126 return map != null; 127 } 128 129 public Set entrySet() { 130 ArrayList entries = new ArrayList (); 131 132 133 if(_registeredBundles != null) { 134 Iterator iterator = _registeredBundles.keySet().iterator(); 135 while(iterator.hasNext()) { 136 Object key = iterator.next(); 137 entries.add(new BundleNodeEntry(key)); 138 } 139 } 140 141 MessageResources resources = null; 142 143 resources = lookupDefaultStrutsBundle(); 144 if(resources != null) 145 entries.add(new BundleNodeEntry(DEFAULT_STRUTS_BUNDLE_NAME)); 146 147 ModuleConfig moduleConfig = lookupCurrentModuleConfig(); 148 if(moduleConfig != null) { 149 MessageResourcesConfig[] mrs = moduleConfig.findMessageResourcesConfigs(); 150 for(int i = 0; i < mrs.length; i++) { 151 String resourceKey = mrs[i].getKey() + moduleConfig.getPrefix(); 152 resources = lookupStrutsBundle(resourceKey); 153 entries.add(new BundleNodeEntry(mrs[i].getKey())); 154 } 155 } 156 157 return new EntrySet((Entry[])entries.toArray(new Entry[] {})); 158 } 159 160 162 private BundleNodeMap lookupScriptableBundle(String name) { 163 BundleNodeMap map = null; 164 165 166 if(_registeredBundles != null && _registeredBundles.containsKey(name)) { 167 map = new BundleNodeMap(name, (BundleNode)_registeredBundles.get(name)); 168 } 169 else if(name.equals(DEFAULT_STRUTS_BUNDLE_NAME)) { 170 MessageResources resources = lookupDefaultStrutsBundle(); 171 if(resources != null) { 172 BundleNode bundleNode = BundleNodeFactory.getInstance().getStrutsBundleNode(name, resources, retrieveUserLocale()); 173 map = new BundleNodeMap(name, bundleNode); 174 } 175 } 176 else if(_servletContext.getAttribute(name) != null) { 177 MessageResources resources = lookupStrutsBundle(name); 178 if(resources != null) { 179 BundleNode bundleNode = BundleNodeFactory.getInstance().getStrutsBundleNode(name, resources, retrieveUserLocale()); 180 map = new BundleNodeMap(name, bundleNode); 181 } 182 } 183 else { 184 ModuleConfig moduleConfig = lookupCurrentModuleConfig(); 185 if(moduleConfig != null) { 186 MessageResourcesConfig[] mrs = moduleConfig.findMessageResourcesConfigs(); 187 if(mrs != null) { 188 for(int i = 0; i < mrs.length; i++) { 189 190 if(mrs[i].getKey().equals(Globals.MESSAGES_KEY)) 191 continue; 192 else if(mrs[i].getKey().equals(name)) { 193 String resourceKey = mrs[i].getKey() + moduleConfig.getPrefix(); 194 MessageResources resources = lookupStrutsBundle(resourceKey); 195 BundleNode bundleNode = BundleNodeFactory.getInstance().getStrutsBundleNode(name, resources, retrieveUserLocale()); 196 map = new BundleNodeMap(name, bundleNode); 197 break; 198 } 199 } 200 } 201 } 202 } 203 204 return map; 205 } 206 207 213 private MessageResources lookupDefaultStrutsBundle() { 214 Object value = _servletRequest.getAttribute(Globals.MESSAGES_KEY); 215 if(value instanceof MessageResources) 216 return (MessageResources)value; 217 else { 218 if(value != null) 219 LOGGER.warn("Can not resolve the default module bundle." 220 + " The object resolved from the request is of type " 221 +(value != null ? value.getClass().toString() : "null")); 222 return null; 223 } 224 } 225 226 234 private MessageResources lookupStrutsBundle(String name) { 235 Object value = _servletContext.getAttribute(name); 236 if(value instanceof MessageResources) 237 return (MessageResources)value; 238 else { 239 if(value != null) 240 LOGGER.warn("Can not resolve module bundle with name \"" + name 241 + "\". The object resolved from ServletContext is of type " 242 +(value != null ? value.getClass().toString() : "null")); 243 return null; 244 } 245 } 246 247 private final ModuleConfig lookupCurrentModuleConfig() { 248 return (ModuleConfig)_servletRequest.getAttribute(Globals.MODULE_KEY); 249 } 250 251 private void handleBundleNotFound(String name) { 252 253 256 String registeredBundles = formatBundleNames(createBundleList()); 257 String strutsBundles = formatBundleNames(createStrutsBundleList()); 258 259 String msg = "The bundle named \"" + name + "\" was not found in the list of registered bundles with names " 260 + registeredBundles + " or implicit bundle names " + strutsBundles + "."; 261 262 LOGGER.error(msg); 263 throw new RuntimeException (msg); 264 } 265 266 private final String formatBundleNames(String [] names) { 267 InternalStringBuilder sb = new InternalStringBuilder(128); 268 sb.append("["); 269 for(int i = 0; i < names.length; i++) { 270 if(i > 0) 271 sb.append(", "); 272 sb.append(names[i]); 273 } 274 sb.append("]"); 275 276 return sb.toString(); 277 } 278 279 private final String [] createBundleList() { 280 String [] names = null; 281 if(_registeredBundles != null) { 282 names = new String [_registeredBundles.size()]; 283 Iterator iterator = _registeredBundles.keySet().iterator(); 284 for(int i = 0; iterator.hasNext(); i++) { 285 names[i] = iterator.next().toString(); 286 } 287 } 288 289 return names; 290 } 291 292 private final String [] createStrutsBundleList() { 293 String [] names = null; 294 ModuleConfig config = lookupCurrentModuleConfig(); 295 if(config != null) { 296 MessageResourcesConfig[] mrs = config.findMessageResourcesConfigs(); 297 names = new String [mrs.length]; 298 if(mrs != null) { 299 for(int i = 0; i < mrs.length; i++) { 300 if(mrs[i].getKey().equals(Globals.MESSAGES_KEY)) 301 names[i] = DEFAULT_STRUTS_BUNDLE_NAME; 302 else names[i] = mrs[i].getKey() + config.getPrefix(); 303 } 304 } 305 } 306 return names; 307 } 308 309 315 private final Locale retrieveUserLocale() { 316 return InternalUtils.lookupLocale(_servletRequest); 317 } 318 319 final class BundleNodeEntry 320 extends Entry 321 { 322 BundleNodeEntry(Object key) { 323 super(key, null); 324 } 325 326 public Object getValue() { 327 assert getKey() instanceof String ; 328 329 String key = (String )getKey(); 330 return lookupScriptableBundle(key); 331 } 332 } 333 334 342 final class BundleNodeMap 343 extends AbstractScriptableMap { 344 345 private String _propertiesName = null; 346 private BundleNode _bundle = null; 347 private Set _entrySet = null; 348 349 BundleNodeMap(String propertiesName, BundleNode bundle) { 350 assert bundle != null; 351 assert propertiesName != null; 352 353 _bundle = bundle; 354 _propertiesName = propertiesName; 355 } 356 357 public Set entrySet() { 358 if(_entrySet == null) { 359 ArrayList list = new ArrayList (); 360 Enumeration enumeration = _bundle.getKeys(); 361 while(enumeration.hasMoreElements()) { 362 String key =(String )enumeration.nextElement(); 363 String msg = _bundle.getString(key); 364 list.add(new Entry(key, msg)); 365 } 366 _entrySet = new EntrySet((Entry[])list.toArray(new Entry[] {})); 367 } 368 369 return _entrySet; 370 } 371 372 public Object get(Object key) { 373 if(key == null) 374 throw new NullPointerException ("Bundle data binding does not accept a null key"); 375 376 String result = _bundle.getString(key.toString()); 377 if(result == null) { 378 String msg = "The bundle property name \"" + key + "\" could not be found in the properties bundle \"" + _propertiesName + "\"."; 379 LOGGER.error(msg); 380 throw new IllegalArgumentException (msg); 381 } 382 else return result; 383 } 384 385 public boolean containsKey(Object key) { 386 if(key == null) 387 return false; 388 else return _bundle.getString(key.toString()) != null; 389 } 390 391 public String toString() { 392 return _bundle != null ? _bundle.toString() : "BundleMap contains an empty BundleNode"; 393 } 394 } 395 } 396 397 | Popular Tags |