1
2
3
4
5
6
7
8
9
10
11 package psiprobe;
12
13 import jakarta.servlet.ServletContextEvent;
14 import jakarta.servlet.ServletContextListener;
15 import jakarta.servlet.annotation.WebListener;
16
17 import javax.imageio.ImageIO;
18
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22
23
24
25
26
27
28
29 @WebListener
30 public class AwtAppContextClassloaderListener implements ServletContextListener {
31
32
33 private static final Logger logger =
34 LoggerFactory.getLogger(AwtAppContextClassloaderListener.class);
35
36
37
38
39
40
41
42 @Override
43 public void contextInitialized(ServletContextEvent sce) {
44 try {
45 final ClassLoader active = Thread.currentThread().getContextClassLoader();
46 try {
47
48 ClassLoader root = active;
49 while (root.getParent() != null) {
50 root = root.getParent();
51 }
52
53 Thread.currentThread().setContextClassLoader(root);
54
55
56
57
58
59
60
61 ImageIO.getCacheDirectory();
62 } finally {
63
64 Thread.currentThread().setContextClassLoader(active);
65 }
66 logger.info("AwtAppContextClassloaderListener Initialized");
67 } catch (Exception e) {
68 logger.error("Failed to address PermGen leak.", e);
69 }
70 }
71
72 @Override
73 public void contextDestroyed(ServletContextEvent sce) {
74
75 }
76
77 }