1
14
15 package com.liferay.portlet;
16
17 import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
18 import com.liferay.portal.kernel.log.Log;
19 import com.liferay.portal.kernel.log.LogFactoryUtil;
20 import com.liferay.portal.kernel.util.LocaleUtil;
21
22 import java.io.IOException;
23
24 import java.util.HashMap;
25 import java.util.Iterator;
26 import java.util.LinkedHashMap;
27 import java.util.Locale;
28 import java.util.Map;
29 import java.util.MissingResourceException;
30 import java.util.PropertyResourceBundle;
31 import java.util.ResourceBundle;
32 import java.util.concurrent.ConcurrentHashMap;
33
34 import javax.servlet.http.HttpServletRequest;
35 import javax.servlet.jsp.PageContext;
36
37 import org.apache.struts.util.RequestUtils;
38
39
44 public class PortletResourceBundles {
45
46 public static String getString(PageContext pageContext, String key) {
47 return _instance._getString(pageContext, key);
48 }
49
50 public static String getString(Locale locale, String key) {
51 return _instance._getString(locale, key);
52 }
53
54 public static String getString(String languageId, String key) {
55 return _instance._getString(languageId, key);
56 }
57
58 public static String getString(
59 String servletContextName, String languageId, String key) {
60
61 return _instance._getString(servletContextName, languageId, key);
62 }
63
64 public static void put(
65 String servletContextName, String languageId, ResourceBundle bundle) {
66
67 _instance._put(servletContextName, languageId, bundle);
68 }
69
70 public static void remove(String servletContextName) {
71 _instance._remove(servletContextName);
72 }
73
74 private PortletResourceBundles() {
75 _contexts = new ConcurrentHashMap<String, Map<String, ResourceBundle>>(
76 new LinkedHashMap<String, Map<String, ResourceBundle>>());
77 }
78
79 private ResourceBundle _getBundle(
80 String servletContextName, String languageId) {
81
82 Map<String, ResourceBundle> bundles = _getBundles(servletContextName);
83
84 return _getBundle(bundles, languageId);
85 }
86
87 private ResourceBundle _getBundle(
88 Map<String, ResourceBundle> bundles, String languageId) {
89
90 ResourceBundle bundle = bundles.get(languageId);
91
92 if (bundle == null) {
93 try {
94 bundle = new PropertyResourceBundle(
95 new UnsyncByteArrayInputStream(new byte[0]));
96
97 bundles.put(languageId, bundle);
98 }
99 catch (IOException ioe) {
100 _log.error(ioe);
101 }
102 }
103
104 return bundle;
105 }
106
107 private Map<String, ResourceBundle> _getBundles(String servletContextName) {
108 Map<String, ResourceBundle> bundles = _contexts.get(servletContextName);
109
110 if (bundles == null) {
111 bundles = new HashMap<String, ResourceBundle>();
112
113 _contexts.put(servletContextName, bundles);
114 }
115
116 return bundles;
117 }
118
119 private String _getString(PageContext pageContext, String key) {
120 Locale locale = RequestUtils.getUserLocale(
121 (HttpServletRequest)pageContext.getRequest(), null);
122
123 return _getString(locale, key);
124 }
125
126 private String _getString(Locale locale, String key) {
127 return _getString(LocaleUtil.toLanguageId(locale), key);
128 }
129
130 private String _getString(String languageId, String key) {
131 return _getString(null, languageId, key);
132 }
133
134 private String _getString(
135 String servletContextName, String languageId, String key) {
136
137 if (servletContextName != null) {
138 ResourceBundle bundle = _getBundle(servletContextName, languageId);
139
140 return bundle.getString(key);
141 }
142
143 Iterator<Map.Entry<String, Map<String, ResourceBundle>>> itr =
144 _contexts.entrySet().iterator();
145
146 while (itr.hasNext()) {
147 Map.Entry<String, Map<String, ResourceBundle>> entry = itr.next();
148
149 Map<String, ResourceBundle> bundles = entry.getValue();
150
151 ResourceBundle bundle = _getBundle(bundles, languageId);
152
153 try {
154 return bundle.getString(key);
155 }
156 catch (MissingResourceException mre) {
157 }
158 }
159
160 return null;
161 }
162
163 private void _put(
164 String servletContextName, String languageId, ResourceBundle bundle) {
165
166 Map<String, ResourceBundle> bundles = _getBundles(servletContextName);
167
168 bundles.put(languageId, bundle);
169 }
170
171 private void _remove(String servletContextName) {
172 _contexts.remove(servletContextName);
173 }
174
175 private static Log _log = LogFactoryUtil.getLog(
176 PortletResourceBundles.class);
177
178 private static PortletResourceBundles _instance =
179 new PortletResourceBundles();
180
181 private Map<String, Map<String, ResourceBundle>> _contexts;
182
183 }