001    /**
002     * Copyright (c) 2000-2012 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.concurrent.LockRegistry;
021    import com.liferay.portal.kernel.log.Log;
022    import com.liferay.portal.kernel.log.LogFactoryUtil;
023    import com.liferay.portal.kernel.portlet.PortletClassLoaderUtil;
024    import com.liferay.portal.kernel.util.ContextPathUtil;
025    import com.liferay.portal.kernel.util.MethodCache;
026    import com.liferay.portal.kernel.util.StringPool;
027    
028    import java.lang.reflect.Method;
029    
030    import javax.servlet.ServletContext;
031    import javax.servlet.ServletContextEvent;
032    
033    import org.springframework.context.ApplicationContext;
034    import org.springframework.web.context.ContextLoader;
035    import org.springframework.web.context.ContextLoaderListener;
036    import org.springframework.web.context.WebApplicationContext;
037    import org.springframework.web.context.support.WebApplicationContextUtils;
038    
039    /**
040     * @author Brian Wing Shun Chan
041     * @see    PortletApplicationContext
042     * @see    PortletContextLoader
043     */
044    public class PortletContextLoaderListener extends ContextLoaderListener {
045    
046            public static String getLockKey(ServletContext servletContext) {
047                    String contextPath = ContextPathUtil.getContextPath(servletContext);
048    
049                    return getLockKey(contextPath);
050            }
051    
052            public static String getLockKey(String contextPath) {
053                    return PortletContextLoaderListener.class.getName().concat(
054                            StringPool.PERIOD).concat(contextPath);
055            }
056    
057            @Override
058            public void contextDestroyed(ServletContextEvent servletContextEvent) {
059                    ClassLoader classLoader = PortletClassLoaderUtil.getClassLoader();
060    
061                    ServletContext servletContext = servletContextEvent.getServletContext();
062    
063                    try {
064                            Class<?> beanLocatorUtilClass = Class.forName(
065                                    "com.liferay.util.bean.PortletBeanLocatorUtil", true,
066                                    classLoader);
067    
068                            Method setBeanLocatorMethod = beanLocatorUtilClass.getMethod(
069                                    "setBeanLocator", new Class[] {BeanLocator.class});
070    
071                            setBeanLocatorMethod.invoke(
072                                    beanLocatorUtilClass, new Object[] {null});
073    
074                            PortletBeanLocatorUtil.setBeanLocator(
075                                    servletContext.getServletContextName(), null);
076                    }
077                    catch (Exception e) {
078                            if (_log.isWarnEnabled()) {
079                                    _log.warn(e, e);
080                            }
081                    }
082    
083                    super.contextDestroyed(servletContextEvent);
084            }
085    
086            @Override
087            public void contextInitialized(ServletContextEvent servletContextEvent) {
088                    MethodCache.reset();
089    
090                    ServletContext servletContext = servletContextEvent.getServletContext();
091    
092                    Object previousApplicationContext = servletContext.getAttribute(
093                            WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
094    
095                    servletContext.removeAttribute(
096                            WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
097    
098                    try {
099                            super.contextInitialized(servletContextEvent);
100                    }
101                    finally {
102                            String lockKey = getLockKey(servletContext);
103    
104                            LockRegistry.freeLock(lockKey, lockKey, true);
105                    }
106    
107                    PortletBeanFactoryCleaner.readBeans();
108    
109                    ClassLoader classLoader = PortletClassLoaderUtil.getClassLoader();
110    
111                    ApplicationContext applicationContext =
112                            WebApplicationContextUtils.getWebApplicationContext(servletContext);
113    
114                    BeanLocator beanLocator = new BeanLocatorImpl(
115                            classLoader, applicationContext);
116    
117                    try {
118                            Class<?> beanLocatorUtilClass = Class.forName(
119                                    "com.liferay.util.bean.PortletBeanLocatorUtil", true,
120                                    classLoader);
121    
122                            Method setBeanLocatorMethod = beanLocatorUtilClass.getMethod(
123                                    "setBeanLocator", new Class[] {BeanLocator.class});
124    
125                            setBeanLocatorMethod.invoke(
126                                    beanLocatorUtilClass, new Object[] {beanLocator});
127    
128                            PortletBeanLocatorUtil.setBeanLocator(
129                                    servletContext.getServletContextName(), beanLocator);
130                    }
131                    catch (Exception e) {
132                            _log.error(e, e);
133                    }
134    
135                    if (previousApplicationContext == null) {
136                            servletContext.removeAttribute(
137                                    WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
138                    }
139                    else {
140                            servletContext.setAttribute(
141                                    WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
142                                    previousApplicationContext);
143                    }
144            }
145    
146            @Override
147            protected ContextLoader createContextLoader() {
148                    return new PortletContextLoader();
149            }
150    
151            private static Log _log = LogFactoryUtil.getLog(
152                    PortletContextLoaderListener.class);
153    
154    }