alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.modules.web.monitor.catalina;

import org.apache.catalina.InstanceEvent;
import javax.servlet.*;
import java.util.*;
import java.lang.reflect.*;

/**
 * DispatchListener.java
 * 
 * This class is used to allow the HTTP Monitor to record data about
 * dispatched requests on pre-Servlet 2.4 Catalina Servlet Containers.
 * This class listens for Catalina InstanceEvents, which are fired
 * when the RequestDispatcher sends a request to a resource. It uses
 * reflection to invoke a method on the MonitorFilter class. 
 * 
 *
 * Created: Mon Feb 18 12:15:52 2002
 *
 * @author Ana von Klopp
 */
 

public class DispatchListener implements org.apache.catalina.InstanceListener {
    
    private final static boolean debug = false;
     
    public DispatchListener() {
	if(debug) log(" Creating the listener"); //NOI18N
    }

    /**
     * Acknowledge the occurrence of the specified event.
     *
     * @param event InstanceEvent that has occurred
     */
    public void instanceEvent(InstanceEvent event) {
	
	//if(debug) log("instanceEvent() : start"); //NOI18N
	
	if((event.getType().equals(InstanceEvent.BEFORE_DISPATCH_EVENT)) || 
	   (event.getType().equals(InstanceEvent.AFTER_DISPATCH_EVENT))) {
	    

	    if(debug) log(event.getType());
	    
	    ServletRequest request = event.getRequest();
	    if(request == null) {
		if(debug) log("Request missing, return"); //NOI18N
		return;
	    }

	    Filter filter =
		(Filter)event.getRequest().getAttribute("netbeans.monitor.filter"); //NOI18N
	    if(filter == null) { 
		if(debug) log("Cannot find the filter, return"); //NOI18N
		return;
	    }

	    // The servlet attribute is set by the valve for the 
	    // original request. We set it here also since there 
	    // will be a different processing servlet for the dispatched 
	    // event. 
	    Servlet servlet = event.getServlet();
	    if(servlet != null) {
		request.setAttribute("netbeans.monitor.servlet", //NOI18N
				     servlet);
	    }

	    // The objective is to invoke the 
	    // MonitorFilter.handleDispatchedBefore(ServletRequest req)
	    // method or the 
	    // MonitorFilter.handleDispatchedAfter(ServletRequest req)
	    // method. 

	    Class clazz = filter.getClass();
	    Class[] argtypes = new Class[1];
	    Object[] args = new Object[1]; 
	    String methodName;

	    try {
		argtypes[0] = 
		    Class.forName("javax.servlet.ServletRequest"); //NOI18N
	    }
	    catch(ClassNotFoundException cnfe) {
		if(debug) log("Couldn't create the javax.servlet classes, returning"); //NOI18N
		return;
	    }

	    args[0] = request;

	    if(event.getType().equals(InstanceEvent.BEFORE_DISPATCH_EVENT)) {
		if(debug) log(" Collect data before the dispatch..."); //NOI18N
		methodName = "handleDispatchedBefore"; //NOI18N
	    }
	    else {
		if(debug) log(" Collect data after the dispatch..."); //NOI18N
		methodName = "handleDispatchedAfter"; //NOI18N
	    }
	
	    // It's no big deal if this does not succeed. All that
	    // happens is that we don't record data for when the
	    // resource that was dispatched to processes. 
	    try {
		Method method = clazz.getMethod(methodName, argtypes);
		method.invoke(filter, args);
		if(debug) log("Success\n"); //NII18N
	    }
	    catch (NoSuchMethodException mex) {
		if(debug) log("That didn't work - no such method"); //NOI18N
		if(debug) mex.printStackTrace();
	    } catch (SecurityException sex) {
		if(debug) log("That didn't work - security exception"); //NOI18N
		if(debug) sex.printStackTrace();
	    }
	    catch(Throwable t) {
		if(debug) log("That didn't work"); //NOI18N
		if(debug) t.printStackTrace();
	    }
	}
	//if(debug) log("instanceEvent(): end"); //NOI18N
    }
    
    
    private void log(String s) {
	System.out.println("ServletListener::" + s); //NOI18N
    }
    
} // ServletListener
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.