001    /*
002    * Copyright 2004 The Apache Software Foundation
003    *
004    * Licensed under the Apache License, Version 2.0 (the "License");
005    * you may not use this file except in compliance with the License.
006    * You may obtain a copy of the License at
007    *
008    *     http://www.apache.org/licenses/LICENSE-2.0
009    *
010    * Unless required by applicable law or agreed to in writing, software
011    * distributed under the License is distributed on an "AS IS" BASIS,
012    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013    * See the License for the specific language governing permissions and
014    * limitations under the License.
015    */
016    
017    package listeners;
018    
019    
020    import javax.servlet.ServletContext;
021    import javax.servlet.ServletContextEvent;
022    import javax.servlet.ServletContextListener;
023    import javax.servlet.http.HttpSessionAttributeListener;
024    import javax.servlet.http.HttpSessionBindingEvent;
025    import javax.servlet.http.HttpSessionEvent;
026    import javax.servlet.http.HttpSessionListener;
027    
028    
029    /**
030     * Example listener for context-related application events, which were
031     * introduced in the 2.3 version of the Servlet API.  This listener
032     * merely documents the occurrence of such events in the application log
033     * associated with our servlet context.
034     *
035     * @author Craig R. McClanahan
036     * @version $Revision: 267129 $ $Date: 2004-03-18 08:40:35 -0800 (Thu, 18 Mar 2004) $
037     */
038    
039    public final class SessionListener
040        implements ServletContextListener,
041                   HttpSessionAttributeListener, HttpSessionListener {
042    
043    
044        // ----------------------------------------------------- Instance Variables
045    
046    
047        /**
048         * The servlet context with which we are associated.
049         */
050        private ServletContext context = null;
051    
052    
053        // --------------------------------------------------------- Public Methods
054    
055    
056        /**
057         * Record the fact that a servlet context attribute was added.
058         *
059         * @param event The session attribute event
060         */
061        public void attributeAdded(HttpSessionBindingEvent event) {
062    
063            log("attributeAdded('" + event.getSession().getId() + "', '" +
064                event.getName() + "', '" + event.getValue() + "')");
065    
066        }
067    
068    
069        /**
070         * Record the fact that a servlet context attribute was removed.
071         *
072         * @param event The session attribute event
073         */
074        public void attributeRemoved(HttpSessionBindingEvent event) {
075    
076            log("attributeRemoved('" + event.getSession().getId() + "', '" +
077                event.getName() + "', '" + event.getValue() + "')");
078    
079        }
080    
081    
082        /**
083         * Record the fact that a servlet context attribute was replaced.
084         *
085         * @param event The session attribute event
086         */
087        public void attributeReplaced(HttpSessionBindingEvent event) {
088    
089            log("attributeReplaced('" + event.getSession().getId() + "', '" +
090                event.getName() + "', '" + event.getValue() + "')");
091    
092        }
093    
094    
095        /**
096         * Record the fact that this web application has been destroyed.
097         *
098         * @param event The servlet context event
099         */
100        public void contextDestroyed(ServletContextEvent event) {
101    
102            log("contextDestroyed()");
103            this.context = null;
104    
105        }
106    
107    
108        /**
109         * Record the fact that this web application has been initialized.
110         *
111         * @param event The servlet context event
112         */
113        public void contextInitialized(ServletContextEvent event) {
114    
115            this.context = event.getServletContext();
116            log("contextInitialized()");
117    
118        }
119    
120    
121        /**
122         * Record the fact that a session has been created.
123         *
124         * @param event The session event
125         */
126        public void sessionCreated(HttpSessionEvent event) {
127    
128            log("sessionCreated('" + event.getSession().getId() + "')");
129    
130        }
131    
132    
133        /**
134         * Record the fact that a session has been destroyed.
135         *
136         * @param event The session event
137         */
138        public void sessionDestroyed(HttpSessionEvent event) {
139    
140            log("sessionDestroyed('" + event.getSession().getId() + "')");
141    
142        }
143    
144    
145        // -------------------------------------------------------- Private Methods
146    
147    
148        /**
149         * Log a message to the servlet context application log.
150         *
151         * @param message Message to be logged
152         */
153        private void log(String message) {
154    
155            if (context != null)
156                context.log("SessionListener: " + message);
157            else
158                System.out.println("SessionListener: " + message);
159    
160        }
161    
162    
163        /**
164         * Log a message and associated exception to the servlet context
165         * application log.
166         *
167         * @param message Message to be logged
168         * @param throwable Exception to be logged
169         */
170        private void log(String message, Throwable throwable) {
171    
172            if (context != null)
173                context.log("SessionListener: " + message, throwable);
174            else {
175                System.out.println("SessionListener: " + message);
176                throwable.printStackTrace(System.out);
177            }
178    
179        }
180    
181    
182    }