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