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