001    /**
002     * Copyright (c) 2000-present 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.kernel.util.ClassLoaderUtil;
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            @Override
037            public void invokeDeploy(HotDeployEvent hotDeployEvent)
038                    throws HotDeployException {
039    
040                    try {
041                            doInvokeDeploy(hotDeployEvent);
042                    }
043                    catch (Throwable t) {
044                            throwHotDeployException(
045                                    hotDeployEvent, "Error initializing Spring for ", t);
046                    }
047            }
048    
049            @Override
050            public void invokeUndeploy(HotDeployEvent hotDeployEvent)
051                    throws HotDeployException {
052    
053                    try {
054                            doInvokeUndeploy(hotDeployEvent);
055                    }
056                    catch (Throwable t) {
057                            throwHotDeployException(
058                                    hotDeployEvent, "Error uninitializing Spring for ", t);
059                    }
060            }
061    
062            protected void doInvokeDeploy(HotDeployEvent hotDeployEvent)
063                    throws Exception {
064    
065                    ServletContext servletContext = hotDeployEvent.getServletContext();
066    
067                    String servletContextName = servletContext.getServletContextName();
068    
069                    ContextLoaderListener contextLoaderListener =
070                            new PortletContextLoaderListener();
071    
072                    ClassLoader contextClassLoader =
073                            ClassLoaderUtil.getContextClassLoader();
074    
075                    try {
076                            ClassLoaderUtil.setContextClassLoader(
077                                    ClassLoaderUtil.getPortalClassLoader());
078    
079                            contextLoaderListener.contextInitialized(
080                                    new ServletContextEvent(servletContext));
081                    }
082                    finally {
083                            ClassLoaderUtil.setContextClassLoader(contextClassLoader);
084                    }
085    
086                    _contextLoaderListeners.put(servletContextName, contextLoaderListener);
087            }
088    
089            protected void doInvokeUndeploy(HotDeployEvent hotDeployEvent)
090                    throws Exception {
091    
092                    ServletContext servletContext = hotDeployEvent.getServletContext();
093    
094                    String servletContextName = servletContext.getServletContextName();
095    
096                    ContextLoaderListener contextLoaderListener =
097                            _contextLoaderListeners.remove(servletContextName);
098    
099                    if (contextLoaderListener == null) {
100                            return;
101                    }
102    
103                    ClassLoader contextClassLoader =
104                            ClassLoaderUtil.getContextClassLoader();
105    
106                    try {
107                            ClassLoaderUtil.setContextClassLoader(
108                                    ClassLoaderUtil.getPortalClassLoader());
109    
110                            contextLoaderListener.contextDestroyed(
111                                    new ServletContextEvent(servletContext));
112                    }
113                    finally {
114                            ClassLoaderUtil.setContextClassLoader(contextClassLoader);
115                    }
116            }
117    
118            private static final Map<String, ContextLoaderListener>
119                    _contextLoaderListeners = new HashMap<>();
120    
121    }