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    
019    package listeners;
020    
021    
022    import javax.servlet.ServletContext;
023    import javax.servlet.ServletContextAttributeEvent;
024    import javax.servlet.ServletContextAttributeListener;
025    import javax.servlet.ServletContextEvent;
026    import javax.servlet.ServletContextListener;
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: 514091 $ $Date: 2007-03-03 01:26:39 -0500 (Sat, 03 Mar 2007) $
037     */
038    
039    public final class ContextListener
040        implements ServletContextAttributeListener, ServletContextListener {
041    
042    
043        // ----------------------------------------------------- Instance Variables
044    
045    
046        /**
047         * The servlet context with which we are associated.
048         */
049        private ServletContext context = null;
050    
051    
052        // --------------------------------------------------------- Public Methods
053    
054    
055        /**
056         * Record the fact that a servlet context attribute was added.
057         *
058         * @param event The servlet context attribute event
059         */
060        public void attributeAdded(ServletContextAttributeEvent event) {
061    
062            log("attributeAdded('" + event.getName() + "', '" +
063                event.getValue() + "')");
064    
065        }
066    
067    
068        /**
069         * Record the fact that a servlet context attribute was removed.
070         *
071         * @param event The servlet context attribute event
072         */
073        public void attributeRemoved(ServletContextAttributeEvent event) {
074    
075            log("attributeRemoved('" + event.getName() + "', '" +
076                event.getValue() + "')");
077    
078        }
079    
080    
081        /**
082         * Record the fact that a servlet context attribute was replaced.
083         *
084         * @param event The servlet context attribute event
085         */
086        public void attributeReplaced(ServletContextAttributeEvent event) {
087    
088            log("attributeReplaced('" + event.getName() + "', '" +
089                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        // -------------------------------------------------------- Private Methods
121    
122    
123        /**
124         * Log a message to the servlet context application log.
125         *
126         * @param message Message to be logged
127         */
128        private void log(String message) {
129    
130            if (context != null)
131                context.log("ContextListener: " + message);
132            else
133                System.out.println("ContextListener: " + message);
134    
135        }
136    
137    
138        /**
139         * Log a message and associated exception to the servlet context
140         * application log.
141         *
142         * @param message Message to be logged
143         * @param throwable Exception to be logged
144         */
145        private void log(String message, Throwable throwable) {
146    
147            if (context != null)
148                context.log("ContextListener: " + message, throwable);
149            else {
150                System.out.println("ContextListener: " + message);
151                throwable.printStackTrace(System.out);
152            }
153    
154        }
155    
156    
157    }