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