1 17 package org.jahia.resourcebundle; 18 19 import java.security.Principal ; 20 import java.util.Locale ; 21 import java.util.ResourceBundle ; 22 23 import org.jahia.params.ParamBean; 24 import org.jahia.registries.ServicesRegistry; 25 import org.jahia.services.pages.ContentPage; 26 import org.jahia.services.sites.JahiaSite; 27 import org.jahia.services.usermanager.JahiaUser; 28 29 35 public class JahiaResourceBundle 36 { 37 38 private static final String CLASS_NAME = JahiaResourceBundle.class.getName(); 39 40 public static final String ENGINE_DEFAULT_RESOURCE_BUNDLE = "JahiaEnginesResources"; 41 public static final String ADMIN_DEFAULT_RESOURCE_BUNDLE = "JahiaAdministrationResources"; 42 public static final String MESSAGE_DEFAULT_RESOURCE_BUNDLE = "JahiaMessageResources"; 43 44 45 82 public static String getEngineResource( String resourceName, 83 ParamBean jParams, 84 Locale locale ){ 85 86 ResourceBundle res = null; 87 String resValue = null; 88 89 if ( resourceName == null || resourceName.trim().equals("") ) 90 return null; 91 92 Locale loc = checkLocale(locale,jParams); 93 94 boolean adminMode = false; 95 try { 96 adminMode = jParams.isInAdminMode(); 97 } catch ( Throwable t ){ 98 t.printStackTrace(); 99 } 100 101 if ( !adminMode ) { 102 res = getGrpUsrEngineResourceBundle(jParams.getPageID(),jParams.getUser()); 104 if ( res != null ){ 105 try { 106 resValue = res.getString(resourceName); 107 return resValue; 108 } catch ( Throwable t ){ 109 } 110 } 111 112 res = getPageEngineResourceBundle(jParams.getPageID(),jParams); 114 if ( res != null ){ 115 try { 116 resValue = res.getString(resourceName); 117 return resValue; 118 } catch ( Throwable t ){ 119 } 120 } 121 122 res = getSiteEngineResourceBundle(jParams.getSite(),jParams,loc); 124 if ( res != null ){ 125 try { 126 resValue = getString(res, resourceName, loc); 127 return resValue; 128 } catch ( Throwable t ){ 129 } 130 } 131 } 132 133 res = getEngineDefaultResourceBundle(jParams,loc); 135 if ( res != null ){ 136 try { 137 resValue = getString(res, resourceName, loc); 138 return resValue; 139 } catch ( Throwable t ){ 140 } 141 } 142 logger.warn("Resource [" + resourceName + 143 "] not found in engine resource bundles using locale [" + 144 locale + "]"); 145 return null; 146 } 147 148 176 public static String getAdminResource( String resourceName, 177 ParamBean jParams, 178 Locale locale ){ 179 180 ResourceBundle res = null; 181 String resValue = null; 182 183 if ( resourceName == null || resourceName.trim().equals("") ) 184 return null; 185 186 Locale loc = checkLocale(locale,jParams); 187 188 boolean adminMode = false; 189 try { 190 adminMode = jParams.isInAdminMode(); 191 } catch ( Throwable t ){ 192 t.printStackTrace(); 193 } 194 195 res = getSiteAdminResourceBundle(jParams.getSite(),jParams,loc); 197 if ( res != null ){ 198 try { 199 resValue = getString(res, resourceName, loc); 200 return resValue; 201 } catch ( Throwable t ){ 202 } 203 } 204 205 res = getAdminDefaultResourceBundle(jParams,loc); 207 if ( res != null ){ 208 try { 209 resValue = getString(res, resourceName, loc); 210 return resValue; 211 } catch ( Throwable t ){ 212 } 213 } 214 logger.warn("Resource [" + resourceName + 215 "] not found in administration resource bundles using locale [" + 216 loc + "]"); 217 return null; 218 } 219 220 235 public static String getMessageResource( String resourceName, 236 Locale locale ){ 237 238 ResourceBundle res = ResourceBundle.getBundle(MESSAGE_DEFAULT_RESOURCE_BUNDLE,locale); 239 String resValue = null; 240 241 if ( resourceName == null || resourceName.trim().equals("") ) 242 return null; 243 244 if ( res != null ){ 245 try { 246 resValue = getString(res, resourceName, locale); 247 return resValue; 248 } catch ( Throwable t ){ 249 } 250 } 251 logger.warn("Resource [" + resourceName + 252 "] not found in message resource bundles using locale [" + 253 locale + "]"); 254 return null; 255 } 256 257 294 public static String getUrlPathResourceEngineResource( String resourceName, 295 ParamBean jParams, 296 Locale locale ) 297 { 298 String res = getEngineResource(resourceName,jParams,locale); 299 if ( res != null ){ 300 res = jParams.getRequest().getContextPath() + res; 301 } 302 return res; 303 } 304 305 306 328 public static String getResource(String bundleCode, String resourceKey, Locale locale, ParamBean jParams) { 329 String result = null; 330 331 if ("administration".equals(bundleCode)) { 332 result = getAdminResource(resourceKey, jParams, locale); 333 } else if ("configuration".equals(bundleCode)) { 334 result = getMessageResource(resourceKey, locale); 335 } else { 336 result = getEngineResource(resourceKey, jParams, locale); 337 } 338 339 return result; 340 } 341 342 343 352 public static String getCommonResource(String resourceName, ParamBean jParams) { 353 354 ResourceBundle res = ResourceBundle.getBundle(ENGINE_DEFAULT_RESOURCE_BUNDLE); 355 try { 356 if (res != null) { 357 return res.getString(resourceName); 358 } 359 } catch (java.util.MissingResourceException mre) { 360 logger.warn("Resource [" + resourceName + "] not found in [" + 361 ENGINE_DEFAULT_RESOURCE_BUNDLE + "] !"); 362 } 363 return null; 364 } 365 366 376 public static String getUrlPathCommonResource(String resourceName, ParamBean jParams) { 377 378 String res = getCommonResource(resourceName,jParams); 379 if ( res != null ){ 380 res = jParams.getRequest().getContextPath() + res; 381 } 382 return res; 383 } 384 385 419 public static ResourceBundle getEngineResourceBundle( 420 String resourceBundle, 421 ParamBean jParams, 422 Locale locale, 423 boolean useDefault ){ 424 425 ResourceBundle res = null; 426 427 Locale loc = checkLocale(locale,jParams); 428 429 try { 430 res = ResourceBundle.getBundle(resourceBundle,loc); 431 } catch ( Throwable t ){ 432 } 434 if ( ((res == null) && useDefault) && (jParams != null) ){ 435 436 res = getGrpUsrEngineResourceBundle(jParams.getPageID(),jParams.getUser()); 438 439 if ( res == null ){ 440 res = getPageEngineResourceBundle(jParams.getPageID(),jParams); 442 } 443 444 if ( res == null ){ 445 res = getSiteEngineResourceBundle(jParams.getSite(),jParams,loc); 447 } 448 if ( res == null ){ 449 res = getEngineDefaultResourceBundle(jParams,loc); 451 } 452 } 453 return res; 454 } 455 456 466 public static ResourceBundle getEngineDefaultResourceBundle( 467 ParamBean jParams, 468 Locale locale ){ 469 470 Locale loc = checkLocale(locale,jParams); 471 472 ResourceBundle res = null; 473 474 try { 475 res = ResourceBundle.getBundle(ENGINE_DEFAULT_RESOURCE_BUNDLE,loc); 476 } catch ( Throwable t ){ 477 logger.debug("Error while using default engine resource bundle (" + 478 ENGINE_DEFAULT_RESOURCE_BUNDLE + ") with locale " + loc, t); 479 } 480 481 return res; 482 } 483 484 494 public static ResourceBundle getAdminDefaultResourceBundle( 495 ParamBean jParams, 496 Locale locale ){ 497 498 Locale loc = checkLocale(locale,jParams); 499 500 ResourceBundle res = null; 501 502 try { 503 res = ResourceBundle.getBundle(ADMIN_DEFAULT_RESOURCE_BUNDLE,loc); 504 } catch ( Throwable t ){ 505 logger.debug("Error while retrieving administration default resource bundle " + 506 ADMIN_DEFAULT_RESOURCE_BUNDLE + " with locale " + loc, t); 507 } 508 509 return res; 510 } 511 512 522 public static ResourceBundle getMessageDefaultResourceBundle( 523 ParamBean jParams, 524 Locale locale ){ 525 526 Locale loc = checkLocale(locale,jParams); 527 528 ResourceBundle res = null; 529 530 try { 531 res = ResourceBundle.getBundle(MESSAGE_DEFAULT_RESOURCE_BUNDLE,loc); 532 } catch ( Throwable t ){ 533 logger.debug("Error while retrieving message default resource bundle " + 534 ADMIN_DEFAULT_RESOURCE_BUNDLE + " with locale " + loc, t); 535 } 536 537 return res; 538 } 539 540 555 public static ResourceBundle getSiteEngineResourceBundle( JahiaSite site, 556 ParamBean jParams, 557 Locale locale ){ 558 559 if ( site == null ) 560 return null; 561 562 Locale loc = checkLocale(locale,jParams); 563 564 ResourceBundle res = null; 565 566 try { 567 res = ResourceBundle.getBundle( ENGINE_DEFAULT_RESOURCE_BUNDLE 568 +site.getSiteKey().toUpperCase(),loc); 569 } catch ( Throwable t ){ 570 } 573 574 return res; 575 } 576 577 592 public static ResourceBundle getSiteAdminResourceBundle( JahiaSite site, 593 ParamBean jParams, 594 Locale locale ){ 595 596 if ( site == null ) 597 return null; 598 599 Locale loc = checkLocale(locale,jParams); 600 601 ResourceBundle res = null; 602 603 try { 604 res = ResourceBundle.getBundle( ADMIN_DEFAULT_RESOURCE_BUNDLE 605 +site.getSiteKey().toUpperCase(),loc); 606 } catch ( Throwable t ){ 607 } 610 611 return res; 612 } 613 614 627 public static ResourceBundle getPageEngineResourceBundle( int pageID , ParamBean jParams){ 628 629 630 if ( jParams == null ) 631 return null; 632 633 if ( pageID == -1 ) 634 return null; 635 636 ContentPage contentPage = null; 637 try { 638 contentPage = ServicesRegistry.getInstance().getJahiaPageService().lookupContentPage(pageID,true); 639 } catch ( Throwable t) { 640 } 643 644 if ( (contentPage == null) ) 645 return null; 646 647 ResourceBundle res = null; 648 649 try { 650 res = PagesEngineResourceBundle.getInstance().getResourceBundle( contentPage , jParams ); 651 } catch ( Throwable t ){ 652 } 655 656 return res; 657 } 658 659 672 public static ResourceBundle getGrpUsrEngineResourceBundle( int pageID , JahiaUser user){ 673 674 675 if ( pageID == -1 || user==null ) 676 return null; 677 678 ContentPage contentPage = null; 679 try { 680 contentPage = ServicesRegistry.getInstance().getJahiaPageService().lookupContentPage(pageID,true); 681 } catch ( Throwable t) { 682 } 685 686 if ( (contentPage == null) ) 687 return null; 688 689 ResourceBundle res = null; 690 691 try { 692 res = GrpUsrEngineResourceBundle.getInstance().getResourceBundle( contentPage , (Principal )user ); 693 } catch ( Throwable t ){ 694 } 697 698 return res; 699 } 700 701 702 private static Locale checkLocale(Locale locale, ParamBean jParams){ 703 Locale resLocale = locale; 704 if ( resLocale == null ){ 705 if ( jParams != null ){ 706 resLocale = jParams.getLocale(); 707 } else { 708 resLocale = Locale.getDefault(); 709 } 710 } 711 return resLocale; 712 } 713 714 private static org.apache.log4j.Logger logger = 715 org.apache.log4j.Logger.getLogger(JahiaResourceBundle.class); 716 717 726 public static String getString( ResourceBundle res, String resName, Locale locale) { 727 String resValue = res.getString(resName); 728 return resValue; 729 } 730 } 731 732 | Popular Tags |