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