tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From craig...@locus.apache.org
Subject cvs commit: jakarta-tomcat-4.0/webapps/examples/WEB-INF/classes/listeners ContextListener.java SessionListener.java
Date Tue, 22 Aug 2000 02:18:49 GMT
craigmcc    00/08/21 19:18:47

  Modified:    catalina/src/share/org/apache/catalina/core
                        ContainerBase.java StandardWrapperValve.java
               catalina/src/share/org/apache/catalina/startup
                        ContextConfig.java
               webapps/examples/WEB-INF web.xml
  Added:       webapps/examples/WEB-INF/classes/listeners
                        ContextListener.java SessionListener.java
  Log:
  Correct the startup call order to ensure that the application class loader
  is completely initialized prior to being required to load the application
  event listener classes.
  
  Add example context and session listeners to the "/examples" webapp.  They
  simply log the occurrence of interesting events using ServletContext.log.
  
  WARNING:  There is a bug in the current version of the 2.3 servletapi
  classes that causes HttpSessionBindingEvent.getSession() to return null.
  This causes NPE exceptions in SessionListener, but the application
  continues to operate so long as it does not rely on getSession().
  
  Revision  Changes    Path
  1.2       +9 -5      jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/core/ContainerBase.java
  
  Index: ContainerBase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/core/ContainerBase.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ContainerBase.java	2000/08/11 23:40:44	1.1
  +++ ContainerBase.java	2000/08/22 02:18:39	1.2
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/core/ContainerBase.java,v
1.1 2000/08/11 23:40:44 craigmcc Exp $
  - * $Revision: 1.1 $
  - * $Date: 2000/08/11 23:40:44 $
  + * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/core/ContainerBase.java,v
1.2 2000/08/22 02:18:39 craigmcc Exp $
  + * $Revision: 1.2 $
  + * $Date: 2000/08/22 02:18:39 $
    *
    * ====================================================================
    *
  @@ -150,7 +150,7 @@
    * class comments of the implementation class.
    *
    * @author Craig R. McClanahan
  - * @version $Revision: 1.1 $ $Date: 2000/08/11 23:40:44 $
  + * @version $Revision: 1.2 $ $Date: 2000/08/22 02:18:39 $
    */
   
   public abstract class ContainerBase
  @@ -1088,7 +1088,6 @@
   	if (started)
   	    throw new LifecycleException
   		(sm.getString("containerBase.alreadyStarted"));
  -	lifecycle.fireLifecycleEvent(START_EVENT, null);
   	started = true;
   
   	// Start our subordinate components, if any
  @@ -1128,6 +1127,9 @@
   	    current = current.getNext();
   	}
   
  +	// Notify our interested LifecycleListeners
  +	lifecycle.fireLifecycleEvent(START_EVENT, null);
  +
       }
   
   
  @@ -1144,6 +1146,8 @@
   	if (!started)
   	    throw new LifecycleException
   		(sm.getString("containerBase.notStarted"));
  +
  +	// Notify our interested LifecycleListeners
   	lifecycle.fireLifecycleEvent(STOP_EVENT, null);
   	started = false;
   
  
  
  
  1.4       +5 -5      jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/core/StandardWrapperValve.java
  
  Index: StandardWrapperValve.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/core/StandardWrapperValve.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- StandardWrapperValve.java	2000/08/22 00:14:02	1.3
  +++ StandardWrapperValve.java	2000/08/22 02:18:40	1.4
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/core/StandardWrapperValve.java,v
1.3 2000/08/22 00:14:02 craigmcc Exp $
  - * $Revision: 1.3 $
  - * $Date: 2000/08/22 00:14:02 $
  + * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/core/StandardWrapperValve.java,v
