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.Validator;
27  import com.liferay.portal.model.Portlet;
28  import com.liferay.portal.model.PortletApp;
29  import com.liferay.portal.model.PortletConstants;
30  import com.liferay.portal.model.PortletInfo;
31  import com.liferay.portal.model.PublicRenderParameter;
32  
33  import java.util.ArrayList;
34  import java.util.Collections;
35  import java.util.Enumeration;
36  import java.util.HashMap;
37  import java.util.HashSet;
38  import java.util.List;
39  import java.util.Locale;
40  import java.util.Map;
41  import java.util.PropertyResourceBundle;
42  import java.util.ResourceBundle;
43  import java.util.Set;
44  
45  import javax.portlet.PortletContext;
46  
47  import javax.xml.namespace.QName;
48  
49  /**
50   * <a href="PortletConfigImpl.java.html"><b><i>View Source</i></b></a>
51   *
52   * @author Brian Wing Shun Chan
53   * @author Eduardo Lundgren
54   */
55  public class PortletConfigImpl implements LiferayPortletConfig {
56  
57      public PortletConfigImpl(Portlet portlet, PortletContext portletContext) {
58          _portletApp = portlet.getPortletApp();
59          _portlet = portlet;
60          _portletName = portlet.getRootPortletId();
61  
62          int pos = _portletName.indexOf(PortletConstants.WAR_SEPARATOR);
63  
64          if (pos != -1) {
65              _portletName = _portletName.substring(0, pos);
66          }
67  
68          _portletContext = portletContext;
69          _bundlePool = new HashMap<String, ResourceBundle>();
70      }
71  
72      public Map<String, String[]> getContainerRuntimeOptions() {
73          return _portletApp.getContainerRuntimeOptions();
74      }
75  
76      public String getDefaultNamespace() {
77          return _portletApp.getDefaultNamespace();
78      }
79  
80      public String getInitParameter(String name) {
81          if (name == null) {
82              throw new IllegalArgumentException();
83          }
84  
85          return _portlet.getInitParams().get(name);
86      }
87  
88      public Enumeration<String> getInitParameterNames() {
89          return Collections.enumeration(_portlet.getInitParams().keySet());
90      }
91  
92      public PortletContext getPortletContext() {
93          return _portletContext;
94      }
95  
96      public String getPortletId() {
97          return _portlet.getPortletId();
98      }
99  
100     public String getPortletName() {
101         return _portletName;
102     }
103 
104     public Enumeration<QName> getProcessingEventQNames() {
105         return Collections.enumeration(
106             toJavaxQNames(_portlet.getProcessingEvents()));
107     }
108 
109     public Enumeration<String> getPublicRenderParameterNames() {
110         List<String> publicRenderParameterNames = new ArrayList<String>();
111 
112         for (PublicRenderParameter publicRenderParameter :
113                 _portlet.getPublicRenderParameters()) {
114 
115             publicRenderParameterNames.add(
116                 publicRenderParameter.getIdentifier());
117         }
118 
119         return Collections.enumeration(publicRenderParameterNames);
120     }
121 
122     public Enumeration<QName> getPublishingEventQNames() {
123         return Collections.enumeration(
124             toJavaxQNames(_portlet.getPublishingEvents()));
125     }
126 
127     public ResourceBundle getResourceBundle(Locale locale) {
128         String resourceBundleClassName = _portlet.getResourceBundle();
129 
130         if (Validator.isNull(resourceBundleClassName)) {
131             String poolId = _portlet.getPortletId();
132 
133             ResourceBundle bundle = _bundlePool.get(poolId);
134 
135             if (bundle == null) {
136                 StringBundler sb = new StringBundler(16);
137 
138                 try {
139                     PortletInfo portletInfo = _portlet.getPortletInfo();
140 
141                     sb.append(JavaConstants.JAVAX_PORTLET_TITLE);
142                     sb.append("=");
143                     sb.append(portletInfo.getTitle());
144                     sb.append("\n");
145 
146                     sb.append(JavaConstants.JAVAX_PORTLET_SHORT_TITLE);
147                     sb.append("=");
148                     sb.append(portletInfo.getShortTitle());
149                     sb.append("\n");
150 
151                     sb.append(JavaConstants.JAVAX_PORTLET_KEYWORDS);
152                     sb.append("=");
153                     sb.append(portletInfo.getKeywords());
154                     sb.append("\n");
155 
156                     sb.append(JavaConstants.JAVAX_PORTLET_DESCRIPTION);
157                     sb.append("=");
158                     sb.append(portletInfo.getDescription());
159                     sb.append("\n");
160 
161                     bundle = new PropertyResourceBundle(
162                         new UnsyncByteArrayInputStream(
163                             sb.toString().getBytes()));
164                 }
165                 catch (Exception e) {
166                     _log.error(e, e);
167                 }
168 
169                 _bundlePool.put(poolId, bundle);
170             }
171 
172             return bundle;
173         }
174         else {
175             String poolId = _portlet.getPortletId() + "." + locale.toString();
176 
177             ResourceBundle bundle = _bundlePool.get(poolId);
178 
179             if (bundle == null) {
180                 if (!_portletApp.isWARFile() &&
181                     resourceBundleClassName.equals(
182                         StrutsResourceBundle.class.getName())) {
183 
184                     bundle = new StrutsResourceBundle(_portletName, locale);
185                 }
186                 else {
187                     PortletBag portletBag = PortletBagPool.get(
188                         _portlet.getRootPortletId());
189 
190                     bundle = portletBag.getResourceBundle(locale);
191                 }
192 
193                 bundle = new PortletResourceBundle(
194                     bundle, _portlet.getPortletInfo());
195 
196                 _bundlePool.put(poolId, bundle);
197             }
198 
199             return bundle;
200         }
201     }
202 
203     public Enumeration<Locale> getSupportedLocales() {
204         List<Locale> supportedLocales = new ArrayList<Locale>();
205 
206         for (String languageId : _portlet.getSupportedLocales()) {
207             supportedLocales.add(LocaleUtil.fromLanguageId(languageId));
208         }
209 
210         return Collections.enumeration(supportedLocales);
211     }
212 
213     public boolean isWARFile() {
214         return _portletApp.isWARFile();
215     }
216 
217     protected Set<javax.xml.namespace.QName> toJavaxQNames(
218         Set<com.liferay.portal.kernel.xml.QName> liferayQNames) {
219 
220         Set<QName> javaxQNames = new HashSet<QName>(liferayQNames.size());
221 
222         for (com.liferay.portal.kernel.xml.QName liferayQName :
223                 liferayQNames) {
224 
225             QName javaxQName = new QName(
226                 liferayQName.getNamespaceURI(), liferayQName.getLocalPart(),
227                 liferayQName.getNamespacePrefix());
228 
229             javaxQNames.add(javaxQName);
230         }
231 
232         return javaxQNames;
233     }
234 
235     private static Log _log = LogFactoryUtil.getLog(PortletConfigImpl.class);
236 
237     private PortletApp _portletApp;
238     private Portlet _portlet;
239     private String _portletName;
240     private PortletContext _portletContext;
241     private Map<String, ResourceBundle> _bundlePool;
242 
243 }