001    /**
002     * Copyright (c) 2000-present 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.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    /**
045     * @author Brian Wing Shun Chan
046     * @author Eduardo Lundgren
047     * @author Shuyang Zhou
048     */
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                    if (resourceBundle == null) {
171                            if (!_portletApp.isWARFile() &&
172                                    resourceBundleClassName.equals(
173                                            StrutsResourceBundle.class.getName())) {
174    
175                                    String resourceBundleId = sb.toString();
176    
177                                    resourceBundle = _resourceBundles.get(resourceBundleId);
178    
179                                    if (resourceBundle == null) {
180                                            resourceBundle = new StrutsResourceBundle(
181                                                    _portletName, locale);
182                                    }
183    
184                                    _resourceBundles.put(resourceBundleId, resourceBundle);
185                            }
186                            else {
187                                    PortletBag portletBag = PortletBagPool.get(
188                                            _portlet.getRootPortletId());
189    
190                                    resourceBundle = portletBag.getResourceBundle(locale);
191                            }
192    
193                            resourceBundle = new PortletResourceBundle(
194                                    resourceBundle, _portlet.getPortletInfo());
195                    }
196    
197                    return resourceBundle;
198            }
199    
200            @Override
201            public Enumeration<Locale> getSupportedLocales() {
202                    List<Locale> supportedLocales = new ArrayList<>();
203    
204                    for (String languageId : _portlet.getSupportedLocales()) {
205                            supportedLocales.add(LocaleUtil.fromLanguageId(languageId));
206                    }
207    
208                    return Collections.enumeration(supportedLocales);
209            }
210    
211            @Override
212            public boolean isCopyRequestParameters() {
213                    return _copyRequestParameters;
214            }
215    
216            @Override
217            public boolean isWARFile() {
218                    return _portletApp.isWARFile();
219            }
220    
221            protected Set<javax.xml.namespace.QName> toJavaxQNames(
222                    Set<com.liferay.portal.kernel.xml.QName> liferayQNames) {
223    
224                    Set<QName> javaxQNames = new HashSet<>(liferayQNames.size());
225    
226                    for (com.liferay.portal.kernel.xml.QName liferayQName : liferayQNames) {
227                            QName javaxQName = new QName(
228                                    liferayQName.getNamespaceURI(), liferayQName.getLocalPart(),
229                                    liferayQName.getNamespacePrefix());
230    
231                            javaxQNames.add(javaxQName);
232                    }
233    
234                    return javaxQNames;
235            }
236    
237            private final boolean _copyRequestParameters;
238            private final Portlet _portlet;
239            private final PortletApp _portletApp;
240            private final PortletContext _portletContext;
241            private final String _portletName;
242            private final Map<String, ResourceBundle> _resourceBundles;
243    
244    }