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                    else {
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(
177                                                    _portletName, locale);
178                                    }
179                                    else {
180                                            PortletBag portletBag = PortletBagPool.get(
181                                                    _portlet.getRootPortletId());
182    
183                                            resourceBundle = portletBag.getResourceBundle(locale);
184                                    }
185    
186                                    resourceBundle = new PortletResourceBundle(
187                                            resourceBundle, _portlet.getPortletInfo());
188    
189                                    _resourceBundles.put(resourceBundleId, resourceBundle);
190                            }
191    
192                            return resourceBundle;
193                    }
194            }
195    
196            @Override
197            public Enumeration<Locale> getSupportedLocales() {
198                    List<Locale> supportedLocales = new ArrayList<Locale>();
199    
200                    for (String languageId : _portlet.getSupportedLocales()) {
201                            supportedLocales.add(LocaleUtil.fromLanguageId(languageId));
202                    }
203    
204                    return Collections.enumeration(supportedLocales);
205            }
206    
207            @Override
208            public boolean isCopyRequestParameters() {
209                    return _copyRequestParameters;
210            }
211    
212            @Override
213            public boolean isWARFile() {
214                    return _portletApp.isWARFile();
215            }
216    
217            protected Set<javax.xml.namespace.QName> toJavaxQNames(
218                    Set<com.liferay.portal.kernel.xml.QName> liferayQNames) {
219    
220                    Set<QName> javaxQNames = new HashSet<QName>(liferayQNames.size());
221    
222                    for (com.liferay.portal.kernel.xml.QName liferayQName : liferayQNames) {
223                            QName javaxQName = new QName(
224                                    liferayQName.getNamespaceURI(), liferayQName.getLocalPart(),
225                                    liferayQName.getNamespacePrefix());
226    
227                            javaxQNames.add(javaxQName);
228                    }
229    
230                    return javaxQNames;
231            }
232    
233            private boolean _copyRequestParameters;
234            private Portlet _portlet;
235            private PortletApp _portletApp;
236            private PortletContext _portletContext;
237            private String _portletName;
238            private Map<String, ResourceBundle> _resourceBundles;
239    
240    }