001    /**
002     * Copyright (c) 2000-2012 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    import com.liferay.portal.kernel.util.MethodCache;
024    import com.liferay.portal.security.lang.PortalSecurityManagerThreadLocal;
025    import com.liferay.portal.security.pacl.PACLPolicy;
026    import com.liferay.portal.security.pacl.PACLPolicyManager;
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            @Override
047            public void contextDestroyed(ServletContextEvent servletContextEvent) {
048                    ClassLoader classLoader = PortletClassLoaderUtil.getClassLoader();
049    
050                    ServletContext servletContext = servletContextEvent.getServletContext();
051    
052                    try {
053                            Class<?> beanLocatorUtilClass = Class.forName(
054                                    "com.liferay.util.bean.PortletBeanLocatorUtil", true,
055                                    classLoader);
056    
057                            Method setBeanLocatorMethod = beanLocatorUtilClass.getMethod(
058                                    "setBeanLocator", new Class[] {BeanLocator.class});
059    
060                            setBeanLocatorMethod.invoke(
061                                    beanLocatorUtilClass, new Object[] {null});
062    
063                            PortletBeanLocatorUtil.setBeanLocator(
064                                    servletContext.getServletContextName(), null);
065                    }
066                    catch (Exception e) {
067                            if (_log.isWarnEnabled()) {
068                                    _log.warn(e, e);
069                            }
070                    }
071    
072                    super.contextDestroyed(servletContextEvent);
073            }
074    
075            @Override
076            public void contextInitialized(ServletContextEvent servletContextEvent) {
077                    MethodCache.reset();
078    
079                    ServletContext servletContext = servletContextEvent.getServletContext();
080    
081                    Object previousApplicationContext = servletContext.getAttribute(
082                            WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
083    
084                    servletContext.removeAttribute(
085                            WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
086    
087                    ClassLoader classLoader = PortletClassLoaderUtil.getClassLoader();
088    
089                    PACLPolicy previousPACLPolicy =
090                            PortalSecurityManagerThreadLocal.getPACLPolicy();
091    
092                    try {
093                            PACLPolicy paclPolicy = PACLPolicyManager.getPACLPolicy(
094                                    classLoader);
095    
096                            PortalSecurityManagerThreadLocal.setPACLPolicy(paclPolicy);
097    
098                            super.contextInitialized(servletContextEvent);
099                    }
100                    finally {
101                            PortalSecurityManagerThreadLocal.setPACLPolicy(previousPACLPolicy);
102                    }
103    
104                    PortletBeanFactoryCleaner.readBeans();
105    
106                    ApplicationContext applicationContext =
107                            WebApplicationContextUtils.getWebApplicationContext(servletContext);
108    
109                    BeanLocatorImpl beanLocatorImpl = new BeanLocatorImpl(
110                            classLoader, applicationContext);
111    
112                    beanLocatorImpl.setPACLServletContextName(
113                            servletContext.getServletContextName());
114    
115                    try {
116                            Class<?> beanLocatorUtilClass = Class.forName(
117                                    "com.liferay.util.bean.PortletBeanLocatorUtil", true,
118                                    classLoader);
119    
120                            Method setBeanLocatorMethod = beanLocatorUtilClass.getMethod(
121                                    "setBeanLocator", new Class[] {BeanLocator.class});
122    
123                            setBeanLocatorMethod.invoke(
124                                    beanLocatorUtilClass, new Object[] {beanLocatorImpl});
125    
126                            PortletBeanLocatorUtil.setBeanLocator(
127                                    servletContext.getServletContextName(), beanLocatorImpl);
128                    }
129                    catch (Exception e) {
130                            _log.error(e, e);
131                    }
132    
133                    if (previousApplicationContext == null) {
134                            servletContext.removeAttribute(
135                                    WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
136                    }
137                    else {
138                            servletContext.setAttribute(
139                                    WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
140                                    previousApplicationContext);
141                    }
142            }
143    
144            @Override
145            protected ContextLoader createContextLoader() {
146                    return new PortletContextLoader();
147            }
148    
149            private static Log _log = LogFactoryUtil.getLog(
150                    PortletContextLoaderListener.class);
151    
152    }