KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > i18n > CmsLocaleManager


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/i18n/CmsLocaleManager.java,v $
3  * Date : $Date: 2006/03/27 14:53:01 $
4  * Version: $Revision: 1.49 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

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 JavaDoc;
47 import java.util.Collection JavaDoc;
48 import java.util.Collections JavaDoc;
49 import java.util.Iterator JavaDoc;
50 import java.util.List JavaDoc;
51 import java.util.Locale JavaDoc;
52 import java.util.Map JavaDoc;
53
54 import javax.servlet.http.HttpServletRequest JavaDoc;
55
56 import org.apache.commons.collections.map.LRUMap;
57 import org.apache.commons.logging.Log;
58
59 /**
60  * Manages the locales configured for this OpenCms installation.<p>
61  *
62  * Locale configuration is done in <code>opencms.xml</code>.<p>
63  *
64  * @author Carsten Weinholz
65  * @author Alexander Kandzior
66  *
67  * @version $Revision: 1.49 $
68  *
69  * @since 6.0.0
70  */

71 public class CmsLocaleManager implements I_CmsEventListener {
72
73     /** Runtime property name for locale handler. */
74     public static final String JavaDoc LOCALE_HANDLER = "class_locale_handler";
75
76     /** Request parameter to force encoding selection. */
77     public static final String JavaDoc PARAMETER_ENCODING = "__encoding";
78
79     /** Request parameter to force locale selection. */
80     public static final String JavaDoc PARAMETER_LOCALE = "__locale";
81
82     /** The log object for this class. */
83     private static final Log LOG = CmsLog.getLog(CmsLocaleManager.class);
84
85     /** The default locale, this is the first configured locale. */
86     private static Locale JavaDoc m_defaultLocale;
87
88     /** A cache for accelerated locale lookup, this should never get so large to require a "real" cache. */
89     private static Map JavaDoc m_localeCache;
90
91     /** The set of available locale names. */
92     private List JavaDoc m_availableLocales;
93
94     /** The default locale names (must be a subset of the available locale names). */
95     private List JavaDoc m_defaultLocales;
96
97     /** Indicates if the locale manager is fully initialized. */
98     private boolean m_initialized;
99
100     /** The configured locale handler. */
101     private I_CmsLocaleHandler m_localeHandler;
102
103     /**
104      * Initializes a new CmsLocaleManager, called from the configuration.<p>
105      */

106     public CmsLocaleManager() {
107
108         setDefaultLocale();
109         m_availableLocales = new ArrayList JavaDoc();
110         m_defaultLocales = new ArrayList JavaDoc();
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             // map must be of type "LRUMap" so that memory monitor can acecss all information
121
monitor.register(this.getClass().getName() + ".m_localeCache", lruMap);
122         }
123
124         // register this object as event listener
125
OpenCms.addCmsEventListener(this, new int[] {I_CmsEventListener.EVENT_CLEAR_CACHES});
126     }
127
128     /**
129      * Initializes a new CmsLocaleManager, used for OpenCms runlevel 1 (unit tests) only.<p>
130      *
131      * @param defaultLocale the default locale to use
132      */

