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