001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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                    PortletBeanFactoryCleaner.readBeans();
074    
075                    ClassLoader classLoader = PortletClassLoaderUtil.getClassLoader();
076    
077                    ServletContext servletContext = event.getServletContext();
078    
079                    ApplicationContext applicationContext =
080                            WebApplicationContextUtils.getWebApplicationContext(servletContext);
081    
082                    BeanLocator beanLocator = new BeanLocatorImpl(
083                            classLoader, applicationContext);
084    
085                    try {
086                            Class<?> beanLocatorUtilClass = Class.forName(
087                                    "com.liferay.util.bean.PortletBeanLocatorUtil", true,
088                                    classLoader);
089    
090                            Method setBeanLocatorMethod = beanLocatorUtilClass.getMethod(
091                                    "setBeanLocator", new Class[] {BeanLocator.class});
092    
093                            setBeanLocatorMethod.invoke(
094                                    beanLocatorUtilClass, new Object[] {beanLocator});
095    
096                            PortletBeanLocatorUtil.setBeanLocator(
097                                    servletContext.getServletContextName(), beanLocator);
098                    }
099                    catch (Exception e) {
100                            _log.error(e, e);
101                    }
102    
103                    servletContext.removeAttribute(
104                            WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
105            }
106    
107            protected ContextLoader createContextLoader() {
108                    return new PortletContextLoader();
109            }
110    
111            private static Log _log = LogFactoryUtil.getLog(
112                    PortletContextLoaderListener.class);
113    
114    }