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.db.DBFactoryUtil;
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.DeployManagerUtil;
028 import com.liferay.portal.kernel.deploy.hot.HotDeployUtil;
029 import com.liferay.portal.kernel.log.Log;
030 import com.liferay.portal.kernel.log.LogFactoryUtil;
031 import com.liferay.portal.kernel.portlet.PortletBagPool;
032 import com.liferay.portal.kernel.process.ClassPathUtil;
033 import com.liferay.portal.kernel.servlet.DirectServletRegistryUtil;
034 import com.liferay.portal.kernel.servlet.ServletContextPool;
035 import com.liferay.portal.kernel.template.TemplateResourceLoaderUtil;
036 import com.liferay.portal.kernel.util.CharBufferPool;
037 import com.liferay.portal.kernel.util.ClearThreadLocalUtil;
038 import com.liferay.portal.kernel.util.ClearTimerThreadUtil;
039 import com.liferay.portal.kernel.util.InstancePool;
040 import com.liferay.portal.kernel.util.JavaConstants;
041 import com.liferay.portal.kernel.util.MethodCache;
042 import com.liferay.portal.kernel.util.PortalLifecycleUtil;
043 import com.liferay.portal.kernel.util.ReferenceRegistry;
044 import com.liferay.portal.kernel.util.ReflectionUtil;
045 import com.liferay.portal.kernel.webcache.WebCachePoolUtil;
046 import com.liferay.portal.module.framework.ModuleFrameworkUtilAdapter;
047 import com.liferay.portal.security.lang.SecurityManagerUtil;
048 import com.liferay.portal.security.permission.PermissionCacheUtil;
049 import com.liferay.portal.servlet.filters.cache.CacheUtil;
050 import com.liferay.portal.spring.bean.BeanReferenceRefreshUtil;
051 import com.liferay.portal.util.ClassLoaderUtil;
052 import com.liferay.portal.util.InitUtil;
053 import com.liferay.portal.util.PropsValues;
054 import com.liferay.portal.util.WebAppPool;
055 import com.liferay.portlet.PortletContextBagPool;
056 import com.liferay.portlet.wiki.util.WikiCacheUtil;
057
058 import java.beans.PropertyDescriptor;
059
060 import java.io.File;
061
062 import java.lang.reflect.Field;
063
064 import java.util.Map;
065
066 import javax.servlet.ServletContext;
067 import javax.servlet.ServletContextEvent;
068
069 import org.springframework.beans.CachedIntrospectionResults;
070 import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
071 import org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory;
072 import org.springframework.context.ApplicationContext;
073 import org.springframework.web.context.ContextLoader;
074 import org.springframework.web.context.ContextLoaderListener;
075
076
081 public class PortalContextLoaderListener extends ContextLoaderListener {
082
083 public static String getPortalServletContextPath() {
084 return _portalServletContextPath;
085 }
086
087 @Override
088 public void contextDestroyed(ServletContextEvent servletContextEvent) {
089 PortalContextLoaderLifecycleThreadLocal.setDestroying(true);
090
091 ThreadLocalCacheManager.destroy();
092
093 try {
094 ClearThreadLocalUtil.clearThreadLocal();
095 }
096 catch (Exception e) {
097 _log.error(e, e);
098 }
099
100 try {
101 ClearTimerThreadUtil.clearTimerThread();
102 }
103 catch (Exception e) {
104 _log.error(e, e);
105 }
106
107 try {
108 ClearEhcacheThreadUtil.clearEhcacheReplicationThread();
109 }
110 catch (Exception e) {
111 _log.error(e, e);
112 }
113
114 try {
115 DirectServletRegistryUtil.clearServlets();
116 }
117 catch (Exception e) {
118 _log.error(e, e);
119 }
120
121 try {
122 HotDeployUtil.reset();
123 }
124 catch (Exception e) {
125 _log.error(e, e);
126 }
127
128 try {
129 ModuleFrameworkUtilAdapter.stopRuntime();
130 }
131 catch (Exception e) {
132 _log.error(e, e);
133 }
134
135 try {
136 PortalLifecycleUtil.reset();
137 }
138 catch (Exception e) {
139 _log.error(e, e);
140 }
141
142 try {
143 super.contextDestroyed(servletContextEvent);
144
145 try {
146 ModuleFrameworkUtilAdapter.stopFramework();
147 }
148 catch (Exception e) {
149 _log.error(e, e);
150 }
151 }
152 finally {
153 PortalContextLoaderLifecycleThreadLocal.setDestroying(false);
154 }
155 }
156
157 @Override
158 public void contextInitialized(ServletContextEvent servletContextEvent) {
159 DBFactoryUtil.reset();
160 DeployManagerUtil.reset();
161 InstancePool.reset();
162 MethodCache.reset();
163 PortalBeanLocatorUtil.reset();
164 PortletBagPool.reset();
165
166 ReferenceRegistry.releaseReferences();
167
168 InitUtil.init();
169
170 ServletContext servletContext = servletContextEvent.getServletContext();
171
172 _portalServletContextPath = servletContext.getContextPath();
173
174 ClassPathUtil.initializeClassPaths(servletContext);
175
176 CacheRegistryUtil.clear();
177 CharBufferPool.cleanUp();
178 PortletContextBagPool.clear();
179 WebAppPool.clear();
180
181 File tempDir = (File)servletContext.getAttribute(
182 JavaConstants.JAVAX_SERVLET_CONTEXT_TEMPDIR);
183
184 PropsValues.LIFERAY_WEB_PORTAL_CONTEXT_TEMPDIR =
185 tempDir.getAbsolutePath();
186
187 try {
188 ModuleFrameworkUtilAdapter.startFramework();
189 }
190 catch (Exception e) {
191 throw new RuntimeException(e);
192 }
193
194 SecurityManagerUtil.applySmartStrategy();
195
196 PortalContextLoaderLifecycleThreadLocal.setInitializing(true);
197
198 try {
199 super.contextInitialized(servletContextEvent);
200 }
201 finally {
202 PortalContextLoaderLifecycleThreadLocal.setInitializing(false);
203 }
204
205 ApplicationContext applicationContext =
206 ContextLoader.getCurrentWebApplicationContext();
207
208 try {
209 BeanReferenceRefreshUtil.refresh(applicationContext);
210 }
211 catch (Exception e) {
212 _log.error(e, e);
213 }
214
215 FinderCacheUtil.clearCache();
216 FinderCacheUtil.clearLocalCache();
217 EntityCacheUtil.clearCache();
218 EntityCacheUtil.clearLocalCache();
219 PermissionCacheUtil.clearCache();
220 PermissionCacheUtil.clearLocalCache();
221 TemplateResourceLoaderUtil.clearCache();
222 WikiCacheUtil.clearCache(0);
223
224 ServletContextPool.clear();
225
226 CacheUtil.clearCache();
227 MultiVMPoolUtil.clear();
228 SingleVMPoolUtil.clear();
229 WebCachePoolUtil.clear();
230
231 ClassLoader portalClassLoader = ClassLoaderUtil.getPortalClassLoader();
232
233 BeanLocatorImpl beanLocatorImpl = new BeanLocatorImpl(
234 portalClassLoader, applicationContext);
235
236 PortalBeanLocatorUtil.setBeanLocator(beanLocatorImpl);
237
238 ClassLoader classLoader = portalClassLoader;
239
240 while (classLoader != null) {
241 CachedIntrospectionResults.clearClassLoader(classLoader);
242
243 classLoader = classLoader.getParent();
244 }
245
246 AutowireCapableBeanFactory autowireCapableBeanFactory =
247 applicationContext.getAutowireCapableBeanFactory();
248
249 clearFilteredPropertyDescriptorsCache(autowireCapableBeanFactory);
250
251 try {
252 ModuleFrameworkUtilAdapter.registerContext(applicationContext);
253 ModuleFrameworkUtilAdapter.registerContext(servletContext);
254
255 ModuleFrameworkUtilAdapter.startRuntime();
256 }
257 catch (Exception e) {
258 throw new RuntimeException(e);
259 }
260 }
261
262 protected void clearFilteredPropertyDescriptorsCache(
263 AutowireCapableBeanFactory autowireCapableBeanFactory) {
264
265 try {
266 Map<Class<?>, PropertyDescriptor[]>
267 filteredPropertyDescriptorsCache =
268 (Map<Class<?>, PropertyDescriptor[]>)
269 _filteredPropertyDescriptorsCacheField.get(
270 autowireCapableBeanFactory);
271
272 filteredPropertyDescriptorsCache.clear();
273 }
274 catch (Exception e) {
275 _log.error(e, e);
276 }
277 }
278
279 private static Log _log = LogFactoryUtil.getLog(
280 PortalContextLoaderListener.class);
281
282 private static Field _filteredPropertyDescriptorsCacheField;
283 private static String _portalServletContextPath = "/";
284
285 static {
286 try {
287 _filteredPropertyDescriptorsCacheField =
288 ReflectionUtil.getDeclaredField(
289 AbstractAutowireCapableBeanFactory.class,
290 "filteredPropertyDescriptorsCache");
291 }
292 catch (Exception e) {
293 _log.error(e, e);
294 }
295 }
296
297 }