001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet;
016    
017    import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.util.LocaleUtil;
021    
022    import java.io.IOException;
023    
024    import java.util.HashMap;
025    import java.util.Iterator;
026    import java.util.LinkedHashMap;
027    import java.util.Locale;
028    import java.util.Map;
029    import java.util.MissingResourceException;
030    import java.util.PropertyResourceBundle;
031    import java.util.ResourceBundle;
032    import java.util.concurrent.ConcurrentHashMap;
033    
034    import javax.servlet.http.HttpServletRequest;
035    import javax.servlet.jsp.PageContext;
036    
037    import org.apache.struts.util.RequestUtils;
038    
039    /**
040     * @author Brian Wing Shun Chan
041     */
042    public class PortletResourceBundles {
043    
044            public static String getString(Locale locale, String key) {
045                    return _instance._getString(locale, key);
046            }
047    
048            public static String getString(PageContext pageContext, String key) {
049                    return _instance._getString(pageContext, key);
050            }
051    
052            public static String getString(String languageId, String key) {
053                    return _instance._getString(languageId, key);
054            }
055    
056            public static String getString(
057                    String servletContextName, String languageId, String key) {
058    
059                    return _instance._getString(servletContextName, languageId, key);
060            }
061    
062            public static void put(
063                    String servletContextName, String languageId,
064                    ResourceBundle resourceBundle) {
065    
066                    _instance._put(servletContextName, languageId, resourceBundle);
067            }
068    
069            public static void remove(String servletContextName) {
070                    _instance._remove(servletContextName);
071            }
072    
073            private Map<String, Map<String, ResourceBundle>> _resourceBundles;
074    
075            private PortletResourceBundles() {
076                    _resourceBundles =
077                            new ConcurrentHashMap<String, Map<String, ResourceBundle>>(
078                                    new LinkedHashMap<String, Map<String, ResourceBundle>>());
079            }
080    
081            private ResourceBundle _getResourceBundle(
082                    Map<String, ResourceBundle> resourceBundles, String languageId) {
083    
084                    ResourceBundle resourceBundle = resourceBundles.get(languageId);
085    
086                    if (resourceBundle == null) {
087                            try {
088                                    resourceBundle = new PropertyResourceBundle(
089                                            new UnsyncByteArrayInputStream(new byte[0]));
090    
091                                    resourceBundles.put(languageId, resourceBundle);
092                            }
093                            catch (IOException ioe) {
094                                    _log.error(ioe);
095                            }
096                    }
097    
098                    return resourceBundle;
099            }
100    
101            private ResourceBundle _getResourceBundle(
102                    String servletContextName, String languageId) {
103    
104                    Map<String, ResourceBundle> resourceBundles = _getResourceBundles(
105                            servletContextName);
106    
107                    return _getResourceBundle(resourceBundles, languageId);
108            }
109    
110            private Map<String, ResourceBundle> _getResourceBundles(
111                    String servletContextName) {
112    
113                    Map<String, ResourceBundle> resourceBundles = _resourceBundles.get(
114                            servletContextName);
115    
116                    if (resourceBundles == null) {
117                            resourceBundles = new HashMap<String, ResourceBundle>();
118    
119                            _resourceBundles.put(servletContextName, resourceBundles);
120                    }
121    
122                    return resourceBundles;
123            }
124    
125            private String _getString(Locale locale, String key) {
126                    return _getString(LocaleUtil.toLanguageId(locale), key);
127            }
128    
129            private String _getString(PageContext pageContext, String key) {
130                    Locale locale = RequestUtils.getUserLocale(
131                            (HttpServletRequest)pageContext.getRequest(), null);
132    
133                    return _getString(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 resourceBundle = _getResourceBundle(
145                                    servletContextName, languageId);
146    
147                            return resourceBundle.getString(key);
148                    }
149    
150                    Iterator<Map.Entry<String, Map<String, ResourceBundle>>> itr =
151                            _resourceBundles.entrySet().iterator();
152    
153                    while (itr.hasNext()) {
154                            Map.Entry<String, Map<String, ResourceBundle>> entry = itr.next();
155    
156                            Map<String, ResourceBundle> resourceBundles = entry.getValue();
157    
158                            ResourceBundle resourceBundle = _getResourceBundle(
159                                    resourceBundles, languageId);
160    
161                            try {
162                                    return resourceBundle.getString(key);
163                            }
164                            catch (MissingResourceException mre) {
165                            }
166                    }
167    
168                    return null;
169            }
170    
171            private void _put(
172                    String servletContextName, String languageId,
173                    ResourceBundle resourceBundle) {
174    
175                    Map<String, ResourceBundle> resourceBundles = _getResourceBundles(
176                            servletContextName);
177    
178                    resourceBundles.put(languageId, resourceBundle);
179            }
180    
181            private void _remove(String servletContextName) {
182                    _resourceBundles.remove(servletContextName);
183            }
184    
185            private static Log _log = LogFactoryUtil.getLog(
186                    PortletResourceBundles.class);
187    
188            private static PortletResourceBundles _instance =
189                    new PortletResourceBundles();
190    
191    }