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