1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet;
16  
17  import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
18  import com.liferay.portal.kernel.log.Log;
19  import com.liferay.portal.kernel.log.LogFactoryUtil;
20  import com.liferay.portal.kernel.portlet.LiferayPortletConfig;
21  import com.liferay.portal.kernel.portlet.PortletBag;
22  import com.liferay.portal.kernel.portlet.PortletBagPool;
23  import com.liferay.portal.kernel.util.JavaConstants;
24  import com.liferay.portal.kernel.util.LocaleUtil;
25  import com.liferay.portal.kernel.util.StringBundler;
26  import com.liferay.portal.kernel.util.StringPool;
27  import com.liferay.portal.kernel.util.Validator;
28  import com.liferay.portal.model.Portlet;
29  import com.liferay.portal.model.PortletApp;
30  import com.liferay.portal.model.PortletConstants;
31  import com.liferay.portal.model.PortletInfo;
32  import com.liferay.portal.model.PublicRenderParameter;
33  
34  import java.util.ArrayList;
35  import java.util.Collections;
36  import java.util.Enumeration;
37  import java.util.HashMap;
38  import java.util.HashSet;
39  import java.util.List;
40  import java.util.Locale;
41  import java.util.Map;
42  import java.util.PropertyResourceBundle;
43  import java.util.ResourceBundle;
44  import java.util.Set;
45  
46  import javax.portlet.PortletContext;
47  
48  import javax.xml.namespace.QName;
49  
50  /**
51   * <a href="PortletConfigImpl.java.html"><b><i>View Source</i></b></a>
52   *
53   * @author Brian Wing Shun Chan
54   * @author Eduardo Lundgren
55   */
56  public class PortletConfigImpl implements LiferayPortletConfig {
57  
58      public PortletConfigImpl(Portlet portlet, PortletContext portletContext) {
59          _portletApp = portlet.getPortletApp();
60          _portlet = portlet;
61          _portletName = portlet.getRootPortletId();
62  
63          int pos = _portletName.indexOf(PortletConstants.WAR_SEPARATOR);
64  
65          if (pos != -1) {
66              _portletName = _portletName.substring(0, pos);
67          }
68  
69          _portletContext = portletContext;
70          _resourceBundles = new HashMap<String, ResourceBundle>();
71      }
72  
73      public Map<String, String[]> getContainerRuntimeOptions() {
74          return _portletApp.getContainerRuntimeOptions();
75      }
76  
77      public String getDefaultNamespace() {
78          return _portletApp.getDefaultNamespace();
79      }
80  
81      public String getInitParameter(String name) {
82          if (name == null) {
83              throw new IllegalArgumentException();
84          }
85  
86          return _portlet.getInitParams().get(name);
87      }
88  
89      public Enumeration<String> getInitParameterNames() {
90          return Collections.enumeration(_portlet.getInitParams().keySet());
91      }
92  
93      public Portlet getPortlet() {
94          return _portlet;
95      }
96  
97      public PortletContext getPortletContext() {
98          return _portletContext;
99      }
100 
101     public String getPortletId() {
102         return _portlet.getPortletId();
103     }
104 
105     public String getPortletName() {
106         return _portletName;
107     }
108 
109     public Enumeration<QName> getProcessingEventQNames() {
110         return Collections.enumeration(
111             toJavaxQNames(_portlet.getProcessingEvents()));
112     }
113 
114     public Enumeration<String> getPublicRenderParameterNames() {
115         List<String> publicRenderParameterNames = new ArrayList<String>();
116 
117         for (PublicRenderParameter publicRenderParameter :
118                 _portlet.getPublicRenderParameters()) {
119 
120             publicRenderParameterNames.add(
121                 publicRenderParameter.getIdentifier());
122         }
123 
124         return Collections.enumeration(publicRenderParameterNames);
125     }
126 
127     public Enumeration<QName> getPublishingEventQNames() {
128         return Collections.enumeration(
129             toJavaxQNames(_portlet.getPublishingEvents()));
130     }
131 
132     public ResourceBundle getResourceBundle(Locale locale) {
133         String resourceBundleClassName = _portlet.getResourceBundle();
134 
135         if (Validator.isNull(resourceBundleClassName)) {
136             String resourceBundleId = _portlet.getPortletId();
137 
138             ResourceBundle resourceBundle = _resourceBundles.get(
139                 resourceBundleId);
140 
141             if (resourceBundle == null) {
142                 StringBundler sb = new StringBundler(16);
143 
144                 try {
145                     PortletInfo portletInfo = _portlet.getPortletInfo();
146 
147                     sb.append(JavaConstants.JAVAX_PORTLET_TITLE);
148                     sb.append(StringPool.EQUAL);
149                     sb.append(portletInfo.getTitle());
150                     sb.append(StringPool.NEW_LINE);
151 
152                     sb.append(JavaConstants.JAVAX_PORTLET_SHORT_TITLE);
153                     sb.append(StringPool.EQUAL);
154                     sb.append(portletInfo.getShortTitle());
155                     sb.append(StringPool.NEW_LINE);
156 
157                     sb.append(JavaConstants.JAVAX_PORTLET_KEYWORDS);
158                     sb.append(StringPool.EQUAL);
159                     sb.append(portletInfo.getKeywords());
160                     sb.append(StringPool.NEW_LINE);
161 
162                     sb.append(JavaConstants.JAVAX_PORTLET_DESCRIPTION);
163                     sb.append(StringPool.EQUAL);
164                     sb.append(portletInfo.getDescription());
165                     sb.append(StringPool.NEW_LINE);
166 
167                     resourceBundle = new PropertyResourceBundle(
168                         new UnsyncByteArrayInputStream(
169                             sb.toString().getBytes()));
170                 }
171                 catch (Exception e) {
172                     _log.error(e, e);
173                 }
174 
175                 _resourceBundles.put(resourceBundleId, resourceBundle);
176             }
177 
178             return resourceBundle;
179         }
180         else {
181             String resourceBundleId = _portlet.getPortletId().concat(
182                 StringPool.PERIOD).concat(locale.toString());
183 
184             ResourceBundle resourceBundle = _resourceBundles.get(
185                 resourceBundleId);
186 
187             if (resourceBundle == null) {
188                 if (!_portletApp.isWARFile() &&
189                     resourceBundleClassName.equals(
190                         StrutsResourceBundle.class.getName())) {
191 
192                     resourceBundle = new StrutsResourceBundle(
193                         _portletName, locale);
194                 }
195                 else {
196                     PortletBag portletBag = PortletBagPool.get(
197                         _portlet.getRootPortletId());
198 
199                     resourceBundle = portletBag.getResourceBundle(locale);
200                 }
201 
202                 resourceBundle = new PortletResourceBundle(
203                     resourceBundle, _portlet.getPortletInfo());
204 
205                 _resourceBundles.put(resourceBundleId, resourceBundle);
206             }
207 
208             return resourceBundle;
209         }
210     }
211 
212     public Enumeration<Locale> getSupportedLocales() {
213         List<Locale> supportedLocales = new ArrayList<Locale>();
214 
215         for (String languageId : _portlet.getSupportedLocales()) {
216             supportedLocales.add(LocaleUtil.fromLanguageId(languageId));
217         }
218 
219         return Collections.enumeration(supportedLocales);
220     }
221 
222     public boolean isWARFile() {
223         return _portletApp.isWARFile();
224     }
225 
226     protected Set<javax.xml.namespace.QName> toJavaxQNames(
227         Set<com.liferay.portal.kernel.xml.QName> liferayQNames) {
228 
229         Set<QName> javaxQNames = new HashSet<QName>(liferayQNames.size());
230 
231         for (com.liferay.portal.kernel.xml.QName liferayQName :
232                 liferayQNames) {
233 
234             QName javaxQName = new QName(
235                 liferayQName.getNamespaceURI(), liferayQName.getLocalPart(),
236                 liferayQName.getNamespacePrefix());
237 
238             javaxQNames.add(javaxQName);
239         }
240 
241         return javaxQNames;
242     }
243 
244     private static Log _log = LogFactoryUtil.getLog(PortletConfigImpl.class);
245 
246     private Portlet _portlet;
247     private PortletApp _portletApp;
248     private PortletContext _portletContext;
249     private String _portletName;
250     private Map<String, ResourceBundle> _resourceBundles;
251 
252 }