001    /**
002     * Copyright (c) 2000-2010 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.io.unsync.UnsyncByteArrayInputStream;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.portlet.LiferayPortletConfig;
021    import com.liferay.portal.kernel.portlet.PortletBag;
022    import com.liferay.portal.kernel.portlet.PortletBagPool;
023    import com.liferay.portal.kernel.util.JavaConstants;
024    import com.liferay.portal.kernel.util.LocaleUtil;
025    import com.liferay.portal.kernel.util.StringBundler;
026    import com.liferay.portal.kernel.util.StringPool;
027    import com.liferay.portal.kernel.util.Utf8PropertyResourceBundle;
028    import com.liferay.portal.kernel.util.Validator;
029    import com.liferay.portal.model.Portlet;
030    import com.liferay.portal.model.PortletApp;
031    import com.liferay.portal.model.PortletConstants;
032    import com.liferay.portal.model.PortletInfo;
033    import com.liferay.portal.model.PublicRenderParameter;
034    
035    import java.util.ArrayList;
036    import java.util.Collections;
037    import java.util.Enumeration;
038    import java.util.HashSet;
039    import java.util.List;
040    import java.util.Locale;
041    import java.util.Map;
042    import java.util.ResourceBundle;
043    import java.util.Set;
044    import java.util.concurrent.ConcurrentHashMap;
045    
046    import javax.portlet.PortletContext;
047    
048    import javax.xml.namespace.QName;
049    
050    /**
051     * @author Brian Wing Shun Chan
052     * @author Eduardo Lundgren
053     */
054    public class PortletConfigImpl implements LiferayPortletConfig {
055    
056            public PortletConfigImpl(Portlet portlet, PortletContext portletContext) {
057                    _portletApp = portlet.getPortletApp();
058                    _portlet = portlet;
059                    _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                    _portletContext = portletContext;
068                    _resourceBundles = new ConcurrentHashMap<String, ResourceBundle>();
069            }
070    
071            public Map<String, String[]> getContainerRuntimeOptions() {
072                    return _portletApp.getContainerRuntimeOptions();
073            }
074    
075            public String getDefaultNamespace() {
076                    return _portletApp.getDefaultNamespace();
077            }
078    
079            public String getInitParameter(String name) {
080                    if (name == null) {
081                            throw new IllegalArgumentException();
082                    }
083    
084                    return _portlet.getInitParams().get(name);
085            }
086    
087            public Enumeration<String> getInitParameterNames() {
088                    return Collections.enumeration(_portlet.getInitParams().keySet());
089            }
090    
091            public Portlet getPortlet() {
092                    return _portlet;
093            }
094    
095            public PortletContext getPortletContext() {
096                    return _portletContext;
097            }
098    
099            public String getPortletId() {
100                    return _portlet.getPortletId();
101            }
102    
103            public String getPortletName() {
104                    return _portletName;
105            }
106    
107            public Enumeration<QName> getProcessingEventQNames() {
108                    return Collections.enumeration(
109                            toJavaxQNames(_portlet.getProcessingEvents()));
110            }
111    
112            public Enumeration<String> getPublicRenderParameterNames() {
113                    List<String> publicRenderParameterNames = new ArrayList<String>();
114    
115                    for (PublicRenderParameter publicRenderParameter :
116                                    _portlet.getPublicRenderParameters()) {
117    
118                            publicRenderParameterNames.add(
119                                    publicRenderParameter.getIdentifier());
120                    }
121    
122                    return Collections.enumeration(publicRenderParameterNames);
123            }
124    
125            public Enumeration<QName> getPublishingEventQNames() {
126                    return Collections.enumeration(
127                            toJavaxQNames(_portlet.getPublishingEvents()));
128            }
129    
130            public ResourceBundle getResourceBundle(Locale locale) {
131                    String resourceBundleClassName = _portlet.getResourceBundle();
132    
133                    if (Validator.isNull(resourceBundleClassName)) {
134                            String resourceBundleId = _portlet.getPortletId();
135    
136                            ResourceBundle resourceBundle = _resourceBundles.get(
137                                    resourceBundleId);
138    
139                            if (resourceBundle == null) {
140                                    StringBundler sb = new StringBundler(16);
141    
142                                    try {
143                                            PortletInfo portletInfo = _portlet.getPortletInfo();
144    
145                                            sb.append(JavaConstants.JAVAX_PORTLET_TITLE);
146                                            sb.append(StringPool.EQUAL);
147                                            sb.append(portletInfo.getTitle());
148                                            sb.append(StringPool.NEW_LINE);
149    
150                                            sb.append(JavaConstants.JAVAX_PORTLET_SHORT_TITLE);
151                                            sb.append(StringPool.EQUAL);
152                                            sb.append(portletInfo.getShortTitle());
153                                            sb.append(StringPool.NEW_LINE);
154    
155                                            sb.append(JavaConstants.JAVAX_PORTLET_KEYWORDS);
156                                            sb.append(StringPool.EQUAL);
157                                            sb.append(portletInfo.getKeywords());
158                                            sb.append(StringPool.NEW_LINE);
159    
160                                            sb.append(JavaConstants.JAVAX_PORTLET_DESCRIPTION);
161                                            sb.append(StringPool.EQUAL);
162                                            sb.append(portletInfo.getDescription());
163                                            sb.append(StringPool.NEW_LINE);
164    
165                                            resourceBundle = new Utf8PropertyResourceBundle(
166                                                    new UnsyncByteArrayInputStream(
167                                                            sb.toString().getBytes()));
168                                    }
169                                    catch (Exception e) {
170                                            _log.error(e, e);
171                                    }
172    
173                                    _resourceBundles.put(resourceBundleId, resourceBundle);
174                            }
175    
176                            return resourceBundle;
177                    }
178                    else {
179                            String resourceBundleId = _portlet.getPortletId().concat(
180                                    StringPool.PERIOD).concat(locale.toString());
181    
182                            ResourceBundle resourceBundle = _resourceBundles.get(
183                                    resourceBundleId);
184    
185                            if (resourceBundle == null) {
186                                    if (!_portletApp.isWARFile() &&
187                                            resourceBundleClassName.equals(
188                                                    StrutsResourceBundle.class.getName())) {
189    
190                                            resourceBundle = new StrutsResourceBundle(
191                                                    _portletName, locale);
192                                    }
193                                    else {
194                                            PortletBag portletBag = PortletBagPool.get(
195                                                    _portlet.getRootPortletId());
196    
197                                            resourceBundle = portletBag.getResourceBundle(locale);
198                                    }
199    
200                                    resourceBundle = new PortletResourceBundle(
201                                            resourceBundle, _portlet.getPortletInfo());
202    
203                                    _resourceBundles.put(resourceBundleId, resourceBundle);
204                            }
205    
206                            return resourceBundle;
207                    }
208            }
209    
210            public Enumeration<Locale> getSupportedLocales() {
211                    List<Locale> supportedLocales = new ArrayList<Locale>();
212    
213                    for (String languageId : _portlet.getSupportedLocales()) {
214                            supportedLocales.add(LocaleUtil.fromLanguageId(languageId));
215                    }
216    
217                    return Collections.enumeration(supportedLocales);
218            }
219    
220            public boolean isWARFile() {
221                    return _portletApp.isWARFile();
222            }
223    
224            protected Set<javax.xml.namespace.QName> toJavaxQNames(
225                    Set<com.liferay.portal.kernel.xml.QName> liferayQNames) {
226    
227                    Set<QName> javaxQNames = new HashSet<QName>(liferayQNames.size());
228    
229                    for (com.liferay.portal.kernel.xml.QName liferayQName :
230                                    liferayQNames) {
231    
232                            QName javaxQName = new QName(
233                                    liferayQName.getNamespaceURI(), liferayQName.getLocalPart(),
234                                    liferayQName.getNamespacePrefix());
235    
236                            javaxQNames.add(javaxQName);
237                    }
238    
239                    return javaxQNames;
240            }
241    
242            private static Log _log = LogFactoryUtil.getLog(PortletConfigImpl.class);
243    
244            private Portlet _portlet;
245            private PortletApp _portletApp;
246            private PortletContext _portletContext;
247            private String _portletName;
248            private Map<String, ResourceBundle> _resourceBundles;
249    
250    }