001
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
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 ClassLoaderUtil.getContextClassLoader();
072
073 try {
074 ClassLoaderUtil.setContextClassLoader(
075 ClassLoaderUtil.getPortalClassLoader());
076
077 contextLoaderListener.contextInitialized(
078 new ServletContextEvent(servletContext));
079 }
080 finally {
081 ClassLoaderUtil.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 ClassLoaderUtil.getContextClassLoader();
103
104 try {
105 ClassLoaderUtil.setContextClassLoader(
106 ClassLoaderUtil.getPortalClassLoader());
107
108 contextLoaderListener.contextDestroyed(
109 new ServletContextEvent(servletContext));
110 }
111 finally {
112 ClassLoaderUtil.setContextClassLoader(contextClassLoader);
113 }
114 }
115
116 private static Map<String, ContextLoaderListener> _contextLoaderListeners =
117 new HashMap<String, ContextLoaderListener>();
118
119 }