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