KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > i18n > I18NUtil


1 package org.alfresco.i18n;
2
3 import java.text.MessageFormat JavaDoc;
4 import java.util.Enumeration JavaDoc;
5 import java.util.HashMap JavaDoc;
6 import java.util.HashSet JavaDoc;
7 import java.util.Locale JavaDoc;
8 import java.util.Map JavaDoc;
9 import java.util.ResourceBundle JavaDoc;
10 import java.util.Set JavaDoc;
11 import java.util.concurrent.locks.Lock JavaDoc;
12 import java.util.concurrent.locks.ReadWriteLock JavaDoc;
13 import java.util.concurrent.locks.ReentrantReadWriteLock JavaDoc;
14
15 /**
16  * Utility class providing methods to access the Locale of the current thread and to get
17  * Localised strings.
18  *
19  * @author Roy Wetherall
20  */

21 public class I18NUtil
22 {
23     /**
24      * Thread local containing the Local for the current thread
25      */

26     private static ThreadLocal JavaDoc<Locale JavaDoc> currentLocale = new ThreadLocal JavaDoc<Locale JavaDoc>();
27     
28     /**
29      * List of registered bundles
30      */

31     private static Set JavaDoc<String JavaDoc> resouceBundleBaseNames = new HashSet JavaDoc<String JavaDoc>();
32     
33     /**
34      * Map of loaded bundles by Locale
35      */

36     private static Map JavaDoc<Locale JavaDoc, Set JavaDoc<String JavaDoc>> loadedResourceBundles = new HashMap JavaDoc<Locale JavaDoc, Set JavaDoc<String JavaDoc>>();
37     
38     /**
39      * Map of cached messaged by Locale
40      */

41     private static Map JavaDoc<Locale JavaDoc, Map JavaDoc<String JavaDoc, String JavaDoc>> cachedMessages = new HashMap JavaDoc<Locale JavaDoc, Map JavaDoc<String JavaDoc, String JavaDoc>>();
42     
43     /**
44      * Lock objects
45      */

46     private static ReadWriteLock JavaDoc lock = new ReentrantReadWriteLock JavaDoc();
47     private static Lock JavaDoc readLock = lock.readLock();
48     private static Lock JavaDoc writeLock = lock.writeLock();
49     
50     /**
51      * Set the locale for the current thread.
52      *
53      * @param locale the locale
54      */

55     public static void setLocale(Locale JavaDoc locale)
56     {
57         currentLocale.set(locale);
58     }
59      
60     /**
61      * Get the local for the current thread, will revert to the default locale if none
62      * specified for this thread.
63      *
64      * @return the Locale
65      */

66     public static Locale JavaDoc getLocale()
67     {
68         Locale JavaDoc locale = currentLocale.get();
69         if (locale == null)
70         {
71             // Get the default locale
72
locale = Locale.getDefault();
73         }
74         return locale;
75     }
76     
77     /**
78      * Register a resource bundle.
79      * <p>
80      * This should be the bundle base name eg, alfresco.messages.errors
81      * <p>
82      * Once registered the messges will be available via getMessage
83      *
84      * @param bundleBaseName the bundle base name
85      */

86     public static void registerResourceBundle(String JavaDoc bundleBaseName)
87     {
88         try
89         {
90             writeLock.lock();
91             resouceBundleBaseNames.add(bundleBaseName);
92         }
93         finally
94         {
95             writeLock.unlock();
96         }
97     }
98     
99     /**
100      * Get message from registered resource bundle.
101      *
102      * @param messageKey message key
103      * @return localised message string, null if not found
104      */

105     public static String JavaDoc getMessage(String JavaDoc messageKey)
106     {
107         return getMessage(messageKey, getLocale());
108     }
109     
110     /**
111      * Get a localised message string
112      *
113      * @param messageKey the message key
114      * @param locale override the current locale
115      * @return the localised message string, null if not found
116      */

117     public static String JavaDoc getMessage(String JavaDoc messageKey, Locale JavaDoc locale)
118     {
119         String JavaDoc message = null;
120         Map JavaDoc<String JavaDoc, String JavaDoc> props = getLocaleProperties(locale);
121         if (props != null)
122         {
123             message = props.get(messageKey);
124         }
125         return message;
126     }
127     
128     /**
129      * Get a localised message string, parameterized using standard MessageFormatter.
130      *
131      * @param messageKey message key
132      * @param params format parameters
133      * @return the localised string, null if not found
134      */

135     public static String JavaDoc getMessage(String JavaDoc messageKey, Object JavaDoc ... params)
136     {
137         return getMessage(messageKey, getLocale(), params);
138     }
139     
140     /**
141      * Get a localised message string, parameterized using standard MessageFormatter.
142      *
143      * @param messageKey the message key
144      * @param locale override current locale
145      * @param params the localised message string
146      * @return the localaised string, null if not found
147      */

148     public static String JavaDoc getMessage(String JavaDoc messageKey, Locale JavaDoc locale, Object JavaDoc ... params)
149     {
150         String JavaDoc message = getMessage(messageKey, locale);
151         if (message != null && params != null)
152         {
153             message = MessageFormat.format(message, params);
154         }
155         return message;
156     }
157     
158     /**
159      * Get the messages for a locale.
160      * <p>
161      * Will use cache where available otherwise will load into cache from bundles.
162      *
163      * @param locale the locale
164      * @return message map
165      */

166     private static Map JavaDoc<String JavaDoc, String JavaDoc> getLocaleProperties(Locale JavaDoc locale)
167     {
168         Set JavaDoc<String JavaDoc> loadedBundles = null;
169         Map JavaDoc<String JavaDoc, String JavaDoc> props = null;
170         int loadedBundleCount = 0;
171         try
172         {
173             readLock.lock();
174             loadedBundles = loadedResourceBundles.get(locale);
175             props = cachedMessages.get(locale);
176             loadedBundleCount = resouceBundleBaseNames.size();
177         }
178         finally
179         {
180             readLock.unlock();
181         }
182         
183         if (loadedBundles == null)
184         {
185             try
186             {
187                 writeLock.lock();
188                 loadedBundles = new HashSet JavaDoc<String JavaDoc>();
189                 loadedResourceBundles.put(locale, loadedBundles);
190             }
191             finally
192             {
193                 writeLock.unlock();
194             }
195         }
196         
197         if (props == null)
198         {
199             try
200             {
201                 writeLock.lock();
202                 props = new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
203                 cachedMessages.put(locale, props);
204             }
205             finally
206             {
207                 writeLock.unlock();
208             }
209         }
210                 
211         if (loadedBundles.size() != loadedBundleCount)
212         {
213             try
214             {
215                 writeLock.lock();
216                 for (String JavaDoc resourceBundleBaseName : resouceBundleBaseNames)
217                 {
218                     if (loadedBundles.contains(resourceBundleBaseName) == false)
219                     {
220                         ResourceBundle JavaDoc resourcebundle = ResourceBundle.getBundle(resourceBundleBaseName, locale);
221                         Enumeration JavaDoc<String JavaDoc> enumKeys = resourcebundle.getKeys();
222                         while (enumKeys.hasMoreElements() == true)
223                         {
224                             String JavaDoc key = enumKeys.nextElement();
225                             props.put(key, resourcebundle.getString(key));
226                         }
227                         loadedBundles.add(resourceBundleBaseName);
228                     }
229                 }
230             }
231             finally
232             {
233                 writeLock.unlock();
234             }
235         }
236         
237         return props;
238     }
239 }
240
Popular Tags