myfaces-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Apache Wiki <wikidi...@apache.org>
Subject [Myfaces Wiki] Update of "TracingMyFacesUsingAspectJ" by JamesRichards
Date Fri, 02 Jun 2006 18:57:09 GMT
Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Myfaces Wiki" for change notification.

The following page has been changed by JamesRichards:
http://wiki.apache.org/myfaces/TracingMyFacesUsingAspectJ

New page:
MyFaces is a very complex component implementation.  Due to an aggressive use of the compositional
pattern, it can often be difficult or obtuse to trace down a particular problem with specificity.
 Using MyFaces 1.1.1, this page will describe one possible mechanism for tracing the program
logic of MyFaces at runtime using statically compiled AspectJ.  More sophisticated applications
of this concept, such as runtime weaving on JDK 1.5, are feasible extensions of this concept
but the page will restrict itself to a basic compiled aspected trace and using such a trace
to locate obscure failures within the MyFaces architecture.

Here is the aspect upon which the discussion will be based:

{{{
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintStream;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.myfaces.taglib.html.ext.HtmlDataTableTag;
import org.apache.myfaces.component.html.ext.HtmlDataTable;
import org.apache.myfaces.custom.datascroller.HtmlDataScroller;
import org.apache.myfaces.lifecycle.LifecycleImpl;
import javax.faces.component.UIViewRoot;
import javax.faces.component.UIComponent;
import javax.faces.component.UIComponentBase;
import javax.faces.event.PhaseEvent;
import org.apache.myfaces.el.PropertyResolverImpl;
import javax.faces.context.FacesContext;
import org.apache.myfaces.context.servlet.ServletFacesContextImpl;

import org.aspectj.lang.JoinPoint;
import org.hibernate.impl.SessionImpl;

aspect Trace {
	private static final Log log = LogFactory.getLog(Trace.class);
	private static BufferedWriter fWrt;
	static {
		try {
		  fWrt = new BufferedWriter(new FileWriter(new File("C:\\trace.out")));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
    pointcut myClass(Object obj): this(obj) && 
    	(
    		within(ServletFacesContextImpl)
    	    || within(LifecycleImpl)
    	    || within(UIViewRoot)
    	)
    	&& !execution(* ServletFacesContextImpl.getApplication(..))
    	&& !execution(* ServletFacesContextImpl.getExternalContext(..))
    	&& !execution(* ServletFacesContextImpl.getM*(..))
    	&& !execution(* ServletFacesContextImpl.getRenderKit(..))
    	&& !execution(* ServletFacesContextImpl.getResponseStream(..))
    	&& !execution(* ServletFacesContextImpl.getResponseWriter(..))
    	&& !execution(* ServletFacesContextImpl.getViewRoot(..))
    	&& !execution(* ServletFacesContextImpl.set*(..))
    	&& !execution(* UIComponentBase.findComponent(..))
    	&& !execution(* UIComponentBase.get*(..))
    	&& !execution(* UIComponentBase.set*(..))
    	&& !execution(* UIComponentBase.is*(..))
    	&& !execution(* UIComponentBase.save*(..))
    	&& !execution(* UIComponentBase.create*(..))
    	&& !execution(* UIComponentBase.encode*(..))
    	&& !execution(* UIComponentBase.processSaveState(..))
    	&& !execution(* UIComponentBase.restoreState(..))
    	&& !execution(* UIComponentBase.restoreValueBindingMap(..))
    	&& !execution(* UIComponentBase.restoreAttributesMap(..))
    	&& !execution(* UIViewRoot.getRenderKitId())
    	&& !execution(* UIViewRoot.getLocale())
    	&& !execution(* UIViewRoot.create*())
    	&& !execution(String *.toString())
    	&& !execution(String *.hashCode());

    public static int TRACELEVEL = 2;
    protected static PrintStream stream = System.out;
    protected static int callDepth = 0;

    public static void initStream(PrintStream s) {
        stream = s;
    }

    protected static void traceEntry(String str) {
        if (TRACELEVEL == 0) return;
        if (TRACELEVEL == 2) callDepth++;
        printEntering(str);
    }

    protected static void traceExit(String str) {
        if (TRACELEVEL == 0) return;
        printExiting(str);
        if (TRACELEVEL == 2) callDepth--;
    }

    private static void printEntering(String str) {
        String idt = getIndent();
//        log.error(idt + ">>>" + str);
        try {
            fWrt.write(idt + ">>>" + str + "\n");
            fWrt.flush();
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
    }

    private static void printExiting(String str) {
        String idt = getIndent();
        try {
            fWrt.write(idt + "<<<" + str + "\n");
            fWrt.flush();
        } catch (Exception e) {
          	System.err.println(e.getMessage());
        }
    }

    private static void printIndent() {
        stream.print(getIndent());
    }
    
    private static String getIndent() {
    	StringBuffer buf = new StringBuffer();
        for (int i = 0; i < callDepth; i++) {
       	    buf.append(" ");
        }
    	return buf.toString();
    }

    pointcut myConstructor(Object obj): myClass(obj) && execution(new(..));
    pointcut myMethod(Object obj): myClass(obj) &&
        (execution(* *.*(..))) && !execution(String toString());

    before(Object obj): myConstructor(obj) {
        traceEntry(obj.hashCode() + ":" + thisJoinPointStaticPart.getSignature());
    }
    after(Object obj): myConstructor(obj) {
        traceExit(obj.hashCode() + ":" + thisJoinPointStaticPart.getSignature());
    }

    Object around(Object obj): myMethod(obj) {
    	Object[] args = thisJoinPoint.getArgs();
    	StringBuffer buf = new StringBuffer();
    	buf.append(thisJoinPoint.getSignature());
    	buf.append(" at line:" + thisJoinPoint.getSourceLocation().getLine());
    	buf.append(": args["); 
    	for (int i=0; i<args.length; i++) {
    		if (i>0) {
    			buf.append(",");
    		}
    		buf.append(args[i] + "");
    	}
    	buf.append("]");
        traceEntry(obj.hashCode() + ":" + thisJoinPoint.getThis() + ":" + buf.toString());
        Object retVal = proceed(obj);
        traceExit(obj.hashCode() + ":" + thisJoinPointStaticPart.getSignature() + " returning
" + retVal); 
        return retVal;
    }
}
}}}

Mime
View raw message