1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.core.util;
17
18 import org.apache.log4j.Logger;
19 import org.kuali.rice.core.framework.util.ApplicationThreadLocal;
20 import org.springframework.beans.BeansException;
21 import org.springframework.context.ApplicationContext;
22 import org.springframework.context.ApplicationContextAware;
23 import org.springframework.context.ApplicationListener;
24 import org.springframework.context.ConfigurableApplicationContext;
25 import org.springframework.context.event.ContextClosedEvent;
26
27
28
29
30 public class ApplicationThreadLocalCleaner implements ApplicationContextAware {
31 private static final Logger LOG = Logger.getLogger(ApplicationThreadLocalCleaner.class);
32
33 @Override
34 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
35
36 if (applicationContext instanceof ConfigurableApplicationContext) {
37 ConfigurableApplicationContext context = (ConfigurableApplicationContext) applicationContext;
38 context.addApplicationListener(new ApplicationListener<ContextClosedEvent>() {
39 @Override
40 public void onApplicationEvent(ContextClosedEvent e) {
41 LOG.info("Context '" + e.getApplicationContext().getDisplayName() + "' closed, removing registered ApplicationThreadLocals");
42 if (!ApplicationThreadLocal.clear()) {
43 LOG.error("Error(s) occurred removing registered ApplicationThreadLocals");
44 }
45 }
46 });
47 }
48 }
49 }