001
014
015 package com.liferay.portlet;
016
017 import com.liferay.portal.kernel.portlet.LiferayPortletConfig;
018 import com.liferay.portal.kernel.portlet.PortletBag;
019 import com.liferay.portal.kernel.portlet.PortletBagPool;
020 import com.liferay.portal.kernel.util.GetterUtil;
021 import com.liferay.portal.kernel.util.LocaleUtil;
022 import com.liferay.portal.kernel.util.StringBundler;
023 import com.liferay.portal.kernel.util.Validator;
024 import com.liferay.portal.model.Portlet;
025 import com.liferay.portal.model.PortletApp;
026 import com.liferay.portal.model.PortletConstants;
027 import com.liferay.portal.model.PublicRenderParameter;
028
029 import java.util.ArrayList;
030 import java.util.Collections;
031 import java.util.Enumeration;
032 import java.util.HashSet;
033 import java.util.List;
034 import java.util.Locale;
035 import java.util.Map;
036 import java.util.ResourceBundle;
037 import java.util.Set;
038 import java.util.concurrent.ConcurrentHashMap;
039
040 import javax.portlet.PortletContext;
041
042 import javax.xml.namespace.QName;
043
044
049 public class PortletConfigImpl implements LiferayPortletConfig {
050
051 public PortletConfigImpl(Portlet portlet, PortletContext portletContext) {
052 _portlet = portlet;
053 _portletContext = portletContext;
054
055 _copyRequestParameters = GetterUtil.getBoolean(
056 getInitParameter("copy-request-parameters"));
057 _portletApp = portlet.getPortletApp();
058
059 String portletName = portlet.getRootPortletId();
060
061 int pos = portletName.indexOf(PortletConstants.WAR_SEPARATOR);
062
063 if (pos != -1) {
064 portletName = portletName.substring(0, pos);
065 }
066
067 _portletName = portletName;
068
069 _resourceBundles = new ConcurrentHashMap<>();
070 }
071
072 @Override
073 public Map<String, String[]> getContainerRuntimeOptions() {
074 return _portletApp.getContainerRuntimeOptions();
075 }
076
077 @Override
078 public String getDefaultNamespace() {
079 return _portletApp.getDefaultNamespace();
080 }
081
082 @Override
083 public String getInitParameter(String name) {
084 if (name == null) {
085 throw new IllegalArgumentException();
086 }
087
088 return _portlet.getInitParams().get(name);
089 }
090
091 @Override
092 public Enumeration<String> getInitParameterNames() {
093 return Collections.enumeration(_portlet.getInitParams().keySet());
094 }
095
096 @Override
097 public Portlet getPortlet() {
098 return _portlet;
099 }
100
101 @Override
102 public PortletContext getPortletContext() {
103 return _portletContext;
104 }
105
106 @Override
107 public String getPortletId() {
108 return _portlet.getPortletId();
109 }
110
111 @Override
112 public String getPortletName() {
113 return _portletName;
114 }
115
116 @Override
117 public Enumeration<QName> getProcessingEventQNames() {
118 return Collections.enumeration(
119 toJavaxQNames(_portlet.getProcessingEvents()));
120 }
121
122 @Override
123 public Enumeration<String> getPublicRenderParameterNames() {
124 List<String> publicRenderParameterNames = new ArrayList<>();
125
126 for (PublicRenderParameter publicRenderParameter :
127 _portlet.getPublicRenderParameters()) {
128
129 publicRenderParameterNames.add(
130 publicRenderParameter.getIdentifier());
131 }
132
133 return Collections.enumeration(publicRenderParameterNames);
134 }
135
136 @Override
137 public Enumeration<QName> getPublishingEventQNames() {
138 return Collections.enumeration(
139 toJavaxQNames(_portlet.getPublishingEvents()));
140 }
141
142 @Override
143 public ResourceBundle getResourceBundle(Locale locale) {
144 String resourceBundleClassName = _portlet.getResourceBundle();
145
146 ResourceBundle resourceBundle = null;
147
148 if (Validator.isNull(resourceBundleClassName)) {
149 String resourceBundleId = _portlet.getPortletId();
150
151 resourceBundle = _resourceBundles.get(resourceBundleId);
152
153 if (resourceBundle == null) {
154 resourceBundle = new PortletResourceBundle(
155 _portlet.getPortletInfo());
156
157 _resourceBundles.put(resourceBundleId, resourceBundle);
158 }
159
160 return resourceBundle;
161 }
162
163 StringBundler sb = new StringBundler(4);
164
165 sb.append(_portlet.getPortletId());
166 sb.append(locale.getLanguage());
167 sb.append(locale.getCountry());
168 sb.append(locale.getVariant());
169
170 String resourceBundleId = sb.toString();
171
172 resourceBundle = _resourceBundles.get(resourceBundleId);
173
174 if (resourceBundle == null) {
175 if (!_portletApp.isWARFile() &&
176 resourceBundleClassName.equals(
177 StrutsResourceBundle.class.getName())) {
178
179 resourceBundle = new StrutsResourceBundle(_portletName, locale);
180 }
181 else {
182 PortletBag portletBag = PortletBagPool.get(
183 _portlet.getRootPortletId());
184
185 resourceBundle = portletBag.getResourceBundle(locale);
186 }
187
188 resourceBundle = new PortletResourceBundle(
189 resourceBundle, _portlet.getPortletInfo());
190
191 _resourceBundles.put(resourceBundleId, resourceBundle);
192 }
193
194 return resourceBundle;
195 }
196
197 @Override
198 public Enumeration<Locale> getSupportedLocales() {
199 List<Locale> supportedLocales = new ArrayList<>();
200
201 for (String languageId : _portlet.getSupportedLocales()) {
202 supportedLocales.add(LocaleUtil.fromLanguageId(languageId));
203 }
204
205 return Collections.enumeration(supportedLocales);
206 }
207
208 @Override
209 public boolean isCopyRequestParameters() {
210 return _copyRequestParameters;
211 }
212
213 @Override
214 public boolean isWARFile() {
215 return _portletApp.isWARFile();
216 }
217
218 protected Set<javax.xml.namespace.QName> toJavaxQNames(
219 Set<com.liferay.portal.kernel.xml.QName> liferayQNames) {
220
221 Set<QName> javaxQNames = new HashSet<>(liferayQNames.size());
222
223 for (com.liferay.portal.kernel.xml.QName liferayQName : liferayQNames) {
224 QName javaxQName = new QName(
225 liferayQName.getNamespaceURI(), liferayQName.getLocalPart(),
226 liferayQName.getNamespacePrefix());
227
228 javaxQNames.add(javaxQName);
229 }
230
231 return javaxQNames;
232 }
233
234 private final boolean _copyRequestParameters;
235 private final Portlet _portlet;
236 private final PortletApp _portletApp;
237 private final PortletContext _portletContext;
238 private final String _portletName;
239 private final Map<String, ResourceBundle> _resourceBundles;
240
241 }