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