myfaces-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "James Richards" <james_richa...@northps.com>
Subject RE: question for filedownload jsp with commandLink and commandButton
Date Tue, 30 May 2006 19:55:50 GMT
I use AspectJ to build a trace of MyFaces behavior and interaction with the application backing
beans.  Starting with the lifecycle, I progressively add elements to the trace to drill down
into the runtime behavior.  It's a tedious debugging approach but is much faster than, for
example, running the web application inside the Eclipse debugger.  Attached is an example
of the kind of trace I use, based on the tracing examples that AspectJ ships with.  Tracing
this way lets me capture detailed information about which calls are leading to problems. 
For example, when facestrace indicates that my app is failing at the update model phase, I
look at the LifecycleImpl source for that phase and add in the classes which are used by the
Lifecycle.  Eventually you can find the problem if you drill down far enough...

Of course if anyone has a better debugging strategy, I'm open to suggestions.

Hope that helps,

James

// SNIP//
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.event.PhaseEvent;
import org.apache.myfaces.el.PropertyResolverImpl;

import com.dbz.lms.faces.action.nested.UserAssignmentHandler;
import com.dbz.lms.service.CreditService;

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(LifecycleImpl) ||
//    	    within(PropertyResolverImpl) ||
    	    execution(* *.afterPhase(PhaseEvent))
//    	    within(UIViewRoot)
    	)
    	&& !within(com.dbz.lms.aspects.*)
    	&& !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();
        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() + ":" + buf.toString());
        Object retVal = proceed(obj);
        traceExit(obj.hashCode() + ":" + thisJoinPointStaticPart.getSignature() + " returning
" + retVal); 
        return retVal;
    }
    
}


// END SNIP//


-----Original Message-----
From: Yasushi Okubo [mailto:yasushi@cabm.rutgers.edu]
Sent: Tue 5/30/2006 3:36 PM
To: MyFaces Discussion
Subject: Re: question for filedownload jsp with commandLink and commandButton
 
I installed ftrace, but I do not see any errors from output, it only 
gave a waring message since the page was directed to jsp page.

Could you explain to me what you are achieved using AspectJ a little 
more ?   I am not sure what you are trying to do here by just looking at 
the link: eclipse/aspectj and its brief tutorial.
What are aspected myfaces classes and where I can find those. how can I 
use  ?

Thanks,
yasushi

James Richards wrote:

>The example I gave was very specific to the application I was working on.  Have you tried
plugging in the FacesTrace from http://facestrace.sourceforge.net/ onto the view in question?
 Someone from the list pointed me to it and it helps to follow what's happening.  If you plug
it into the page that's failing, you may see that it's failing in a particular phase, say
applying model updates, and that will target your search.
>
>MyFaces has proven fairly complex to debug so I've actually been compiling a trace into
MyFaces using AspectJ.  I copy the aspected MyFaces classes into my WEB-INF/classes directory.
 This lets me, for example, follow exactly what LifecycleImpl is doing.
>
>AspectJ is from eclipse.org/aspectj
>
>Hope that helps,
>
>James
>
>
>-----Original Message-----
>From: Yasushi Okubo [mailto:yasushi@cabm.rutgers.edu]
>Sent: Tue 5/30/2006 11:15 AM
>To: MyFaces Discussion
>Subject: Re: question for filedownload jsp with commandLink and commandButton
> 
>Hi, James
>
>Thanks for the info, but I do not see any code fragment for 
>renderResponse inside my code.  I am using myface 1.1.1, is this a 
>generic issue on myfaces 1.1.1 ?
>
>Thanks,
>yasushi
>
>James Richards wrote:
>
>  
>
>>I ran into something along these lines recently.  In my case, there was a managed
bean with a 
>>
>>public void changeSpecifiedRole(ValueChangeEvent event)
>>
>>method being called by MyFaces after the first button press which was erroneously
[for that version of MyFaces] invoking "renderResponse()".  
>>
>>Perhaps you have a similar listener which explictly calls renderResponse?
>>
>>Hope that helps,
>>
>>James
>> 
>>
>>
>>-----Original Message-----
>>From: Yasushi Okubo [mailto:yasushi@cabm.rutgers.edu]
>>Sent: Tue 5/30/2006 10:01 AM
>>To: MyFaces Discussion
>>Subject: question for filedownload jsp with commandLink and commandButton
>>
>>Hi experts,
>>
>>I have multiple download links [commandLink] /buttons[commandButton] on 
>>the same page.  I realized that a user needs to click twice to see an 
>>open directory window from the second attempt, I do not understand why 
>>this is happening.
>>
>>The download page was implemented with jsp page. 
>>commandLink/commandbutton  are simply taking  a user to this download 
>>page by clicking a link or a button.
>>
>>At the first click, a use always go to this page and the browser asks a 
>>user if one wants to save a directory or open on the browser. Then a 
>>user can decide to cancel or save or open. 
>>At the second attempts, the problem is that a user clicks link/button 
>>subsequently right after the first attempt, the action does not take a 
>>user to the jsp page, and it only reloads page with a round trip.
>>Then a user needs to click a link/ a button  second time, then it works.
>>
>>Could someone advise me why this is happening and is there anyway to 
>>stop this ?
>>
>>I implemented a commandbutton + actionListener [in backing bean] 
>>solution for file download, but the result was the same.  From the 
>>second attempt, a user always needs to click twice [double click] button.
>>I saw the one post on google is that adding _blank on form tag helps, 
>>but it actually did not help at all.
>>
>>Any idea ?
>>
>>yasushi
>>
>>
>>==== partial code for jsp page ====
>>....
>>
>>if(allowCache == null || allowCache.equalsIgnoreCase("false"))
>>   {
>>       response.setHeader("pragma", "no-cache");
>>       response.setHeader("Cache-control", "no-cache, no-store, 
>>must-revalidate");
>>       response.setHeader("Expires", "01 Apr 1995 01:10:10 GMT");
>>       response.addHeader("Cache-Control", "post-check=0, pre-check=0");
>>       response.setDateHeader ("Expires", 0);
>>   }
>>
>>   if(contentType!=null)
>>   {
>>       response.setContentType(contentType);
>>   }
>>
>>   if(fileName != null)
>>   {
>>
>>       StringBuffer contentDisposition = new StringBuffer();
>>
>>       if("true".equals(openDirectly))
>>       {
>>           contentDisposition.append("attachment;");
>>            logger.info("==> attachement");
>>       }
>>
>>       contentDisposition.append("filename=\"");
>>       contentDisposition.append(fileName);
>>       contentDisposition.append("\"");
>>
>>       response.setHeader ("Content-Disposition", 
>>contentDisposition.toString());
>>   }
>>
>>   if (bytes != null)
>>   {
>>       response.getOutputStream().write(bytes);
>>   }
>>
>>
>>
>>
>>
>> 
>>
>>    
>>
>
>
>  
>



Mime
View raw message