001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portlet;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.portlet.LiferayPortletConfig;
020    import com.liferay.portal.kernel.portlet.PortletBag;
021    import com.liferay.portal.kernel.portlet.PortletBagPool;
022    import com.liferay.portal.kernel.util.JavaConstants;
023    import com.liferay.portal.kernel.util.LocaleUtil;
024    import com.liferay.portal.kernel.util.PropertyResourceBundle;
025    import com.liferay.portal.kernel.util.StringBundler;
026    import com.liferay.portal.kernel.util.StringPool;
027    import com.liferay.portal.kernel.util.Validator;
028    import com.liferay.portal.model.Portlet;
029    import com.liferay.portal.model.PortletApp;
030    import com.liferay.portal.model.PortletConstants;
031    import com.liferay.portal.model.PortletInfo;
032    import com.liferay.portal.model.PublicRenderParameter;
033    
034    import java.util.ArrayList;
035    import java.util.Collections;
036    import java.util.Enumeration;
037    import java.util.HashSet;
038    import java.util.List;
039    import java.util.Locale;
040    import java.util.Map;
041    import java.util.ResourceBundle;
042    import java.util.Set;
043    import java.util.concurrent.ConcurrentHashMap;
044    
045    import javax.portlet.PortletContext;
046    
047    import javax.xml.namespace.QName;
048    
049    /**
050     * @author Brian Wing Shun Chan
051     * @author Eduardo Lundgren
052     * @author Shuyang Zhou
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 PropertyResourceBundle(
166                                                    sb.toString(), StringPool.UTF8);
167                                    }
168                                    catch (Exception e) {
169                                            _log.error(e, e);
170                                    }
171    
172                                    _resourceBundles.put(resourceBundleId, resourceBundle);
173                            }
174    
175                            return resourceBundle;
176                    }
177                    else {
178                            StringBundler sb = new StringBundler(4);
179    
180                            sb.append(_portlet.getPortletId());
181                            sb.append(locale.getLanguage());
182                            sb.append(locale.getCountry());
183                            sb.append(locale.getVariant());
184    
185                            String resourceBundleId = sb.toString();
186    
187                            ResourceBundle resourceBundle = _resourceBundles.get(
188                                    resourceBundleId);
189    
190                            if (resourceBundle == null) {
191                                    if (!_portletApp.isWARFile() &&
192                                            resourceBundleClassName.equals(
193                                                    StrutsResourceBundle.class.getName())) {
194    
195                                            resourceBundle = new StrutsResourceBundle(
196                                                    _portletName, locale);
197                                    }
198                                    else {
199                                            PortletBag portletBag = PortletBagPool.get(
200                                                    _portlet.getRootPortletId());
201    
202                                            resourceBundle = portletBag.getResourceBundle(locale);
203                                    }
204    
205                                    resourceBundle = new PortletResourceBundle(
206                                            resourceBundle, _portlet.getPortletInfo());
207    
208                                    _resourceBundles.put(resourceBundleId, resourceBundle);
209                            }
210    
211                            return resourceBundle;
212                    }
213            }
214    
215            public Enumeration<Locale> getSupportedLocales() {
216                    List<Locale> supportedLocales = new ArrayList<Locale>();
217    
218                    for (String languageId : _portlet.getSupportedLocales()) {
219                            supportedLocales.add(LocaleUtil.fromLanguageId(languageId));
220                    }
221    
222                    return Collections.enumeration(supportedLocales);
223            }
224    
225            public boolean isWARFile() {
226                    return _portletApp.isWARFile();
227            }
228    
229            protected Set<javax.xml.namespace.QName> toJavaxQNames(
230                    Set<com.liferay.portal.kernel.xml.QName> liferayQNames) {
231    
232                    Set<QName> javaxQNames = new HashSet<QName>(liferayQNames.size());
233    
234                    for (com.liferay.portal.kernel.xml.QName liferayQName :
235                                    liferayQNames) {
236    
237                            QName javaxQName = new QName(
238                                    liferayQName.getNamespaceURI(), liferayQName.getLocalPart(),
239                                    liferayQName.getNamespacePrefix());
240    
241                            javaxQNames.add(javaxQName);
242                    }
243    
244                    return javaxQNames;
245            }
246    
247            private static Log _log = LogFactoryUtil.getLog(PortletConfigImpl.class);
248    
249            private Portlet _portlet;
250            private PortletApp _portletApp;
251            private PortletContext _portletContext;
252            private String _portletName;
253            private Map<String, ResourceBundle> _resourceBundles;
254    
255    }