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