1 55 56 package org.jboss.axis.i18n; 57 58 import org.jboss.logging.Logger; 59 60 import java.util.Enumeration ; 61 import java.util.HashSet ; 62 import java.util.Hashtable ; 63 import java.util.Iterator ; 64 import java.util.Locale ; 65 import java.util.MissingResourceException ; 66 import java.util.ResourceBundle ; 67 68 90 public class ProjectResourceBundle extends ResourceBundle 91 { 92 private static Logger log = Logger.getLogger(ProjectResourceBundle.class); 93 94 private static final Hashtable bundleCache = new Hashtable (); 98 99 private static final Locale defaultLocale = Locale.getDefault(); 100 101 private final ResourceBundle resourceBundle; 102 private final String resourceName; 103 104 105 protected Object handleGetObject(String key) 106 throws MissingResourceException 107 { 108 if (log.isTraceEnabled()) 109 log.trace(this.toString() + "::handleGetObject(" + key + ")"); 110 111 Object obj; 112 try 113 { 114 obj = resourceBundle.getObject(key); 115 } 116 catch (MissingResourceException e) 117 { 118 122 obj = null; 123 } 124 return obj; 125 } 126 127 public Enumeration getKeys() 128 { 129 Enumeration myKeys = resourceBundle.getKeys(); 130 if (parent == null) 131 { 132 return myKeys; 133 } 134 else 135 { 136 final HashSet set = new HashSet (); 137 while (myKeys.hasMoreElements()) 138 { 139 set.add(myKeys.nextElement()); 140 } 141 142 Enumeration pKeys = parent.getKeys(); 143 while (pKeys.hasMoreElements()) 144 { 145 set.add(pKeys.nextElement()); 146 } 147 148 return new Enumeration () 149 { 150 private Iterator it = set.iterator(); 151 152 public boolean hasMoreElements() 153 { 154 return it.hasNext(); 155 } 156 157 public Object nextElement() 158 { 159 return it.next(); 160 } 161 }; 162 } 163 } 164 165 166 176 public static ProjectResourceBundle getBundle(String projectName, 177 String packageName, 178 String resourceName) 179 throws MissingResourceException 180 { 181 return getBundle(projectName, packageName, resourceName, null, null, null); 182 } 183 184 198 public static ProjectResourceBundle getBundle(String projectName, 199 Class caller, 200 String resourceName, 201 Locale locale) 202 throws MissingResourceException 203 { 204 return getBundle(projectName, 205 caller, 206 resourceName, 207 locale, 208 null); 209 } 210 211 223 public static ProjectResourceBundle getBundle(String projectName, 224 String packageName, 225 String resourceName, 226 Locale locale, 227 ClassLoader loader) 228 throws MissingResourceException 229 { 230 return getBundle(projectName, packageName, resourceName, locale, loader, null); 231 } 232 233 250 public static ProjectResourceBundle getBundle(String projectName, 251 Class caller, 252 String resourceName, 253 Locale locale, 254 ResourceBundle extendsBundle) 255 throws MissingResourceException 256 { 257 return getBundle(projectName, 258 getPackage(caller.getClass().getName()), 259 resourceName, 260 locale, 261 caller.getClass().getClassLoader(), 262 extendsBundle); 263 } 264 265 279 public static ProjectResourceBundle getBundle(String projectName, 280 String packageName, 281 String resourceName, 282 Locale locale, 283 ClassLoader loader, 284 ResourceBundle extendsBundle) 285 throws MissingResourceException 286 { 287 if (log.isDebugEnabled()) 288 log.debug("getBundle(" + projectName + "," + packageName + "," + resourceName + "," + String.valueOf(locale) + ",...)"); 289 290 Context context = new Context(); 291 context.setLocale(locale); 292 context.setLoader(loader); 293 context.setProjectName(projectName); 294 context.setResourceName(resourceName); 295 context.setParentBundle(extendsBundle); 296 297 packageName = context.validate(packageName); 298 299 ProjectResourceBundle bundle = null; 300 bundle = getBundle(context, packageName); 301 302 if (bundle == null) 303 { 304 throw new MissingResourceException ("Cannot find resource '" + 305 packageName + '.' + resourceName + "'", 306 resourceName, ""); 307 } 308 309 return bundle; 310 } 311 312 318 private static synchronized ProjectResourceBundle getBundle(Context context, String packageName) 319 throws MissingResourceException 320 { 321 String cacheKey = context.getCacheKey(packageName); 322 323 ProjectResourceBundle prb = (ProjectResourceBundle)bundleCache.get(cacheKey); 324 325 if (prb == null) 326 { 327 String name = packageName + '.' + context.getResourceName(); 328 ResourceBundle rb = context.loadBundle(packageName); 329 ResourceBundle parent = context.getParentBundle(packageName); 330 331 if (rb != null) 332 { 333 prb = new ProjectResourceBundle(name, rb); 334 prb.setParent(parent); 335 336 if (log.isDebugEnabled()) 337 log.debug("Created " + prb + ", linked to parent " + String.valueOf(parent)); 338 } 339 else 340 { 341 if (parent != null) 342 { 343 if (parent instanceof ProjectResourceBundle) 344 { 345 prb = (ProjectResourceBundle)parent; 346 } 347 else 348 { 349 prb = new ProjectResourceBundle(name, parent); 350 } 351 352 if (log.isDebugEnabled()) 353 log.debug("Root package not found, cross link to " + parent); 354 } 355 } 356 357 if (prb != null) 358 { 359 bundleCache.put(cacheKey, prb); 361 } 362 } 363 364 return prb; 365 } 366 367 private static final String getPackage(String name) 368 { 369 return name.substring(0, name.lastIndexOf('.')).intern(); 370 } 371 372 375 private ProjectResourceBundle(String name, ResourceBundle bundle) 376 throws MissingResourceException 377 { 378 this.resourceBundle = bundle; 379 this.resourceName = name; 380 } 381 382 public String getResourceName() 383 { 384 return resourceName; 385 } 386 387 390 public static void clearCache() 391 { 392 bundleCache.clear(); 393 } 394 395 public String toString() 396 { 397 return resourceName; 398 } 399 400 401 private static class Context 402 { 403 private Locale _locale; 404 private ClassLoader _loader; 405 private String _projectName; 406 private String _resourceName; 407 private ResourceBundle _parent; 408 409 void setLocale(Locale l) 410 { 411 417 _locale = (l == null) ? defaultLocale : l; 418 } 419 420 void setLoader(ClassLoader l) 421 { 422 _loader = (l != null) ? l : this.getClass().getClassLoader(); 423 if (_loader == null) 425 { 426 _loader = ClassLoader.getSystemClassLoader(); 427 } 428 } 430 431 void setProjectName(String name) 432 { 433 _projectName = name.intern(); 434 } 435 436 void setResourceName(String name) 437 { 438 _resourceName = name.intern(); 439 } 440 441 void setParentBundle(ResourceBundle b) 442 { 443 _parent = b; 444 } 445 446 Locale getLocale() 447 { 448 return _locale; 449 } 450 451 ClassLoader getLoader() 452 { 453 return _loader; 454 } 455 456 String getProjectName() 457 { 458 return _projectName; 459 } 460 461 String getResourceName() 462 { 463 return _resourceName; 464 } 465 466 ResourceBundle getParentBundle() 467 { 468 return _parent; 469 } 470 471 String getCacheKey(String packageName) 472 { 473 String loaderName = (_loader == null) ? "" : (":" + _loader.hashCode()); 474 return packageName + "." + _resourceName + ":" + _locale + ":" + defaultLocale + loaderName; 475 } 476 477 ResourceBundle loadBundle(String packageName) 478 { 479 try 480 { 481 return ResourceBundle.getBundle(packageName + '.' + _resourceName, 482 _locale, 483 _loader); 484 } 485 catch (MissingResourceException e) 486 { 487 log.debug("loadBundle: Ignoring MissingResourceException: " + e.getMessage()); 489 } 490 return null; 491 } 492 493 ResourceBundle getParentBundle(String packageName) 494 { 495 ResourceBundle p; 496 if (packageName != _projectName) 497 { 498 p = getBundle(this, getPackage(packageName)); 499 } 500 else 501 { 502 p = _parent; 503 _parent = null; 504 } 505 return p; 506 } 507 508 String validate(String packageName) 509 throws MissingResourceException 510 { 511 if (_projectName == null || _projectName.length() == 0) 512 { 513 throw new MissingResourceException ("Project name not specified", 514 "", ""); 515 } 516 517 if (packageName == null || packageName.length() == 0) 518 { 519 throw new MissingResourceException ("Package not specified", 520 packageName, ""); 521 } 522 packageName = packageName.intern(); 523 524 527 if (packageName != _projectName && !packageName.startsWith(_projectName + '.')) 528 { 529 throw new MissingResourceException ("Project '" + _projectName 530 + "' must be a prefix of Package '" 531 + packageName + "'", 532 packageName + '.' + _resourceName, ""); 533 } 534 535 return packageName; 536 } 537 } 538 } 539 | Popular Tags |