001    /**
002     * Copyright (c) 2000-2013 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    import com.liferay.portal.kernel.util.ResourceBundleUtil;
022    
023    import java.io.IOException;
024    
025    import java.util.HashMap;
026    import java.util.Iterator;
027    import java.util.LinkedHashMap;
028    import java.util.Locale;
029    import java.util.Map;
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 PortletResourceBundles() {
074                    _resourceBundles =
075                            new ConcurrentHashMap<String, Map<String, ResourceBundle>>(
076                                    new LinkedHashMap<String, Map<String, ResourceBundle>>());
077            }
078    
079            private ResourceBundle _getResourceBundle(
080                    Map<String, ResourceBundle> resourceBundles, String languageId) {
081    
082                    ResourceBundle resourceBundle = resourceBundles.get(languageId);
083    
084                    if (resourceBundle == null) {
085                            try {
086                                    resourceBundle = new NullSafeResourceBundle(
087                                            new PropertyResourceBundle(
088                                                    new UnsyncByteArrayInputStream(new byte[0])));
089    
090                                    resourceBundles.put(languageId, resourceBundle);
091                            }
092                            catch (IOException ioe) {
093                                    _log.error(ioe);
094                            }
095                    }
096    
097                    return resourceBundle;
098            }
099    
100            private ResourceBundle _getResourceBundle(
101                    String servletContextName, String languageId) {
102    
103                    Map<String, ResourceBundle> resourceBundles = _getResourceBundles(
104                            servletContextName);
105    
106                    return _getResourceBundle(resourceBundles, languageId);
107            }
108    
109            private Map<String, ResourceBundle> _getResourceBundles(
110                    String servletContextName) {
111    
112                    Map<String, ResourceBundle> resourceBundles = _resourceBundles.get(
113                            servletContextName);
114    
115                    if (resourceBundles == null) {
116                            resourceBundles = new HashMap<String, ResourceBundle>();
117    
118                            _resourceBundles.put(servletContextName, resourceBundles);
119                    }
120    
121                    return resourceBundles;
122            }
123    
124            private String _getString(Locale locale, String key) {
125                    return _getString(LocaleUtil.toLanguageId(locale), key);
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(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 resourceBundle = _getResourceBundle(
144                                    servletContextName, languageId);
145    
146                            return resourceBundle.getString(key);
147                    }
148    
149                    Iterator<Map.Entry<String, Map<String, ResourceBundle>>> itr =
150                            _resourceBundles.entrySet().iterator();
151    
152                    while (itr.hasNext()) {
153                            Map.Entry<String, Map<String, ResourceBundle>> entry = itr.next();
154    
155                            Map<String, ResourceBundle> resourceBundles = entry.getValue();
156    
157                            ResourceBundle resourceBundle = _getResourceBundle(
158                                    resourceBundles, languageId);
159    
160                            return ResourceBundleUtil.getString(resourceBundle, key);
161                    }
162    
163                    return null;
164            }
165    
166            private void _put(
167                    String servletContextName, String languageId,
168                    ResourceBundle resourceBundle) {
169    
170                    Map<String, ResourceBundle> resourceBundles = _getResourceBundles(
171                            servletContextName);
172    
173                    resourceBundle = new NullSafeResourceBundle(resourceBundle);
174    
175                    resourceBundles.put(languageId, resourceBundle);
176            }
177    
178            private void _remove(String servletContextName) {
179                    _resourceBundles.remove(servletContextName);
180            }
181    
182            private static Log _log = LogFactoryUtil.getLog(
183                    PortletResourceBundles.class);
184    
185            private static PortletResourceBundles _instance =
186                    new PortletResourceBundles();
187    
188            private Map<String, Map<String, ResourceBundle>> _resourceBundles;
189    
190    }