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