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