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