1
22
23 package com.liferay.portlet;
24
25 import com.liferay.portal.kernel.log.Log;
26 import com.liferay.portal.kernel.log.LogFactoryUtil;
27 import com.liferay.portal.kernel.util.LocaleUtil;
28
29 import java.io.ByteArrayInputStream;
30 import java.io.IOException;
31
32 import java.util.HashMap;
33 import java.util.Iterator;
34 import java.util.LinkedHashMap;
35 import java.util.Locale;
36 import java.util.Map;
37 import java.util.MissingResourceException;
38 import java.util.PropertyResourceBundle;
39 import java.util.ResourceBundle;
40 import java.util.concurrent.ConcurrentHashMap;
41
42 import javax.servlet.http.HttpServletRequest;
43 import javax.servlet.jsp.PageContext;
44
45 import org.apache.struts.util.RequestUtils;
46
47
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 = new ConcurrentHashMap<String, Map<String, ResourceBundle>>(
84 new LinkedHashMap<String, Map<String, ResourceBundle>>());
85 }
86
87 private ResourceBundle _getBundle(
88 String servletContextName, String languageId) {
89
90 Map<String, ResourceBundle> bundles = _getBundles(servletContextName);
91
92 return _getBundle(bundles, languageId);
93 }
94
95 private ResourceBundle _getBundle(
96 Map<String, ResourceBundle> bundles, String languageId) {
97
98 ResourceBundle bundle = bundles.get(languageId);
99
100 if (bundle == null) {
101 try {
102 bundle = new PropertyResourceBundle(
103 new ByteArrayInputStream(new byte[0]));
104
105 bundles.put(languageId, bundle);
106 }
107 catch (IOException ioe) {
108 _log.error(ioe);
109 }
110 }
111
112 return bundle;
113 }
114
115 private Map<String, ResourceBundle> _getBundles(String servletContextName) {
116 Map<String, ResourceBundle> bundles = _contexts.get(servletContextName);
117
118 if (bundles == null) {
119 bundles = new HashMap<String, ResourceBundle>();
120
121 _contexts.put(servletContextName, bundles);
122 }
123
124 return bundles;
125 }
126
127 private String _getString(PageContext pageContext, String key) {
128 Locale locale = RequestUtils.getUserLocale(
129 (HttpServletRequest)pageContext.getRequest(), null);
130
131 return _getString(locale, key);
132 }
133
134 private String _getString(Locale locale, String key) {
135 return _getString(LocaleUtil.toLanguageId(locale), key);
136 }
137
138 private String _getString(String languageId, String key) {
139 return _getString(null, languageId, key);
140 }
141
142 private String _getString(
143 String servletContextName, String languageId, String key) {
144
145 if (servletContextName != null) {
146 ResourceBundle bundle = _getBundle(servletContextName, languageId);
147
148 return bundle.getString(key);
149 }
150
151 Iterator<Map.Entry<String, Map<String, ResourceBundle>>> itr =
152 _contexts.entrySet().iterator();
153
154 while (itr.hasNext()) {
155 Map.Entry<String, Map<String, ResourceBundle>> entry = itr.next();
156
157 Map<String, ResourceBundle> bundles = entry.getValue();
158
159 ResourceBundle bundle = _getBundle(bundles, languageId);
160
161 try {
162 return bundle.getString(key);
163 }
164 catch (MissingResourceException mre) {
165 }
166 }
167
168 return null;
169 }
170
171 private void _put(
172 String servletContextName, String languageId, ResourceBundle bundle) {
173
174 Map<String, ResourceBundle> bundles = _getBundles(servletContextName);
175
176 bundles.put(languageId, bundle);
177 }
178
179 private void _remove(String servletContextName) {
180 _contexts.remove(servletContextName);
181 }
182
183 private static Log _log =
184 LogFactoryUtil.getLog(PortletResourceBundles.class);
185
186 private static PortletResourceBundles _instance =
187 new PortletResourceBundles();
188
189 private Map<String, Map<String, ResourceBundle>> _contexts;
190
191 }