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.List;
46  import java.util.Locale;
47  import java.util.Map;
48  import java.util.PropertyResourceBundle;
49  import java.util.ResourceBundle;
50  
51  import javax.portlet.PortletConfig;
52  import javax.portlet.PortletContext;
53  
54  import javax.xml.namespace.QName;
55  
56  /**
57   * <a href="PortletConfigImpl.java.html"><b><i>View Source</i></b></a>
58   *
59   * @author Brian Wing Shun Chan
60   *
61   */
62  public class PortletConfigImpl implements PortletConfig {
63  
64      public static final String RUNTIME_OPTION_ESCAPE_XML =
65          "javax.portlet.escapeXml";
66  
67      public static final String RUNTIME_OPTION_PORTAL_CONTEXT =
68          "com.liferay.portal.portalContext";
69  
70      public PortletConfigImpl(Portlet portlet, PortletContext portletContext) {
71          _portletApp = portlet.getPortletApp();
72          _portlet = portlet;
73          _portletName = portlet.getRootPortletId();
74  
75          int pos = _portletName.indexOf(PortletConstants.WAR_SEPARATOR);
76  
77          if (pos != -1) {
78              _portletName = _portletName.substring(0, pos);
79          }
80  
81          _portletContext = portletContext;
82          _bundlePool = new HashMap<String, ResourceBundle>();
83      }
84  
85      public Map<String, String[]> getContainerRuntimeOptions() {
86          return _portletApp.getContainerRuntimeOptions();
87      }
88  
89      public String getDefaultNamespace() {
90          return _portletApp.getDefaultNamespace();
91      }
92  
93      public String getInitParameter(String name) {
94          if (name == null) {
95              throw new IllegalArgumentException();
96          }
97  
98          return _portlet.getInitParams().get(name);
99      }
100 
101     public Enumeration<String> getInitParameterNames() {
102         return Collections.enumeration(_portlet.getInitParams().keySet());
103     }
104 
105     public PortletContext getPortletContext() {
106         return _portletContext;
107     }
108 
109     public String getPortletId() {
110         return _portlet.getPortletId();
111     }
112 
113     public String getPortletName() {
114         return _portletName;
115     }
116 
117     public Enumeration<QName> getProcessingEventQNames() {
118         return Collections.enumeration(_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(_portlet.getPublishingEvents());
136     }
137 
138     public ResourceBundle getResourceBundle(Locale locale) {
139         String resourceBundleClassName = _portlet.getResourceBundle();
140 
141         if (Validator.isNull(resourceBundleClassName)) {
142             String poolId = _portlet.getPortletId();
143 
144             ResourceBundle bundle = _bundlePool.get(poolId);
145 
146             if (bundle == null) {
147                 StringBuilder sb = new StringBuilder();
148 
149                 try {
150                     PortletInfo portletInfo = _portlet.getPortletInfo();
151 
152                     sb.append(JavaConstants.JAVAX_PORTLET_TITLE);
153                     sb.append("=");
154                     sb.append(portletInfo.getTitle());
155                     sb.append("\n");
156 
157                     sb.append(JavaConstants.JAVAX_PORTLET_SHORT_TITLE);
158                     sb.append("=");
159                     sb.append(portletInfo.getShortTitle());
160                     sb.append("\n");
161 
162                     sb.append(JavaConstants.JAVAX_PORTLET_KEYWORDS);
163                     sb.append("=");
164                     sb.append(portletInfo.getKeywords());
165                     sb.append("\n");
166 
167                     bundle = new PropertyResourceBundle(
168                         new ByteArrayInputStream(sb.toString().getBytes()));
169                 }
170                 catch (Exception e) {
171                     _log.error(e, e);
172                 }
173 
174                 _bundlePool.put(poolId, bundle);
175             }
176 
177             return bundle;
178         }
179         else {
180             String poolId = _portlet.getPortletId() + "." + locale.toString();
181 
182             ResourceBundle bundle = _bundlePool.get(poolId);
183 
184             if (bundle == null) {
185                 if (!_portletApp.isWARFile() &&
186                     resourceBundleClassName.equals(
187                         StrutsResourceBundle.class.getName())) {
188 
189                     long companyId = PortalInstances.getDefaultCompanyId();
190 
191                     bundle = StrutsResourceBundle.getBundle(
192                         _portletName, companyId, locale);
193                 }
194                 else {
195                     PortletBag portletBag = PortletBagPool.get(
196                         _portlet.getRootPortletId());
197 
198                     bundle = portletBag.getResourceBundle(locale);
199                 }
200 
201                 bundle = new PortletResourceBundle(
202                     bundle, _portlet.getPortletInfo());
203 
204                 _bundlePool.put(poolId, bundle);
205             }
206 
207             return bundle;
208         }
209     }
210 
211     public Enumeration<Locale> getSupportedLocales() {
212         List<Locale> supportedLocales = new ArrayList<Locale>();
213 
214         for (String languageId : _portlet.getSupportedLocales()) {
215             supportedLocales.add(LocaleUtil.fromLanguageId(languageId));
216         }
217 
218         return Collections.enumeration(supportedLocales);
219     }
220 
221     public boolean isWARFile() {
222         return _portletApp.isWARFile();
223     }
224 
225     private static Log _log = LogFactoryUtil.getLog(PortletConfigImpl.class);
226 
227     private PortletApp _portletApp;
228     private Portlet _portlet;
229     private String _portletName;
230     private PortletContext _portletContext;
231     private Map<String, ResourceBundle> _bundlePool;
232 
233 }