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