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