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