KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > portlet > plugins > language > ResourceBundles


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.portlet.plugins.language;
10
11 import java.util.HashMap JavaDoc;
12 import java.util.Iterator JavaDoc;
13 import java.util.Locale JavaDoc;
14 import java.util.Map JavaDoc;
15 import java.util.ResourceBundle JavaDoc;
16 import java.util.MissingResourceException JavaDoc;
17
18 import org.apache.log4j.Logger;
19 import org.jboss.portal.common.metadata.MetaData;
20 import org.jboss.portal.common.util.ParentChildResourceBundle;
21 import org.jboss.portal.common.util.Tools;
22 import org.jboss.portal.portlet.metadata.LanguagesMetaData;
23 import org.jboss.portal.server.Application;
24 import org.jboss.portal.server.Component;
25 import org.jboss.portal.server.plugins.PluginService;
26
27 /**
28  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
29  * @author <a HREF="mailto:theute@jboss.org">Thomas Heute</a>
30  * @version $Revision: 1.9 $
31  */

32 public class ResourceBundles
33    extends PluginService
34 {
35
36    private Logger log = Logger.getLogger(ResourceBundles.class);
37
38    /** */
39    private ResourceBundle JavaDoc defaultBundle;
40
41    /** */
42    private Map JavaDoc localeBundles;
43
44    /** */
45    private LanguagesMetaData metaData;
46
47    /**
48     * No arg constructor for the object factory.
49     */

50    public ResourceBundles()
51    {
52    }
53
54    /**
55     * Constructor that fully initialize the object.
56     */

57    public ResourceBundles(LanguagesMetaData metaData, ClassLoader JavaDoc loader)
58    {
59       init(metaData, loader);
60       this.metaData = metaData;
61    }
62
63    public void start() throws Exception JavaDoc
64    {
65       Component component = (Component)container;
66       Application application = component.getApplication();
67       ClassLoader JavaDoc loader = application.getClassLoader();
68       init(this.metaData, loader);
69    }
70
71    public void setMetaData(MetaData metaData)
72    {
73       this.metaData = (LanguagesMetaData)metaData;
74    }
75
76    public MetaData getMetaData()
77    {
78       return metaData;
79    }
80
81    /**
82     * The default bundle.
83     */

84    public ResourceBundle JavaDoc getDefaultBundle()
85    {
86       return defaultBundle;
87    }
88
89    /**
90     * Return a bundle for the given locale. If the complete locale (language + country + variant) does not
91     * exist then it falls back to (language + country) or (language) or the default file.
92     *
93     * When the resource bundle object is found and was not in the global map, it put it in that map
94     * with a copy on write.
95     *
96     * @throws IllegalArgumentException if the locale is null
97     */

98    public ResourceBundle JavaDoc get(Locale JavaDoc locale) throws IllegalArgumentException JavaDoc
99    {
100       // Arg check
101
if (locale == null)
102       {
103          throw new IllegalArgumentException JavaDoc("Locale cannot be null");
104       }
105
106       // Try to get the bundle if the map
107
ResourceBundle JavaDoc bundle = (ResourceBundle JavaDoc)localeBundles.get(locale);
108       if (bundle != null)
109       {
110          return bundle;
111       }
112
113       // Try to fallback on language/country
114
if (!locale.getVariant().equals(""))
115       {
116          Locale JavaDoc localeLC = new Locale JavaDoc(locale.getLanguage(), locale.getCountry());
117          bundle = (ResourceBundle JavaDoc)localeBundles.get(localeLC);
118          if (bundle != null)
119          {
120             // Cache it with copy on write
121
Map JavaDoc tmp = new HashMap JavaDoc(localeBundles);
122             tmp.put(localeLC, bundle);
123             localeBundles = tmp;
124             return bundle;
125          }
126       }
127       
128       // Try to fallback on language
129
if (!locale.getCountry().equals(""))
130       {
131          Locale JavaDoc localeL = new Locale JavaDoc(locale.getLanguage());
132          bundle = (ResourceBundle JavaDoc)localeBundles.get(localeL);
133          if (bundle != null)
134          {
135             // Cache it with copy on write
136
Map JavaDoc tmp = new HashMap JavaDoc(localeBundles);
137             tmp.put(localeL, bundle);
138             localeBundles = tmp;
139             return bundle;
140          }
141       }
142       
143       return defaultBundle;
144    }
145
146    private void init(LanguagesMetaData languagesMD, ClassLoader JavaDoc loader)
147    {
148       // Get the resource bundle containing the portlet info
149
LanguagesMetaData.InfoMetaData infoMD = languagesMD.getInfo();
150       ResourceBundle JavaDoc infoBundle = infoMD != null ? new InfoBundle(infoMD) : null;
151
152       // Create the default bundle if possible, if no default bundle look for language en
153
ResourceBundle JavaDoc defaultLoaderBundle = null;
154       String JavaDoc resourceBundle = languagesMD.getResourceBundle();
155       if (resourceBundle != null)
156       {
157          defaultLoaderBundle = loadBundle(loader, resourceBundle, Locale.ENGLISH);
158       }
159
160       // If we have built a default bundle use it otherwise use the info bundle
161
if (defaultLoaderBundle != null)
162       {
163          if (infoBundle != null)
164          {
165             defaultBundle = new ParentChildResourceBundle(infoBundle, defaultLoaderBundle);
166          }
167          else
168          {
169             defaultBundle = defaultLoaderBundle;
170          }
171       }
172       else
173       {
174          if (infoBundle != null)
175          {
176             defaultBundle = infoBundle;
177          }
178          else
179          {
180             defaultBundle = Tools.EMPTY_BUNDLE;
181          }
182       }
183
184       //
185
localeBundles = new HashMap JavaDoc();
186       String JavaDoc resourceBundleName = languagesMD.getResourceBundle();
187       if (resourceBundleName != null)
188       {
189          // Build locale bundles
190
for (Iterator JavaDoc i = languagesMD.getSupportedLocales().iterator();i.hasNext();)
191          {
192             Locale JavaDoc tmp = (Locale JavaDoc)i.next();
193             ResourceBundle JavaDoc localeBundle = new ParentChildResourceBundle(Tools.EMPTY_BUNDLE, defaultBundle);
194
195             ResourceBundle JavaDoc tmpBundle = loadBundle(loader, resourceBundleName, tmp);
196             if (tmpBundle != null)
197             {
198                localeBundle = new ParentChildResourceBundle(localeBundle, tmpBundle);
199             }
200
201             localeBundles.put(tmp, localeBundle);
202          }
203       }
204       else
205       {
206          // Should we warn that no resource bundle is specified but locales are declared ?
207
}
208    }
209
210    /**
211     * Simply load the bundle or return null if not found.
212     */

213    private ResourceBundle JavaDoc loadBundle(ClassLoader JavaDoc loader, String JavaDoc baseName, Locale JavaDoc locale)
214    {
215       log.debug("Want to load bundle " + baseName + " for locale " + locale);
216       ResourceBundle JavaDoc bundle = null;
217       try
218       {
219          bundle = ResourceBundle.getBundle(baseName, locale, loader);
220          log.debug("Created bundle " + baseName + " for locale " + locale);
221       }
222       catch (MissingResourceException JavaDoc e)
223       {
224          log.warn("Bundle " + baseName + " for locale " + locale + " not found", e);
225       }
226       return bundle;
227    }
228 }
229
Popular Tags