1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package listeners;
18
19
20 import javax.servlet.ServletContext;
21 import javax.servlet.ServletContextAttributeEvent;
22 import javax.servlet.ServletContextAttributeListener;
23 import javax.servlet.ServletContextEvent;
24 import javax.servlet.ServletContextListener;
25
26
27 /**
28 * Example listener for context-related application events, which were
29 * introduced in the 2.3 version of the Servlet API. This listener
30 * merely documents the occurrence of such events in the application log
31 * associated with our servlet context.
32 *
33 * @author Craig R. McClanahan
34 * @version $Revision: 267129 $ $Date: 2004-03-18 08:40:35 -0800 (Thu, 18 Mar 2004) $
35 */
36
37 public final class ContextListener
38 implements ServletContextAttributeListener, ServletContextListener {
39
40
41
42
43
44 /**
45 * The servlet context with which we are associated.
46 */
47 private ServletContext context = null;
48
49
50
51
52
53 /**
54 * Record the fact that a servlet context attribute was added.
55 *
56 * @param event The servlet context attribute event
57 */
58 public void attributeAdded(ServletContextAttributeEvent event) {
59
60 log("attributeAdded('" + event.getName() + "', '" +
61 event.getValue() + "')");
62
63 }
64
65
66 /**
67 * Record the fact that a servlet context attribute was removed.
68 *
69 * @param event The servlet context attribute event
70 */
71 public void attributeRemoved(ServletContextAttributeEvent event) {
72
73 log("attributeRemoved('" + event.getName() + "', '" +
74 event.getValue() + "')");
75
76 }
77
78
79 /**
80 * Record the fact that a servlet context attribute was replaced.
81 *
82 * @param event The servlet context attribute event
83 */
84 public void attributeReplaced(ServletContextAttributeEvent event) {
85
86 log("attributeReplaced('" + event.getName() + "', '" +
87 event.getValue() + "')");
88
89 }
90
91
92 /**
93 * Record the fact that this web application has been destroyed.
94 *
95 * @param event The servlet context event
96 */
97 public void contextDestroyed(ServletContextEvent event) {
98
99 log("contextDestroyed()");
100 this.context = null;
101
102 }
103
104
105 /**
106 * Record the fact that this web application has been initialized.
107 *
108 * @param event The servlet context event
109 */
110 public void contextInitialized(ServletContextEvent event) {
111
112 this.context = event.getServletContext();
113 log("contextInitialized()");
114
115 }
116
117
118
119
120
121 /**
122 * Log a message to the servlet context application log.
123 *
124 * @param message Message to be logged
125 */
126 private void log(String message) {
127
128 if (context != null)
129 context.log("ContextListener: " + message);
130 else
131 System.out.println("ContextListener: " + message);
132
133 }
134
135
136 /**
137 * Log a message and associated exception to the servlet context
138 * application log.
139 *
140 * @param message Message to be logged
141 * @param throwable Exception to be logged
142 */
143 private void log(String message, Throwable throwable) {
144
145 if (context != null)
146 context.log("ContextListener: " + message, throwable);
147 else {
148 System.out.println("ContextListener: " + message);
149 throwable.printStackTrace(System.out);
150 }
151
152 }
153
154
155 }