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