1.4 2000/08/22 02:18:40 craigmcc Exp $
  + * $Revision: 1.4 $
  + * $Date: 2000/08/22 02:18:40 $
    *
    * ====================================================================
    *
  @@ -102,7 +102,7 @@
    * <code>StandardWrapper</code> container implementation.
    *
    * @author Craig R. McClanahan
  - * @version $Revision: 1.3 $ $Date: 2000/08/22 00:14:02 $
  + * @version $Revision: 1.4 $ $Date: 2000/08/22 02:18:40 $
    */
   
   final class StandardWrapperValve
  @@ -115,7 +115,7 @@
       /**
        * The debugging detail level for this component.
        */
  -    private int debug = 3;
  +    private int debug = 0;
   
   
       /**
  
  
  
  1.8       +16 -14    jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/startup/ContextConfig.java
  
  Index: ContextConfig.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/startup/ContextConfig.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- ContextConfig.java	2000/08/20 03:42:07	1.7
  +++ ContextConfig.java	2000/08/22 02:18:43	1.8
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/startup/ContextConfig.java,v
1.7 2000/08/20 03:42:07 craigmcc Exp $
  - * $Revision: 1.7 $
  - * $Date: 2000/08/20 03:42:07 $
  + * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/startup/ContextConfig.java,v
1.8 2000/08/22 02:18:43 craigmcc Exp $
  + * $Revision: 1.8 $
  + * $Date: 2000/08/22 02:18:43 $
    *
    * ====================================================================
    *
  @@ -108,7 +108,7 @@
    * of that Context, and the associated defined servlets.
    *
    * @author Craig R. McClanahan
  - * @version $Revision: 1.7 $ $Date: 2000/08/20 03:42:07 $
  + * @version $Revision: 1.8 $ $Date: 2000/08/22 02:18:43 $
    */
   
   public final class ContextConfig
  @@ -597,16 +597,18 @@
           Object results[] = new Object[listeners.length];
   	boolean error = false;
   	for (int i = 0; i < results.length; i++) {
  -	  try {
  -	      Class clazz = loader.loadClass(listeners[i]);
  -	      results[i] = clazz.newInstance();
  -	  } catch (Throwable t) {
  -	      // FIXME - should we do anything besides log these?
  -	      log(sm.getString("contextConfig.applicationListener",
  -			       listeners[i]), t);
  -	      error = true;
  -	  }
  -
  +	    if (debug >= 2)
  +		log(" Configuring event listener class '" +
  +		    listeners[i] + "'");
  +	    try {
  +		Class clazz = loader.loadClass(listeners[i]);
  +		results[i] = clazz.newInstance();
  +	    } catch (Throwable t) {
  +		// FIXME - should we do anything besides log these?
  +		log(sm.getString("contextConfig.applicationListener",
  +				 listeners[i]), t);
  +		error = true;
  +	    }
   	}
   	if (!error)
   	    context.setApplicationListeners(results);
  
  
  
  1.2       +11 -1     jakarta-tomcat-4.0/webapps/examples/WEB-INF/web.xml
  
  Index: web.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat-4.0/webapps/examples/WEB-INF/web.xml,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- web.xml	2000/08/17 00:57:51	1.1
  +++ web.xml	2000/08/22 02:18:45	1.2
  @@ -2,9 +2,19 @@
   
   <!DOCTYPE web-app
       PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
  -    "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
  +    "http://java.sun.com/j2ee/dtds/web-app_2_3.dtd">
   
   <web-app>
  +
  +    <!-- Define example application events listeners -->
  +    <listener>
  +        <listener-class>listeners.ContextListener</listener-class>
  +    </listener>
  +    <listener>
  +        <listener-class>listeners.SessionListener</listener-class>
  +    </listener>
  +
  +    <!-- Define servlets that are included in the example application -->
       <servlet>
           <servlet-name>
               snoop
  
  
  
  1.1                  jakarta-tomcat-4.0/webapps/examples/WEB-INF/classes/listeners/ContextListener.java
  
  Index: ContextListener.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-tomcat-4.0/webapps/examples/WEB-INF/classes/listeners/ContextListener.java,v
