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