001    /**
002     * Copyright (c) 2000-2010 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.portal.spring.context;
016    
017    import com.liferay.portal.bean.BeanLocatorImpl;
018    import com.liferay.portal.kernel.bean.BeanLocator;
019    import com.liferay.portal.kernel.bean.PortletBeanLocatorUtil;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.portlet.PortletClassLoaderUtil;
023    
024    import java.lang.reflect.Method;
025    
026    import javax.servlet.ServletContext;
027    import javax.servlet.ServletContextEvent;
028    
029    import org.springframework.context.ApplicationContext;
030    import org.springframework.web.context.ContextLoader;
031    import org.springframework.web.context.ContextLoaderListener;
032    import org.springframework.web.context.WebApplicationContext;
033    import org.springframework.web.context.support.WebApplicationContextUtils;
034    
035    /**
036     * @author Brian Wing Shun Chan
037     * @see    PortletApplicationContext
038     * @see    PortletContextLoader
039     */
040    public class PortletContextLoaderListener extends ContextLoaderListener {
041    
042            public void contextDestroyed(ServletContextEvent event) {
043                    ClassLoader classLoader = PortletClassLoaderUtil.getClassLoader();
044    
045                    ServletContext servletContext = event.getServletContext();
046    
047                    try {
048                            Class<?> beanLocatorUtilClass = Class.forName(
049                                    "com.liferay.util.bean.PortletBeanLocatorUtil", true,
050                                    classLoader);
051    
052                            Method setBeanLocatorMethod = beanLocatorUtilClass.getMethod(
053                                    "setBeanLocator", new Class[] {BeanLocator.class});
054    
055                            setBeanLocatorMethod.invoke(
056                                    beanLocatorUtilClass, new Object[] {null});
057    
058                            PortletBeanLocatorUtil.setBeanLocator(
059                                    servletContext.getServletContextName(), null);
060                    }
061                    catch (Exception e) {
062                            if (_log.isWarnEnabled()) {
063                                    _log.warn(e, e);
064                            }
065                    }
066    
067                    super.contextDestroyed(event);
068            }
069    
070            public void contextInitialized(ServletContextEvent event) {
071                    super.contextInitialized(event);
072    
073                    ClassLoader classLoader = PortletClassLoaderUtil.getClassLoader();
074    
075                    ServletContext servletContext = event.getServletContext();
076    
077                    ApplicationContext applicationContext =
078                            WebApplicationContextUtils.getWebApplicationContext(servletContext);
079    
080                    BeanLocator beanLocator = new BeanLocatorImpl(
081                            classLoader, applicationContext);
082    
083                    try {
084                            Class<?> beanLocatorUtilClass = Class.forName(
085                                    "com.liferay.util.bean.PortletBeanLocatorUtil", true,
086                                    classLoader);
087    
088                            Method setBeanLocatorMethod = beanLocatorUtilClass.getMethod(
089                                    "setBeanLocator", new Class[] {BeanLocator.class});
090    
091                            setBeanLocatorMethod.invoke(
092                                    beanLocatorUtilClass, new Object[] {beanLocator});
093    
094                            PortletBeanLocatorUtil.setBeanLocator(
095                                    servletContext.getServletContextName(), beanLocator);
096                    }
097                    catch (Exception e) {
098                            _log.error(e, e);
099                    }
100    
101                    servletContext.removeAttribute(
102                            WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
103            }
104    
105            protected ContextLoader createContextLoader() {
106                    return new PortletContextLoader();
107            }
108    
109            private static Log _log = LogFactoryUtil.getLog(
110                    PortletContextLoaderListener.class);
111    
112    }