1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.portlet.PortletBag;
28  import com.liferay.portal.kernel.portlet.PortletBagPool;
29  import com.liferay.portal.kernel.util.JavaConstants;
30  import com.liferay.portal.kernel.util.LocaleUtil;
31  import com.liferay.portal.kernel.util.Validator;
32  import com.liferay.portal.model.Portlet;
33  import com.liferay.portal.model.PortletApp;
34  import com.liferay.portal.model.PortletConstants;
35  import com.liferay.portal.model.PortletInfo;
36  import com.liferay.portal.model.PublicRenderParameter;
37  import com.liferay.portal.util.PortalInstances;
38  
39  import java.io.ByteArrayInputStream;
40  
41  import java.util.ArrayList;
42  import java.util.Collections;
43  import java.util.Enumeration;
44  import java.util.HashMap;
45  import java.util.HashSet;
46  import java.util.List;
47  import java.util.Locale;
48  import java.util.Map;
49  import java.util.PropertyResourceBundle;
50  import java.util.ResourceBundle;
51  import java.util.Set;
52  
53  import javax.portlet.PortletConfig;
54  import javax.portlet.PortletContext;
55  
56  import javax.xml.namespace.QName;
57  
58  /**
59   * <a href="PortletConfigImpl.java.html"><b><i>View Source</i></b></a>
60   *
61   * @author Brian Wing Shun Chan
62   *
63   */
64  public class PortletConfigImpl implements PortletConfig {
65  
66      public static final String RUNTIME_OPTION_ESCAPE_XML =
67          "javax.portlet.escapeXml";
68  
69      public PortletConfigImpl(Portlet portlet, PortletContext portletContext) {
70          _portletApp = portlet.getPortletApp();
71          _portlet = portlet;
72          _portletName = portlet.getRootPortletId();
73  
74          int pos = _portletName.indexOf(PortletConstants.WAR_SEPARATOR);
75  
76          if (pos != -1) {
77              _portletName = _portletName.substring(0, pos);
78          }
79  
80          _portletContext = portletContext;
81          _bundlePool = new HashMap<String, ResourceBundle>();
82      }
83  
84      public Map<String, String[]> getContainerRuntimeOptions() {
85          return _portletApp.getContainerRuntimeOptions();
86      }
87  
88      public String getDefaultNamespace() {
89          return _portletApp.getDefaultNamespace();
90      }
91  
92      public String getInitParameter(String name) {
93          if (name == null) {
94              throw new IllegalArgumentException();
95          }
96  
97          return _portlet.getInitParams().get(name);
98      }
99  
100     public Enumeration<String> getInitParameterNames() {
101         return Collections.enumeration(_portlet.getInitParams().keySet());
102     }
103 
104     public PortletContext getPortletContext() {
105         return _portletContext;
106     }
107 
108     public String getPortletId() {
109         return _portlet.getPortletId();
110     }
111 
112     public String getPortletName() {
113         return _portletName;
114     }
115 
116     public Enumeration<QName> getProcessingEventQNames() {
117         return Collections.enumeration(
118             toJavaxQNames(_portlet.getProcessingEvents()));
119     }
120 
121     public Enumeration<String> getPublicRenderParameterNames() {
122         List<String> publicRenderParameterNames = new ArrayList<String>();
123 
124         for (PublicRenderParameter publicRenderParameter :
125                 _portlet.getPublicRenderParameters()) {
126 
127             publicRenderParameterNames.add(
128                 publicRenderParameter.getIdentifier());
129         }
130 
131         return Collections.enumeration(publicRenderParameterNames);
132     }
133 
134     public Enumeration<QName> getPublishingEventQNames() {
135         return Collections.enumeration(
136             toJavaxQNames(_portlet.getPublishingEvents()));
137     }
138 
139     public ResourceBundle getResourceBundle(Locale locale) {
140         String resourceBundleClassName = _portlet.getResourceBundle();
141 
142         if (Validator.isNull(resourceBundleClassName)) {
143             String poolId = _portlet.getPortletId();
144 
145             ResourceBundle bundle = _bundlePool.get(poolId);
146 
147             if (bundle == null) {
148                 StringBuilder sb = new StringBuilder();
149 
150                 try {
151                     PortletInfo portletInfo = _portlet.getPortletInfo();
152 
153                     sb.append(JavaConstants.JAVAX_PORTLET_TITLE);
154                     sb.append("=");
155                     sb.append(portletInfo.getTitle());
156                     sb.append("\n");
157 
158                     sb.append(JavaConstants.JAVAX_PORTLET_SHORT_TITLE);
159                     sb.append("=");
160                     sb.append(portletInfo.getShortTitle());
161                     sb.append("\n");
162 
163                     sb.append(JavaConstants.JAVAX_PORTLET_KEYWORDS);
164                     sb.append("=");
165                     sb.append(portletInfo.getKeywords());
166                     sb.append("\n");
167 
168                     bundle = new PropertyResourceBundle(
169                         new ByteArrayInputStream(sb.toString().getBytes()));
170                 }
171                 catch (Exception e) {
172                     _log.error(e, e);
173                 }
174 
175                 _bundlePool.put(poolId, bundle);
176             }
177 
178             return bundle;
179         }
180         else {
181             String poolId = _portlet.getPortletId() + "." + locale.toString();
182 
183             ResourceBundle bundle = _bundlePool.get(poolId);
184 
185             if (bundle == null) {
186                 if (!_portletApp.isWARFile() &&
187                     resourceBundleClassName.equals(
188                         StrutsResourceBundle.class.getName())) {
189 
190                     long companyId = PortalInstances.getDefaultCompanyId();
191 
192                     bundle = StrutsResourceBundle.getBundle(
193                         _portletName, companyId, locale);
194                 }
195                 else {
196                     PortletBag portletBag = PortletBagPool.get(
197                         _portlet.getRootPortletId());
198 
199                     bundle = portletBag.getResourceBundle(locale);
200                 }
201 
202                 bundle = new PortletResourceBundle(
203                     bundle, _portlet.getPortletInfo());
204 
205                 _bundlePool.put(poolId, bundle);
206             }
207 
208             return bundle;
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 PortletApp _portletApp;
247     private Portlet _portlet;
248     private String _portletName;
249     private PortletContext _portletContext;
250     private Map<String, ResourceBundle> _bundlePool;
251 
252 }