001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet;
016    
017    import com.liferay.asset.kernel.model.AssetRendererFactory;
018    import com.liferay.expando.kernel.model.CustomAttributesDisplay;
019    import com.liferay.exportimport.kernel.lar.PortletDataHandler;
020    import com.liferay.exportimport.kernel.lar.StagedModelDataHandler;
021    import com.liferay.portal.kernel.atom.AtomCollectionAdapter;
022    import com.liferay.portal.kernel.configuration.Configuration;
023    import com.liferay.portal.kernel.configuration.ConfigurationFactoryUtil;
024    import com.liferay.portal.kernel.log.Log;
025    import com.liferay.portal.kernel.log.LogFactoryUtil;
026    import com.liferay.portal.kernel.model.Portlet;
027    import com.liferay.portal.kernel.notifications.UserNotificationDefinition;
028    import com.liferay.portal.kernel.notifications.UserNotificationDeliveryType;
029    import com.liferay.portal.kernel.notifications.UserNotificationHandler;
030    import com.liferay.portal.kernel.poller.PollerProcessor;
031    import com.liferay.portal.kernel.pop.MessageListener;
032    import com.liferay.portal.kernel.portlet.ConfigurationAction;
033    import com.liferay.portal.kernel.portlet.ControlPanelEntry;
034    import com.liferay.portal.kernel.portlet.FriendlyURLMapper;
035    import com.liferay.portal.kernel.portlet.FriendlyURLMapperTracker;
036    import com.liferay.portal.kernel.portlet.PortletBag;
037    import com.liferay.portal.kernel.portlet.PortletBagPool;
038    import com.liferay.portal.kernel.portlet.PortletInstanceFactoryUtil;
039    import com.liferay.portal.kernel.portlet.PortletLayoutListener;
040    import com.liferay.portal.kernel.portlet.PortletPreferencesFactoryUtil;
041    import com.liferay.portal.kernel.scheduler.SchedulerEntry;
042    import com.liferay.portal.kernel.scheduler.messaging.SchedulerEventMessageListener;
043    import com.liferay.portal.kernel.scheduler.messaging.SchedulerEventMessageListenerWrapper;
044    import com.liferay.portal.kernel.search.Indexer;
045    import com.liferay.portal.kernel.search.OpenSearch;
046    import com.liferay.portal.kernel.security.permission.PermissionPropagator;
047    import com.liferay.portal.kernel.service.PortletLocalServiceUtil;
048    import com.liferay.portal.kernel.servlet.URLEncoder;
049    import com.liferay.portal.kernel.template.TemplateHandler;
050    import com.liferay.portal.kernel.trash.TrashHandler;
051    import com.liferay.portal.kernel.util.GetterUtil;
052    import com.liferay.portal.kernel.util.HttpUtil;
053    import com.liferay.portal.kernel.util.InstanceFactory;
054    import com.liferay.portal.kernel.util.PortalUtil;
055    import com.liferay.portal.kernel.util.PropsKeys;
056    import com.liferay.portal.kernel.util.PropsUtil;
057    import com.liferay.portal.kernel.util.ProxyFactory;
058    import com.liferay.portal.kernel.util.StringPool;
059    import com.liferay.portal.kernel.util.StringUtil;
060    import com.liferay.portal.kernel.util.Validator;
061    import com.liferay.portal.kernel.webdav.WebDAVStorage;
062    import com.liferay.portal.kernel.workflow.WorkflowHandler;
063    import com.liferay.portal.kernel.xml.Document;
064    import com.liferay.portal.kernel.xml.Element;
065    import com.liferay.portal.kernel.xml.UnsecureSAXReaderUtil;
066    import com.liferay.portal.kernel.xmlrpc.Method;
067    import com.liferay.portal.notifications.UserNotificationHandlerImpl;
068    import com.liferay.portal.util.JavaFieldsParser;
069    import com.liferay.portal.util.PropsValues;
070    import com.liferay.registry.Filter;
071    import com.liferay.registry.Registry;
072    import com.liferay.registry.RegistryUtil;
073    import com.liferay.registry.collections.ServiceTrackerCollections;
074    import com.liferay.registry.collections.ServiceTrackerList;
075    import com.liferay.social.kernel.model.SocialActivityInterpreter;
076    import com.liferay.social.kernel.model.SocialRequestInterpreter;
077    import com.liferay.social.kernel.model.impl.SocialActivityInterpreterImpl;
078    import com.liferay.social.kernel.model.impl.SocialRequestInterpreterImpl;
079    
080    import java.util.Collections;
081    import java.util.HashMap;
082    import java.util.List;
083    import java.util.Map;
084    
085    import javax.portlet.PreferencesValidator;
086    
087    import javax.servlet.ServletContext;
088    
089    /**
090     * @author Brian Wing Shun Chan
091     * @author Brian Myunghun Kim
092     * @author Ivica Cardic
093     * @author Raymond Aug??
094     */
095    public class PortletBagFactory {
096    
097            public PortletBag create(Portlet portlet) throws Exception {
098                    return create(portlet, false);
099            }
100    
101            public PortletBag create(Portlet portlet, boolean destroyPrevious)
102                    throws Exception {
103    
104                    validate();
105    
106                    javax.portlet.Portlet portletInstance = getPortletInstance(portlet);
107    
108                    Registry registry = RegistryUtil.getRegistry();
109    
110                    Filter filter = registry.getFilter(
111                            "(|(javax.portlet.name=" + portlet.getPortletId() +
112                                    ")(javax.portlet.name=ALL))");
113    
114                    Map<String, Object> properties =
115                            Collections.<String, Object>singletonMap(
116                                    "javax.portlet.name", portlet.getPortletId());
117    
118                    List<ConfigurationAction> configurationActionInstances =
119                            newConfigurationActions(portlet, filter, properties);
120    
121                    List<Indexer<?>> indexerInstances = newIndexers(
122                            portlet, filter, properties);
123    
124                    List<OpenSearch> openSearchInstances = newOpenSearches(
125                            portlet, filter, properties);
126    
127                    List<SchedulerEventMessageListener> schedulerEventMessageListeners =
128                            newSchedulerEventMessageListeners(portlet, filter, properties);
129    
130                    FriendlyURLMapperTracker friendlyURLMapperTracker =
131                            newFriendlyURLMappers(portlet);
132    
133                    List<URLEncoder> urlEncoderInstances = newURLEncoders(
134                            portlet, filter, properties);
135    
136                    List<PortletDataHandler> portletDataHandlerInstances =
137                            newPortletDataHandlers(portlet, filter, properties);
138    
139                    List<StagedModelDataHandler<?>> stagedModelDataHandlerInstances =
140                            newStagedModelDataHandler(portlet, filter, properties);
141    
142                    List<TemplateHandler> templateHandlerInstances = newTemplateHandlers(
143                            portlet, filter, properties);
144    
145                    List<PortletLayoutListener> portletLayoutListenerInstances =
146                            newPortletLayoutListeners(portlet, filter, properties);
147    
148                    List<PollerProcessor> pollerProcessorInstances = newPollerProcessors(
149                            portlet, filter, properties);
150    
151                    List<MessageListener> popMessageListenerInstances =
152                            newPOPMessageListeners(portlet, filter, properties);
153    
154                    List<SocialActivityInterpreter> socialActivityInterpreterInstances =
155                            newSocialActivityInterpreterInstances(portlet, filter, properties);
156    
157                    List<SocialRequestInterpreter> socialRequestInterpreterInstances =
158                            newSocialRequestInterpreterInstances(portlet, filter, properties);
159    
160                    List<UserNotificationDefinition> userNotificationDefinitionInstances =
161                            newUserNotificationDefinitionInstances(portlet, filter, properties);
162    
163                    List<UserNotificationHandler> userNotificationHandlerInstances =
164                            newUserNotificationHandlerInstances(portlet, filter, properties);
165    
166                    List<WebDAVStorage> webDAVStorageInstances = newWebDAVStorageInstances(
167                            portlet, filter, properties);
168    
169                    List<Method> xmlRpcMethodInstances = newXmlRpcMethodInstances(
170                            portlet, filter, properties);
171    
172                    List<ControlPanelEntry> controlPanelEntryInstances =
173                            newControlPanelEntryInstances(portlet, filter, properties);
174    
175                    List<AssetRendererFactory<?>> assetRendererFactoryInstances =
176                            newAssetRendererFactoryInstances(portlet, filter, properties);
177    
178                    List<AtomCollectionAdapter<?>> atomCollectionAdapterInstances =
179                            newAtomCollectionAdapterInstances(portlet, filter, properties);
180    
181                    List<CustomAttributesDisplay> customAttributesDisplayInstances =
182                            newCustomAttributesDisplayInstances(portlet, filter, properties);
183    
184                    List<PermissionPropagator> permissionPropagatorInstances =
185                            newPermissionPropagators(portlet, filter, properties);
186    
187                    List<TrashHandler> trashHandlerInstances = newTrashHandlerInstances(
188                            portlet, filter, properties);
189    
190                    List<WorkflowHandler<?>> workflowHandlerInstances =
191                            newWorkflowHandlerInstances(portlet, filter, properties);
192    
193                    List<PreferencesValidator> preferencesValidatorInstances =
194                            newPreferencesValidatorInstances(portlet, filter, properties);
195    
196                    PortletBag portletBag = new PortletBagImpl(
197                            portlet.getPortletId(), _servletContext, portletInstance,
198                            portlet.getResourceBundle(), configurationActionInstances,
199                            indexerInstances, openSearchInstances,
200                            schedulerEventMessageListeners, friendlyURLMapperTracker,
201                            urlEncoderInstances, portletDataHandlerInstances,
202                            stagedModelDataHandlerInstances, templateHandlerInstances,
203                            portletLayoutListenerInstances, pollerProcessorInstances,
204                            popMessageListenerInstances, socialActivityInterpreterInstances,
205                            socialRequestInterpreterInstances,
206                            userNotificationDefinitionInstances,
207                            userNotificationHandlerInstances, webDAVStorageInstances,
208                            xmlRpcMethodInstances, controlPanelEntryInstances,
209                            assetRendererFactoryInstances, atomCollectionAdapterInstances,
210                            customAttributesDisplayInstances, permissionPropagatorInstances,
211                            trashHandlerInstances, workflowHandlerInstances,
212                            preferencesValidatorInstances);
213    
214                    PortletBagPool.put(portlet.getRootPortletId(), portletBag);
215    
216                    try {
217                            PortletInstanceFactoryUtil.create(
218                                    portlet, _servletContext, destroyPrevious);
219                    }
220                    catch (Exception e) {
221                            _log.error(e, e);
222                    }
223    
224                    return portletBag;
225            }
226    
227            public void setClassLoader(ClassLoader classLoader) {
228                    _classLoader = classLoader;
229            }
230    
231            public void setServletContext(ServletContext servletContext) {
232                    _servletContext = servletContext;
233            }
234    
235            public void setWARFile(boolean warFile) {
236                    _warFile = warFile;
237            }
238    
239            /**
240             * @see FriendlyURLMapperTrackerImpl#getContent(ClassLoader, String)
241             */
242            protected String getContent(String fileName) throws Exception {
243                    String queryString = HttpUtil.getQueryString(fileName);
244    
245                    if (Validator.isNull(queryString)) {
246                            return StringUtil.read(_classLoader, fileName);
247                    }
248    
249                    int pos = fileName.indexOf(StringPool.QUESTION);
250    
251                    String xml = StringUtil.read(_classLoader, fileName.substring(0, pos));
252    
253                    Map<String, String[]> parameterMap = HttpUtil.getParameterMap(
254                            queryString);
255    
256                    if (parameterMap == null) {
257                            return xml;
258                    }
259    
260                    for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
261                            String name = entry.getKey();
262                            String[] values = entry.getValue();
263    
264                            if (values.length == 0) {
265                                    continue;
266                            }
267    
268                            String value = values[0];
269    
270                            xml = StringUtil.replace(xml, "@" + name + "@", value);
271                    }
272    
273                    return xml;
274            }
275    
276            protected String getPluginPropertyValue(String propertyKey)
277                    throws Exception {
278    
279                    if (_configuration == null) {
280                            _configuration = ConfigurationFactoryUtil.getConfiguration(
281                                    _classLoader, "portlet");
282                    }
283    
284                    return _configuration.get(propertyKey);
285            }
286    
287            protected javax.portlet.Portlet getPortletInstance(Portlet portlet)
288                    throws IllegalAccessException, InstantiationException {
289    
290                    Class<?> portletClass = null;
291    
292                    try {
293                            portletClass = _classLoader.loadClass(portlet.getPortletClass());
294                    }
295                    catch (Throwable t) {
296                            _log.error(t, t);
297    
298                            PortletLocalServiceUtil.destroyPortlet(portlet);
299    
300                            return null;
301                    }
302    
303                    return (javax.portlet.Portlet)portletClass.newInstance();
304            }
305    
306            protected List<AssetRendererFactory<?>> newAssetRendererFactoryInstances(
307                            Portlet portlet, Filter filter, Map<String, Object> properties)
308                    throws Exception {
309    
310                    ServiceTrackerList<AssetRendererFactory<?>>
311                            assetRendererFactoryInstances = ServiceTrackerCollections.openList(
312                                    (Class<AssetRendererFactory<?>>)(Class<?>)
313                                            AssetRendererFactory.class,
314                                    filter, properties);
315    
316                    for (String assetRendererFactoryClass :
317                                    portlet.getAssetRendererFactoryClasses()) {
318    
319                            String assetRendererEnabledPropertyKey =
320                                    PropsKeys.ASSET_RENDERER_ENABLED + assetRendererFactoryClass;
321    
322                            String assetRendererEnabledPropertyValue = null;
323    
324                            if (_warFile) {
325                                    assetRendererEnabledPropertyValue = getPluginPropertyValue(
326                                            assetRendererEnabledPropertyKey);
327                            }
328                            else {
329                                    assetRendererEnabledPropertyValue = PropsUtil.get(
330                                            assetRendererEnabledPropertyKey);
331                            }
332    
333                            boolean assetRendererEnabledValue = GetterUtil.getBoolean(
334                                    assetRendererEnabledPropertyValue, true);
335    
336                            if (assetRendererEnabledValue) {
337                                    AssetRendererFactory<?> assetRendererFactoryInstance =
338                                            (AssetRendererFactory<?>)newInstance(
339                                                    AssetRendererFactory.class, assetRendererFactoryClass);
340    
341                                    assetRendererFactoryInstance.setClassName(
342                                            assetRendererFactoryInstance.getClassName());
343                                    assetRendererFactoryInstance.setPortletId(
344                                            portlet.getPortletId());
345    
346                                    assetRendererFactoryInstances.add(assetRendererFactoryInstance);
347                            }
348                    }
349    
350                    return assetRendererFactoryInstances;
351            }
352    
353            protected List<AtomCollectionAdapter<?>> newAtomCollectionAdapterInstances(
354                            Portlet portlet, Filter filter, Map<String, Object> properties)
355                    throws Exception {
356    
357                    ServiceTrackerList<AtomCollectionAdapter<?>>
358                            atomCollectionAdapterInstances = ServiceTrackerCollections.openList(
359                                    (Class<AtomCollectionAdapter<?>>)(Class<?>)
360                                            AtomCollectionAdapter.class, filter, properties);
361    
362                    for (String atomCollectionAdapterClass :
363                                    portlet.getAtomCollectionAdapterClasses()) {
364    
365                            AtomCollectionAdapter<?> atomCollectionAdapterInstance =
366                                    (AtomCollectionAdapter<?>)newInstance(
367                                            AtomCollectionAdapter.class, atomCollectionAdapterClass);
368    
369                            atomCollectionAdapterInstances.add(atomCollectionAdapterInstance);
370                    }
371    
372                    return atomCollectionAdapterInstances;
373            }
374    
375            protected List<ConfigurationAction> newConfigurationActions(
376                            Portlet portlet, Filter filter, Map<String, Object> properties)
377                    throws Exception {
378    
379                    ServiceTrackerList<ConfigurationAction> configurationActionInstances =
380                            ServiceTrackerCollections.openList(
381                                    ConfigurationAction.class, filter, properties);
382    
383                    if (Validator.isNotNull(portlet.getConfigurationActionClass())) {
384                            ConfigurationAction configurationAction =
385                                    (ConfigurationAction)newInstance(
386                                            ConfigurationAction.class,
387                                            portlet.getConfigurationActionClass());
388    
389                            configurationActionInstances.add(configurationAction);
390                    }
391    
392                    return configurationActionInstances;
393            }
394    
395            protected List<ControlPanelEntry> newControlPanelEntryInstances(
396                            Portlet portlet, Filter filter, Map<String, Object> properties)
397                    throws Exception {
398    
399                    ServiceTrackerList<ControlPanelEntry> controlPanelEntryInstances =
400                            ServiceTrackerCollections.openList(
401                                    ControlPanelEntry.class, filter, properties);
402    
403                    if (Validator.isNotNull(portlet.getControlPanelEntryClass())) {
404                            ControlPanelEntry controlPanelEntryInstance =
405                                    (ControlPanelEntry)newInstance(
406                                            ControlPanelEntry.class,
407                                            portlet.getControlPanelEntryClass());
408    
409                            controlPanelEntryInstances.add(controlPanelEntryInstance);
410                    }
411    
412                    return controlPanelEntryInstances;
413            }
414    
415            protected List<CustomAttributesDisplay> newCustomAttributesDisplayInstances(
416                            Portlet portlet, Filter filter, Map<String, Object> properties)
417                    throws Exception {
418    
419                    ServiceTrackerList<CustomAttributesDisplay>
420                            customAttributesDisplayInstances =
421                                    ServiceTrackerCollections.openList(
422                                            CustomAttributesDisplay.class, filter, properties);
423    
424                    for (String customAttributesDisplayClass :
425                                    portlet.getCustomAttributesDisplayClasses()) {
426    
427                            CustomAttributesDisplay customAttributesDisplayInstance =
428                                    (CustomAttributesDisplay)newInstance(
429                                            CustomAttributesDisplay.class,
430                                            customAttributesDisplayClass);
431    
432                            customAttributesDisplayInstance.setClassNameId(
433                                    PortalUtil.getClassNameId(
434                                            customAttributesDisplayInstance.getClassName()));
435                            customAttributesDisplayInstance.setPortletId(
436                                    portlet.getPortletId());
437    
438                            customAttributesDisplayInstances.add(
439                                    customAttributesDisplayInstance);
440                    }
441    
442                    return customAttributesDisplayInstances;
443            }
444    
445            protected FriendlyURLMapperTracker newFriendlyURLMappers(Portlet portlet)
446                    throws Exception {
447    
448                    FriendlyURLMapperTracker friendlyURLMapperTracker =
449                            new FriendlyURLMapperTrackerImpl(portlet);
450    
451                    if (Validator.isNotNull(portlet.getFriendlyURLMapperClass())) {
452                            FriendlyURLMapper friendlyURLMapper =
453                                    (FriendlyURLMapper)newInstance(
454                                            FriendlyURLMapper.class,
455                                            portlet.getFriendlyURLMapperClass());
456    
457                            friendlyURLMapperTracker.register(friendlyURLMapper);
458                    }
459    
460                    return friendlyURLMapperTracker;
461            }
462    
463            protected List<Indexer<?>> newIndexers(
464                            Portlet portlet, Filter filter, Map<String, Object> properties)
465                    throws Exception {
466    
467                    ServiceTrackerList<Indexer<?>> indexerInstances =
468                            ServiceTrackerCollections.openList(
469                                    (Class<Indexer<?>>)(Class<?>)Indexer.class, filter, properties);
470    
471                    List<String> indexerClasses = portlet.getIndexerClasses();
472    
473                    for (String indexerClass : indexerClasses) {
474                            Indexer<?> indexerInstance = (Indexer<?>)newInstance(
475                                    Indexer.class, indexerClass);
476    
477                            indexerInstances.add(indexerInstance);
478                    }
479    
480                    return indexerInstances;
481            }
482    
483            protected Object newInstance(Class<?> interfaceClass, String implClassName)
484                    throws Exception {
485    
486                    return newInstance(new Class[] {interfaceClass}, implClassName);
487            }
488    
489            protected Object newInstance(
490                            Class<?>[] interfaceClasses, String implClassName)
491                    throws Exception {
492    
493                    if (_warFile) {
494                            return ProxyFactory.newInstance(
495                                    _classLoader, interfaceClasses, implClassName);
496                    }
497                    else {
498                            Class<?> clazz = _classLoader.loadClass(implClassName);
499    
500                            return clazz.newInstance();
501                    }
502            }
503    
504            protected List<OpenSearch> newOpenSearches(
505                            Portlet portlet, Filter filter, Map<String, Object> properties)
506                    throws Exception {
507    
508                    ServiceTrackerList<OpenSearch> openSearchInstances =
509                            ServiceTrackerCollections.openList(
510                                    OpenSearch.class, filter, properties);
511    
512                    if (Validator.isNotNull(portlet.getOpenSearchClass())) {
513                            OpenSearch openSearch = (OpenSearch)newInstance(
514                                    OpenSearch.class, portlet.getOpenSearchClass());
515    
516                            openSearchInstances.add(openSearch);
517                    }
518    
519                    return openSearchInstances;
520            }
521    
522            protected List<PermissionPropagator> newPermissionPropagators(
523                            Portlet portlet, Filter filter, Map<String, Object> properties)
524                    throws Exception {
525    
526                    ServiceTrackerList<PermissionPropagator> permissionPropagatorInstances =
527                            ServiceTrackerCollections.openList(
528                                    PermissionPropagator.class, filter, properties);
529    
530                    if (Validator.isNotNull(portlet.getPermissionPropagatorClass())) {
531                            PermissionPropagator permissionPropagatorInstance =
532                                    (PermissionPropagator)newInstance(
533                                            PermissionPropagator.class,
534                                            portlet.getPermissionPropagatorClass());
535    
536                            permissionPropagatorInstances.add(permissionPropagatorInstance);
537                    }
538    
539                    return permissionPropagatorInstances;
540            }
541    
542            protected List<PollerProcessor> newPollerProcessors(
543                            Portlet portlet, Filter filter, Map<String, Object> properties)
544                    throws Exception {
545    
546                    ServiceTrackerList<PollerProcessor> pollerProcessorInstances =
547                            ServiceTrackerCollections.openList(
548                                    PollerProcessor.class, filter, properties);
549    
550                    if (Validator.isNotNull(portlet.getPollerProcessorClass())) {
551                            PollerProcessor pollerProcessorInstance =
552                                    (PollerProcessor)newInstance(
553                                            PollerProcessor.class, portlet.getPollerProcessorClass());
554    
555                            pollerProcessorInstances.add(pollerProcessorInstance);
556                    }
557    
558                    return pollerProcessorInstances;
559            }
560    
561            protected List<MessageListener> newPOPMessageListeners(
562                            Portlet portlet, Filter filter, Map<String, Object> properties)
563                    throws Exception {
564    
565                    ServiceTrackerList<MessageListener> messageListenerInstances =
566                            ServiceTrackerCollections.openList(
567                                    MessageListener.class, filter, properties);
568    
569                    if (Validator.isNotNull(portlet.getPopMessageListenerClass())) {
570                            MessageListener popMessageListenerInstance =
571                                    (MessageListener)newInstance(
572                                            MessageListener.class,
573                                            portlet.getPopMessageListenerClass());
574    
575                            messageListenerInstances.add(popMessageListenerInstance);
576                    }
577    
578                    return messageListenerInstances;
579            }
580    
581            protected List<PortletDataHandler> newPortletDataHandlers(
582                            Portlet portlet, Filter filter, Map<String, Object> properties)
583                    throws Exception {
584    
585                    ServiceTrackerList<PortletDataHandler> portletDataHandlerInstances =
586                            ServiceTrackerCollections.openList(
587                                    PortletDataHandler.class, filter, properties);
588    
589                    if (Validator.isNotNull(portlet.getPortletDataHandlerClass())) {
590                            PortletDataHandler portletDataHandlerInstance =
591                                    (PortletDataHandler)newInstance(
592                                            PortletDataHandler.class,
593                                            portlet.getPortletDataHandlerClass());
594    
595                            portletDataHandlerInstance.setPortletId(portlet.getPortletId());
596    
597                            portletDataHandlerInstances.add(portletDataHandlerInstance);
598                    }
599    
600                    return portletDataHandlerInstances;
601            }
602    
603            protected List<PortletLayoutListener> newPortletLayoutListeners(
604                            Portlet portlet, Filter filter, Map<String, Object> properties)
605                    throws Exception {
606    
607                    ServiceTrackerList<PortletLayoutListener>
608                            portletLayoutListenerInstances = ServiceTrackerCollections.openList(
609                                    PortletLayoutListener.class, filter, properties);
610    
611                    if (Validator.isNotNull(portlet.getPortletLayoutListenerClass())) {
612                            PortletLayoutListener portletLayoutListener =
613                                    (PortletLayoutListener)newInstance(
614                                            PortletLayoutListener.class,
615                                            portlet.getPortletLayoutListenerClass());
616    
617                            portletLayoutListenerInstances.add(portletLayoutListener);
618                    }
619    
620                    return portletLayoutListenerInstances;
621            }
622    
623            protected List<PreferencesValidator> newPreferencesValidatorInstances(
624                            Portlet portlet, Filter filter, Map<String, Object> properties)
625                    throws Exception {
626    
627                    ServiceTrackerList<PreferencesValidator> preferencesValidatorInstances =
628                            ServiceTrackerCollections.openList(
629                                    PreferencesValidator.class, filter, properties);
630    
631                    if (Validator.isNotNull(portlet.getPreferencesValidator())) {
632                            PreferencesValidator preferencesValidatorInstance =
633                                    (PreferencesValidator)newInstance(
634                                            PreferencesValidator.class,
635                                            portlet.getPreferencesValidator());
636    
637                            try {
638                                    if (PropsValues.PREFERENCE_VALIDATE_ON_STARTUP) {
639                                            preferencesValidatorInstance.validate(
640                                                    PortletPreferencesFactoryUtil.fromDefaultXML(
641                                                            portlet.getDefaultPreferences()));
642                                    }
643                            }
644                            catch (Exception e) {
645                                    if (_log.isWarnEnabled()) {
646                                            _log.warn(
647                                                    "Portlet with the name " + portlet.getPortletId() +
648                                                            " does not have valid default preferences");
649                                    }
650                            }
651    
652                            preferencesValidatorInstances.add(preferencesValidatorInstance);
653                    }
654    
655                    return preferencesValidatorInstances;
656            }
657    
658            protected List<SchedulerEventMessageListener>
659                            newSchedulerEventMessageListeners(
660                                    Portlet portlet, Filter filter, Map<String, Object> properties)
661                    throws Exception {
662    
663                    ServiceTrackerList<SchedulerEventMessageListener>
664                            schedulerEventMessageListeners = ServiceTrackerCollections.openList(
665                                    SchedulerEventMessageListener.class, filter, properties);
666    
667                    List<SchedulerEntry> schedulerEntries = portlet.getSchedulerEntries();
668    
669                    for (SchedulerEntry schedulerEntry : schedulerEntries) {
670                            SchedulerEventMessageListenerWrapper
671                                    schedulerEventMessageListenerWrapper =
672                                            new SchedulerEventMessageListenerWrapper();
673    
674                            com.liferay.portal.kernel.messaging.MessageListener
675                                    messageListener =
676                                            (com.liferay.portal.kernel.messaging.MessageListener)
677                                                    InstanceFactory.newInstance(
678                                                            _classLoader,
679                                                            schedulerEntry.getEventListenerClass());
680    
681                            schedulerEventMessageListenerWrapper.setMessageListener(
682                                    messageListener);
683    
684                            schedulerEventMessageListenerWrapper.setSchedulerEntry(
685                                    schedulerEntry);
686    
687                            schedulerEventMessageListeners.add(
688                                    schedulerEventMessageListenerWrapper);
689                    }
690    
691                    return schedulerEventMessageListeners;
692            }
693    
694            protected List<SocialActivityInterpreter>
695                            newSocialActivityInterpreterInstances(
696                                    Portlet portlet, Filter filter, Map<String, Object> properties)
697                    throws Exception {
698    
699                    ServiceTrackerList<SocialActivityInterpreter>
700                            socialActivityInterpreterInstances =
701                                    ServiceTrackerCollections.openList(
702                                            SocialActivityInterpreter.class, filter, properties);
703    
704                    for (String socialActivityInterpreterClass :
705                                    portlet.getSocialActivityInterpreterClasses()) {
706    
707                            SocialActivityInterpreter socialActivityInterpreterInstance =
708                                    (SocialActivityInterpreter)newInstance(
709                                            SocialActivityInterpreter.class,
710                                            socialActivityInterpreterClass);
711    
712                            socialActivityInterpreterInstance =
713                                    new SocialActivityInterpreterImpl(
714                                            portlet.getPortletId(), socialActivityInterpreterInstance);
715    
716                            socialActivityInterpreterInstances.add(
717                                    socialActivityInterpreterInstance);
718                    }
719    
720                    return socialActivityInterpreterInstances;
721            }
722    
723            protected List<SocialRequestInterpreter>
724                            newSocialRequestInterpreterInstances(
725                                    Portlet portlet, Filter filter, Map<String, Object> properties)
726                    throws Exception {
727    
728                    ServiceTrackerList<SocialRequestInterpreter>
729                            socialRequestInterpreterInstances =
730                                    ServiceTrackerCollections.openList(
731                                            SocialRequestInterpreter.class, filter, properties);
732    
733                    if (Validator.isNotNull(portlet.getSocialRequestInterpreterClass())) {
734                            SocialRequestInterpreter socialRequestInterpreterInstance =
735                                    (SocialRequestInterpreter)newInstance(
736                                            SocialRequestInterpreter.class,
737                                            portlet.getSocialRequestInterpreterClass());
738    
739                            socialRequestInterpreterInstance = new SocialRequestInterpreterImpl(
740                                    portlet.getPortletId(), socialRequestInterpreterInstance);
741    
742                            socialRequestInterpreterInstances.add(
743                                    socialRequestInterpreterInstance);
744                    }
745    
746                    return socialRequestInterpreterInstances;
747            }
748    
749            protected List<StagedModelDataHandler<?>> newStagedModelDataHandler(
750                            Portlet portlet, Filter filter, Map<String, Object> properties)
751                    throws Exception {
752    
753                    ServiceTrackerList<StagedModelDataHandler<?>>
754                            stagedModelDataHandlerInstances =
755                                    ServiceTrackerCollections.openList(
756                                            (Class<StagedModelDataHandler<?>>)(Class<?>)
757                                                    StagedModelDataHandler.class,
758                                            filter, properties);
759    
760                    List<String> stagedModelDataHandlerClasses =
761                            portlet.getStagedModelDataHandlerClasses();
762    
763                    for (String stagedModelDataHandlerClass :
764                                    stagedModelDataHandlerClasses) {
765    
766                            StagedModelDataHandler<?> stagedModelDataHandler =
767                                    (StagedModelDataHandler<?>)newInstance(
768                                            StagedModelDataHandler.class, stagedModelDataHandlerClass);
769    
770                            stagedModelDataHandlerInstances.add(stagedModelDataHandler);
771                    }
772    
773                    return stagedModelDataHandlerInstances;
774            }
775    
776            protected List<TemplateHandler> newTemplateHandlers(
777                            Portlet portlet, Filter filter, Map<String, Object> properties)
778                    throws Exception {
779    
780                    ServiceTrackerList<TemplateHandler> templateHandlerInstances =
781                            ServiceTrackerCollections.openList(
782                                    TemplateHandler.class, filter, properties);
783    
784                    if (Validator.isNotNull(portlet.getTemplateHandlerClass())) {
785                            TemplateHandler templateHandler = (TemplateHandler)newInstance(
786                                    TemplateHandler.class, portlet.getTemplateHandlerClass());
787    
788                            templateHandlerInstances.add(templateHandler);
789                    }
790    
791                    return templateHandlerInstances;
792            }
793    
794            protected List<TrashHandler> newTrashHandlerInstances(
795                            Portlet portlet, Filter filter, Map<String, Object> properties)
796                    throws Exception {
797    
798                    ServiceTrackerList<TrashHandler> trashHandlerInstances =
799                            ServiceTrackerCollections.openList(
800                                    TrashHandler.class, filter, properties);
801    
802                    for (String trashHandlerClass : portlet.getTrashHandlerClasses()) {
803                            TrashHandler trashHandlerInstance = (TrashHandler)newInstance(
804                                    TrashHandler.class, trashHandlerClass);
805    
806                            trashHandlerInstances.add(trashHandlerInstance);
807                    }
808    
809                    return trashHandlerInstances;
810            }
811    
812            protected List<URLEncoder> newURLEncoders(
813                            Portlet portlet, Filter filter, Map<String, Object> properties)
814                    throws Exception {
815    
816                    ServiceTrackerList<URLEncoder> urlEncoderInstances =
817                            ServiceTrackerCollections.openList(
818                                    URLEncoder.class, filter, properties);
819    
820                    if (Validator.isNotNull(portlet.getURLEncoderClass())) {
821                            URLEncoder urlEncoder = (URLEncoder)newInstance(
822                                    URLEncoder.class, portlet.getURLEncoderClass());
823    
824                            urlEncoderInstances.add(urlEncoder);
825                    }
826    
827                    return urlEncoderInstances;
828            }
829    
830            protected List<UserNotificationDefinition>
831                            newUserNotificationDefinitionInstances(
832                                    Portlet portlet, Filter filter, Map<String, Object> properties)
833                    throws Exception {
834    
835                    ServiceTrackerList<UserNotificationDefinition>
836                            userNotificationDefinitionInstances =
837                                    ServiceTrackerCollections.openList(
838                                            UserNotificationDefinition.class, filter, properties);
839    
840                    if (Validator.isNull(portlet.getUserNotificationDefinitions())) {
841                            return userNotificationDefinitionInstances;
842                    }
843    
844                    String xml = getContent(portlet.getUserNotificationDefinitions());
845    
846                    xml = JavaFieldsParser.parse(_classLoader, xml);
847    
848                    Document document = UnsecureSAXReaderUtil.read(xml);
849    
850                    Element rootElement = document.getRootElement();
851    
852                    for (Element definitionElement : rootElement.elements("definition")) {
853                            String modelName = definitionElement.elementText("model-name");
854    
855                            long classNameId = 0;
856    
857                            if (Validator.isNotNull(modelName)) {
858                                    classNameId = PortalUtil.getClassNameId(modelName);
859                            }
860    
861                            int notificationType = GetterUtil.getInteger(
862                                    definitionElement.elementText("notification-type"));
863    
864                            String description = GetterUtil.getString(
865                                    definitionElement.elementText("description"));
866    
867                            UserNotificationDefinition userNotificationDefinition =
868                                    new UserNotificationDefinition(
869                                            portlet.getPortletId(), classNameId, notificationType,
870                                            description);
871    
872                            for (Element deliveryTypeElement :
873                                            definitionElement.elements("delivery-type")) {
874    
875                                    String name = deliveryTypeElement.elementText("name");
876                                    int type = GetterUtil.getInteger(
877                                            deliveryTypeElement.elementText("type"));
878                                    boolean defaultValue = GetterUtil.getBoolean(
879                                            deliveryTypeElement.elementText("default"));
880                                    boolean modifiable = GetterUtil.getBoolean(
881                                            deliveryTypeElement.elementText("modifiable"));
882    
883                                    userNotificationDefinition.addUserNotificationDeliveryType(
884                                            new UserNotificationDeliveryType(
885                                                    name, type, defaultValue, modifiable));
886                            }
887    
888                            userNotificationDefinitionInstances.add(userNotificationDefinition);
889                    }
890    
891                    return userNotificationDefinitionInstances;
892            }
893    
894            protected List<UserNotificationHandler> newUserNotificationHandlerInstances(
895                            Portlet portlet, Filter filter, Map<String, Object> properties)
896                    throws Exception {
897    
898                    ServiceTrackerList<UserNotificationHandler>
899                            userNotificationHandlerInstances =
900                                    ServiceTrackerCollections.openList(
901                                            UserNotificationHandler.class, filter, properties);
902    
903                    for (String userNotificationHandlerClass :
904                                    portlet.getUserNotificationHandlerClasses()) {
905    
906                            UserNotificationHandler userNotificationHandlerInstance =
907                                    (UserNotificationHandler)newInstance(
908                                            UserNotificationHandler.class,
909                                            userNotificationHandlerClass);
910    
911                            userNotificationHandlerInstance = new UserNotificationHandlerImpl(
912                                    userNotificationHandlerInstance);
913    
914                            userNotificationHandlerInstances.add(
915                                    userNotificationHandlerInstance);
916                    }
917    
918                    return userNotificationHandlerInstances;
919            }
920    
921            protected List<WebDAVStorage> newWebDAVStorageInstances(
922                            Portlet portlet, Filter filter, Map<String, Object> properties)
923                    throws Exception {
924    
925                    ServiceTrackerList<WebDAVStorage> webDAVStorageInstances =
926                            ServiceTrackerCollections.openList(
927                                    WebDAVStorage.class, filter, properties);
928    
929                    if (Validator.isNotNull(portlet.getWebDAVStorageClass())) {
930                            WebDAVStorage webDAVStorageInstance = (WebDAVStorage)newInstance(
931                                    WebDAVStorage.class, portlet.getWebDAVStorageClass());
932    
933                            Map<String, Object> webDAVProperties = new HashMap<>();
934    
935                            webDAVProperties.put("javax.portlet.name", portlet.getPortletId());
936                            webDAVProperties.put(
937                                    "webdav.storage.token", portlet.getWebDAVStorageToken());
938    
939                            webDAVStorageInstances.add(webDAVStorageInstance, webDAVProperties);
940                    }
941    
942                    return webDAVStorageInstances;
943            }
944    
945            protected List<WorkflowHandler<?>> newWorkflowHandlerInstances(
946                            Portlet portlet, Filter filter, Map<String, Object> properties)
947                    throws Exception {
948    
949                    ServiceTrackerList<WorkflowHandler<?>> workflowHandlerInstances =
950                            ServiceTrackerCollections.openList(
951                                    (Class<WorkflowHandler<?>>)(Class<?>)WorkflowHandler.class,
952                                    filter, properties);
953    
954                    for (String workflowHandlerClass :
955                                    portlet.getWorkflowHandlerClasses()) {
956    
957                            WorkflowHandler<?> workflowHandlerInstance =
958                                    (WorkflowHandler<?>)newInstance(
959                                            WorkflowHandler.class, workflowHandlerClass);
960    
961                            workflowHandlerInstances.add(workflowHandlerInstance);
962                    }
963    
964                    return workflowHandlerInstances;
965            }
966    
967            protected List<Method> newXmlRpcMethodInstances(
968                            Portlet portlet, Filter filter, Map<String, Object> properties)
969                    throws Exception {
970    
971                    ServiceTrackerList<Method> xmlRpcMethodInstances =
972                            ServiceTrackerCollections.openList(
973                                    Method.class, filter, properties);
974    
975                    if (Validator.isNotNull(portlet.getXmlRpcMethodClass())) {
976                            Method xmlRpcMethodInstance = (Method)newInstance(
977                                    Method.class, portlet.getXmlRpcMethodClass());
978    
979                            xmlRpcMethodInstances.add(xmlRpcMethodInstance);
980                    }
981    
982                    return xmlRpcMethodInstances;
983            }
984    
985            protected void validate() {
986                    if (_classLoader == null) {
987                            throw new IllegalStateException("Class loader is null");
988                    }
989    
990                    if (_servletContext == null) {
991                            throw new IllegalStateException("Servlet context is null");
992                    }
993    
994                    if (_warFile == null) {
995                            throw new IllegalStateException("WAR file is null");
996                    }
997            }
998    
999            private static final Log _log = LogFactoryUtil.getLog(
1000                    PortletBagFactory.class);
1001    
1002            private ClassLoader _classLoader;
1003            private Configuration _configuration;
1004            private ServletContext _servletContext;
1005            private Boolean _warFile;
1006    
1007    }