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