1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet;
21  
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portal.kernel.portlet.PortletBag;
25  import com.liferay.portal.kernel.portlet.PortletBagPool;
26  import com.liferay.portal.kernel.util.JavaConstants;
27  import com.liferay.portal.kernel.util.LocaleUtil;
28  import com.liferay.portal.kernel.util.Validator;
29  import com.liferay.portal.model.Portlet;
30  import com.liferay.portal.model.PortletApp;
31  import com.liferay.portal.model.PortletConstants;
32  import com.liferay.portal.model.PortletInfo;
33  import com.liferay.portal.model.PublicRenderParameter;
34  import com.liferay.portal.util.PortalInstances;
35  
36  import java.io.ByteArrayInputStream;
37  
38  import java.util.ArrayList;
39  import java.util.Collections;
40  import java.util.Enumeration;
41  import java.util.HashMap;
42  import java.util.HashSet;
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  import java.util.Set;
49  
50  import javax.portlet.PortletConfig;
51  import javax.portlet.PortletContext;
52  
53  import javax.xml.namespace.QName;
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 static final String RUNTIME_OPTION_PORTAL_CONTEXT =
67          "com.liferay.portal.portalContext";
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 }