1
14
15 package com.liferay.portlet;
16
17 import com.liferay.portal.dao.shard.ShardPollerProcessorWrapper;
18 import com.liferay.portal.kernel.job.Scheduler;
19 import com.liferay.portal.kernel.log.Log;
20 import com.liferay.portal.kernel.log.LogFactoryUtil;
21 import com.liferay.portal.kernel.poller.PollerProcessor;
22 import com.liferay.portal.kernel.pop.MessageListener;
23 import com.liferay.portal.kernel.portlet.ConfigurationAction;
24 import com.liferay.portal.kernel.portlet.FriendlyURLMapper;
25 import com.liferay.portal.kernel.portlet.PortletBag;
26 import com.liferay.portal.kernel.portlet.PortletBagPool;
27 import com.liferay.portal.kernel.portlet.PortletLayoutListener;
28 import com.liferay.portal.kernel.search.Indexer;
29 import com.liferay.portal.kernel.search.IndexerRegistryUtil;
30 import com.liferay.portal.kernel.search.OpenSearch;
31 import com.liferay.portal.kernel.servlet.ServletContextPool;
32 import com.liferay.portal.kernel.servlet.URLEncoder;
33 import com.liferay.portal.kernel.util.LocaleUtil;
34 import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
35 import com.liferay.portal.kernel.util.ProxyFactory;
36 import com.liferay.portal.kernel.util.SetUtil;
37 import com.liferay.portal.kernel.util.Validator;
38 import com.liferay.portal.lar.PortletDataHandler;
39 import com.liferay.portal.model.Portlet;
40 import com.liferay.portal.model.PortletApp;
41 import com.liferay.portal.poller.PollerProcessorUtil;
42 import com.liferay.portal.pop.POPServerUtil;
43 import com.liferay.portal.service.PortletLocalServiceUtil;
44 import com.liferay.portal.util.PortalUtil;
45 import com.liferay.portal.util.PropsValues;
46 import com.liferay.portal.webdav.WebDAVStorage;
47 import com.liferay.portal.webdav.WebDAVUtil;
48 import com.liferay.portlet.social.model.SocialActivityInterpreter;
49 import com.liferay.portlet.social.model.SocialRequestInterpreter;
50 import com.liferay.portlet.social.model.impl.SocialActivityInterpreterImpl;
51 import com.liferay.portlet.social.model.impl.SocialRequestInterpreterImpl;
52 import com.liferay.portlet.social.service.SocialActivityInterpreterLocalServiceUtil;
53 import com.liferay.portlet.social.service.SocialRequestInterpreterLocalServiceUtil;
54
55 import java.util.HashMap;
56 import java.util.Locale;
57 import java.util.Map;
58 import java.util.MissingResourceException;
59 import java.util.ResourceBundle;
60 import java.util.Set;
61
62 import javax.portlet.PreferencesValidator;
63
64 import javax.servlet.ServletContext;
65
66
74 public class PortletBagFactory {
75
76 public PortletBag create(Portlet portlet) throws Exception {
77 PortletApp portletApp = portlet.getPortletApp();
78
79 if (!portletApp.isWARFile() && _warFile) {
80 String contextPath = PortalUtil.getPathContext();
81
82 _servletContext = ServletContextPool.get(contextPath);
83
84 _classLoader = PortalClassLoaderUtil.getClassLoader();
85 }
86
87 Class<?> portletClass = null;
88
89 try {
90 portletClass = _classLoader.loadClass(portlet.getPortletClass());
91 }
92 catch (Throwable e) {
93 _log.error(e, e);
94
95 PortletLocalServiceUtil.destroyPortlet(portlet);
96
97 return null;
98 }
99
100 javax.portlet.Portlet portletInstance =
101 (javax.portlet.Portlet)portletClass.newInstance();
102
103 ConfigurationAction configurationActionInstance =
104 newConfigurationAction(portlet);
105
106 Indexer indexerInstance = newIndexer(portlet);
107
108 OpenSearch openSearchInstance = newOpenSearch(portlet);
109
110 Scheduler schedulerInstance = null;
111
112 if (PropsValues.SCHEDULER_ENABLED &&
113 Validator.isNotNull(portlet.getSchedulerClass())) {
114
115 schedulerInstance = (Scheduler)newInstance(
116 Scheduler.class, portlet.getSchedulerClass());
117
118 schedulerInstance.schedule();
119 }
120
121 FriendlyURLMapper friendlyURLMapperInstance = newFriendlyURLMapper(
122 portlet);
123
124 URLEncoder urlEncoderInstance = newURLEncoder(portlet);
125
126 PortletDataHandler portletDataHandlerInstance = newPortletDataHandler(
127 portlet);
128
129 PortletLayoutListener portletLayoutListenerInstance =
130 newPortletLayoutListener(portlet);
131
132 PollerProcessor pollerProcessorInstance = newPollerProcessor(portlet);
133
134 MessageListener popMessageListenerInstance = newPOPMessageListener(
135 portlet);
136
137 SocialActivityInterpreter socialActivityInterpreterInstance =
138 initSocialActivityInterpreterInstance(portlet);
139
140 SocialRequestInterpreter socialRequestInterpreterInstance = null;
141
142 if (Validator.isNotNull(portlet.getSocialRequestInterpreterClass())) {
143 socialRequestInterpreterInstance =
144 (SocialRequestInterpreter)newInstance(
145 SocialRequestInterpreter.class,
146 portlet.getSocialRequestInterpreterClass());
147
148 socialRequestInterpreterInstance = new SocialRequestInterpreterImpl(
149 portlet.getPortletId(), socialRequestInterpreterInstance);
150
151 SocialRequestInterpreterLocalServiceUtil.addRequestInterpreter(
152 socialRequestInterpreterInstance);
153 }
154
155 WebDAVStorage webDAVStorageInstance = null;
156
157 if (Validator.isNotNull(portlet.getWebDAVStorageClass())) {
158 webDAVStorageInstance = (WebDAVStorage)newInstance(
159 WebDAVStorage.class, portlet.getWebDAVStorageClass());
160
161 webDAVStorageInstance.setToken(portlet.getWebDAVStorageToken());
162
163 WebDAVUtil.addStorage(webDAVStorageInstance);
164 }
165
166 ControlPanelEntry controlPanelEntryInstance = null;
167
168 if (Validator.isNotNull(portlet.getControlPanelEntryClass())) {
169 controlPanelEntryInstance = (ControlPanelEntry)newInstance(
170 ControlPanelEntry.class, portlet.getControlPanelEntryClass());
171 }
172
173 PreferencesValidator preferencesValidatorInstance = null;
174
175 if (Validator.isNotNull(portlet.getPreferencesValidator())) {
176 preferencesValidatorInstance = (PreferencesValidator)newInstance(
177 PreferencesValidator.class, portlet.getPreferencesValidator());
178
179 try {
180 if (PropsValues.PREFERENCE_VALIDATE_ON_STARTUP) {
181 preferencesValidatorInstance.validate(
182 PortletPreferencesSerializer.fromDefaultXML(
183 portlet.getDefaultPreferences()));
184 }
185 }
186 catch (Exception e) {
187 _log.warn(
188 "Portlet with the name " + portlet.getPortletId() +
189 " does not have valid default preferences");
190 }
191 }
192
193 Map<String, ResourceBundle> resourceBundles = null;
194
195 if (Validator.isNotNull(portlet.getResourceBundle())) {
196 resourceBundles = new HashMap<String, ResourceBundle>();
197
198 initResourceBundle(
199 resourceBundles, portlet, LocaleUtil.getDefault());
200
201 Set<String> supportedLocales = portlet.getSupportedLocales();
202
203 if (supportedLocales.isEmpty()) {
204 supportedLocales = SetUtil.fromArray(PropsValues.LOCALES);
205 }
206
207 for (String supportedLocale : supportedLocales) {
208 Locale locale = LocaleUtil.fromLanguageId(supportedLocale);
209
210 initResourceBundle(resourceBundles, portlet, locale);
211 }
212 }
213
214 PortletBag portletBag = new PortletBagImpl(
215 portlet.getPortletId(), _servletContext, portletInstance,
216 configurationActionInstance, indexerInstance, openSearchInstance,
217 schedulerInstance, friendlyURLMapperInstance, urlEncoderInstance,
218 portletDataHandlerInstance, portletLayoutListenerInstance,
219 pollerProcessorInstance, popMessageListenerInstance,
220 socialActivityInterpreterInstance, socialRequestInterpreterInstance,
221 webDAVStorageInstance, controlPanelEntryInstance,
222 preferencesValidatorInstance, resourceBundles);
223
224 PortletBagPool.put(portlet.getRootPortletId(), portletBag);
225
226 try {
227 PortletInstanceFactoryUtil.create(portlet, _servletContext);
228 }
229 catch (Exception e) {
230 _log.error(e, e);
231 }
232
233 return portletBag;
234 }
235
236 public void setClassLoader(ClassLoader classLoader) {
237 _classLoader = classLoader;
238 }
239
240 public void setServletContext(ServletContext servletContext) {
241 _servletContext = servletContext;
242 }
243
244 public void setWARFile(boolean warFile) {
245 _warFile = warFile;
246 }
247
248 protected void initResourceBundle(
249 Map<String, ResourceBundle> resourceBundles, Portlet portlet,
250 Locale locale) {
251
252 try {
253 ResourceBundle resourceBundle = ResourceBundle.getBundle(
254 portlet.getResourceBundle(), locale, _classLoader);
255
256 resourceBundles.put(
257 LocaleUtil.toLanguageId(locale), resourceBundle);
258 }
259 catch (MissingResourceException mre) {
260 _log.warn(mre.getMessage());
261 }
262 }
263
264 protected SocialActivityInterpreter initSocialActivityInterpreterInstance(
265 Portlet portlet)
266 throws Exception {
267
268 if (Validator.isNull(portlet.getSocialActivityInterpreterClass())) {
269 return null;
270 }
271
272 SocialActivityInterpreter socialActivityInterpreterInstance =
273 (SocialActivityInterpreter)newInstance(
274 SocialActivityInterpreter.class,
275 portlet.getSocialActivityInterpreterClass());
276
277 socialActivityInterpreterInstance =
278 new SocialActivityInterpreterImpl(
279 portlet.getPortletId(), socialActivityInterpreterInstance);
280
281 SocialActivityInterpreterLocalServiceUtil.addActivityInterpreter(
282 socialActivityInterpreterInstance);
283
284 return socialActivityInterpreterInstance;
285 }
286
287 protected ConfigurationAction newConfigurationAction(Portlet portlet)
288 throws Exception {
289
290 if (Validator.isNull(portlet.getConfigurationActionClass())) {
291 return null;
292 }
293
294 return (ConfigurationAction)newInstance(
295 ConfigurationAction.class, portlet.getConfigurationActionClass());
296 }
297
298 protected FriendlyURLMapper newFriendlyURLMapper(Portlet portlet)
299 throws Exception {
300
301 if (Validator.isNull(portlet.getFriendlyURLMapperClass())) {
302 return null;
303 }
304
305 return (FriendlyURLMapper)newInstance(
306 FriendlyURLMapper.class, portlet.getFriendlyURLMapperClass());
307 }
308
309 protected Indexer newIndexer(Portlet portlet) throws Exception {
310 if (Validator.isNull(portlet.getIndexerClass())) {
311 return null;
312 }
313
314 Indexer indexerInstance = (Indexer)newInstance(
315 Indexer.class, portlet.getIndexerClass());
316
317 IndexerRegistryUtil.register(indexerInstance);
318
319 return indexerInstance;
320 }
321
322 protected Object newInstance(Class<?> interfaceClass, String implClassName)
323 throws Exception {
324
325 return newInstance(new Class[] {interfaceClass}, implClassName);
326 }
327
328 protected Object newInstance(
329 Class<?>[] interfaceClasses, String implClassName)
330 throws Exception {
331
332 if (_warFile) {
333 return ProxyFactory.newInstance(
334 _classLoader, interfaceClasses, implClassName);
335 }
336 else {
337 Class<?> classObj = _classLoader.loadClass(implClassName);
338
339 return classObj.newInstance();
340 }
341 }
342
343 protected OpenSearch newOpenSearch(Portlet portlet) throws Exception {
344 if (Validator.isNull(portlet.getOpenSearchClass())) {
345 return null;
346 }
347
348 return (OpenSearch)newInstance(
349 OpenSearch.class, portlet.getOpenSearchClass());
350 }
351
352 protected PollerProcessor newPollerProcessor(Portlet portlet)
353 throws Exception {
354
355 if (Validator.isNull(portlet.getPollerProcessorClass())) {
356 return null;
357 }
358
359 PollerProcessor pollerProcessorInstance = (PollerProcessor)newInstance(
360 PollerProcessor.class, portlet.getPollerProcessorClass());
361
362 PollerProcessorUtil.addPollerProcessor(
363 portlet.getPortletId(),
364 new ShardPollerProcessorWrapper(pollerProcessorInstance));
365
366 return pollerProcessorInstance;
367 }
368
369 protected MessageListener newPOPMessageListener(Portlet portlet)
370 throws Exception {
371
372 if (Validator.isNull(portlet.getPopMessageListenerClass())) {
373 return null;
374 }
375
376 MessageListener popMessageListenerInstance =
377 (MessageListener)newInstance(
378 MessageListener.class, portlet.getPopMessageListenerClass());
379
380 POPServerUtil.addListener(popMessageListenerInstance);
381
382 return popMessageListenerInstance;
383 }
384
385 protected PortletDataHandler newPortletDataHandler(Portlet portlet)
386 throws Exception {
387
388 if (Validator.isNull(portlet.getPortletDataHandlerClass())) {
389 return null;
390 }
391
392 return (PortletDataHandler)newInstance(
393 PortletDataHandler.class, portlet.getPortletDataHandlerClass());
394 }
395
396 protected PortletLayoutListener newPortletLayoutListener(Portlet portlet)
397 throws Exception {
398
399 if (Validator.isNull(portlet.getPortletLayoutListenerClass())) {
400 return null;
401 }
402
403 return (PortletLayoutListener)newInstance(
404 PortletLayoutListener.class,
405 portlet.getPortletLayoutListenerClass());
406 }
407
408 protected URLEncoder newURLEncoder(Portlet portlet) throws Exception {
409 if (Validator.isNull(portlet.getURLEncoderClass())) {
410 return null;
411 }
412
413 return (URLEncoder)newInstance(
414 URLEncoder.class, portlet.getURLEncoderClass());
415 }
416
417 private static Log _log = LogFactoryUtil.getLog(PortletBagFactory.class);
418
419 private ClassLoader _classLoader;
420 private ServletContext _servletContext;
421 private boolean _warFile;
422
423 }