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.BeanLocator;
020 import com.liferay.portal.kernel.bean.PortalBeanLocatorUtil;
021 import com.liferay.portal.kernel.cache.CacheRegistryUtil;
022 import com.liferay.portal.kernel.cache.MultiVMPoolUtil;
023 import com.liferay.portal.kernel.cache.SingleVMPoolUtil;
024 import com.liferay.portal.kernel.cache.ThreadLocalCacheManager;
025 import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
026 import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
027 import com.liferay.portal.kernel.deploy.hot.HotDeployUtil;
028 import com.liferay.portal.kernel.log.Log;
029 import com.liferay.portal.kernel.log.LogFactoryUtil;
030 import com.liferay.portal.kernel.portlet.PortletBagPool;
031 import com.liferay.portal.kernel.process.ClassPathUtil;
032 import com.liferay.portal.kernel.servlet.DirectServletRegistry;
033 import com.liferay.portal.kernel.servlet.ServletContextPool;
034 import com.liferay.portal.kernel.util.CharBufferPool;
035 import com.liferay.portal.kernel.util.ClearThreadLocalUtil;
036 import com.liferay.portal.kernel.util.ClearTimerThreadUtil;
037 import com.liferay.portal.kernel.util.InstancePool;
038 import com.liferay.portal.kernel.util.MethodCache;
039 import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
040 import com.liferay.portal.kernel.util.ReferenceRegistry;
041 import com.liferay.portal.kernel.util.ReflectionUtil;
042 import com.liferay.portal.kernel.webcache.WebCachePoolUtil;
043 import com.liferay.portal.osgi.service.OSGiServiceUtil;
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 contextInitialized(ServletContextEvent servletContextEvent) {
078 HotDeployUtil.reset();
079 InstancePool.reset();
080 MethodCache.reset();
081 PortletBagPool.reset();
082
083 ReferenceRegistry.releaseReferences();
084
085 InitUtil.init();
086
087 ServletContext servletContext = servletContextEvent.getServletContext();
088
089 ClassPathUtil.initializeClassPaths(servletContext);
090
091 DirectServletRegistry.clearServlets();
092
093 CacheRegistryUtil.clear();
094 CharBufferPool.cleanUp();
095 PortletContextBagPool.clear();
096 WebAppPool.clear();
097
098 if (PropsValues.OSGI_ENABLED) {
099 try {
100 OSGiServiceUtil.init();
101 }
102 catch (Exception e) {
103 _log.error(e, e);
104 }
105 }
106
107 PortalContextLoaderLifecycleThreadLocal.setInitializing(true);
108
109 try {
110 super.contextInitialized(servletContextEvent);
111 }
112 finally {
113 PortalContextLoaderLifecycleThreadLocal.setInitializing(false);
114 }
115
116 FinderCacheUtil.clearCache();
117 FinderCacheUtil.clearLocalCache();
118 EntityCacheUtil.clearCache();
119 EntityCacheUtil.clearLocalCache();
120 LiferayResourceCacheUtil.clear();
121 PermissionCacheUtil.clearCache();
122 PermissionCacheUtil.clearLocalCache();
123 WikiCacheUtil.clearCache(0);
124
125 ServletContextPool.clear();
126
127 CacheUtil.clearCache();
128 MultiVMPoolUtil.clear();
129 SingleVMPoolUtil.clear();
130 WebCachePoolUtil.clear();
131
132 ApplicationContext applicationContext =
133 ContextLoader.getCurrentWebApplicationContext();
134
135 ClassLoader portalClassLoader = PortalClassLoaderUtil.getClassLoader();
136
137 BeanLocator beanLocator = new BeanLocatorImpl(
138 portalClassLoader, applicationContext);
139
140 PortalBeanLocatorUtil.setBeanLocator(beanLocator);
141
142 ClassLoader classLoader = portalClassLoader;
143
144 while (classLoader != null) {
145 CachedIntrospectionResults.clearClassLoader(classLoader);
146
147 classLoader = classLoader.getParent();
148 }
149
150 AutowireCapableBeanFactory autowireCapableBeanFactory =
151 applicationContext.getAutowireCapableBeanFactory();
152
153 clearFilteredPropertyDescriptorsCache(autowireCapableBeanFactory);
154
155 if (PropsValues.OSGI_ENABLED) {
156 try {
157 OSGiServiceUtil.registerContext(applicationContext);
158
159 OSGiServiceUtil.start();
160 }
161 catch (Exception e) {
162 _log.error(e, e);
163 }
164 }
165 }
166
167 @Override
168 public void contextDestroyed(ServletContextEvent event) {
169 PortalContextLoaderLifecycleThreadLocal.setDestroying(true);
170
171 ThreadLocalCacheManager.destroy();
172
173 try {
174 ClearThreadLocalUtil.clearThreadLocal();
175 }
176 catch (Exception e) {
177 _log.error(e, e);
178 }
179
180 try {
181 ClearTimerThreadUtil.clearTimerThread();
182 }
183 catch (Exception e) {
184 _log.error(e, e);
185 }
186
187 try {
188 ClearEhcacheThreadUtil.clearEhcacheReplicationThread();
189 }
190 catch (Exception e) {
191 _log.error(e, e);
192 }
193
194 try {
195 OSGiServiceUtil.stopRuntime();
196 }
197 catch (Exception e) {
198 _log.error(e, e);
199 }
200
201 try {
202 super.contextDestroyed(event);
203
204 try {
205 OSGiServiceUtil.stopFramework();
206 }
207 catch (Exception e) {
208 _log.error(e, e);
209 }
210 }
211 finally {
212 PortalContextLoaderLifecycleThreadLocal.setDestroying(false);
213 }
214 }
215
216 protected void clearFilteredPropertyDescriptorsCache(
217 AutowireCapableBeanFactory autowireCapableBeanFactory) {
218
219 try {
220 Map<Class<?>, PropertyDescriptor[]>
221 filteredPropertyDescriptorsCache =
222 (Map<Class<?>, PropertyDescriptor[]>)
223 _filteredPropertyDescriptorsCacheField.get(
224 autowireCapableBeanFactory);
225
226 filteredPropertyDescriptorsCache.clear();
227 }
228 catch (Exception e) {
229 _log.error(e, e);
230 }
231 }
232
233 private static Field _filteredPropertyDescriptorsCacheField;
234
235 private static Log _log = LogFactoryUtil.getLog(
236 PortalContextLoaderListener.class);
237
238 static {
239 try {
240 _filteredPropertyDescriptorsCacheField =
241 ReflectionUtil.getDeclaredField(
242 AbstractAutowireCapableBeanFactory.class,
243 "filteredPropertyDescriptorsCache");
244 }
245 catch (Exception e) {
246 _log.error(e, e);
247 }
248 }
249
250 }