001    /**
002     * Copyright (c) 2000-2012 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.dao.shard.ShardPollerProcessorWrapper;
018    import com.liferay.portal.kernel.atom.AtomCollectionAdapter;
019    import com.liferay.portal.kernel.atom.AtomCollectionAdapterRegistryUtil;
020    import com.liferay.portal.kernel.lar.PortletDataHandler;
021    import com.liferay.portal.kernel.lar.StagedModelDataHandler;
022    import com.liferay.portal.kernel.lar.StagedModelDataHandlerRegistryUtil;
023    import com.liferay.portal.kernel.log.Log;
024    import com.liferay.portal.kernel.log.LogFactoryUtil;
025    import com.liferay.portal.kernel.poller.PollerProcessor;
026    import com.liferay.portal.kernel.pop.MessageListener;
027    import com.liferay.portal.kernel.portlet.ConfigurationAction;
028    import com.liferay.portal.kernel.portlet.FriendlyURLMapper;
029    import com.liferay.portal.kernel.portlet.PortletBag;
030    import com.liferay.portal.kernel.portlet.PortletBagPool;
031    import com.liferay.portal.kernel.portlet.PortletLayoutListener;
032    import com.liferay.portal.kernel.portlet.Route;
033    import com.liferay.portal.kernel.portlet.Router;
034    import com.liferay.portal.kernel.portletdisplaytemplate.PortletDisplayTemplateHandler;
035    import com.liferay.portal.kernel.portletdisplaytemplate.PortletDisplayTemplateHandlerRegistryUtil;
036    import com.liferay.portal.kernel.scheduler.SchedulerEngineHelperUtil;
037    import com.liferay.portal.kernel.scheduler.SchedulerEntry;
038    import com.liferay.portal.kernel.scheduler.SchedulerException;
039    import com.liferay.portal.kernel.scheduler.StorageType;
040    import com.liferay.portal.kernel.search.Indexer;
041    import com.liferay.portal.kernel.search.IndexerRegistryUtil;
042    import com.liferay.portal.kernel.search.OpenSearch;
043    import com.liferay.portal.kernel.servlet.ServletContextPool;
044    import com.liferay.portal.kernel.servlet.URLEncoder;
045    import com.liferay.portal.kernel.trash.TrashHandler;
046    import com.liferay.portal.kernel.trash.TrashHandlerRegistryUtil;
047    import com.liferay.portal.kernel.util.GetterUtil;
048    import com.liferay.portal.kernel.util.HttpUtil;
049    import com.liferay.portal.kernel.util.LocaleUtil;
050    import com.liferay.portal.kernel.util.PrefsPropsUtil;
051    import com.liferay.portal.kernel.util.PropsKeys;
052    import com.liferay.portal.kernel.util.PropsUtil;
053    import com.liferay.portal.kernel.util.ProxyFactory;
054    import com.liferay.portal.kernel.util.SetUtil;
055    import com.liferay.portal.kernel.util.StringBundler;
056    import com.liferay.portal.kernel.util.StringPool;
057    import com.liferay.portal.kernel.util.StringUtil;
058    import com.liferay.portal.kernel.util.Validator;
059    import com.liferay.portal.kernel.webdav.WebDAVStorage;
060    import com.liferay.portal.kernel.webdav.WebDAVUtil;
061    import com.liferay.portal.kernel.workflow.WorkflowHandler;
062    import com.liferay.portal.kernel.workflow.WorkflowHandlerRegistryUtil;
063    import com.liferay.portal.kernel.xml.Document;
064    import com.liferay.portal.kernel.xml.Element;
065    import com.liferay.portal.kernel.xml.SAXReaderUtil;
066    import com.liferay.portal.kernel.xmlrpc.Method;
067    import com.liferay.portal.language.LanguageResources;
068    import com.liferay.portal.language.LiferayResourceBundle;
069    import com.liferay.portal.model.Portlet;
070    import com.liferay.portal.model.PortletApp;
071    import com.liferay.portal.poller.PollerProcessorUtil;
072    import com.liferay.portal.pop.POPServerUtil;
073    import com.liferay.portal.security.pacl.PACLClassLoaderUtil;
074    import com.liferay.portal.security.permission.PermissionPropagator;
075    import com.liferay.portal.service.PortletLocalServiceUtil;
076    import com.liferay.portal.util.PortalUtil;
077    import com.liferay.portal.util.PropsValues;
078    import com.liferay.portal.xmlrpc.XmlRpcServlet;
079    import com.liferay.portlet.asset.AssetRendererFactoryRegistryUtil;
080    import com.liferay.portlet.asset.model.AssetRendererFactory;
081    import com.liferay.portlet.expando.model.CustomAttributesDisplay;
082    import com.liferay.portlet.social.model.SocialActivityInterpreter;
083    import com.liferay.portlet.social.model.SocialRequestInterpreter;
084    import com.liferay.portlet.social.model.impl.SocialActivityInterpreterImpl;
085    import com.liferay.portlet.social.model.impl.SocialRequestInterpreterImpl;
086    import com.liferay.portlet.social.service.SocialActivityInterpreterLocalServiceUtil;
087    import com.liferay.portlet.social.service.SocialRequestInterpreterLocalServiceUtil;
088    import com.liferay.util.portlet.PortletProps;
089    
090    import java.io.InputStream;
091    
092    import java.util.ArrayList;
093    import java.util.HashMap;
094    import java.util.List;
095    import java.util.Locale;
096    import java.util.Map;
097    import java.util.ResourceBundle;
098    import java.util.Set;
099    
100    import javax.portlet.PreferencesValidator;
101    
102    import javax.servlet.ServletContext;
103    
104    /**
105     * @author Brian Wing Shun Chan
106     * @author Brian Myunghun Kim
107     * @author Ivica Cardic
108     * @author Raymond Augé
109     */
110    public class PortletBagFactory {
111    
112            public PortletBag create(Portlet portlet) throws Exception {
113                    PortletApp portletApp = portlet.getPortletApp();
114    
115                    if (!portletApp.isWARFile() && _warFile) {
116                            String contextPath = PortalUtil.getPathContext();
117    
118                            _servletContext = ServletContextPool.get(contextPath);
119    
120                            _classLoader = PACLClassLoaderUtil.getPortalClassLoader();
121                    }
122    
123                    Class<?> portletClass = null;
124    
125                    try {
126                            portletClass = _classLoader.loadClass(portlet.getPortletClass());
127                    }
128                    catch (Throwable t) {
129                            _log.error(t, t);
130    
131                            PortletLocalServiceUtil.destroyPortlet(portlet);
132    
133                            return null;
134                    }
135    
136                    javax.portlet.Portlet portletInstance =
137                            (javax.portlet.Portlet)portletClass.newInstance();
138    
139                    ConfigurationAction configurationActionInstance =
140                            newConfigurationAction(portlet);
141    
142                    List<Indexer> indexerInstances = newIndexers(portlet);
143    
144                    OpenSearch openSearchInstance = newOpenSearch(portlet);
145    
146                    FriendlyURLMapper friendlyURLMapperInstance = newFriendlyURLMapper(
147                            portlet);
148    
149                    URLEncoder urlEncoderInstance = newURLEncoder(portlet);
150    
151                    PortletDataHandler portletDataHandlerInstance = newPortletDataHandler(
152                            portlet);
153    
154                    List<StagedModelDataHandler<?>> stagedModelDataHandlerInstances =
155                            newStagedModelDataHandler(portlet);
156    
157                    PortletDisplayTemplateHandler portletDisplayTemplateHandlerInstance =
158                            newPortletDisplayTemplateHandler(portlet);
159    
160                    if (portletDisplayTemplateHandlerInstance != null) {
161                            PortletDisplayTemplateHandlerRegistryUtil.register(
162                                    portletDisplayTemplateHandlerInstance);
163                    }
164    
165                    PortletLayoutListener portletLayoutListenerInstance =
166                            newPortletLayoutListener(portlet);
167    
168                    PollerProcessor pollerProcessorInstance = newPollerProcessor(portlet);
169    
170                    MessageListener popMessageListenerInstance = newPOPMessageListener(
171                            portlet);
172    
173                    SocialActivityInterpreter socialActivityInterpreterInstance =
174                            initSocialActivityInterpreterInstance(portlet);
175    
176                    SocialRequestInterpreter socialRequestInterpreterInstance = null;
177    
178                    if (Validator.isNotNull(portlet.getSocialRequestInterpreterClass())) {
179                            socialRequestInterpreterInstance =
180                                    (SocialRequestInterpreter)newInstance(
181                                            SocialRequestInterpreter.class,
182                                            portlet.getSocialRequestInterpreterClass());
183    
184                            socialRequestInterpreterInstance = new SocialRequestInterpreterImpl(
185                                    portlet.getPortletId(), socialRequestInterpreterInstance);
186    
187                            SocialRequestInterpreterLocalServiceUtil.addRequestInterpreter(
188                                    socialRequestInterpreterInstance);
189                    }
190    
191                    WebDAVStorage webDAVStorageInstance = null;
192    
193                    if (Validator.isNotNull(portlet.getWebDAVStorageClass())) {
194                            webDAVStorageInstance = (WebDAVStorage)newInstance(
195                                    WebDAVStorage.class, portlet.getWebDAVStorageClass());
196    
197                            webDAVStorageInstance.setToken(portlet.getWebDAVStorageToken());
198    
199                            WebDAVUtil.addStorage(webDAVStorageInstance);
200                    }
201    
202                    Method xmlRpcMethodInstance = null;
203    
204                    if (Validator.isNotNull(portlet.getXmlRpcMethodClass())) {
205                            xmlRpcMethodInstance = (Method)newInstance(
206                                    Method.class, portlet.getXmlRpcMethodClass());
207    
208                            XmlRpcServlet.registerMethod(xmlRpcMethodInstance);
209                    }
210    
211                    ControlPanelEntry controlPanelEntryInstance = null;
212    
213                    if (Validator.isNotNull(portlet.getControlPanelEntryClass())) {
214                            controlPanelEntryInstance = (ControlPanelEntry)newInstance(
215                                    ControlPanelEntry.class, portlet.getControlPanelEntryClass());
216                    }
217    
218                    List<AssetRendererFactory> assetRendererFactoryInstances =
219                            newAssetRendererFactoryInstances(portlet);
220    
221                    List<AtomCollectionAdapter<?>> atomCollectionAdapterInstances =
222                            newAtomCollectionAdapterInstances(portlet);
223    
224                    List<CustomAttributesDisplay> customAttributesDisplayInstances =
225                            new ArrayList<CustomAttributesDisplay>();
226    
227                    for (String customAttributesDisplayClass :
228                                    portlet.getCustomAttributesDisplayClasses()) {
229    
230                            CustomAttributesDisplay customAttributesDisplayInstance =
231                                    (CustomAttributesDisplay)newInstance(
232                                            CustomAttributesDisplay.class,
233                                            customAttributesDisplayClass);
234    
235                            customAttributesDisplayInstance.setClassNameId(
236                                    PortalUtil.getClassNameId(
237                                            customAttributesDisplayInstance.getClassName()));
238                            customAttributesDisplayInstance.setPortletId(
239                                    portlet.getPortletId());
240    
241                            customAttributesDisplayInstances.add(
242                                    customAttributesDisplayInstance);
243                    }
244    
245                    PermissionPropagator permissionPropagatorInstance =
246                            newPermissionPropagator(portlet);
247    
248                    List<TrashHandler> trashHandlerInstances =
249                            new ArrayList<TrashHandler>();
250    
251                    for (String trashHandlerClass : portlet.getTrashHandlerClasses()) {
252                            TrashHandler trashHandlerInstance = (TrashHandler)newInstance(
253                                    TrashHandler.class, trashHandlerClass);
254    
255                            trashHandlerInstances.add(trashHandlerInstance);
256    
257                            TrashHandlerRegistryUtil.register(trashHandlerInstance);
258                    }
259    
260                    List<WorkflowHandler> workflowHandlerInstances =
261                            new ArrayList<WorkflowHandler>();
262    
263                    for (String workflowHandlerClass :
264                                    portlet.getWorkflowHandlerClasses()) {
265    
266                            WorkflowHandler workflowHandlerInstance =
267                                    (WorkflowHandler)newInstance(
268                                            WorkflowHandler.class, workflowHandlerClass);
269    
270                            workflowHandlerInstances.add(workflowHandlerInstance);
271    
272                            WorkflowHandlerRegistryUtil.register(workflowHandlerInstance);
273                    }
274    
275                    PreferencesValidator preferencesValidatorInstance = null;
276    
277                    if (Validator.isNotNull(portlet.getPreferencesValidator())) {
278                            preferencesValidatorInstance = (PreferencesValidator)newInstance(
279                                    PreferencesValidator.class, portlet.getPreferencesValidator());
280    
281                            try {
282                                    if (PropsValues.PREFERENCE_VALIDATE_ON_STARTUP) {
283                                            preferencesValidatorInstance.validate(
284                                                    PortletPreferencesFactoryUtil.fromDefaultXML(
285                                                            portlet.getDefaultPreferences()));
286                                    }
287                            }
288                            catch (Exception e) {
289                                    _log.warn(
290                                            "Portlet with the name " + portlet.getPortletId() +
291                                                    " does not have valid default preferences");
292                            }
293                    }
294    
295                    Map<String, ResourceBundle> resourceBundles = null;
296    
297                    String resourceBundle = portlet.getResourceBundle();
298    
299                    if (Validator.isNotNull(resourceBundle) &&
300                            !resourceBundle.equals(StrutsResourceBundle.class.getName())) {
301    
302                            resourceBundles = new HashMap<String, ResourceBundle>();
303    
304                            initResourceBundle(resourceBundles, portlet, null);
305                            initResourceBundle(
306                                    resourceBundles, portlet, LocaleUtil.getDefault());
307    
308                            Set<String> supportedLocales = portlet.getSupportedLocales();
309    
310                            if (supportedLocales.isEmpty()) {
311                                    supportedLocales = SetUtil.fromArray(PropsValues.LOCALES);
312                            }
313    
314                            for (String supportedLocale : supportedLocales) {
315                                    Locale locale = LocaleUtil.fromLanguageId(supportedLocale);
316    
317                                    initResourceBundle(resourceBundles, portlet, locale);
318                            }
319                    }
320    
321                    PortletBag portletBag = new PortletBagImpl(
322                            portlet.getPortletId(), _servletContext, portletInstance,
323                            configurationActionInstance, indexerInstances, openSearchInstance,
324                            friendlyURLMapperInstance, urlEncoderInstance,
325                            portletDataHandlerInstance, stagedModelDataHandlerInstances,
326                            portletDisplayTemplateHandlerInstance,
327                            portletLayoutListenerInstance, pollerProcessorInstance,
328                            popMessageListenerInstance, socialActivityInterpreterInstance,
329                            socialRequestInterpreterInstance, webDAVStorageInstance,
330                            xmlRpcMethodInstance, controlPanelEntryInstance,
331                            assetRendererFactoryInstances, atomCollectionAdapterInstances,
332                            customAttributesDisplayInstances, permissionPropagatorInstance,
333                            trashHandlerInstances, workflowHandlerInstances,
334                            preferencesValidatorInstance, resourceBundles);
335    
336                    PortletBagPool.put(portlet.getRootPortletId(), portletBag);
337    
338                    initSchedulers(portlet);
339    
340                    try {
341                            PortletInstanceFactoryUtil.create(portlet, _servletContext);
342                    }
343                    catch (Exception e) {
344                            _log.error(e, e);
345                    }
346    
347                    return portletBag;
348            }
349    
350            public void setClassLoader(ClassLoader classLoader) {
351                    _classLoader = classLoader;
352            }
353    
354            public void setServletContext(ServletContext servletContext) {
355                    _servletContext = servletContext;
356            }
357    
358            public void setWARFile(boolean warFile) {
359                    _warFile = warFile;
360            }
361    
362            protected String getContent(String fileName) throws Exception {
363                    String queryString = HttpUtil.getQueryString(fileName);
364    
365                    if (Validator.isNull(queryString)) {
366                            return StringUtil.read(_classLoader, fileName);
367                    }
368    
369                    int pos = fileName.indexOf(StringPool.QUESTION);
370    
371                    String xml = StringUtil.read(_classLoader, fileName.substring(0, pos));
372    
373                    Map<String, String[]> parameterMap = HttpUtil.getParameterMap(
374                            queryString);
375    
376                    if (parameterMap == null) {
377                            return xml;
378                    }
379    
380                    for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
381                            String name = entry.getKey();
382                            String[] values = entry.getValue();
383    
384                            if (values.length == 0) {
385                                    continue;
386                            }
387    
388                            String value = values[0];
389    
390                            xml = StringUtil.replace(xml, "@" + name + "@", value);
391                    }
392    
393                    return xml;
394            }
395    
396            protected String getPluginPropertyValue(String propertyKey)
397                    throws Exception {
398    
399                    Class<?> clazz = _classLoader.loadClass(PortletProps.class.getName());
400    
401                    java.lang.reflect.Method method = clazz.getMethod("get", String.class);
402    
403                    return (String)method.invoke(null, propertyKey);
404            }
405    
406            protected InputStream getResourceBundleInputStream(
407                    String resourceBundleName, Locale locale) {
408    
409                    resourceBundleName = resourceBundleName.replace(
410                            StringPool.PERIOD, StringPool.SLASH);
411    
412                    Locale newLocale = locale;
413    
414                    InputStream inputStream = null;
415    
416                    while (inputStream == null) {
417                            locale = newLocale;
418    
419                            StringBundler sb = new StringBundler(4);
420    
421                            sb.append(resourceBundleName);
422    
423                            if (locale != null) {
424                                    String localeName = locale.toString();
425    
426                                    if (localeName.length() > 0) {
427                                            sb.append(StringPool.UNDERLINE);
428                                            sb.append(localeName);
429                                    }
430                            }
431    
432                            if (!resourceBundleName.endsWith(".properties")) {
433                                    sb.append(".properties");
434                            }
435    
436                            String localizedResourceBundleName = sb.toString();
437    
438                            if (_log.isInfoEnabled()) {
439                                    _log.info("Attempting to load " + localizedResourceBundleName);
440                            }
441    
442                            inputStream = _classLoader.getResourceAsStream(
443                                    localizedResourceBundleName);
444    
445                            if (locale == null) {
446                                    break;
447                            }
448    
449                            newLocale = LanguageResources.getSuperLocale(locale);
450    
451                            if (newLocale == null) {
452                                    break;
453                            }
454    
455                            if (newLocale.equals(locale)) {
456                                    break;
457                            }
458                    }
459    
460                    return inputStream;
461            }
462    
463            protected void initResourceBundle(
464                    Map<String, ResourceBundle> resourceBundles, Portlet portlet,
465                    Locale locale) {
466    
467                    try {
468                            InputStream inputStream = getResourceBundleInputStream(
469                                    portlet.getResourceBundle(), locale);
470    
471                            if (inputStream != null) {
472                                    ResourceBundle parentResourceBundle = null;
473    
474                                    if (locale != null) {
475                                            parentResourceBundle = resourceBundles.get(null);
476                                    }
477    
478                                    ResourceBundle resourceBundle = new LiferayResourceBundle(
479                                            parentResourceBundle, inputStream, StringPool.UTF8);
480    
481                                    String languageId = null;
482    
483                                    if (locale != null) {
484                                            languageId = LocaleUtil.toLanguageId(locale);
485                                    }
486    
487                                    resourceBundles.put(languageId, resourceBundle);
488                            }
489                    }
490                    catch (Exception e) {
491                            _log.warn(e.getMessage());
492                    }
493            }
494    
495            protected void initScheduler(
496                            SchedulerEntry schedulerEntry, String portletId)
497                    throws Exception {
498    
499                    String propertyKey = schedulerEntry.getPropertyKey();
500    
501                    if (Validator.isNotNull(propertyKey)) {
502                            String triggerValue = null;
503    
504                            if (_warFile) {
505                                    triggerValue = getPluginPropertyValue(propertyKey);
506                            }
507                            else {
508                                    triggerValue = PrefsPropsUtil.getString(propertyKey);
509                            }
510    
511                            if (_log.isDebugEnabled()) {
512                                    _log.debug(
513                                            "Scheduler property key " + propertyKey +
514                                                    " has trigger value " + triggerValue);
515                            }
516    
517                            if (Validator.isNull(triggerValue)) {
518                                    throw new SchedulerException(
519                                            "Property key " + propertyKey + " requires a value");
520                            }
521    
522                            schedulerEntry.setTriggerValue(triggerValue);
523                    }
524    
525                    if (_classLoader == PACLClassLoaderUtil.getPortalClassLoader()) {
526                            portletId = null;
527                    }
528    
529                    SchedulerEngineHelperUtil.schedule(
530                            schedulerEntry, StorageType.MEMORY_CLUSTERED, portletId, 0);
531            }
532    
533            protected void initSchedulers(Portlet portlet) throws Exception {
534                    if (!PropsValues.SCHEDULER_ENABLED) {
535                            return;
536                    }
537    
538                    List<SchedulerEntry> schedulerEntries = portlet.getSchedulerEntries();
539    
540                    if ((schedulerEntries == null) || schedulerEntries.isEmpty()) {
541                            return;
542                    }
543    
544                    for (SchedulerEntry schedulerEntry : schedulerEntries) {
545                            initScheduler(schedulerEntry, portlet.getPortletId());
546                    }
547            }
548    
549            protected SocialActivityInterpreter initSocialActivityInterpreterInstance(
550                            Portlet portlet)
551                    throws Exception {
552    
553                    if (Validator.isNull(portlet.getSocialActivityInterpreterClass())) {
554                            return null;
555                    }
556    
557                    SocialActivityInterpreter socialActivityInterpreterInstance =
558                            (SocialActivityInterpreter)newInstance(
559                                    SocialActivityInterpreter.class,
560                                    portlet.getSocialActivityInterpreterClass());
561    
562                    socialActivityInterpreterInstance = new SocialActivityInterpreterImpl(
563                            portlet.getPortletId(), socialActivityInterpreterInstance);
564    
565                    SocialActivityInterpreterLocalServiceUtil.addActivityInterpreter(
566                            socialActivityInterpreterInstance);
567    
568                    return socialActivityInterpreterInstance;
569            }
570    
571            protected AssetRendererFactory newAssetRendererFactoryInstance(
572                            Portlet portlet, String assetRendererFactoryClass)
573                    throws Exception {
574    
575                    AssetRendererFactory assetRendererFactoryInstance =
576                            (AssetRendererFactory)newInstance(
577                                    AssetRendererFactory.class, assetRendererFactoryClass);
578    
579                    assetRendererFactoryInstance.setClassName(
580                            assetRendererFactoryInstance.getClassName());
581                    assetRendererFactoryInstance.setPortletId(portlet.getPortletId());
582    
583                    AssetRendererFactoryRegistryUtil.register(assetRendererFactoryInstance);
584    
585                    return assetRendererFactoryInstance;
586            }
587    
588            protected List<AssetRendererFactory> newAssetRendererFactoryInstances(
589                            Portlet portlet)
590                    throws Exception {
591    
592                    List<AssetRendererFactory> assetRendererFactoryInstances =
593                            new ArrayList<AssetRendererFactory>();
594    
595                    for (String assetRendererFactoryClass :
596                                    portlet.getAssetRendererFactoryClasses()) {
597    
598                            String assetRendererEnabledPropertyKey =
599                                    PropsKeys.ASSET_RENDERER_ENABLED + assetRendererFactoryClass;
600    
601                            String assetRendererEnabledPropertyValue = null;
602    
603                            if (_warFile) {
604                                    assetRendererEnabledPropertyValue = getPluginPropertyValue(
605                                            assetRendererEnabledPropertyKey);
606                            }
607                            else {
608                                    assetRendererEnabledPropertyValue = PropsUtil.get(
609                                            assetRendererEnabledPropertyKey);
610                            }
611    
612                            boolean assetRendererEnabledValue = GetterUtil.getBoolean(
613                                    assetRendererEnabledPropertyValue, true);
614    
615                            if (assetRendererEnabledValue) {
616                                    AssetRendererFactory assetRendererFactoryInstance =
617                                            newAssetRendererFactoryInstance(
618                                                    portlet, assetRendererFactoryClass);
619    
620                                    assetRendererFactoryInstances.add(assetRendererFactoryInstance);
621                            }
622                    }
623    
624                    return assetRendererFactoryInstances;
625            }
626    
627            protected List<AtomCollectionAdapter<?>> newAtomCollectionAdapterInstances(
628                            Portlet portlet)
629                    throws Exception {
630    
631                    List<AtomCollectionAdapter<?>> atomCollectionAdapterInstances =
632                            new ArrayList<AtomCollectionAdapter<?>>();
633    
634                    for (String atomCollectionAdapterClass :
635                                    portlet.getAtomCollectionAdapterClasses()) {
636    
637                            AtomCollectionAdapter<?> atomCollectionAdapterInstance =
638                                    (AtomCollectionAdapter<?>)newInstance(
639                                            AtomCollectionAdapter.class, atomCollectionAdapterClass);
640    
641                            AtomCollectionAdapterRegistryUtil.register(
642                                    atomCollectionAdapterInstance);
643    
644                            atomCollectionAdapterInstances.add(atomCollectionAdapterInstance);
645                    }
646    
647                    return atomCollectionAdapterInstances;
648            }
649    
650            protected ConfigurationAction newConfigurationAction(Portlet portlet)
651                    throws Exception {
652    
653                    if (Validator.isNull(portlet.getConfigurationActionClass())) {
654                            return null;
655                    }
656    
657                    return (ConfigurationAction)newInstance(
658                            ConfigurationAction.class, portlet.getConfigurationActionClass());
659            }
660    
661            protected FriendlyURLMapper newFriendlyURLMapper(Portlet portlet)
662                    throws Exception {
663    
664                    if (Validator.isNull(portlet.getFriendlyURLMapperClass())) {
665                            return null;
666                    }
667    
668                    FriendlyURLMapper friendlyURLMapper = (FriendlyURLMapper)newInstance(
669                            FriendlyURLMapper.class, portlet.getFriendlyURLMapperClass());
670    
671                    friendlyURLMapper.setMapping(portlet.getFriendlyURLMapping());
672                    friendlyURLMapper.setPortletId(portlet.getPortletId());
673                    friendlyURLMapper.setPortletInstanceable(portlet.isInstanceable());
674    
675                    Router router = newFriendlyURLRouter(portlet);
676    
677                    friendlyURLMapper.setRouter(router);
678    
679                    return friendlyURLMapper;
680            }
681    
682            protected Router newFriendlyURLRouter(Portlet portlet) throws Exception {
683                    if (Validator.isNull(portlet.getFriendlyURLRoutes())) {
684                            return null;
685                    }
686    
687                    Router router = new RouterImpl();
688    
689                    String xml = getContent(portlet.getFriendlyURLRoutes());
690    
691                    Document document = SAXReaderUtil.read(xml, true);
692    
693                    Element rootElement = document.getRootElement();
694    
695                    for (Element routeElement : rootElement.elements("route")) {
696                            String pattern = routeElement.elementText("pattern");
697    
698                            Route route = router.addRoute(pattern);
699    
700                            for (Element generatedParameterElement :
701                                            routeElement.elements("generated-parameter")) {
702    
703                                    String name = generatedParameterElement.attributeValue("name");
704                                    String value = generatedParameterElement.getText();
705    
706                                    route.addGeneratedParameter(name, value);
707                            }
708    
709                            for (Element ignoredParameterElement :
710                                            routeElement.elements("ignored-parameter")) {
711    
712                                    String name = ignoredParameterElement.attributeValue("name");
713    
714                                    route.addIgnoredParameter(name);
715                            }
716    
717                            for (Element implicitParameterElement :
718                                            routeElement.elements("implicit-parameter")) {
719    
720                                    String name = implicitParameterElement.attributeValue("name");
721                                    String value = implicitParameterElement.getText();
722    
723                                    route.addImplicitParameter(name, value);
724                            }
725    
726                            for (Element overriddenParameterElement :
727                                            routeElement.elements("overridden-parameter")) {
728    
729                                    String name = overriddenParameterElement.attributeValue("name");
730                                    String value = overriddenParameterElement.getText();
731    
732                                    route.addOverriddenParameter(name, value);
733                            }
734                    }
735    
736                    return router;
737            }
738    
739            protected List<Indexer> newIndexers(Portlet portlet) throws Exception {
740                    List<Indexer> indexerInstances = new ArrayList<Indexer>();
741    
742                    List<String> indexerClasses = portlet.getIndexerClasses();
743    
744                    for (String indexerClass : indexerClasses) {
745                            Indexer indexerInstance = (Indexer)newInstance(
746                                    Indexer.class, indexerClass);
747    
748                            IndexerRegistryUtil.register(indexerInstance);
749    
750                            indexerInstances.add(indexerInstance);
751                    }
752    
753                    return indexerInstances;
754            }
755    
756            protected Object newInstance(Class<?> interfaceClass, String implClassName)
757                    throws Exception {
758    
759                    return newInstance(new Class[] {interfaceClass}, implClassName);
760            }
761    
762            protected Object newInstance(
763                            Class<?>[] interfaceClasses, String implClassName)
764                    throws Exception {
765    
766                    if (_warFile) {
767                            return ProxyFactory.newInstance(
768                                    _classLoader, interfaceClasses, implClassName);
769                    }
770                    else {
771                            Class<?> clazz = _classLoader.loadClass(implClassName);
772    
773                            return clazz.newInstance();
774                    }
775            }
776    
777            protected OpenSearch newOpenSearch(Portlet portlet) throws Exception {
778                    if (Validator.isNull(portlet.getOpenSearchClass())) {
779                            return null;
780                    }
781    
782                    return (OpenSearch)newInstance(
783                            OpenSearch.class, portlet.getOpenSearchClass());
784            }
785    
786            protected PermissionPropagator newPermissionPropagator(Portlet portlet)
787                    throws Exception {
788    
789                    if (Validator.isNull(portlet.getPermissionPropagatorClass())) {
790                            return null;
791                    }
792    
793                    return (PermissionPropagator)newInstance(
794                            PermissionPropagator.class, portlet.getPermissionPropagatorClass());
795            }
796    
797            protected PollerProcessor newPollerProcessor(Portlet portlet)
798                    throws Exception {
799    
800                    if (Validator.isNull(portlet.getPollerProcessorClass())) {
801                            return null;
802                    }
803    
804                    PollerProcessor pollerProcessorInstance = (PollerProcessor)newInstance(
805                            PollerProcessor.class, portlet.getPollerProcessorClass());
806    
807                    PollerProcessorUtil.addPollerProcessor(
808                            portlet.getPortletId(),
809                            new ShardPollerProcessorWrapper(pollerProcessorInstance));
810    
811                    return pollerProcessorInstance;
812            }
813    
814            protected MessageListener newPOPMessageListener(Portlet portlet)
815                    throws Exception {
816    
817                    if (Validator.isNull(portlet.getPopMessageListenerClass())) {
818                            return null;
819                    }
820    
821                    MessageListener popMessageListenerInstance =
822                            (MessageListener)newInstance(
823                                    MessageListener.class, portlet.getPopMessageListenerClass());
824    
825                    POPServerUtil.addListener(popMessageListenerInstance);
826    
827                    return popMessageListenerInstance;
828            }
829    
830            protected PortletDataHandler newPortletDataHandler(Portlet portlet)
831                    throws Exception {
832    
833                    if (Validator.isNull(portlet.getPortletDataHandlerClass())) {
834                            return null;
835                    }
836    
837                    return (PortletDataHandler)newInstance(
838                            PortletDataHandler.class, portlet.getPortletDataHandlerClass());
839            }
840    
841            protected PortletDisplayTemplateHandler newPortletDisplayTemplateHandler(
842                            Portlet portlet)
843                    throws Exception {
844    
845                    if (Validator.isNull(portlet.getPortletDisplayTemplateHandlerClass())) {
846                            return null;
847                    }
848    
849                    return (PortletDisplayTemplateHandler)newInstance(
850                            PortletDisplayTemplateHandler.class,
851                            portlet.getPortletDisplayTemplateHandlerClass());
852            }
853    
854            protected PortletLayoutListener newPortletLayoutListener(Portlet portlet)
855                    throws Exception {
856    
857                    if (Validator.isNull(portlet.getPortletLayoutListenerClass())) {
858                            return null;
859                    }
860    
861                    return (PortletLayoutListener)newInstance(
862                            PortletLayoutListener.class,
863                            portlet.getPortletLayoutListenerClass());
864            }
865    
866            protected List<StagedModelDataHandler<?>> newStagedModelDataHandler(
867                            Portlet portlet)
868                    throws Exception {
869    
870                    List<StagedModelDataHandler<?>> stagedModelDataHandlerInstances =
871                            new ArrayList<StagedModelDataHandler<?>>();
872    
873                    List<String> stagedModelDataHandlerClasses =
874                            portlet.getStagedModelDataHandlerClasses();
875    
876                    for (String stagedModelDataHandlerClass :
877                                    stagedModelDataHandlerClasses) {
878    
879                            StagedModelDataHandler<?> stagedModelDataHandler =
880                                    (StagedModelDataHandler<?>)newInstance(
881                                            StagedModelDataHandler.class, stagedModelDataHandlerClass);
882    
883                            stagedModelDataHandlerInstances.add(stagedModelDataHandler);
884    
885                            StagedModelDataHandlerRegistryUtil.register(stagedModelDataHandler);
886                    }
887    
888                    return stagedModelDataHandlerInstances;
889            }
890    
891            protected URLEncoder newURLEncoder(Portlet portlet) throws Exception {
892                    if (Validator.isNull(portlet.getURLEncoderClass())) {
893                            return null;
894                    }
895    
896                    return (URLEncoder)newInstance(
897                            URLEncoder.class, portlet.getURLEncoderClass());
898            }
899    
900            private static Log _log = LogFactoryUtil.getLog(PortletBagFactory.class);
901    
902            private ClassLoader _classLoader;
903            private ServletContext _servletContext;
904            private boolean _warFile;
905    
906    }