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.SAXReaderUtil;
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                                    _log.warn(
283                                            "Portlet with the name " + portlet.getPortletId() +
284                                                    " does not have valid default preferences");
285                            }
286                    }
287    
288                    Map<String, ResourceBundle> resourceBundles = null;
289    
290                    String resourceBundle = portlet.getResourceBundle();
291    
292                    if (Validator.isNotNull(resourceBundle) &&
293                            !resourceBundle.equals(StrutsResourceBundle.class.getName())) {
294    
295                            resourceBundles = new HashMap<String, ResourceBundle>();
296    
297                            initResourceBundle(resourceBundles, portlet, null);
298                            initResourceBundle(
299                                    resourceBundles, portlet, LocaleUtil.getDefault());
300    
301                            Set<String> supportedLanguageIds = portlet.getSupportedLocales();
302    
303                            if (supportedLanguageIds.isEmpty()) {
304                                    supportedLanguageIds = SetUtil.fromArray(PropsValues.LOCALES);
305                            }
306    
307                            for (String supportedLanguageId : supportedLanguageIds) {
308                                    Locale locale = LocaleUtil.fromLanguageId(supportedLanguageId);
309    
310                                    initResourceBundle(resourceBundles, portlet, locale);
311                            }
312                    }
313    
314                    PortletBag portletBag = new PortletBagImpl(
315                            portlet.getPortletId(), _servletContext, portletInstance,
316                            configurationActionInstance, indexerInstances, openSearchInstance,
317                            friendlyURLMapperInstance, urlEncoderInstance,
318                            portletDataHandlerInstance, stagedModelDataHandlerInstances,
319                            templateHandlerInstance, portletLayoutListenerInstance,
320                            pollerProcessorInstance, popMessageListenerInstance,
321                            socialActivityInterpreterInstances,
322                            socialRequestInterpreterInstance, userNotificationHandlerInstances,
323                            webDAVStorageInstance, xmlRpcMethodInstance,
324                            controlPanelEntryInstance, assetRendererFactoryInstances,
325                            atomCollectionAdapterInstances, customAttributesDisplayInstances,
326                            permissionPropagatorInstance, trashHandlerInstances,
327                            workflowHandlerInstances, preferencesValidatorInstance,
328                            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 javax.portlet.Portlet getPortletInstance(Portlet portlet)
401                    throws IllegalAccessException, InstantiationException {
402    
403                    Class<?> portletClass = null;
404    
405                    try {
406                            portletClass = _classLoader.loadClass(portlet.getPortletClass());
407                    }
408                    catch (Throwable t) {
409                            _log.error(t, t);
410    
411                            PortletLocalServiceUtil.destroyPortlet(portlet);
412    
413                            return null;
414                    }
415    
416                    return (javax.portlet.Portlet)portletClass.newInstance();
417            }
418    
419            protected InputStream getResourceBundleInputStream(
420                    String resourceBundleName, Locale locale) {
421    
422                    resourceBundleName = resourceBundleName.replace(
423                            StringPool.PERIOD, StringPool.SLASH);
424    
425                    Locale newLocale = locale;
426    
427                    InputStream inputStream = null;
428    
429                    while (inputStream == null) {
430                            locale = newLocale;
431    
432                            StringBundler sb = new StringBundler(4);
433    
434                            sb.append(resourceBundleName);
435    
436                            if (locale != null) {
437                                    String localeName = locale.toString();
438    
439                                    if (localeName.length() > 0) {
440                                            sb.append(StringPool.UNDERLINE);
441                                            sb.append(localeName);
442                                    }
443                            }
444    
445                            if (!resourceBundleName.endsWith(".properties")) {
446                                    sb.append(".properties");
447                            }
448    
449                            String localizedResourceBundleName = sb.toString();
450    
451                            if (_log.isInfoEnabled()) {
452                                    _log.info("Attempting to load " + localizedResourceBundleName);
453                            }
454    
455                            inputStream = _classLoader.getResourceAsStream(
456                                    localizedResourceBundleName);
457    
458                            if (locale == null) {
459                                    break;
460                            }
461    
462                            newLocale = LanguageResources.getSuperLocale(locale);
463    
464                            if (newLocale == null) {
465                                    break;
466                            }
467    
468                            if (newLocale.equals(locale)) {
469                                    break;
470                            }
471                    }
472    
473                    return inputStream;
474            }
475    
476            protected void initResourceBundle(
477                    Map<String, ResourceBundle> resourceBundles, Portlet portlet,
478                    Locale locale) {
479    
480                    try {
481                            InputStream inputStream = getResourceBundleInputStream(
482                                    portlet.getResourceBundle(), locale);
483    
484                            if (inputStream != null) {
485                                    ResourceBundle parentResourceBundle = null;
486    
487                                    if (locale != null) {
488                                            parentResourceBundle = resourceBundles.get(null);
489                                    }
490    
491                                    ResourceBundle resourceBundle = new LiferayResourceBundle(
492                                            parentResourceBundle, inputStream, StringPool.UTF8);
493    
494                                    String languageId = null;
495    
496                                    if (locale != null) {
497                                            languageId = LocaleUtil.toLanguageId(locale);
498                                    }
499    
500                                    resourceBundles.put(languageId, resourceBundle);
501                            }
502                    }
503                    catch (Exception e) {
504                            _log.warn(e.getMessage());
505                    }
506            }
507    
508            protected void initScheduler(
509                            SchedulerEntry schedulerEntry, String portletId)
510                    throws Exception {
511    
512                    String propertyKey = schedulerEntry.getPropertyKey();
513    
514                    if (Validator.isNotNull(propertyKey)) {
515                            String triggerValue = null;
516    
517                            if (_warFile) {
518                                    triggerValue = getPluginPropertyValue(propertyKey);
519                            }
520                            else {
521                                    triggerValue = PrefsPropsUtil.getString(propertyKey);
522                            }
523    
524                            if (_log.isDebugEnabled()) {
525                                    _log.debug(
526                                            "Scheduler property key " + propertyKey +
527                                                    " has trigger value " + triggerValue);
528                            }
529    
530                            if (Validator.isNull(triggerValue)) {
531                                    throw new SchedulerException(
532                                            "Property key " + propertyKey + " requires a value");
533                            }
534    
535                            schedulerEntry.setTriggerValue(triggerValue);
536                    }
537    
538                    if (_classLoader == ClassLoaderUtil.getPortalClassLoader()) {
539                            portletId = null;
540                    }
541    
542                    SchedulerEngineHelperUtil.schedule(
543                            schedulerEntry, StorageType.MEMORY_CLUSTERED, portletId, 0);
544            }
545    
546            protected void initSchedulers(Portlet portlet) throws Exception {
547                    if (!PropsValues.SCHEDULER_ENABLED) {
548                            return;
549                    }
550    
551                    List<SchedulerEntry> schedulerEntries = portlet.getSchedulerEntries();
552    
553                    if ((schedulerEntries == null) || schedulerEntries.isEmpty()) {
554                            return;
555                    }
556    
557                    for (SchedulerEntry schedulerEntry : schedulerEntries) {
558                            initScheduler(schedulerEntry, portlet.getPortletId());
559                    }
560            }
561    
562            protected void initUserNotificationDefinition(Portlet portlet)
563                    throws Exception {
564    
565                    if (Validator.isNull(portlet.getUserNotificationDefinitions())) {
566                            return;
567                    }
568    
569                    String xml = getContent(portlet.getUserNotificationDefinitions());
570    
571                    xml = JavaFieldsParser.parse(_classLoader, xml);
572    
573                    Document document = SAXReaderUtil.read(xml);
574    
575                    Element rootElement = document.getRootElement();
576    
577                    for (Element definitionElement : rootElement.elements("definition")) {
578                            String modelName = definitionElement.elementText("model-name");
579    
580                            long classNameId = 0;
581    
582                            if (Validator.isNotNull(modelName)) {
583                                    classNameId = PortalUtil.getClassNameId(modelName);
584                            }
585    
586                            int notificationType = GetterUtil.getInteger(
587                                    definitionElement.elementText("notification-type"));
588    
589                            String description = GetterUtil.getString(
590                                    definitionElement.elementText("description"));
591    
592                            UserNotificationDefinition userNotificationDefinition =
593                                    new UserNotificationDefinition(
594                                            portlet.getPortletId(), classNameId, notificationType,
595                                            description);
596    
597                            for (Element deliveryTypeElement :
598                                            definitionElement.elements("delivery-type")) {
599    
600                                    String name = deliveryTypeElement.elementText("name");
601                                    int type = GetterUtil.getInteger(
602                                            deliveryTypeElement.elementText("type"));
603                                    boolean defaultValue = GetterUtil.getBoolean(
604                                            deliveryTypeElement.elementText("default"));
605                                    boolean modifiable = GetterUtil.getBoolean(
606                                            deliveryTypeElement.elementText("modifiable"));
607    
608                                    userNotificationDefinition.addUserNotificationDeliveryType(
609                                            new UserNotificationDeliveryType(
610                                                    name, type, defaultValue, modifiable));
611                            }
612    
613                            UserNotificationManagerUtil.addUserNotificationDefinition(
614                                    portlet.getPortletId(), userNotificationDefinition);
615                    }
616            }
617    
618            protected AssetRendererFactory newAssetRendererFactoryInstance(
619                            Portlet portlet, String assetRendererFactoryClass)
620                    throws Exception {
621    
622                    AssetRendererFactory assetRendererFactoryInstance =
623                            (AssetRendererFactory)newInstance(
624                                    AssetRendererFactory.class, assetRendererFactoryClass);
625    
626                    assetRendererFactoryInstance.setClassName(
627                            assetRendererFactoryInstance.getClassName());
628                    assetRendererFactoryInstance.setPortletId(portlet.getPortletId());
629    
630                    AssetRendererFactoryRegistryUtil.register(assetRendererFactoryInstance);
631    
632                    return assetRendererFactoryInstance;
633            }
634    
635            protected List<AssetRendererFactory> newAssetRendererFactoryInstances(
636                            Portlet portlet)
637                    throws Exception {
638    
639                    List<AssetRendererFactory> assetRendererFactoryInstances =
640                            new ArrayList<AssetRendererFactory>();
641    
642                    for (String assetRendererFactoryClass :
643                                    portlet.getAssetRendererFactoryClasses()) {
644    
645                            String assetRendererEnabledPropertyKey =
646                                    PropsKeys.ASSET_RENDERER_ENABLED + assetRendererFactoryClass;
647    
648                            String assetRendererEnabledPropertyValue = null;
649    
650                            if (_warFile) {
651                                    assetRendererEnabledPropertyValue = getPluginPropertyValue(
652                                            assetRendererEnabledPropertyKey);
653                            }
654                            else {
655                                    assetRendererEnabledPropertyValue = PropsUtil.get(
656                                            assetRendererEnabledPropertyKey);
657                            }
658    
659                            boolean assetRendererEnabledValue = GetterUtil.getBoolean(
660                                    assetRendererEnabledPropertyValue, true);
661    
662                            if (assetRendererEnabledValue) {
663                                    AssetRendererFactory assetRendererFactoryInstance =
664                                            newAssetRendererFactoryInstance(
665                                                    portlet, assetRendererFactoryClass);
666    
667                                    assetRendererFactoryInstances.add(assetRendererFactoryInstance);
668                            }
669                    }
670    
671                    return assetRendererFactoryInstances;
672            }
673    
674            protected List<AtomCollectionAdapter<?>> newAtomCollectionAdapterInstances(
675                            Portlet portlet)
676                    throws Exception {
677    
678                    List<AtomCollectionAdapter<?>> atomCollectionAdapterInstances =
679                            new ArrayList<AtomCollectionAdapter<?>>();
680    
681                    for (String atomCollectionAdapterClass :
682                                    portlet.getAtomCollectionAdapterClasses()) {
683    
684                            AtomCollectionAdapter<?> atomCollectionAdapterInstance =
685                                    (AtomCollectionAdapter<?>)newInstance(
686                                            AtomCollectionAdapter.class, atomCollectionAdapterClass);
687    
688                            AtomCollectionAdapterRegistryUtil.register(
689                                    atomCollectionAdapterInstance);
690    
691                            atomCollectionAdapterInstances.add(atomCollectionAdapterInstance);
692                    }
693    
694                    return atomCollectionAdapterInstances;
695            }
696    
697            protected ConfigurationAction newConfigurationAction(Portlet portlet)
698                    throws Exception {
699    
700                    if (Validator.isNull(portlet.getConfigurationActionClass())) {
701                            return null;
702                    }
703    
704                    return (ConfigurationAction)newInstance(
705                            ConfigurationAction.class, portlet.getConfigurationActionClass());
706            }
707    
708            protected DDMDisplay newDDMDisplay(Portlet portlet) throws Exception {
709                    if (Validator.isNull(portlet.getDDMDisplayClass())) {
710                            return null;
711                    }
712    
713                    return (DDMDisplay)newInstance(
714                            DDMDisplay.class, portlet.getDDMDisplayClass());
715            }
716    
717            protected FriendlyURLMapper newFriendlyURLMapper(Portlet portlet)
718                    throws Exception {
719    
720                    if (Validator.isNull(portlet.getFriendlyURLMapperClass())) {
721                            return null;
722                    }
723    
724                    FriendlyURLMapper friendlyURLMapper = (FriendlyURLMapper)newInstance(
725                            FriendlyURLMapper.class, portlet.getFriendlyURLMapperClass());
726    
727                    friendlyURLMapper.setMapping(portlet.getFriendlyURLMapping());
728                    friendlyURLMapper.setPortletId(portlet.getPortletId());
729                    friendlyURLMapper.setPortletInstanceable(portlet.isInstanceable());
730    
731                    Router router = newFriendlyURLRouter(portlet);
732    
733                    friendlyURLMapper.setRouter(router);
734    
735                    return friendlyURLMapper;
736            }
737    
738            protected Router newFriendlyURLRouter(Portlet portlet) throws Exception {
739                    if (Validator.isNull(portlet.getFriendlyURLRoutes())) {
740                            return null;
741                    }
742    
743                    Router router = new RouterImpl();
744    
745                    String xml = getContent(portlet.getFriendlyURLRoutes());
746    
747                    Document document = SAXReaderUtil.read(xml, true);
748    
749                    Element rootElement = document.getRootElement();
750    
751                    for (Element routeElement : rootElement.elements("route")) {
752                            String pattern = routeElement.elementText("pattern");
753    
754                            Route route = router.addRoute(pattern);
755    
756                            for (Element generatedParameterElement :
757                                            routeElement.elements("generated-parameter")) {
758    
759                                    String name = generatedParameterElement.attributeValue("name");
760                                    String value = generatedParameterElement.getText();
761    
762                                    route.addGeneratedParameter(name, value);
763                            }
764    
765                            for (Element ignoredParameterElement :
766                                            routeElement.elements("ignored-parameter")) {
767    
768                                    String name = ignoredParameterElement.attributeValue("name");
769    
770                                    route.addIgnoredParameter(name);
771                            }
772    
773                            for (Element implicitParameterElement :
774                                            routeElement.elements("implicit-parameter")) {
775    
776                                    String name = implicitParameterElement.attributeValue("name");
777                                    String value = implicitParameterElement.getText();
778    
779                                    route.addImplicitParameter(name, value);
780                            }
781    
782                            for (Element overriddenParameterElement :
783                                            routeElement.elements("overridden-parameter")) {
784    
785                                    String name = overriddenParameterElement.attributeValue("name");
786                                    String value = overriddenParameterElement.getText();
787    
788                                    route.addOverriddenParameter(name, value);
789                            }
790                    }
791    
792                    return router;
793            }
794    
795            protected List<Indexer> newIndexers(Portlet portlet) throws Exception {
796                    List<Indexer> indexerInstances = new ArrayList<Indexer>();
797    
798                    List<String> indexerClasses = portlet.getIndexerClasses();
799    
800                    for (String indexerClass : indexerClasses) {
801                            Indexer indexerInstance = (Indexer)newInstance(
802                                    Indexer.class, indexerClass);
803    
804                            IndexerRegistryUtil.register(indexerInstance);
805    
806                            indexerInstances.add(indexerInstance);
807                    }
808    
809                    return indexerInstances;
810            }
811    
812            protected Object newInstance(Class<?> interfaceClass, String implClassName)
813                    throws Exception {
814    
815                    return newInstance(new Class[] {interfaceClass}, implClassName);
816            }
817    
818            protected Object newInstance(
819                            Class<?>[] interfaceClasses, String implClassName)
820                    throws Exception {
821    
822                    if (_warFile) {
823                            return ProxyFactory.newInstance(
824                                    _classLoader, interfaceClasses, implClassName);
825                    }
826                    else {
827                            Class<?> clazz = _classLoader.loadClass(implClassName);
828    
829                            return clazz.newInstance();
830                    }
831            }
832    
833            protected OpenSearch newOpenSearch(Portlet portlet) throws Exception {
834                    if (Validator.isNull(portlet.getOpenSearchClass())) {
835                            return null;
836                    }
837    
838                    return (OpenSearch)newInstance(
839                            OpenSearch.class, portlet.getOpenSearchClass());
840            }
841    
842            protected PermissionPropagator newPermissionPropagator(Portlet portlet)
843                    throws Exception {
844    
845                    if (Validator.isNull(portlet.getPermissionPropagatorClass())) {
846                            return null;
847                    }
848    
849                    return (PermissionPropagator)newInstance(
850                            PermissionPropagator.class, portlet.getPermissionPropagatorClass());
851            }
852    
853            protected PollerProcessor newPollerProcessor(Portlet portlet)
854                    throws Exception {
855    
856                    if (Validator.isNull(portlet.getPollerProcessorClass())) {
857                            return null;
858                    }
859    
860                    PollerProcessor pollerProcessorInstance = (PollerProcessor)newInstance(
861                            PollerProcessor.class, portlet.getPollerProcessorClass());
862    
863                    PollerProcessorUtil.addPollerProcessor(
864                            portlet.getPortletId(),
865                            new ShardPollerProcessorWrapper(pollerProcessorInstance));
866    
867                    return pollerProcessorInstance;
868            }
869    
870            protected MessageListener newPOPMessageListener(Portlet portlet)
871                    throws Exception {
872    
873                    if (Validator.isNull(portlet.getPopMessageListenerClass())) {
874                            return null;
875                    }
876    
877                    MessageListener popMessageListenerInstance =
878                            (MessageListener)newInstance(
879                                    MessageListener.class, portlet.getPopMessageListenerClass());
880    
881                    POPServerUtil.addListener(popMessageListenerInstance);
882    
883                    return popMessageListenerInstance;
884            }
885    
886            protected PortletDataHandler newPortletDataHandler(Portlet portlet)
887                    throws Exception {
888    
889                    if (Validator.isNull(portlet.getPortletDataHandlerClass())) {
890                            return null;
891                    }
892    
893                    PortletDataHandler portletDataHandlerInstance =
894                            (PortletDataHandler)newInstance(
895                                    PortletDataHandler.class, portlet.getPortletDataHandlerClass());
896    
897                    portletDataHandlerInstance.setPortletId(portlet.getPortletId());
898    
899                    return portletDataHandlerInstance;
900            }
901    
902            protected PortletLayoutListener newPortletLayoutListener(Portlet portlet)
903                    throws Exception {
904    
905                    if (Validator.isNull(portlet.getPortletLayoutListenerClass())) {
906                            return null;
907                    }
908    
909                    return (PortletLayoutListener)newInstance(
910                            PortletLayoutListener.class,
911                            portlet.getPortletLayoutListenerClass());
912            }
913    
914            protected SocialActivityInterpreter newSocialActivityInterpreterInstance(
915                            Portlet portlet, String socialActivityInterpreterClass)
916                    throws Exception {
917    
918                    SocialActivityInterpreter socialActivityInterpreterInstance =
919                            (SocialActivityInterpreter)newInstance(
920                                    SocialActivityInterpreter.class,
921                                    socialActivityInterpreterClass);
922    
923                    socialActivityInterpreterInstance = new SocialActivityInterpreterImpl(
924                            portlet.getPortletId(), socialActivityInterpreterInstance);
925    
926                    SocialActivityInterpreterLocalServiceUtil.addActivityInterpreter(
927                            socialActivityInterpreterInstance);
928    
929                    return socialActivityInterpreterInstance;
930            }
931    
932            protected List<SocialActivityInterpreter>
933                            newSocialActivityInterpreterInstances(Portlet portlet)
934                    throws Exception {
935    
936                    List<SocialActivityInterpreter> socialActivityInterpreterInstances =
937                            new ArrayList<SocialActivityInterpreter>();
938    
939                    for (String socialActivityInterpreterClass :
940                                    portlet.getSocialActivityInterpreterClasses()) {
941    
942                            SocialActivityInterpreter socialActivityInterpreterInstance =
943                                    newSocialActivityInterpreterInstance(
944                                            portlet, socialActivityInterpreterClass);
945    
946                            socialActivityInterpreterInstances.add(
947                                    socialActivityInterpreterInstance);
948                    }
949    
950                    return socialActivityInterpreterInstances;
951            }
952    
953            protected List<StagedModelDataHandler<?>> newStagedModelDataHandler(
954                            Portlet portlet)
955                    throws Exception {
956    
957                    List<StagedModelDataHandler<?>> stagedModelDataHandlerInstances =
958                            new ArrayList<StagedModelDataHandler<?>>();
959    
960                    List<String> stagedModelDataHandlerClasses =
961                            portlet.getStagedModelDataHandlerClasses();
962    
963                    for (String stagedModelDataHandlerClass :
964                                    stagedModelDataHandlerClasses) {
965    
966                            StagedModelDataHandler<?> stagedModelDataHandler =
967                                    (StagedModelDataHandler<?>)newInstance(
968                                            StagedModelDataHandler.class, stagedModelDataHandlerClass);
969    
970                            stagedModelDataHandlerInstances.add(stagedModelDataHandler);
971    
972                            StagedModelDataHandlerRegistryUtil.register(stagedModelDataHandler);
973                    }
974    
975                    return stagedModelDataHandlerInstances;
976            }
977    
978            protected TemplateHandler newTemplateHandler(Portlet portlet)
979                    throws Exception {
980    
981                    if (Validator.isNull(portlet.getTemplateHandlerClass())) {
982                            return null;
983                    }
984    
985                    return (TemplateHandler)newInstance(
986                            TemplateHandler.class, portlet.getTemplateHandlerClass());
987            }
988    
989            protected URLEncoder newURLEncoder(Portlet portlet) throws Exception {
990                    if (Validator.isNull(portlet.getURLEncoderClass())) {
991                            return null;
992                    }
993    
994                    return (URLEncoder)newInstance(
995                            URLEncoder.class, portlet.getURLEncoderClass());
996            }
997    
998            protected UserNotificationHandler newUserNotificationHandlerInstance(
999                            String userNotificationHandlerClass)
1000                    throws Exception {
1001    
1002                    UserNotificationHandler userNotificationHandlerInstance =
1003                            (UserNotificationHandler)newInstance(
1004                                    UserNotificationHandler.class, userNotificationHandlerClass);
1005    
1006                    userNotificationHandlerInstance = new UserNotificationHandlerImpl(
1007                            userNotificationHandlerInstance);
1008    
1009                    UserNotificationManagerUtil.addUserNotificationHandler(
1010                            userNotificationHandlerInstance);
1011    
1012                    return userNotificationHandlerInstance;
1013            }
1014    
1015            protected List<UserNotificationHandler> newUserNotificationHandlerInstances(
1016                            Portlet portlet)
1017                    throws Exception {
1018    
1019                    List<UserNotificationHandler> userNotificationHandlerInstances =
1020                            new ArrayList<UserNotificationHandler>();
1021    
1022                    for (String userNotificationHandlerClass :
1023                                    portlet.getUserNotificationHandlerClasses()) {
1024    
1025                            UserNotificationHandler userNotificationHandlerInstance =
1026                                    newUserNotificationHandlerInstance(
1027                                            userNotificationHandlerClass);
1028    
1029                            userNotificationHandlerInstances.add(
1030                                    userNotificationHandlerInstance);
1031                    }
1032    
1033                    return userNotificationHandlerInstances;
1034            }
1035    
1036            protected void validate() {
1037                    if (_classLoader == null) {
1038                            throw new IllegalStateException("Class loader is null");
1039                    }
1040    
1041                    if (_servletContext == null) {
1042                            throw new IllegalStateException("Servlet context is null");
1043                    }
1044    
1045                    if (_warFile == null) {
1046                            throw new IllegalStateException("WAR file is null");
1047                    }
1048            }
1049    
1050            private static Log _log = LogFactoryUtil.getLog(PortletBagFactory.class);
1051    
1052            private ClassLoader _classLoader;
1053            private ServletContext _servletContext;
1054            private Boolean _warFile;
1055    
1056    }