1 31 32 package org.opencms.i18n; 33 34 import org.opencms.file.CmsObject; 35 import org.opencms.file.CmsProject; 36 import org.opencms.file.CmsPropertyDefinition; 37 import org.opencms.file.CmsUser; 38 import org.opencms.main.CmsEvent; 39 import org.opencms.main.CmsException; 40 import org.opencms.main.CmsLog; 41 import org.opencms.main.I_CmsEventListener; 42 import org.opencms.main.OpenCms; 43 import org.opencms.monitor.CmsMemoryMonitor; 44 import org.opencms.util.CmsStringUtil; 45 46 import java.util.ArrayList ; 47 import java.util.Collection ; 48 import java.util.Collections ; 49 import java.util.Iterator ; 50 import java.util.List ; 51 import java.util.Locale ; 52 import java.util.Map ; 53 54 import javax.servlet.http.HttpServletRequest ; 55 56 import org.apache.commons.collections.map.LRUMap; 57 import org.apache.commons.logging.Log; 58 59 71 public class CmsLocaleManager implements I_CmsEventListener { 72 73 74 public static final String LOCALE_HANDLER = "class_locale_handler"; 75 76 77 public static final String PARAMETER_ENCODING = "__encoding"; 78 79 80 public static final String PARAMETER_LOCALE = "__locale"; 81 82 83 private static final Log LOG = CmsLog.getLog(CmsLocaleManager.class); 84 85 86 private static Locale m_defaultLocale; 87 88 89 private static Map m_localeCache; 90 91 92 private List m_availableLocales; 93 94 95 private List m_defaultLocales; 96 97 98 private boolean m_initialized; 99 100 101 private I_CmsLocaleHandler m_localeHandler; 102 103 106 public CmsLocaleManager() { 107 108 setDefaultLocale(); 109 m_availableLocales = new ArrayList (); 110 m_defaultLocales = new ArrayList (); 111 m_localeHandler = new CmsDefaultLocaleHandler(); 112 if (CmsLog.INIT.isInfoEnabled()) { 113 CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_I18N_CONFIG_START_0)); 114 } 115 116 LRUMap lruMap = new LRUMap(256); 117 m_localeCache = Collections.synchronizedMap(lruMap); 118 CmsMemoryMonitor monitor = OpenCms.getMemoryMonitor(); 119 if ((monitor != null) && monitor.enabled()) { 120 monitor.register(this.getClass().getName() + ".m_localeCache", lruMap); 122 } 123 124 OpenCms.addCmsEventListener(this, new int[] {I_CmsEventListener.EVENT_CLEAR_CACHES}); 126 } 127 128 133 public CmsLocaleManager(Locale defaultLocale) { 134 135 setDefaultLocale(); 136 m_initialized = false; 137 138 m_availableLocales = new ArrayList (); 139 m_defaultLocales = new ArrayList (); 140 m_localeHandler = new CmsDefaultLocaleHandler(); 141 m_localeCache = Collections.synchronizedMap(new LRUMap(256)); 142 143 m_defaultLocale = defaultLocale; 144 m_defaultLocales.add(defaultLocale); 145 m_availableLocales.add(defaultLocale); 146 } 147 148 151 static { 152 setDefaultLocale(); 153 } 154 155 162 public static Locale getDefaultLocale() { 163 164 return m_defaultLocale; 165 } 166 167 180 public static Locale getLocale(String localeName) { 181 182 if (CmsStringUtil.isEmpty(localeName)) { 183 return getDefaultLocale(); 184 } 185 Locale locale; 186 locale = (Locale )m_localeCache.get(localeName); 187 if (locale == null) { 188 try { 189 String [] localeNames = CmsStringUtil.splitAsArray(localeName, '_'); 190 locale = new Locale ( 191 localeNames[0], 192 (localeNames.length > 1) ? localeNames[1] : "", 193 (localeNames.length > 2) ? localeNames[2] : ""); 194 } catch (Throwable t) { 195 LOG.debug(Messages.get().getBundle().key(Messages.LOG_CREATE_LOCALE_FAILED_1, localeName), t); 196 locale = getDefaultLocale(); 198 } 199 m_localeCache.put(localeName, locale); 200 } 201 return locale; 202 } 203 204 216 public static String getLocaleNames(List localeNames) { 217 218 StringBuffer result = new StringBuffer (); 219 if (localeNames != null) { 220 Iterator i = localeNames.iterator(); 221 while (i.hasNext()) { 222 result.append(i.next().toString()); 223 if (i.hasNext()) { 224 result.append(", "); 225 } 226 } 227 } 228 return result.toString(); 229 } 230 231 237 public static List getLocales(List localeNames) { 238 239 List result = new ArrayList (localeNames.size()); 240 for (int i = 0; i < localeNames.size(); i++) { 241 result.add(getLocale(localeNames.get(i).toString().trim())); 242 } 243 return result; 244 } 245 246 252 public static List getLocales(String localeNames) { 253 254 if (localeNames == null) { 255 return null; 256 } 257 return getLocales(CmsStringUtil.splitAsList(localeNames, ',')); 258 } 259 260 280 private static void setDefaultLocale() { 281 282 286 Locale oldLocale = Locale.getDefault(); 287 if (!(Locale.ENGLISH.getLanguage().equals(oldLocale.getLanguage()))) { 288 try { 290 Locale.setDefault(Locale.ENGLISH); 291 if (CmsLog.INIT.isInfoEnabled()) { 292 CmsLog.INIT.info(Messages.get().getBundle().key( 293 Messages.INIT_I18N_DEFAULT_LOCALE_2, 294 Locale.ENGLISH, 295 oldLocale)); 296 } 297 } catch (Exception e) { 298 CmsLog.INIT.error(Messages.get().getBundle().key( 301 Messages.LOG_UNABLE_TO_SET_DEFAULT_LOCALE_2, 302 Locale.ENGLISH, 303 oldLocale), e); 304 } 305 } else { 306 if (CmsLog.INIT.isInfoEnabled()) { 307 CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_I18N_KEEPING_DEFAULT_LOCALE_1, oldLocale)); 308 } 309 } 310 311 m_defaultLocale = Locale.getDefault(); 313 } 314 315 320 public void addAvailableLocale(String localeName) { 321 322 Locale locale = getLocale(localeName); 323 if (!m_availableLocales.contains(locale)) { 325 m_availableLocales.add(locale); 326 if (CmsLog.INIT.isInfoEnabled()) { 327 CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_I18N_CONFIG_ADD_LOCALE_1, locale)); 328 } 329 } 330 locale = new Locale (locale.getLanguage(), locale.getCountry()); 332 if (!m_availableLocales.contains(locale)) { 333 m_availableLocales.add(locale); 334 if (CmsLog.INIT.isInfoEnabled()) { 335 CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_I18N_CONFIG_ADD_LOCALE_1, locale)); 336 } 337 } 338 locale = new Locale (locale.getLanguage()); 340 if (!m_availableLocales.contains(locale)) { 341 m_availableLocales.add(locale); 342 if (CmsLog.INIT.isInfoEnabled()) { 343 CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_I18N_CONFIG_ADD_LOCALE_1, locale)); 344 } 345 } 346 } 347 348 353 public void addDefaultLocale(String localeName) { 354 355 Locale locale = getLocale(localeName); 356 if (!m_defaultLocales.contains(locale)) { 357 m_defaultLocales.add(locale); 358 if (CmsLog.INIT.isInfoEnabled()) { 359 CmsLog.INIT.info(Messages.get().getBundle().key( 360 Messages.INIT_I18N_CONFIG_DEFAULT_LOCALE_2, 361 new Integer (m_defaultLocales.size()), 362 locale)); 363 364 } 365 } 366 } 367 368 375 public void cmsEvent(CmsEvent event) { 376 377 switch (event.getType()) { 378 case I_CmsEventListener.EVENT_CLEAR_CACHES: 379 clearCaches(); 380 break; 381 default: } 383 } 384 385 390 public List getAvailableLocales() { 391 392 return m_availableLocales; 393 } 394 395 402 public List getAvailableLocales(CmsObject cms, String resourceName) { 403 404 String availableNames = null; 405 try { 406 availableNames = cms.readPropertyObject( 407 resourceName, 408 CmsPropertyDefinition.PROPERTY_AVAILABLE_LOCALES, 409 true).getValue(); 410 } catch (CmsException exc) { 411 } 413 414 List result = null; 415 if (availableNames != null) { 416 result = getAvailableLocales(availableNames); 417 } 418 if ((result == null) || (result.size() == 0)) { 419 return m_availableLocales; 420 } else { 421 return result; 422 } 423 } 424 425 434 public List getAvailableLocales(String names) { 435 436 return checkLocaleNames(getLocales(names)); 437 } 438 439 449 public Locale getBestMatchingLocale(Locale requestedLocale, List defaults, Collection available) { 450 451 if ((available == null) || available.isEmpty()) { 452 return null; 454 } 455 456 if (available.contains(requestedLocale)) { 458 return requestedLocale; 460 } 461 if (requestedLocale.getVariant().length() > 0) { 462 Locale check = new Locale (requestedLocale.getLanguage(), requestedLocale.getCountry(), ""); 464 if (available.contains(check)) { 465 return check; 466 } 467 } 468 if (requestedLocale.getCountry().length() > 0) { 469 Locale check = new Locale (requestedLocale.getLanguage(), "", ""); 471 if (available.contains(check)) { 472 return check; 473 } 474 } 475 476 if ((defaults == null) || defaults.isEmpty()) { 478 return null; 480 } 481 482 return getFirstMatchingLocale(defaults, available); 484 } 485 486 495 public Locale getDefaultLocale(CmsObject cms, String resourceName) { 496 497 List defaultLocales = getDefaultLocales(cms, resourceName); 498 Locale result; 499 if (defaultLocales.size() > 0) { 500 result = (Locale )defaultLocales.get(0); 501 } else { 502 result = getDefaultLocale(); 503 } 504 return result; 505 } 506 507 512 public List getDefaultLocales() { 513 514 return m_defaultLocales; 515 } 516 517 530 public List getDefaultLocales(CmsObject cms, String resourceName) { 531 532 String defaultNames = null; 533 try { 534 defaultNames = cms.readPropertyObject(resourceName, CmsPropertyDefinition.PROPERTY_LOCALE, true).getValue(); 535 } catch (CmsException e) { 536 LOG.warn(Messages.get().getBundle().key(Messages.ERR_READ_ENCODING_PROP_1, resourceName), e); 537 } 538 539 List result = null; 540 if (defaultNames != null) { 541 result = getAvailableLocales(defaultNames); 542 } 543 if ((result == null) || (result.size() == 0)) { 544 return m_defaultLocales; 545 } else { 546 return result; 547 } 548 } 549 550 558 public Locale getFirstMatchingLocale(List locales, Collection available) { 559 560 Iterator i; 561 i = locales.iterator(); 563 while (i.hasNext()) { 564 Locale locale = (Locale )i.next(); 565 if (available.contains(locale)) { 566 return locale; 568 } 569 } 570 571 i = locales.iterator(); 573 while (i.hasNext()) { 574 Locale locale = (Locale )i.next(); 575 if (locale.getVariant().length() > 0) { 576 locale = new Locale (locale.getLanguage(), locale.getCountry(), ""); 578 if (available.contains(locale)) { 579 return locale; 581 } 582 } 583 } 584 585 i = locales.iterator(); 587 while (i.hasNext()) { 588 Locale locale = (Locale )i.next(); 589 if (locale.getCountry().length() > 0) { 590 locale = new Locale (locale.getLanguage(), "", ""); 592 if (available.contains(locale)) { 593 return locale; 595 } 596 } 597 } 598 599 return null; 601 } 602 603 619 public CmsI18nInfo getI18nInfo(HttpServletRequest req, CmsUser user, CmsProject project, String resource) { 620 621 CmsI18nInfo i18nInfo = null; 622 623 if (OpenCms.getSiteManager().isWorkplaceRequest(req)) { 625 List wpLocalizedFolders = OpenCms.getWorkplaceManager().getLocalizedFolders(); 627 for (int i = wpLocalizedFolders.size() - 1; i >= 0; i--) { 628 if (resource.startsWith((String )wpLocalizedFolders.get(i))) { 629 i18nInfo = OpenCms.getWorkplaceManager().getI18nInfo(req, user, project, resource); 631 break; 632 } 633 } 634 } 635 if (i18nInfo == null) { 636 i18nInfo = m_localeHandler.getI18nInfo(req, user, project, resource); 638 } 639 640 Locale locale = null; 642 String encoding = null; 643 if (req != null) { 644 String localeParam = req.getParameter(CmsLocaleManager.PARAMETER_LOCALE); 645 if (localeParam != null) { 647 locale = CmsLocaleManager.getLocale(localeParam); 649 } 650 encoding = req.getParameter(CmsLocaleManager.PARAMETER_ENCODING); 652 } 653 654 if (locale == null) { 656 locale = i18nInfo.getLocale(); 657 } 658 if (encoding == null) { 659 encoding = i18nInfo.getEncoding(); 660 } 661 662 if (locale == null) { 664 locale = getDefaultLocale(); 665 if (LOG.isDebugEnabled()) { 666 LOG.debug(Messages.get().getBundle().key(Messages.LOG_LOCALE_NOT_FOUND_1, locale)); 667 } 668 } 669 if (encoding == null) { 670 encoding = OpenCms.getSystemInfo().getDefaultEncoding(); 671 if (LOG.isDebugEnabled()) { 672 LOG.debug(Messages.get().getBundle().key(Messages.LOG_ENCODING_NOT_FOUND_1, encoding)); 673 } 674 } 675 676 return new CmsI18nInfo(locale, encoding); 678 } 679 680 686 public I_CmsLocaleHandler getLocaleHandler() { 687 688 return m_localeHandler; 689 } 690 691 696 public void initialize(CmsObject cms) { 697 698 m_localeHandler.initHandler(cms); 700 m_defaultLocale = (Locale )m_defaultLocales.get(0); 702 m_initialized = true; 704 if (CmsLog.INIT.isInfoEnabled()) { 705 CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_I18N_CONFIG_VFSACCESS_0)); 706 } 707 } 708 709 718 public boolean isInitialized() { 719 720 return m_initialized; 721 } 722 723 728 public void setLocaleHandler(I_CmsLocaleHandler localeHandler) { 729 730 if (localeHandler != null) { 731 m_localeHandler = localeHandler; 732 } 733 if (CmsLog.INIT.isInfoEnabled()) { 734 CmsLog.INIT.info(Messages.get().getBundle().key( 735 Messages.INIT_I18N_CONFIG_LOC_HANDLER_1, 736 m_localeHandler.getClass().getName())); 737 } 738 } 739 740 749 private List checkLocaleNames(List locales) { 750 751 if (locales == null) { 752 return null; 753 } 754 List result = new ArrayList (); 755 Iterator i = locales.iterator(); 756 while (i.hasNext()) { 757 Locale locale = (Locale )i.next(); 758 if (m_availableLocales.contains(locale)) { 759 result.add(locale); 760 } 761 } 762 return result; 763 } 764 765 768 private void clearCaches() { 769 770 m_localeCache.clear(); 772 CmsResourceBundleLoader.flushBundleCache(); 773 774 if (LOG.isDebugEnabled()) { 775 LOG.debug(Messages.get().getBundle().key(Messages.LOG_LOCALE_MANAGER_FLUSH_CACHE_1, "EVENT_CLEAR_CACHES")); 776 } 777 } 778 } | Popular Tags |