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