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