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