1 | /* |
---|
2 | * Licensed to the Apache Software Foundation (ASF) under one or more |
---|
3 | * contributor license agreements. See the NOTICE file distributed with |
---|
4 | * this work for additional information regarding copyright ownership. |
---|
5 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
---|
6 | * (the "License"); you may not use this file except in compliance with |
---|
7 | * the License. You may obtain a copy of the License at |
---|
8 | * |
---|
9 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
10 | * |
---|
11 | * Unless required by applicable law or agreed to in writing, software |
---|
12 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
14 | * See the License for the specific language governing permissions and |
---|
15 | * limitations under the License. |
---|
16 | */ |
---|
17 | |
---|
18 | package listeners; |
---|
19 | |
---|
20 | |
---|
21 | import javax.servlet.ServletContext; |
---|
22 | import javax.servlet.ServletContextEvent; |
---|
23 | import javax.servlet.ServletContextListener; |
---|
24 | import javax.servlet.http.HttpSessionAttributeListener; |
---|
25 | import javax.servlet.http.HttpSessionBindingEvent; |
---|
26 | import javax.servlet.http.HttpSessionEvent; |
---|
27 | import javax.servlet.http.HttpSessionListener; |
---|
28 | |
---|
29 | |
---|
30 | /** |
---|
31 | * Example listener for context-related application events, which were |
---|
32 | * introduced in the 2.3 version of the Servlet API. This listener |
---|
33 | * merely documents the occurrence of such events in the application log |
---|
34 | * associated with our servlet context. |
---|
35 | * |
---|
36 | * @author Craig R. McClanahan |
---|
37 | * @version $Revision: 500674 $ $Date: 2007-01-28 00:15:00 +0100 (Sun, 28 Jan 2007) $ |
---|
38 | */ |
---|
39 | |
---|
40 | public final class SessionListener |
---|
41 | implements ServletContextListener, |
---|
42 | HttpSessionAttributeListener, HttpSessionListener { |
---|
43 | |
---|
44 | |
---|
45 | // ----------------------------------------------------- Instance Variables |
---|
46 | |
---|
47 | |
---|
48 | /** |
---|
49 | * The servlet context with which we are associated. |
---|
50 | */ |
---|
51 | private ServletContext context = null; |
---|
52 | |
---|
53 | |
---|
54 | // --------------------------------------------------------- Public Methods |
---|
55 | |
---|
56 | |
---|
57 | /** |
---|
58 | * Record the fact that a servlet context attribute was added. |
---|
59 | * |
---|
60 | * @param event The session attribute event |
---|
61 | */ |
---|
62 | public void attributeAdded(HttpSessionBindingEvent event) { |
---|
63 | |
---|
64 | log("attributeAdded('" + event.getSession().getId() + "', '" + |
---|
65 | event.getName() + "', '" + event.getValue() + "')"); |
---|
66 | |
---|
67 | } |
---|
68 | |
---|
69 | |
---|
70 | /** |
---|
71 | * Record the fact that a servlet context attribute was removed. |
---|
72 | * |
---|
73 | * @param event The session attribute event |
---|
74 | */ |
---|
75 | public void attributeRemoved(HttpSessionBindingEvent event) { |
---|
76 | |
---|
77 | log("attributeRemoved('" + event.getSession().getId() + "', '" + |
---|
78 | event.getName() + "', '" + event.getValue() + "')"); |
---|
79 | |
---|
80 | } |
---|
81 | |
---|
82 | |
---|
83 | /** |
---|
84 | * Record the fact that a servlet context attribute was replaced. |
---|
85 | * |
---|
86 | * @param event The session attribute event |
---|
87 | */ |
---|
88 | public void attributeReplaced(HttpSessionBindingEvent event) { |
---|
89 | |
---|
90 | log("attributeReplaced('" + event.getSession().getId() + "', '" + |
---|
91 | event.getName() + "', '" + event.getValue() + "')"); |
---|
92 | |
---|
93 | } |
---|
94 | |
---|
95 | |
---|
96 | /** |
---|
97 | * Record the fact that this web application has been destroyed. |
---|
98 | * |
---|
99 | * @param event The servlet context event |
---|
100 | */ |
---|
101 | public void contextDestroyed(ServletContextEvent event) { |
---|
102 | |
---|
103 | log("contextDestroyed()"); |
---|
104 | this.context = null; |
---|
105 | |
---|
106 | } |
---|
107 | |
---|
108 | |
---|
109 | /** |
---|
110 | * Record the fact that this web application has been initialized. |
---|
111 | * |
---|
112 | * @param event The servlet context event |
---|
113 | */ |
---|
114 | public void contextInitialized(ServletContextEvent event) { |
---|
115 | |
---|
116 | this.context = event.getServletContext(); |
---|
117 | log("contextInitialized()"); |
---|
118 | |
---|
119 | } |
---|
120 | |
---|
121 | |
---|
122 | /** |
---|
123 | * Record the fact that a session has been created. |
---|
124 | * |
---|
125 | * @param event The session event |
---|
126 | */ |
---|
127 | public void sessionCreated(HttpSessionEvent event) { |
---|
128 | |
---|
129 | log("sessionCreated('" + event.getSession().getId() + "')"); |
---|
130 | |
---|
131 | } |
---|
132 | |
---|
133 | |
---|
134 | /** |
---|
135 | * Record the fact that a session has been destroyed. |
---|
136 | * |
---|
137 | * @param event The session event |
---|
138 | */ |
---|
139 | public void sessionDestroyed(HttpSessionEvent event) { |
---|
140 | |
---|
141 | log("sessionDestroyed('" + event.getSession().getId() + "')"); |
---|
142 | |
---|
143 | } |
---|
144 | |
---|
145 | |
---|
146 | // -------------------------------------------------------- Private Methods |
---|
147 | |
---|
148 | |
---|
149 | /** |
---|
150 | * Log a message to the servlet context application log. |
---|
151 | * |
---|
152 | * @param message Message to be logged |
---|
153 | */ |
---|
154 | private void log(String message) { |
---|
155 | |
---|
156 | if (context != null) |
---|
157 | context.log("SessionListener: " + message); |
---|
158 | else |
---|
159 | System.out.println("SessionListener: " + message); |
---|
160 | |
---|
161 | } |
---|
162 | |
---|
163 | } |
---|