1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.util.LocaleUtil;
28  
29  import java.io.ByteArrayInputStream;
30  import java.io.IOException;
31  
32  import java.util.HashMap;
33  import java.util.Iterator;
34  import java.util.LinkedHashMap;
35  import java.util.Locale;
36  import java.util.Map;
37  import java.util.MissingResourceException;
38  import java.util.PropertyResourceBundle;
39  import java.util.ResourceBundle;
40  import java.util.concurrent.ConcurrentHashMap;
41  
42  import javax.servlet.http.HttpServletRequest;
43  import javax.servlet.jsp.PageContext;
44  
45  import org.apache.struts.util.RequestUtils;
46  
47  /**
48   * <a href="PortletResourceBundles.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Brian Wing Shun Chan
51   *
52   */
53  public class PortletResourceBundles {
54  
55      public static String getString(PageContext pageContext, String key) {
56          return _instance._getString(pageContext, key);
57      }
58  
59      public static String getString(Locale locale, String key) {
60          return _instance._getString(locale, key);
61      }
62  
63      public static String getString(String languageId, String key) {
64          return _instance._getString(languageId, key);
65      }
66  
67      public static String getString(
68          String servletContextName, String languageId, String key) {
69  
70          return _instance._getString(servletContextName, languageId, key);
71      }
72  
73      public static void put(
74          String servletContextName, String languageId, ResourceBundle bundle) {
75  
76          _instance._put(servletContextName, languageId, bundle);
77      }
78  
79      public static void remove(String servletContextName) {
80          _instance._remove(servletContextName);
81      }
82  
83      private PortletResourceBundles() {
84          _contexts = new ConcurrentHashMap<String, Map<String, ResourceBundle>>(
85              new LinkedHashMap<String, Map<String, ResourceBundle>>());
86      }
87  
88      private ResourceBundle _getBundle(
89          String servletContextName, String languageId) {
90  
91          Map<String, ResourceBundle> bundles = _getBundles(servletContextName);
92  
93          return _getBundle(bundles, languageId);
94      }
95  
96      private ResourceBundle _getBundle(
97          Map<String, ResourceBundle> bundles, String languageId) {
98  
99          ResourceBundle bundle = bundles.get(languageId);
100 
101         if (bundle == null) {
102             try {
103                 bundle = new PropertyResourceBundle(
104                     new ByteArrayInputStream(new byte[0]));
105 
106                 bundles.put(languageId, bundle);
107             }
108             catch (IOException ioe) {
109                 _log.error(ioe);
110             }
111         }
112 
113         return bundle;
114     }
115 
116     private Map<String, ResourceBundle> _getBundles(String servletContextName) {
117         Map<String, ResourceBundle> bundles = _contexts.get(servletContextName);
118 
119         if (bundles == null) {
120             bundles = new HashMap<String, ResourceBundle>();
121 
122             _contexts.put(servletContextName, bundles);
123         }
124 
125         return bundles;
126     }
127 
128     private String _getString(PageContext pageContext, String key) {
129         Locale locale = RequestUtils.getUserLocale(
130             (HttpServletRequest)pageContext.getRequest(), null);
131 
132         return _getString(locale, key);
133     }
134 
135     private String _getString(Locale locale, String key) {
136         return _getString(LocaleUtil.toLanguageId(locale), key);
137     }
138 
139     private String _getString(String languageId, String key) {
140         return _getString(null, languageId, key);
141     }
142 
143     private String _getString(
144         String servletContextName, String languageId, String key) {
145 
146         if (servletContextName != null) {
147             ResourceBundle bundle = _getBundle(servletContextName, languageId);
148 
149             return bundle.getString(key);
150         }
151 
152         Iterator<Map.Entry<String, Map<String, ResourceBundle>>> itr =
153             _contexts.entrySet().iterator();
154 
155         while (itr.hasNext()) {
156             Map.Entry<String, Map<String, ResourceBundle>> entry = itr.next();
157 
158             Map<String, ResourceBundle> bundles = entry.getValue();
159 
160             ResourceBundle bundle = _getBundle(bundles, languageId);
161 
162             try {
163                 return bundle.getString(key);
164             }
165             catch (MissingResourceException mre) {
166             }
167         }
168 
169         return null;
170     }
171 
172     private void _put(
173         String servletContextName, String languageId, ResourceBundle bundle) {
174 
175         Map<String, ResourceBundle> bundles = _getBundles(servletContextName);
176 
177         bundles.put(languageId, bundle);
178     }
179 
180     private void _remove(String servletContextName) {
181         _contexts.remove(servletContextName);
182     }
183 
184     private static Log _log =
185          LogFactoryUtil.getLog(PortletResourceBundles.class);
186 
187     private static PortletResourceBundles _instance =
188         new PortletResourceBundles();
189 
190     private Map<String, Map<String, ResourceBundle>> _contexts;
191 
192 }