1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.deploy.hot;
24  
25  import com.liferay.portal.apache.bridges.struts.LiferayServletContextProvider;
26  import com.liferay.portal.kernel.configuration.Configuration;
27  import com.liferay.portal.kernel.configuration.ConfigurationFactoryUtil;
28  import com.liferay.portal.kernel.deploy.hot.HotDeployEvent;
29  import com.liferay.portal.kernel.deploy.hot.HotDeployException;
30  import com.liferay.portal.kernel.job.Scheduler;
31  import com.liferay.portal.kernel.language.LanguageUtil;
32  import com.liferay.portal.kernel.log.Log;
33  import com.liferay.portal.kernel.log.LogFactoryUtil;
34  import com.liferay.portal.kernel.poller.InvokerPollerProcessor;
35  import com.liferay.portal.kernel.poller.PollerProcessor;
36  import com.liferay.portal.kernel.pop.MessageListener;
37  import com.liferay.portal.kernel.portlet.ConfigurationAction;
38  import com.liferay.portal.kernel.portlet.FriendlyURLMapper;
39  import com.liferay.portal.kernel.portlet.PortletBag;
40  import com.liferay.portal.kernel.portlet.PortletBagPool;
41  import com.liferay.portal.kernel.portlet.PortletLayoutListener;
42  import com.liferay.portal.kernel.search.Indexer;
43  import com.liferay.portal.kernel.search.IndexerRegistryUtil;
44  import com.liferay.portal.kernel.search.InvokerIndexer;
45  import com.liferay.portal.kernel.search.OpenSearch;
46  import com.liferay.portal.kernel.servlet.PortletServlet;
47  import com.liferay.portal.kernel.servlet.ServletContextProvider;
48  import com.liferay.portal.kernel.servlet.URLEncoder;
49  import com.liferay.portal.kernel.util.ClassUtil;
50  import com.liferay.portal.kernel.util.GetterUtil;
51  import com.liferay.portal.kernel.util.HttpUtil;
52  import com.liferay.portal.kernel.util.LocaleUtil;
53  import com.liferay.portal.kernel.util.ObjectValuePair;
54  import com.liferay.portal.kernel.util.StringUtil;
55  import com.liferay.portal.kernel.util.Validator;
56  import com.liferay.portal.lar.InvokerPortletDataHandler;
57  import com.liferay.portal.lar.PortletDataHandler;
58  import com.liferay.portal.model.Portlet;
59  import com.liferay.portal.model.PortletApp;
60  import com.liferay.portal.model.PortletCategory;
61  import com.liferay.portal.model.PortletFilter;
62  import com.liferay.portal.model.PortletURLListener;
63  import com.liferay.portal.poller.PollerProcessorUtil;
64  import com.liferay.portal.pop.POPServerUtil;
65  import com.liferay.portal.security.permission.ResourceActionsUtil;
66  import com.liferay.portal.service.PortletLocalServiceUtil;
67  import com.liferay.portal.service.ResourceActionLocalServiceUtil;
68  import com.liferay.portal.service.ResourceCodeLocalServiceUtil;
69  import com.liferay.portal.util.Portal;
70  import com.liferay.portal.util.PortalInstances;
71  import com.liferay.portal.util.PropsValues;
72  import com.liferay.portal.util.WebAppPool;
73  import com.liferay.portal.util.WebKeys;
74  import com.liferay.portal.webdav.WebDAVStorage;
75  import com.liferay.portal.webdav.WebDAVUtil;
76  import com.liferay.portlet.ControlPanelEntry;
77  import com.liferay.portlet.CustomUserAttributes;
78  import com.liferay.portlet.PortletBagImpl;
79  import com.liferay.portlet.PortletConfigFactory;
80  import com.liferay.portlet.PortletContextBag;
81  import com.liferay.portlet.PortletContextBagPool;
82  import com.liferay.portlet.PortletFilterFactory;
83  import com.liferay.portlet.PortletInstanceFactoryUtil;
84  import com.liferay.portlet.PortletPreferencesSerializer;
85  import com.liferay.portlet.PortletResourceBundles;
86  import com.liferay.portlet.PortletURLListenerFactory;
87  import com.liferay.portlet.social.model.SocialActivityInterpreter;
88  import com.liferay.portlet.social.model.SocialRequestInterpreter;
89  import com.liferay.portlet.social.model.impl.SocialActivityInterpreterImpl;
90  import com.liferay.portlet.social.model.impl.SocialRequestInterpreterImpl;
91  import com.liferay.portlet.social.service.SocialActivityInterpreterLocalServiceUtil;
92  import com.liferay.portlet.social.service.SocialRequestInterpreterLocalServiceUtil;
93  
94  import java.util.HashMap;
95  import java.util.HashSet;
96  import java.util.Iterator;
97  import java.util.List;
98  import java.util.Locale;
99  import java.util.Map;
100 import java.util.MissingResourceException;
101 import java.util.Properties;
102 import java.util.ResourceBundle;
103 import java.util.Set;
104 
105 import javax.portlet.PortletConfig;
106 import javax.portlet.PortletContext;
107 import javax.portlet.PortletURLGenerationListener;
108 import javax.portlet.PreferencesValidator;
109 
110 import javax.servlet.ServletContext;
111 
112 import org.apache.portals.bridges.struts.StrutsPortlet;
113 
114 /**
115  * <a href="PortletHotDeployListener.java.html"><b><i>View Source</i></b></a>
116  *
117  * @author Brian Wing Shun Chan
118  * @author Brian Myunghun Kim
119  * @author Ivica Cardic
120  * @author Raymond Augé
121  *
122  */
123 public class PortletHotDeployListener extends BaseHotDeployListener {
124 
125     public void invokeDeploy(HotDeployEvent event) throws HotDeployException {
126         try {
127             doInvokeDeploy(event);
128         }
129         catch (Throwable t) {
130             throwHotDeployException(
131                 event, "Error registering portlets for ", t);
132         }
133     }
134 
135     public void invokeUndeploy(HotDeployEvent event) throws HotDeployException {
136         try {
137             doInvokeUndeploy(event);
138         }
139         catch (Throwable t) {
140             throwHotDeployException(
141                 event, "Error unregistering portlets for ", t);
142         }
143     }
144 
145     protected void destroyPortlet(Portlet portlet, Set<String> portletIds)
146         throws Exception {
147 
148         PortletApp portletApp = portlet.getPortletApp();
149 
150         Set<PortletFilter> portletFilters = portletApp.getPortletFilters();
151 
152         for (PortletFilter portletFilter : portletFilters) {
153             PortletFilterFactory.destroy(portletFilter);
154         }
155 
156         Set<PortletURLListener> portletURLListeners =
157             portletApp.getPortletURLListeners();
158 
159         for (PortletURLListener portletURLListener : portletURLListeners) {
160             PortletURLListenerFactory.destroy(portletURLListener);
161         }
162 
163         Indexer indexer = portlet.getIndexerInstance();
164 
165         if (indexer != null) {
166             for (String className : indexer.getClassNames()) {
167                 IndexerRegistryUtil.unregister(className);
168             }
169         }
170 
171         Scheduler scheduler = portlet.getSchedulerInstance();
172 
173         if (scheduler != null) {
174             scheduler.unschedule();
175         }
176 
177         PollerProcessorUtil.deletePollerProcessor(portlet.getPortletId());
178 
179         POPServerUtil.deleteListener(portlet.getPopMessageListenerInstance());
180 
181         SocialActivityInterpreterLocalServiceUtil.deleteActivityInterpreter(
182             portlet.getSocialActivityInterpreterInstance());
183 
184         SocialRequestInterpreterLocalServiceUtil.deleteRequestInterpreter(
185             portlet.getSocialRequestInterpreterInstance());
186 
187         WebDAVUtil.deleteStorage(portlet.getWebDAVStorageInstance());
188 
189         PortletInstanceFactoryUtil.destroy(portlet);
190 
191         portletIds.add(portlet.getPortletId());
192     }
193 
194     protected void doInvokeDeploy(HotDeployEvent event) throws Exception {
195 
196         // Servlet context
197 
198         ServletContext servletContext = event.getServletContext();
199 
200         String servletContextName = servletContext.getServletContextName();
201 
202         if (_log.isDebugEnabled()) {
203             _log.debug("Invoking deploy for " + servletContextName);
204         }
205 
206         // Company ids
207 
208         long[] companyIds = PortalInstances.getCompanyIds();
209 
210         // Initialize portlets
211 
212         String[] xmls = new String[] {
213             HttpUtil.URLtoString(servletContext.getResource(
214                 "/WEB-INF/" + Portal.PORTLET_XML_FILE_NAME_STANDARD)),
215             HttpUtil.URLtoString(servletContext.getResource(
216                 "/WEB-INF/" + Portal.PORTLET_XML_FILE_NAME_CUSTOM)),
217             HttpUtil.URLtoString(servletContext.getResource(
218                 "/WEB-INF/liferay-portlet.xml")),
219             HttpUtil.URLtoString(servletContext.getResource("/WEB-INF/web.xml"))
220         };
221 
222         if (xmls[0] == null) {
223             return;
224         }
225 
226         if (_log.isInfoEnabled()) {
227             _log.info("Registering portlets for " + servletContextName);
228         }
229 
230         List<Portlet> portlets = PortletLocalServiceUtil.initWAR(
231             servletContextName, servletContext, xmls, event.getPluginPackage());
232 
233         // Class loader
234 
235         ClassLoader portletClassLoader = event.getContextClassLoader();
236 
237         servletContext.setAttribute(
238             PortletServlet.PORTLET_CLASS_LOADER, portletClassLoader);
239 
240         // Portlet context wrapper
241 
242         _portletAppInitialized = false;
243         _strutsBridges = false;
244 
245         Iterator<Portlet> itr = portlets.iterator();
246 
247         while (itr.hasNext()) {
248             Portlet portlet = itr.next();
249 
250             initPortlet(portlet, servletContext, portletClassLoader, itr);
251         }
252 
253         // Struts bridges
254 
255         if (!_strutsBridges) {
256             _strutsBridges = GetterUtil.getBoolean(
257                 servletContext.getInitParameter(
258                     "struts-bridges-context-provider"));
259         }
260 
261         if (_strutsBridges) {
262             servletContext.setAttribute(
263                 ServletContextProvider.STRUTS_BRIDGES_CONTEXT_PROVIDER,
264                 new LiferayServletContextProvider());
265         }
266 
267         // Portlet display
268 
269         String xml = HttpUtil.URLtoString(servletContext.getResource(
270             "/WEB-INF/liferay-display.xml"));
271 
272         PortletCategory newPortletCategory =
273             PortletLocalServiceUtil.getWARDisplay(servletContextName, xml);
274 
275         for (int i = 0; i < companyIds.length; i++) {
276             long companyId = companyIds[i];
277 
278             PortletCategory portletCategory =
279                 (PortletCategory)WebAppPool.get(
280                     String.valueOf(companyId), WebKeys.PORTLET_CATEGORY);
281 
282             if (portletCategory != null) {
283                 portletCategory.merge(newPortletCategory);
284             }
285             else {
286                 _log.error(
287                     "Unable to register portlet for company " + companyId +
288                         " because it does not exist");
289             }
290         }
291 
292         // Portlet properties
293 
294         processPortletProperties(servletContextName, portletClassLoader);
295 
296         // Resource actions and codes
297 
298         itr = portlets.iterator();
299 
300         while (itr.hasNext()) {
301             Portlet portlet = itr.next();
302 
303             List<String> modelNames =
304                 ResourceActionsUtil.getPortletModelResources(
305                     portlet.getPortletId());
306 
307             for (long companyId : companyIds) {
308                 ResourceCodeLocalServiceUtil.checkResourceCodes(
309                     companyId, portlet.getPortletId());
310 
311                 for (String modelName : modelNames) {
312                     ResourceCodeLocalServiceUtil.checkResourceCodes(
313                         companyId, modelName);
314                 }
315             }
316 
317             List<String> portletActions =
318                 ResourceActionsUtil.getPortletResourceActions(
319                     portlet.getPortletId());
320 
321             ResourceActionLocalServiceUtil.checkResourceActions(
322                 portlet.getPortletId(), portletActions);
323 
324             for (String modelName : modelNames) {
325                 List<String> modelActions =
326                     ResourceActionsUtil.getModelResourceActions(modelName);
327 
328                 ResourceActionLocalServiceUtil.checkResourceActions(
329                     modelName, modelActions);
330             }
331         }
332 
333         // ClpMessageListener
334 
335         registerClpMessageListeners(servletContext, portletClassLoader);
336 
337         // Variables
338 
339         _vars.put(
340             servletContextName,
341             new ObjectValuePair<long[], List<Portlet>>(
342                 companyIds, portlets));
343 
344         if (_log.isInfoEnabled()) {
345             if (portlets.size() == 1) {
346                 _log.info(
347                     "1 portlet for " + servletContextName +
348                         " is available for use");
349             }
350             else {
351                 _log.info(
352                     portlets.size() + " portlets for " + servletContextName +
353                         " are available for use");
354             }
355         }
356     }
357 
358     protected void doInvokeUndeploy(HotDeployEvent event) throws Exception {
359         ServletContext servletContext = event.getServletContext();
360 
361         String servletContextName = servletContext.getServletContextName();
362 
363         if (_log.isDebugEnabled()) {
364             _log.debug("Invoking undeploy for " + servletContextName);
365         }
366 
367         ObjectValuePair<long[], List<Portlet>> ovp =
368             _vars.remove(servletContextName);
369 
370         if (ovp == null) {
371             return;
372         }
373 
374         long[] companyIds = ovp.getKey();
375         List<Portlet> portlets = ovp.getValue();
376 
377         Set<String> portletIds = new HashSet<String>();
378 
379         if (portlets != null) {
380             if (_log.isInfoEnabled()) {
381                 _log.info(
382                     "Unregistering portlets for " + servletContextName);
383             }
384 
385             Iterator<Portlet> itr = portlets.iterator();
386 
387             while (itr.hasNext()) {
388                 Portlet portlet = itr.next();
389 
390                 destroyPortlet(portlet, portletIds);
391             }
392         }
393 
394         if (portletIds.size() > 0) {
395             for (int i = 0; i < companyIds.length; i++) {
396                 long companyId = companyIds[i];
397 
398                 PortletCategory portletCategory =
399                     (PortletCategory)WebAppPool.get(
400                         String.valueOf(companyId), WebKeys.PORTLET_CATEGORY);
401 
402                 portletCategory.separate(portletIds);
403             }
404         }
405 
406         PortletResourceBundles.remove(servletContextName);
407 
408         unregisterClpMessageListeners(servletContext);
409 
410         if (_log.isInfoEnabled()) {
411             if (portlets.size() == 1) {
412                 _log.info(
413                     "1 portlet for " + servletContextName +
414                         " was unregistered");
415             }
416             else {
417                 _log.info(
418                     portlets.size() + " portlets for " + servletContextName +
419                         " was unregistered");
420             }
421         }
422     }
423 
424     protected void initPortlet(
425             Portlet portlet, ServletContext servletContext,
426             ClassLoader portletClassLoader, Iterator<Portlet> portletsItr)
427         throws Exception {
428 
429         Class<?> portletClass = null;
430 
431         try {
432             portletClass = portletClassLoader.loadClass(
433                 portlet.getPortletClass());
434         }
435         catch (Throwable e) {
436             _log.error(e, e);
437 
438             portletsItr.remove();
439 
440             PortletLocalServiceUtil.destroyPortlet(portlet);
441 
442             return;
443         }
444 
445         javax.portlet.Portlet portletInstance =
446             (javax.portlet.Portlet)portletClass.newInstance();
447 
448         if (ClassUtil.isSubclass(portletClass, StrutsPortlet.class.getName())) {
449             _strutsBridges = true;
450         }
451 
452         ConfigurationAction configurationActionInstance = null;
453 
454         if (Validator.isNotNull(portlet.getConfigurationActionClass())) {
455             configurationActionInstance =
456                 (ConfigurationAction)portletClassLoader.loadClass(
457                     portlet.getConfigurationActionClass()).newInstance();
458         }
459 
460         Indexer indexerInstance = null;
461 
462         if (Validator.isNotNull(portlet.getIndexerClass())) {
463             indexerInstance = (Indexer)portletClassLoader.loadClass(
464                 portlet.getIndexerClass()).newInstance();
465 
466             indexerInstance = new InvokerIndexer(
467                 indexerInstance, portletClassLoader);
468 
469             for (String className : indexerInstance.getClassNames()) {
470                 IndexerRegistryUtil.register(className, indexerInstance);
471             }
472         }
473 
474         OpenSearch openSearchInstance = null;
475 
476         if (Validator.isNotNull(portlet.getOpenSearchClass())) {
477             openSearchInstance = (OpenSearch)portletClassLoader.loadClass(
478                 portlet.getOpenSearchClass()).newInstance();
479         }
480 
481         Scheduler schedulerInstance = null;
482 
483         if (PropsValues.SCHEDULER_ENABLED &&
484             Validator.isNotNull(portlet.getSchedulerClass())) {
485 
486             schedulerInstance = (Scheduler)portletClassLoader.loadClass(
487                 portlet.getSchedulerClass()).newInstance();
488 
489             schedulerInstance.schedule();
490         }
491 
492         FriendlyURLMapper friendlyURLMapperInstance = null;
493 
494         if (Validator.isNotNull(portlet.getFriendlyURLMapperClass())) {
495             friendlyURLMapperInstance =
496                 (FriendlyURLMapper)portletClassLoader.loadClass(
497                     portlet.getFriendlyURLMapperClass()).newInstance();
498         }
499 
500         URLEncoder urlEncoderInstance = null;
501 
502         if (Validator.isNotNull(portlet.getURLEncoderClass())) {
503             urlEncoderInstance = (URLEncoder)portletClassLoader.loadClass(
504                 portlet.getURLEncoderClass()).newInstance();
505         }
506 
507         PortletDataHandler portletDataHandlerInstance = null;
508 
509         if (Validator.isNotNull(portlet.getPortletDataHandlerClass())) {
510             portletDataHandlerInstance =
511                 (PortletDataHandler)portletClassLoader.loadClass(
512                     portlet.getPortletDataHandlerClass()).newInstance();
513 
514             portletDataHandlerInstance = new InvokerPortletDataHandler(
515                 portletDataHandlerInstance, portletClassLoader);
516         }
517 
518         PortletLayoutListener portletLayoutListenerInstance = null;
519 
520         if (Validator.isNotNull(portlet.getPortletLayoutListenerClass())) {
521             portletLayoutListenerInstance =
522                 (PortletLayoutListener)portletClassLoader.loadClass(
523                     portlet.getPortletLayoutListenerClass()).newInstance();
524         }
525 
526         PollerProcessor pollerProcessorInstance = null;
527 
528         if (Validator.isNotNull(portlet.getPollerProcessorClass())) {
529             pollerProcessorInstance =
530                 (PollerProcessor)portletClassLoader.loadClass(
531                     portlet.getPollerProcessorClass()).newInstance();
532 
533             pollerProcessorInstance = new InvokerPollerProcessor(
534                 pollerProcessorInstance, portletClassLoader);
535 
536             PollerProcessorUtil.addPollerProcessor(
537                 portlet.getPortletId(), pollerProcessorInstance);
538         }
539 
540         MessageListener popMessageListenerInstance = null;
541 
542         if (Validator.isNotNull(portlet.getPopMessageListenerClass())) {
543             popMessageListenerInstance =
544                 (MessageListener)portletClassLoader.loadClass(
545                     portlet.getPopMessageListenerClass()).newInstance();
546 
547             POPServerUtil.addListener(popMessageListenerInstance);
548         }
549 
550         SocialActivityInterpreter socialActivityInterpreterInstance = null;
551 
552         if (Validator.isNotNull(portlet.getSocialActivityInterpreterClass())) {
553             socialActivityInterpreterInstance =
554                 (SocialActivityInterpreter)portletClassLoader.loadClass(
555                     portlet.getSocialActivityInterpreterClass()).newInstance();
556 
557             socialActivityInterpreterInstance =
558                 new SocialActivityInterpreterImpl(
559                     portlet.getPortletId(), socialActivityInterpreterInstance);
560 
561             SocialActivityInterpreterLocalServiceUtil.addActivityInterpreter(
562                 socialActivityInterpreterInstance);
563         }
564 
565         SocialRequestInterpreter socialRequestInterpreterInstance = null;
566 
567         if (Validator.isNotNull(portlet.getSocialRequestInterpreterClass())) {
568             socialRequestInterpreterInstance =
569                 (SocialRequestInterpreter)portletClassLoader.loadClass(
570                     portlet.getSocialRequestInterpreterClass()).newInstance();
571 
572             socialRequestInterpreterInstance = new SocialRequestInterpreterImpl(
573                 portlet.getPortletId(), socialRequestInterpreterInstance);
574 
575             SocialRequestInterpreterLocalServiceUtil.addRequestInterpreter(
576                 socialRequestInterpreterInstance);
577         }
578 
579         WebDAVStorage webDAVStorageInstance = null;
580 
581         if (Validator.isNotNull(portlet.getWebDAVStorageClass())) {
582             webDAVStorageInstance = (WebDAVStorage)portletClassLoader.loadClass(
583                 portlet.getWebDAVStorageClass()).newInstance();
584 
585             webDAVStorageInstance.setToken(portlet.getWebDAVStorageToken());
586 
587             WebDAVUtil.addStorage(webDAVStorageInstance);
588         }
589 
590         ControlPanelEntry controlPanelEntryInstance = null;
591 
592         if (Validator.isNotNull(portlet.getControlPanelEntryClass())) {
593             controlPanelEntryInstance =
594                 (ControlPanelEntry)portletClassLoader.loadClass(
595                     portlet.getControlPanelEntryClass()).newInstance();
596         }
597 
598         PreferencesValidator preferencesValidatorInstance = null;
599 
600         if (Validator.isNotNull(portlet.getPreferencesValidator())) {
601             preferencesValidatorInstance =
602                 (PreferencesValidator)portletClassLoader.loadClass(
603                     portlet.getPreferencesValidator()).newInstance();
604 
605             try {
606                 if (PropsValues.PREFERENCE_VALIDATE_ON_STARTUP) {
607                     preferencesValidatorInstance.validate(
608                         PortletPreferencesSerializer.fromDefaultXML(
609                             portlet.getDefaultPreferences()));
610                 }
611             }
612             catch (Exception e) {
613                 _log.warn(
614                     "Portlet with the name " + portlet.getPortletId() +
615                         " does not have valid default preferences");
616             }
617         }
618 
619         Map<String, ResourceBundle> resourceBundles = null;
620 
621         if (Validator.isNotNull(portlet.getResourceBundle())) {
622             resourceBundles = new HashMap<String, ResourceBundle>();
623 
624             initResourceBundle(
625                 resourceBundles, portlet, portletClassLoader,
626                 LocaleUtil.getDefault());
627 
628             Iterator<String> supportLocalesItr =
629                 portlet.getSupportedLocales().iterator();
630 
631             while (supportLocalesItr.hasNext()) {
632                 String supportedLocale = supportLocalesItr.next();
633 
634                 Locale locale = LocaleUtil.fromLanguageId(supportedLocale);
635 
636                 initResourceBundle(
637                     resourceBundles, portlet, portletClassLoader, locale);
638             }
639         }
640 
641         PortletBag portletBag = new PortletBagImpl(
642             portlet.getPortletId(), servletContext, portletInstance,
643             configurationActionInstance, indexerInstance, openSearchInstance,
644             schedulerInstance, friendlyURLMapperInstance, urlEncoderInstance,
645             portletDataHandlerInstance, portletLayoutListenerInstance,
646             pollerProcessorInstance, popMessageListenerInstance,
647             socialActivityInterpreterInstance, socialRequestInterpreterInstance,
648             webDAVStorageInstance, controlPanelEntryInstance,
649             preferencesValidatorInstance, resourceBundles);
650 
651         PortletBagPool.put(portlet.getPortletId(), portletBag);
652 
653         if (!_portletAppInitialized) {
654             initPortletApp(portlet, servletContext, portletClassLoader);
655 
656             _portletAppInitialized = true;
657         }
658 
659         try {
660             PortletInstanceFactoryUtil.create(portlet, servletContext);
661         }
662         catch (Exception e) {
663             _log.error(e, e);
664         }
665     }
666 
667     protected void initPortletApp(
668             Portlet portlet, ServletContext servletContext,
669             ClassLoader portletClassLoader)
670         throws Exception {
671 
672         String servletContextName = servletContext.getServletContextName();
673 
674         PortletConfig portletConfig = PortletConfigFactory.create(
675             portlet, servletContext);
676 
677         PortletContext portletContext = portletConfig.getPortletContext();
678 
679         PortletContextBag portletContextBag = new PortletContextBag(
680             servletContextName);
681 
682         PortletContextBagPool.put(servletContextName, portletContextBag);
683 
684         PortletApp portletApp = portlet.getPortletApp();
685 
686         Map<String, String> customUserAttributes =
687             portletApp.getCustomUserAttributes();
688 
689         for (Map.Entry<String, String> entry :
690                 customUserAttributes.entrySet()) {
691 
692             String attrCustomClass = entry.getValue();
693 
694             CustomUserAttributes customUserAttributesInstance =
695                 (CustomUserAttributes)portletClassLoader.loadClass(
696                     attrCustomClass).newInstance();
697 
698             portletContextBag.getCustomUserAttributes().put(
699                 attrCustomClass, customUserAttributesInstance);
700         }
701 
702         Set<PortletFilter> portletFilters = portletApp.getPortletFilters();
703 
704         for (PortletFilter portletFilter : portletFilters) {
705             javax.portlet.filter.PortletFilter portletFilterInstance =
706                 (javax.portlet.filter.PortletFilter)
707                     portletClassLoader.loadClass(
708                         portletFilter.getFilterClass()).newInstance();
709 
710             portletContextBag.getPortletFilters().put(
711                 portletFilter.getFilterName(), portletFilterInstance);
712 
713             PortletFilterFactory.create(portletFilter, portletContext);
714         }
715 
716         Set<PortletURLListener> portletURLListeners =
717             portletApp.getPortletURLListeners();
718 
719         for (PortletURLListener portletURLListener : portletURLListeners) {
720             PortletURLGenerationListener portletURLListenerInstance =
721                 (PortletURLGenerationListener)portletClassLoader.loadClass(
722                     portletURLListener.getListenerClass()).newInstance();
723 
724             portletContextBag.getPortletURLListeners().put(
725                 portletURLListener.getListenerClass(),
726                 portletURLListenerInstance);
727 
728             PortletURLListenerFactory.create(portletURLListener);
729         }
730     }
731 
732     protected void initResourceBundle(
733         Map<String, ResourceBundle> resourceBundles, Portlet portlet,
734         ClassLoader portletClassLoader, Locale locale) {
735 
736         try {
737             ResourceBundle resourceBundle = ResourceBundle.getBundle(
738                 portlet.getResourceBundle(), locale, portletClassLoader);
739 
740             resourceBundles.put(
741                 LocaleUtil.toLanguageId(locale), resourceBundle);
742         }
743         catch (MissingResourceException mre) {
744             _log.warn(mre.getMessage());
745         }
746     }
747 
748     protected void processPortletProperties(
749             String servletContextName, ClassLoader portletClassLoader)
750         throws Exception {
751 
752         Configuration portletPropertiesConfiguration = null;
753 
754         try {
755             portletPropertiesConfiguration =
756                 ConfigurationFactoryUtil.getConfiguration(
757                     portletClassLoader, "portlet");
758         }
759         catch (Exception e) {
760             if (_log.isDebugEnabled()) {
761                 _log.debug("Unable to read portlet.properties");
762             }
763 
764             return;
765         }
766 
767         Properties portletProperties =
768             portletPropertiesConfiguration.getProperties();
769 
770         if (portletProperties.size() == 0) {
771             return;
772         }
773 
774         String languageBundleName = portletProperties.getProperty(
775             "language.bundle");
776 
777         if (Validator.isNotNull(languageBundleName)) {
778             Locale[] locales = LanguageUtil.getAvailableLocales();
779 
780             for (int i = 0; i < locales.length; i++) {
781                 ResourceBundle bundle = ResourceBundle.getBundle(
782                     languageBundleName, locales[i], portletClassLoader);
783 
784                 PortletResourceBundles.put(
785                     servletContextName, LocaleUtil.toLanguageId(locales[i]),
786                     bundle);
787             }
788         }
789 
790         String[] resourceActionConfigs = StringUtil.split(
791             portletProperties.getProperty("resource.actions.configs"));
792 
793         for (int i = 0; i < resourceActionConfigs.length; i++) {
794             ResourceActionsUtil.read(
795                 servletContextName, portletClassLoader,
796                 resourceActionConfigs[i]);
797         }
798     }
799 
800     private static Log _log =
801          LogFactoryUtil.getLog(PortletHotDeployListener.class);
802 
803     private static Map<String, ObjectValuePair<long[], List<Portlet>>> _vars =
804         new HashMap<String, ObjectValuePair<long[], List<Portlet>>>();
805 
806     private boolean _portletAppInitialized;
807     private boolean _strutsBridges;
808 
809 }