1.1 2000/08/22 02:18:46 craigmcc Exp $
   * $Revision: 1.1 $
   * $Date: 2000/08/22 02:18:46 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * [Additional notices, if required by prior licensing conditions]
   *
   */
  
  
  package listeners;
  
  
  import javax.servlet.ServletContext;
  import javax.servlet.ServletContextAttributeEvent;
  import javax.servlet.ServletContextAttributesListener;
  import javax.servlet.ServletContextEvent;
  import javax.servlet.ServletContextListener;
  
  
  /**
   * Example listener for context-related application events, which were
   * introduced in the 2.3 version of the Servlet API.  This listener
   * merely documents the occurrence of such events in the application log
   * associated with our servlet context.
   *
   * @author Craig R. McClanahan
   * @version $Revision: 1.1 $ $Date: 2000/08/22 02:18:46 $
   */
  
  public final class ContextListener
      implements ServletContextAttributesListener, ServletContextListener {
  
  
      // ----------------------------------------------------- Instance Variables
  
  
      /**
       * The servlet context with which we are associated.
       */
      private ServletContext context = null;
  
  
      // --------------------------------------------------------- Public Methods
  
  
      /**
       * Record the fact that a servlet context attribute was added.
       *
       * @param event The servlet context attribute event
       */
      public void attributeAdded(ServletContextAttributeEvent event) {
  
  	log("attributeAdded('" + event.getName() + "', '" +
  	    event.getValue() + "')");
  
      }
  
  
      /**
       * Record the fact that a servlet context attribute was removed.
       *
       * @param event The servlet context attribute event
       */
      public void attributeRemoved(ServletContextAttributeEvent event) {
  
  	log("attributeRemoved('" + event.getName() + "', '" +
  	    event.getValue() + "')");
  
      }
  
  
      /**
       * Record the fact that a servlet context attribute was replaced.
       *
       * @param event The servlet context attribute event
       */
      public void attributeReplaced(ServletContextAttributeEvent event) {
  
  	log("attributeReplaced('" + event.getName() + "', '" +
  	    event.getValue() + "')");
  
      }
  
  
      /**
       * Record the fact that this web application has been destroyed.
       *
       * @param event The servlet context event
       */
      public void contextDestroyed(ServletContextEvent event) {
  
  	log("contextDestroyed()");
  	this.context = null;
  
      }
  
  
      /**
       * Record the fact that this web application has been initialized.
       *
       * @param event The servlet context event
       */
      public void contextInitialized(ServletContextEvent event) {
  
  	this.context = event.getServletContext();
  	log("contextInitialized()");
  
      }
  
  
      // -------------------------------------------------------- Private Methods
  
  
      /**
       * Log a message to the servlet context application log.
       *
       * @param message Message to be logged
       */
      private void log(String message) {
  
  	if (context != null)
  	    context.log("ContextListener: " + message);
  	else
  	    System.out.println("ContextListener: " + message);
  
      }
  
  
      /**
       * Log a message and associated exception to the servlet context
       * application log.
       *
       * @param message Message to be logged
       * @param throwable Exception to be logged
       */
      private void log(String message, Throwable throwable) {
  
  	if (context != null)
  	    context.log("ContextListener: " + message, throwable);
  	else {
  	    System.out.println("ContextListener: " + message);
  	    throwable.printStackTrace(System.out);
  	}
  
      }
  
  
  }
  
  
  
  1.1                  jakarta-tomcat-4.0/webapps/examples/WEB-INF/classes/listeners/SessionListener.java
  
  Index: SessionListener.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-tomcat-4.0/webapps/examples/WEB-INF/classes/listeners/SessionListener.java,v
