001
014
015 package com.liferay.portal.spring.context;
016
017 import com.liferay.portal.bean.BeanLocatorImpl;
018 import com.liferay.portal.cache.ehcache.ClearEhcacheThreadUtil;
019 import com.liferay.portal.kernel.bean.PortalBeanLocatorUtil;
020 import com.liferay.portal.kernel.cache.CacheRegistryUtil;
021 import com.liferay.portal.kernel.cache.MultiVMPoolUtil;
022 import com.liferay.portal.kernel.cache.SingleVMPoolUtil;
023 import com.liferay.portal.kernel.cache.ThreadLocalCacheManager;
024 import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
025 import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
026 import com.liferay.portal.kernel.deploy.hot.HotDeployUtil;
027 import com.liferay.portal.kernel.log.Log;
028 import com.liferay.portal.kernel.log.LogFactoryUtil;
029 import com.liferay.portal.kernel.portlet.PortletBagPool;
030 import com.liferay.portal.kernel.process.ClassPathUtil;
031 import com.liferay.portal.kernel.servlet.DirectServletRegistryUtil;
032 import com.liferay.portal.kernel.servlet.ServletContextPool;
033 import com.liferay.portal.kernel.util.CharBufferPool;
034 import com.liferay.portal.kernel.util.ClearThreadLocalUtil;
035 import com.liferay.portal.kernel.util.ClearTimerThreadUtil;
036 import com.liferay.portal.kernel.util.InstancePool;
037 import com.liferay.portal.kernel.util.MethodCache;
038 import com.liferay.portal.kernel.util.ReferenceRegistry;
039 import com.liferay.portal.kernel.util.ReflectionUtil;
040 import com.liferay.portal.kernel.webcache.WebCachePoolUtil;
041 import com.liferay.portal.module.framework.ModuleFrameworkUtil;
042 import com.liferay.portal.security.lang.PortalSecurityManagerThreadLocal;
043 import com.liferay.portal.security.pacl.PACLClassLoaderUtil;
044 import com.liferay.portal.security.permission.PermissionCacheUtil;
045 import com.liferay.portal.servlet.filters.cache.CacheUtil;
046 import com.liferay.portal.util.InitUtil;
047 import com.liferay.portal.util.PropsValues;
048 import com.liferay.portal.util.WebAppPool;
049 import com.liferay.portal.velocity.LiferayResourceCacheUtil;
050 import com.liferay.portlet.PortletContextBagPool;
051 import com.liferay.portlet.wiki.util.WikiCacheUtil;
052
053 import java.beans.PropertyDescriptor;
054
055 import java.lang.reflect.Field;
056
057 import java.util.Map;
058
059 import javax.servlet.ServletContext;
060 import javax.servlet.ServletContextEvent;
061
062 import org.springframework.beans.CachedIntrospectionResults;
063 import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
064 import org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory;
065 import org.springframework.context.ApplicationContext;
066 import org.springframework.web.context.ContextLoader;
067 import org.springframework.web.context.ContextLoaderListener;
068
069
074 public class PortalContextLoaderListener extends ContextLoaderListener {
075
076 @Override
077 public void contextDestroyed(ServletContextEvent servletContextEvent) {
078 PortalContextLoaderLifecycleThreadLocal.setDestroying(true);
079
080 ThreadLocalCacheManager.destroy();
081
082 try {
083 ClearThreadLocalUtil.clearThreadLocal();
084 }
085 catch (Exception e) {
086 _log.error(e, e);
087 }
088
089 try {
090 ClearTimerThreadUtil.clearTimerThread();
091 }
092 catch (Exception e) {
093 _log.error(e, e);
094 }
095
096 try {
097 ClearEhcacheThreadUtil.clearEhcacheReplicationThread();
098 }
099 catch (Exception e) {
100 _log.error(e, e);
101 }
102
103 try {
104 DirectServletRegistryUtil.clearServlets();
105 }
106 catch (Exception e) {
107 _log.error(e, e);
108 }
109
110 try {
111 HotDeployUtil.reset();
112 }
113 catch (Exception e) {
114 _log.error(e, e);
115 }
116
117 try {
118 ModuleFrameworkUtil.stopRuntime();
119 }
120 catch (Exception e) {
121 _log.error(e, e);
122 }
123
124 try {
125 super.contextDestroyed(servletContextEvent);
126
127 try {
128 ModuleFrameworkUtil.stopFramework();
129 }
130 catch (Exception e) {
131 _log.error(e, e);
132 }
133 }
134 finally {
135 PortalContextLoaderLifecycleThreadLocal.setDestroying(false);
136 }
137 }
138
139 @Override
140 public void contextInitialized(ServletContextEvent servletContextEvent) {
141 PortalSecurityManagerThreadLocal.setEnabled(false);
142
143 InstancePool.reset();
144 MethodCache.reset();
145 PortletBagPool.reset();
146
147 ReferenceRegistry.releaseReferences();
148
149 InitUtil.init();
150
151 ServletContext servletContext = servletContextEvent.getServletContext();
152
153 ClassPathUtil.initializeClassPaths(servletContext);
154
155 CacheRegistryUtil.clear();
156 CharBufferPool.cleanUp();
157 PortletContextBagPool.clear();
158 WebAppPool.clear();
159
160 if (PropsValues.MODULE_FRAMEWORK_ENABLED) {
161 try {
162 ModuleFrameworkUtil.startFramework();
163 }
164 catch (Exception e) {
165 _log.error(e, e);
166 }
167 }
168
169 PortalContextLoaderLifecycleThreadLocal.setInitializing(true);
170
171 try {
172 super.contextInitialized(servletContextEvent);
173 }
174 finally {
175 PortalContextLoaderLifecycleThreadLocal.setInitializing(false);
176
177 PortalSecurityManagerThreadLocal.setEnabled(true);
178 }
179
180 FinderCacheUtil.clearCache();
181 FinderCacheUtil.clearLocalCache();
182 EntityCacheUtil.clearCache();
183 EntityCacheUtil.clearLocalCache();
184 LiferayResourceCacheUtil.clear();
185 PermissionCacheUtil.clearCache();
186 PermissionCacheUtil.clearLocalCache();
187 WikiCacheUtil.clearCache(0);
188
189 ServletContextPool.clear();
190
191 CacheUtil.clearCache();
192 MultiVMPoolUtil.clear();
193 SingleVMPoolUtil.clear();
194 WebCachePoolUtil.clear();
195
196 ApplicationContext applicationContext =
197 ContextLoader.getCurrentWebApplicationContext();
198
199 ClassLoader portalClassLoader =
200 PACLClassLoaderUtil.getPortalClassLoader();
201
202 BeanLocatorImpl beanLocatorImpl = new BeanLocatorImpl(
203 portalClassLoader, applicationContext);
204
205 beanLocatorImpl.setPACLWrapPersistence(true);
206
207 PortalBeanLocatorUtil.setBeanLocator(beanLocatorImpl);
208
209 ClassLoader classLoader = portalClassLoader;
210
211 while (classLoader != null) {
212 CachedIntrospectionResults.clearClassLoader(classLoader);
213
214 classLoader = classLoader.getParent();
215 }
216
217 AutowireCapableBeanFactory autowireCapableBeanFactory =
218 applicationContext.getAutowireCapableBeanFactory();
219
220 clearFilteredPropertyDescriptorsCache(autowireCapableBeanFactory);
221
222 if (PropsValues.MODULE_FRAMEWORK_ENABLED) {
223 try {
224 ModuleFrameworkUtil.registerContext(applicationContext);
225 ModuleFrameworkUtil.registerContext(servletContext);
226
227 ModuleFrameworkUtil.startRuntime();
228 }
229 catch (Exception e) {
230 _log.error(e, e);
231 }
232 }
233 }
234
235 protected void clearFilteredPropertyDescriptorsCache(
236 AutowireCapableBeanFactory autowireCapableBeanFactory) {
237
238 try {
239 Map<Class<?>, PropertyDescriptor[]>
240 filteredPropertyDescriptorsCache =
241 (Map<Class<?>, PropertyDescriptor[]>)
242 _filteredPropertyDescriptorsCacheField.get(
243 autowireCapableBeanFactory);
244
245 filteredPropertyDescriptorsCache.clear();
246 }
247 catch (Exception e) {
248 _log.error(e, e);
249 }
250 }
251
252 private static Log _log = LogFactoryUtil.getLog(
253 PortalContextLoaderListener.class);
254
255 private static Field _filteredPropertyDescriptorsCacheField;
256
257 static {
258 try {
259 _filteredPropertyDescriptorsCacheField =
260 ReflectionUtil.getDeclaredField(
261 AbstractAutowireCapableBeanFactory.class,
262 "filteredPropertyDescriptorsCache");
263 }
264 catch (Exception e) {
265 _log.error(e, e);
266 }
267 }
268
269 }