1   /**
2    * Copyright (c) 2000-2007 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.util.LocaleUtil;
26  import com.liferay.util.CollectionFactory;
27  
28  import java.io.ByteArrayInputStream;
29  import java.io.IOException;
30  
31  import java.util.Iterator;
32  import java.util.LinkedHashMap;
33  import java.util.Locale;
34  import java.util.Map;
35  import java.util.MissingResourceException;
36  import java.util.PropertyResourceBundle;
37  import java.util.ResourceBundle;
38  
39  import javax.servlet.http.HttpServletRequest;
40  import javax.servlet.jsp.PageContext;
41  
42  import org.apache.commons.logging.Log;
43  import org.apache.commons.logging.LogFactory;
44  import org.apache.struts.util.RequestUtils;
45  
46  /**
47   * <a href="PortletResourceBundles.java.html"><b><i>View Source</i></b></a>
48   *
49   * @author Brian Wing Shun Chan
50   *
51   */
52  public class PortletResourceBundles {
53  
54      public static String getString(PageContext pageContext, String key) {
55          return _instance._getString(pageContext, key);
56      }
57  
58      public static String getString(Locale locale, String key) {
59          return _instance._getString(locale, key);
60      }
61  
62      public static String getString(String languageId, String key) {
63          return _instance._getString(languageId, key);
64      }
65  
66      public static String getString(
67          String servletContextName, String languageId, String key) {
68  
69          return _instance._getString(servletContextName, languageId, key);
70      }
71  
72      public static void put(
73          String servletContextName, String languageId, ResourceBundle bundle) {
74  
75          _instance._put(servletContextName, languageId, bundle);
76      }
77  
78      public static void remove(String servletContextName) {
79          _instance._remove(servletContextName);
80      }
81  
82      private PortletResourceBundles() {
83          _contexts = CollectionFactory.getSyncHashMap(new LinkedHashMap());
84      }
85  
86      private ResourceBundle _getBundle(
87          String servletContextName, String languageId) {
88  
89          Map bundles = _getBundles(servletContextName);
90  
91          return _getBundle(bundles, languageId);
92      }
93  
94      private ResourceBundle _getBundle(Map bundles, String languageId) {
95          ResourceBundle bundle = (ResourceBundle)bundles.get(languageId);
96  
97          if (bundle == null) {
98              try {
99                  bundle = new PropertyResourceBundle(
100                     new ByteArrayInputStream(new byte[0]));
101 
102                 bundles.put(languageId, bundle);
103             }
104             catch (IOException ioe) {
105                 _log.error(ioe);
106             }
107         }
108 
109         return bundle;
110     }
111 
112     private Map _getBundles(String servletContextName) {
113         Map bundles = (Map)_contexts.get(servletContextName);
114 
115         if (bundles == null) {
116             bundles = CollectionFactory.getHashMap();
117 
118             _contexts.put(servletContextName, bundles);
119         }
120 
121         return bundles;
122     }
123 
124     private String _getString(PageContext pageContext, String key) {
125         Locale locale = RequestUtils.getUserLocale(
126             (HttpServletRequest)pageContext.getRequest(), null);
127 
128         return _getString(locale, key);
129     }
130 
131     private String _getString(Locale locale, String key) {
132         return _getString(LocaleUtil.toLanguageId(locale), key);
133     }
134 
135     private String _getString(String languageId, String key) {
136         return _getString(null, languageId, key);
137     }
138 
139     private String _getString(
140         String servletContextName, String languageId, String key) {
141 
142         if (servletContextName != null) {
143             ResourceBundle bundle = _getBundle(servletContextName, languageId);
144 
145             return bundle.getString(key);
146         }
147 
148         Iterator itr = _contexts.entrySet().iterator();
149 
150         while (itr.hasNext()) {
151             Map.Entry entry = (Map.Entry)itr.next();
152 
153             Map bundles = (Map)entry.getValue();
154 
155             ResourceBundle bundle = _getBundle(bundles, languageId);
156 
157             try {
158                 return bundle.getString(key);
159             }
160             catch (MissingResourceException mre) {
161             }
162         }
163 
164         return null;
165     }
166 
167     private void _put(
168         String servletContextName, String languageId, ResourceBundle bundle) {
169 
170         Map bundles = _getBundles(servletContextName);
171 
172         bundles.put(languageId, bundle);
173     }
174 
175     private void _remove(String servletContextName) {
176         _contexts.remove(servletContextName);
177     }
178 
179     private static Log _log = LogFactory.getLog(PortletResourceBundles.class);
180 
181     private static PortletResourceBundles _instance =
182         new PortletResourceBundles();
183 
184     private Map _contexts;
185 
186 }