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.deploy.hot;
016    
017    import com.liferay.portal.kernel.deploy.hot.BaseHotDeployListener;
018    import com.liferay.portal.kernel.deploy.hot.HotDeployEvent;
019    import com.liferay.portal.kernel.deploy.hot.HotDeployException;
020    import com.liferay.portal.security.pacl.PACLClassLoaderUtil;
021    import com.liferay.portal.spring.context.PortletContextLoaderListener;
022    
023    import java.util.HashMap;
024    import java.util.Map;
025    
026    import javax.servlet.ServletContext;
027    import javax.servlet.ServletContextEvent;
028    
029    import org.springframework.web.context.ContextLoaderListener;
030    
031    /**
032     * @author Brian Wing Shun Chan
033     */
034    public class SpringHotDeployListener extends BaseHotDeployListener {
035    
036            public void invokeDeploy(HotDeployEvent hotDeployEvent)
037                    throws HotDeployException {
038    
039                    try {
040                            doInvokeDeploy(hotDeployEvent);
041                    }
042                    catch (Throwable t) {
043                            throwHotDeployException(
044                                    hotDeployEvent, "Error initializing Spring for ", t);
045                    }
046            }
047    
048            public void invokeUndeploy(HotDeployEvent hotDeployEvent)
049                    throws HotDeployException {
050    
051                    try {
052                            doInvokeUndeploy(hotDeployEvent);
053                    }
054                    catch (Throwable t) {
055                            throwHotDeployException(
056                                    hotDeployEvent, "Error uninitializing Spring for ", t);
057                    }
058            }
059    
060            protected void doInvokeDeploy(HotDeployEvent hotDeployEvent)
061                    throws Exception {
062    
063                    ServletContext servletContext = hotDeployEvent.getServletContext();
064    
065                    String servletContextName = servletContext.getServletContextName();
066    
067                    ContextLoaderListener contextLoaderListener =
068                            new PortletContextLoaderListener();
069    
070                    ClassLoader contextClassLoader =
071                            PACLClassLoaderUtil.getContextClassLoader();
072    
073                    try {
074                            PACLClassLoaderUtil.setContextClassLoader(
075                                    PACLClassLoaderUtil.getPortalClassLoader());
076    
077                            contextLoaderListener.contextInitialized(
078                                    new ServletContextEvent(servletContext));
079                    }
080                    finally {
081                            PACLClassLoaderUtil.setContextClassLoader(contextClassLoader);
082                    }
083    
084                    _contextLoaderListeners.put(servletContextName, contextLoaderListener);
085            }
086    
087            protected void doInvokeUndeploy(HotDeployEvent hotDeployEvent)
088                    throws Exception {
089    
090                    ServletContext servletContext = hotDeployEvent.getServletContext();
091    
092                    String servletContextName = servletContext.getServletContextName();
093    
094                    ContextLoaderListener contextLoaderListener =
095                            _contextLoaderListeners.remove(servletContextName);
096    
097                    if (contextLoaderListener == null) {
098                            return;
099                    }
100    
101                    ClassLoader contextClassLoader =
102                            PACLClassLoaderUtil.getContextClassLoader();
103    
104                    try {
105                            PACLClassLoaderUtil.setContextClassLoader(
106                                    PACLClassLoaderUtil.getPortalClassLoader());
107    
108                            contextLoaderListener.contextDestroyed(
109                                    new ServletContextEvent(servletContext));
110                    }
111                    finally {
112                            PACLClassLoaderUtil.setContextClassLoader(contextClassLoader);
113                    }
114            }
115    
116            private static Map<String, ContextLoaderListener> _contextLoaderListeners =
117                    new HashMap<String, ContextLoaderListener>();
118    
119    }