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