133     public CmsLocaleManager(Locale JavaDoc defaultLocale) {
134
135         setDefaultLocale();
136         m_initialized = false;
137
138         m_availableLocales = new ArrayList JavaDoc();
139         m_defaultLocales = new ArrayList JavaDoc();
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     /**
149      * Required for setting the default locale on the first possible time.<p>
150      */

151     static {
152         setDefaultLocale();
153     }
154
155     /**
156      * Returns the default locale configured in <code>opencms-system.xml</code>.<p>
157      *
158      * The default locale is the first locale int the list of configured default locales.
159      *
160      * @return the default locale
161      */

162     public static Locale JavaDoc getDefaultLocale() {
163
164         return m_defaultLocale;
165     }
166
167     /**
168      * Returns a locale created from the given full name.<p>
169      *
170      * The full name must consist of language code,
171      * country code(optional), variant(optional) separated by "_".<p>
172      *
173      * This method will always return a valid Locale!
174      * If the provided locale name is not valid (i.e. leads to an Exception
175      * when trying to create the Locale, then the configured default Locale is returned.<p>
176      *
177      * @param localeName the full locale name
178      * @return the locale or <code>null</code> if not available
179      */

180     public static Locale JavaDoc getLocale(String JavaDoc localeName) {
181
182         if (CmsStringUtil.isEmpty(localeName)) {
183             return getDefaultLocale();
184         }
185         Locale JavaDoc locale;
186         locale = (Locale JavaDoc)m_localeCache.get(localeName);
187         if (locale == null) {
188             try {
189                 String JavaDoc[] localeNames = CmsStringUtil.splitAsArray(localeName, '_');
190                 locale = new Locale JavaDoc(
191                     localeNames[0],
192                     (localeNames.length > 1) ? localeNames[1] : "",
193                     (localeNames.length > 2) ? localeNames[2] : "");
194             } catch (Throwable JavaDoc t) {
195                 LOG.debug(Messages.get().getBundle().key(Messages.LOG_CREATE_LOCALE_FAILED_1, localeName), t);
196                 // map this error to the default locale
197
locale = getDefaultLocale();
198             }
199             m_localeCache.put(localeName, locale);
200         }
201         return locale;
202     }
203
204     /**
205      * Returns the locale names from the given List of locales as a comma separated String.<p>
206      *
207      * For example, if the input List contains <code>{@link Locale#ENGLISH}</code> and
208      * <code>{@link Locale#GERMANY}</code>, the result will be <code>"en, de_DE"</code>.<p>
209      *
210      * An empty String is returned if the input is <code>null</code>, or contains no elements.<p>
211      *
212      * @param localeNames the locale names to generate a String from
213      *
214      * @return the locale names from the given List of locales as a comma separated String
215      */

216     public static String JavaDoc getLocaleNames(List JavaDoc localeNames) {
217
218         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
219         if (localeNames != null) {
220             Iterator JavaDoc 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     /**
232      * Returns a List of locales from an array of locale names.<p>
233      *
234      * @param localeNames array of locale names
235      * @return a List of locales derived from the given locale names
236      */

237     public static List JavaDoc getLocales(List JavaDoc localeNames) {
238
239         List JavaDoc result = new ArrayList JavaDoc(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     /**
247      * Returns a List of locales from a comma-separated string of locale names.<p>
248      *
249      * @param localeNames a comma-separated string of locale names
250      * @return a List of locales derived from the given locale names
251      */

252     public static List JavaDoc getLocales(String JavaDoc localeNames) {
253
254         if (localeNames == null) {
255             return null;
256         }
257         return getLocales(CmsStringUtil.splitAsList(localeNames, ','));
258     }
259
260     /**
261      * Sets the default locale of the Java VM to <code>{@link Locale#ENGLISH}</code> if the
262      * current default has any other language then English set.<p>
263      *
264      * This is required because otherwise the default (English) resource bundles
265      * would not be displayed for the English locale if a translated default locale exists.<p>
266      *
267      * Here's an example of how this issues shows up:
268      * On a German server, the default locale usually is <code>{@link Locale#GERMAN}</code>.
269      * All English translations for OpenCms are located in the "default" message files, for example
270      * <code>org.opencms.i18n.message.properties</code>. If the German localization is installed, it will be
271      * located in <code>org.opencms.i18n.message_de.properties</code>. If user has English selected
272      * as his locale, the default Java lookup mechanism first tries to find
273      * <code>org.opencms.i18n.message_en.properties</code>. However, this file does not exist, since the
274      * English localization is kept in the default file. Next, the Java lookup mechanism tries to find the servers
275      * default locale, which in this example is German. Since there is a German message file, the Java lookup mechanism
276      * is finished and uses this German localization, not the default file. Therefore the
277      * user get the German localization, not the English one.
278      * Setting the default locale explicitly to English avoids this issue.<p>
279      */

280     private static void setDefaultLocale() {
281
282         // set the default locale to english
283
// this is required because otherwise the default (english) resource bundles
284
// would not be displayed for the english locale if a translated locale exists
285

286         Locale JavaDoc oldLocale = Locale.getDefault();
287         if (!(Locale.ENGLISH.getLanguage().equals(oldLocale.getLanguage()))) {
288             // default language is not English
289
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 JavaDoc e) {
298                 // any Exception: the locale has not been changed, so there may be issues with the English
299
// localization but OpenCms will run in general
300
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         // initialize the static member with the new default
312
m_defaultLocale = Locale.getDefault();
313     }
314
315     /**
316      * Adds a locale to the list of available locales.<p>
317      *
318      * @param localeName the locale to add
319      */

320     public void addAvailableLocale(String JavaDoc localeName) {
321
322         Locale JavaDoc locale = getLocale(localeName);
323         // add full variation (language / country / variant)
324
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         // add variation with only language and country
331
locale = new Locale JavaDoc(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         // add variation with language only
339
locale = new Locale JavaDoc(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     /**
349      * Adds a locale to the list of default locales.<p>
350      *
351      * @param localeName the locale to add
352      */

353     public void addDefaultLocale(String JavaDoc localeName) {
354
355         Locale JavaDoc 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 JavaDoc(m_defaultLocales.size()),
362                     locale));
363
364             }
365         }
366     }
367
368     /**
369      * Implements the CmsEvent interface,
370      * the locale manager the events to clear
371      * the list of cached keys .<p>
372      *
373      * @param event CmsEvent that has occurred
374      */

375     public void cmsEvent(CmsEvent event) {
376
377         switch (event.getType()) {
378             case I_CmsEventListener.EVENT_CLEAR_CACHES:
379                 clearCaches();
380                 break;
381             default: // no operation
382
}
383     }
384
385     /**
386      * Returns the list of available locales configured in.<p>
387      *
388      * @return the list of available locale names, e.g. <code>en, de</code>
389      */

390     public List JavaDoc getAvailableLocales() {
391
392         return m_availableLocales;
393     }
394
395     /**
396      * Returns an array of available locale names for the given resource.<p>
397      *
398      * @param cms the current cms permission object
399      * @param resourceName the name of the resource
400      * @return an array of available locale names
401      */

402     public List JavaDoc getAvailableLocales(CmsObject cms, String JavaDoc resourceName) {
403
404         String JavaDoc availableNames = null;
405         try {
406             availableNames = cms.readPropertyObject(
407                 resourceName,
408                 CmsPropertyDefinition.PROPERTY_AVAILABLE_LOCALES,
409                 true).getValue();
410         } catch (CmsException exc) {
411             // noop
412
}
413
414         List JavaDoc 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     /**
426      * Returns a List of available locales from a comma separated string of locale names.<p>
427      *
428      * All names are filtered against the allowed available locales
429      * configured in <code>opencms.properties</code>.<P>
430      *
431      * @param names a comma-separated String of locale names
432      * @return List of locales created from the given locale names
433      */

434     public List JavaDoc getAvailableLocales(String JavaDoc names) {
435
436         return checkLocaleNames(getLocales(names));
437     }
438
439     /**
440      * Tries to find the given requested locale (eventually simplified) in the collection of available locales,
441      * if the requested locale is not found it will return the first match from the given list of default locales.<p>
442      *
443      * @param requestedLocale the requested locale, if this (or a simplified version of it) is available it will be returned
444      * @param defaults a list of default locales to use in case the requested locale is not available
445      * @param available the available locales to find a match in
446      *
447      * @return the best matching locale name or null if no name matches
448      */

449     public Locale JavaDoc getBestMatchingLocale(Locale JavaDoc requestedLocale, List JavaDoc defaults, Collection JavaDoc available) {
450
451         if ((available == null) || available.isEmpty()) {
452             // no locales are available at all
453
return null;
454         }
455
456         // the requested locale is the match we want to find most
457
if (available.contains(requestedLocale)) {
458             // check if the requested locale is directly available
459
return requestedLocale;
460         }
461         if (requestedLocale.getVariant().length() > 0) {
462             // locale has a variant like "en_EN_whatever", try only with language and country
463
Locale JavaDoc check = new Locale JavaDoc(requestedLocale.getLanguage(), requestedLocale.getCountry(), "");
464             if (available.contains(check)) {
465                 return check;
466             }
467         }
468         if (requestedLocale.getCountry().length() > 0) {
469             // locale has a country like "en_EN", try only with language
470
Locale JavaDoc check = new Locale JavaDoc(requestedLocale.getLanguage(), "", "");
471             if (available.contains(check)) {
472                 return check;
473             }
474         }
475
476         // available locales do not match the requested locale
477
if ((defaults == null) || defaults.isEmpty()) {
478             // if we have no default locales we are out of luck
479
return null;
480         }
481
482         // no match found for the requested locale, return the first match from the default locales
483
return getFirstMatchingLocale(defaults, available);
484     }
485
486     /**
487      * Returns the "best" default locale for the given resource.<p>
488      *
489      * @param cms the current cms permission object
490      * @param resourceName the name of the resource
491      * @return an array of default locale names
492      *
493      * @see #getDefaultLocales(CmsObject, String)
494      */

495     public Locale JavaDoc getDefaultLocale(CmsObject cms, String JavaDoc resourceName) {
496
497         List JavaDoc defaultLocales = getDefaultLocales(cms, resourceName);
498         Locale JavaDoc result;
499         if (defaultLocales.size() > 0) {
500             result = (Locale JavaDoc)defaultLocales.get(0);
501         } else {
502             result = getDefaultLocale();
503         }
504         return result;
505     }
506
507     /**
508      * Returns the list of default locale names configured in <code>opencms.properties</code>.<p>
509      *
510      * @return the list of default locale names, e.g. <code>en, de</code>
511      */

512     public List JavaDoc getDefaultLocales() {
513
514         return m_defaultLocales;
515     }
516
517     /**
518      * Returns an array of default locales for the given resource.<p>
519      *
520      * Use this method in case you need to get all available default options for a resource,
521      * if you just need the "best" default locale for a resource,
522      * use <code>{@link #getDefaultLocale(CmsObject, String)}</code>.<p>
523      *
524      * @param cms the current cms permission object
525      * @param resourceName the name of the resource
526      * @return an array of default locale names
527      *
528      * @see #getDefaultLocale(CmsObject, String)
529      */

530     public List JavaDoc getDefaultLocales(CmsObject cms, String JavaDoc resourceName) {
531
532         String JavaDoc 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 JavaDoc 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     /**
551      * Returns the first matching locale (eventually simplified) from the available locales.<p>
552      *
553      * @param locales must be an ascending sorted list of locales in order of preference
554      * @param available the available locales to find a match in
555      *
556      * @return the first precise or simplified match
557      */

558     public Locale JavaDoc getFirstMatchingLocale(List JavaDoc locales, Collection JavaDoc available) {
559
560         Iterator JavaDoc i;
561         // first try a precise match
562
i = locales.iterator();
563         while (i.hasNext()) {
564             Locale JavaDoc locale = (Locale JavaDoc)i.next();
565             if (available.contains(locale)) {
566                 // precise match
567
return locale;
568             }
569         }
570
571         // now try a match only with language and country
572
i = locales.iterator();
573         while (i.hasNext()) {
574             Locale JavaDoc locale = (Locale JavaDoc)i.next();
575             if (locale.getVariant().length() > 0) {
576                 // the locale has a variant, try to match without the variant
577
locale = new Locale JavaDoc(locale.getLanguage(), locale.getCountry(), "");
578                 if (available.contains(locale)) {
579                     // match
580
return locale;
581                 }
582             }
583         }
584
585         // finally try a match only with language
586
i = locales.iterator();
587         while (i.hasNext()) {
588             Locale JavaDoc locale = (Locale JavaDoc)i.next();
589             if (locale.getCountry().length() > 0) {
590                 // the locale has a country, try to match without the country
591
locale = new Locale JavaDoc(locale.getLanguage(), "", "");
592                 if (available.contains(locale)) {
593                     // match
594
return locale;
595                 }
596             }
597         }
598
599         // no match
600
return null;
601     }
602
603     /**
604      * Returns the the appropriate locale/encoding for a request,
605      * using the "right" locale handler for the given resource.<p>
606      *
607      * Certain system folders (like the Workplace) require a special
608      * locale handler different from the configured handler.
609      * Use this method if you want to resolve locales exactly like
610      * the system does for a request.<p>
611      *
612      * @param req the current http request
613      * @param user the current user
614      * @param project the current project
615      * @param resource the URI of the requested resource (with full site root added)
616      *
617      * @return the i18n information to use for the given request context
618      */

619     public CmsI18nInfo getI18nInfo(HttpServletRequest JavaDoc req, CmsUser user, CmsProject project, String JavaDoc resource) {
620
621         CmsI18nInfo i18nInfo = null;
622
623         // check if this is a request against a Workplace folder
624
if (OpenCms.getSiteManager().isWorkplaceRequest(req)) {
625             // The list of configured localized workplace folders
626
List JavaDoc wpLocalizedFolders = OpenCms.getWorkplaceManager().getLocalizedFolders();
627             for (int i = wpLocalizedFolders.size() - 1; i >= 0; i--) {
628                 if (resource.startsWith((String JavaDoc)wpLocalizedFolders.get(i))) {
629                     // use the workplace locale handler for this resource
630
i18nInfo = OpenCms.getWorkplaceManager().getI18nInfo(req, user, project, resource);
631                     break;
632                 }
633             }
634         }
635         if (i18nInfo == null) {
636             // use default locale handler
637
i18nInfo = m_localeHandler.getI18nInfo(req, user, project, resource);
638         }
639
640         // check the request for special parameters overriding the locale handler
641
Locale JavaDoc locale = null;
642         String JavaDoc encoding = null;
643         if (req != null) {
644             String JavaDoc localeParam = req.getParameter(CmsLocaleManager.PARAMETER_LOCALE);
645             // check request for parameters
646
if (localeParam != null) {
647                 // "__locale" parameter found in request
648
locale = CmsLocaleManager.getLocale(localeParam);
649             }
650             // check for "__encoding" parameter in request
651
encoding = req.getParameter(CmsLocaleManager.PARAMETER_ENCODING);
652         }
653
654         // merge values from request with values from locale handler
655
if (locale == null) {
656             locale = i18nInfo.getLocale();
657         }
658         if (encoding == null) {
659             encoding = i18nInfo.getEncoding();
660         }
661
662         // still some values might be "null"
663
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 the merged values
677
return new CmsI18nInfo(locale, encoding);
678     }
679
680     /**
681      * Returns the configured locale handler.<p>
682      * This handler is used to derive the appropriate locale/encoding for a request.
683      *
684      * @return the locale handler
685      */

686     public I_CmsLocaleHandler getLocaleHandler() {
687
688         return m_localeHandler;
689     }
690
691     /**
692      * Initializes this locale manager with the OpenCms system configuration.<p>
693      *
694      * @param cms an OpenCms context object that must have been initialized with "Admin" permissions
695      */

696     public void initialize(CmsObject cms) {
697
698         // init the locale handler
699
m_localeHandler.initHandler(cms);
700         // set default locale
701
m_defaultLocale = (Locale JavaDoc)m_defaultLocales.get(0);
702         // set initialized status
703
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     /**
710      * Returns <code>true</code> if this locale manager is fully initialized.<p>
711      *
712      * This is required to prevent errors during unit tests,
713      * simple unit tests will usually not have a fully
714      * initialized locale manager available.<p>
715      *
716      * @return true if the locale manager is fully initialized
717      */

718     public boolean isInitialized() {
719
720         return m_initialized;
721     }
722
723     /**
724      * Sets the configured locale handler.<p>
725      *
726      * @param localeHandler the locale handler to set
727      */

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     /**
741      * Returns a list of available locale names derived from the given locale names.<p>
742      *
743      * Each name in the given list is checked against the internal hash map of allowed locales,
744      * and is appended to the resulting list only if the locale exists.<p>
745      *
746      * @param locales List of locales to check
747      * @return list of available locales derived from the given locale names
748      */

749     private List JavaDoc checkLocaleNames(List JavaDoc locales) {
750
751         if (locales == null) {
752             return null;
753         }
754         List JavaDoc result = new ArrayList JavaDoc();
755         Iterator JavaDoc i = locales.iterator();
756         while (i.hasNext()) {
757             Locale JavaDoc locale = (Locale JavaDoc)i.next();
758             if (m_availableLocales.contains(locale)) {
759                 result.add(locale);
760             }
761         }
762         return result;
763     }
764
765     /**
766      * Clears the caches in the locale manager.<p>
767      */

768     private void clearCaches() {
769
770         // flush all caches
771
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