1.1 2000/08/22 02:18:47 craigmcc Exp $
   * $Revision: 1.1 $
   * $Date: 2000/08/22 02:18:47 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * [Additional notices, if required by prior licensing conditions]
   *
   */
  
  
  package listeners;
  
  
  import javax.servlet.ServletContext;
  import javax.servlet.ServletContextEvent;
  import javax.servlet.ServletContextListener;
  import javax.servlet.http.HttpSessionAttributesListener;
  import javax.servlet.http.HttpSessionBindingEvent;
  import javax.servlet.http.HttpSessionEvent;
  import javax.servlet.http.HttpSessionListener;
  
  
  /**
   * Example listener for context-related application events, which were
   * introduced in the 2.3 version of the Servlet API.  This listener
   * merely documents the occurrence of such events in the application log
   * associated with our servlet context.
   *
   * @author Craig R. McClanahan
   * @version $Revision: 1.1 $ $Date: 2000/08/22 02:18:47 $
   */
  
  public final class SessionListener
      implements ServletContextListener,
  	       HttpSessionAttributesListener, HttpSessionListener {
  
  
      // ----------------------------------------------------- Instance Variables
  
  
      /**
       * The servlet context with which we are associated.
       */
      private ServletContext context = null;
  
  
      // --------------------------------------------------------- Public Methods
  
  
      /**
       * Record the fact that a servlet context attribute was added.
       *
       * @param event The session attribute event
       */
      public void attributeAdded(HttpSessionBindingEvent event) {
  
  	log("attributeAdded('" + event.getSession().getId() + "', '" +
  	    event.getName() + "', '" + event.getValue() + "')");
  
      }
  
  
      /**
       * Record the fact that a servlet context attribute was removed.
       *
       * @param event The session attribute event
       */
      public void attributeRemoved(HttpSessionBindingEvent event) {
  
  	log("attributeRemoved('" + event.getSession().getId() + "', '" +
  	    event.getName() + "', '" + event.getValue() + "')");
  
      }
  
  
      /**
       * Record the fact that a servlet context attribute was replaced.
       *
       * @param event The session attribute event
       */
      public void attributeReplaced(HttpSessionBindingEvent event) {
  
  	log("attributeReplaced('" + event.getSession().getId() + "', '" +
  	    event.getName() + "', '" + event.getValue() + "')");
  
      }
  
  
      /**
       * Record the fact that this web application has been destroyed.
       *
       * @param event The servlet context event
       */
      public void contextDestroyed(ServletContextEvent event) {
  
  	log("contextDestroyed()");
  	this.context = null;
  
      }
  
  
      /**
       * Record the fact that this web application has been initialized.
       *
       * @param event The servlet context event
       */
      public void contextInitialized(ServletContextEvent event) {
  
  	this.context = event.getServletContext();
  	log("contextInitialized()");
  
      }
  
  
      /**
       * Record the fact that a session has been created.
       *
       * @param event The session event
       */
      public void sessionCreated(HttpSessionEvent event) {
  
  	log("sessionCreated('" + event.getSession().getId() + "')");
  
      }
  
  
      /**
       * Record the fact that a session has been destroyed.
       *
       * @param event The session event
       */
      public void sessionDestroyed(HttpSessionEvent event) {
  
  	log("sessionDestroyed('" + event.getSession().getId() + "')");
  
      }
  
  
      // -------------------------------------------------------- Private Methods
  
  
      /**
       * Log a message to the servlet context application log.
       *
       * @param message Message to be logged
       */
      private void log(String message) {
  
  	if (context != null)
  	    context.log("SessionListener: " + message);
  	else
  	    System.out.println("SessionListener: " + message);
  
      }
  
  
      /**
       * Log a message and associated exception to the servlet context
       * application log.
       *
       * @param message Message to be logged
       * @param throwable Exception to be logged
       */
      private void log(String message, Throwable throwable) {
  
  	if (context != null)
  	    context.log("SessionListener: " + message, throwable);
  	else {
  	    System.out.println("SessionListener: " + message);
  	    throwable.printStackTrace(System.out);
  	}
  
      }
  
  
  }
  
  
  

Mime
View raw message