001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet;
022:
023: import com.liferay.portal.kernel.util.LocaleUtil;
024: import com.liferay.util.CollectionFactory;
025:
026: import java.io.ByteArrayInputStream;
027: import java.io.IOException;
028:
029: import java.util.Iterator;
030: import java.util.LinkedHashMap;
031: import java.util.Locale;
032: import java.util.Map;
033: import java.util.MissingResourceException;
034: import java.util.PropertyResourceBundle;
035: import java.util.ResourceBundle;
036:
037: import javax.servlet.http.HttpServletRequest;
038: import javax.servlet.jsp.PageContext;
039:
040: import org.apache.commons.logging.Log;
041: import org.apache.commons.logging.LogFactory;
042: import org.apache.struts.util.RequestUtils;
043:
044: /**
045: * <a href="PortletResourceBundles.java.html"><b><i>View Source</i></b></a>
046: *
047: * @author Brian Wing Shun Chan
048: *
049: */
050: public class PortletResourceBundles {
051:
052: public static String getString(PageContext pageContext, String key) {
053: return _instance._getString(pageContext, key);
054: }
055:
056: public static String getString(Locale locale, String key) {
057: return _instance._getString(locale, key);
058: }
059:
060: public static String getString(String languageId, String key) {
061: return _instance._getString(languageId, key);
062: }
063:
064: public static String getString(String servletContextName,
065: String languageId, String key) {
066:
067: return _instance
068: ._getString(servletContextName, languageId, key);
069: }
070:
071: public static void put(String servletContextName,
072: String languageId, ResourceBundle bundle) {
073:
074: _instance._put(servletContextName, languageId, bundle);
075: }
076:
077: public static void remove(String servletContextName) {
078: _instance._remove(servletContextName);
079: }
080:
081: private PortletResourceBundles() {
082: _contexts = CollectionFactory
083: .getSyncHashMap(new LinkedHashMap());
084: }
085:
086: private ResourceBundle _getBundle(String servletContextName,
087: String languageId) {
088:
089: Map bundles = _getBundles(servletContextName);
090:
091: return _getBundle(bundles, languageId);
092: }
093:
094: private ResourceBundle _getBundle(Map bundles, String languageId) {
095: ResourceBundle bundle = (ResourceBundle) bundles
096: .get(languageId);
097:
098: if (bundle == null) {
099: try {
100: bundle = new PropertyResourceBundle(
101: new ByteArrayInputStream(new byte[0]));
102:
103: bundles.put(languageId, bundle);
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(String servletContextName,
140: String languageId, String key) {
141:
142: if (servletContextName != null) {
143: ResourceBundle bundle = _getBundle(servletContextName,
144: languageId);
145:
146: return bundle.getString(key);
147: }
148:
149: Iterator itr = _contexts.entrySet().iterator();
150:
151: while (itr.hasNext()) {
152: Map.Entry entry = (Map.Entry) itr.next();
153:
154: Map bundles = (Map) entry.getValue();
155:
156: ResourceBundle bundle = _getBundle(bundles, languageId);
157:
158: try {
159: return bundle.getString(key);
160: } catch (MissingResourceException mre) {
161: }
162: }
163:
164: return null;
165: }
166:
167: private void _put(String servletContextName, String languageId,
168: 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
180: .getLog(PortletResourceBundles.class);
181:
182: private static PortletResourceBundles _instance = new PortletResourceBundles();
183:
184: private Map _contexts;
185:
186: }
|