001    package railo.intergral.fusiondebug.server;
002    
003    import java.util.ArrayList;
004    import java.util.List;
005    
006    import railo.commons.io.res.Resource;
007    import railo.commons.io.res.util.ResourceUtil;
008    import railo.commons.lang.StringUtil;
009    import railo.runtime.PageContextImpl;
010    import railo.runtime.PageSource;
011    import railo.runtime.engine.ThreadLocalPageContext;
012    import railo.runtime.exp.NativeException;
013    import railo.runtime.exp.PageException;
014    import railo.runtime.exp.TemplateException;
015    import railo.runtime.op.Caster;
016    import railo.transformer.bytecode.util.ASMUtil;
017    
018    import com.intergral.fusiondebug.server.FDSignalException;
019    
020    public class FDSignal {
021            private static ThreadLocal hash=new ThreadLocal();
022    
023            public static void signal(PageException pe, boolean caught) {
024                    try {
025                            String id = pe.hashCode()+":"+caught;
026                            if(Caster.toString(hash.get(),"").equals(id)) return;
027                            
028                            List stack = createExceptionStack(pe);
029                            if(stack.size()>0){
030                                    FDSignalException se = new FDSignalException(); 
031                                    se.setExceptionStack(stack);
032                                    se.setRuntimeExceptionCaughtStatus(caught);
033                                    se.setRuntimeExceptionExpression(createRuntimeExceptionExpression(pe));
034                                    if(pe instanceof NativeException) se.setRuntimeExceptionType("native");
035                                    else se.setRuntimeExceptionType(pe.getTypeAsString());
036                                    se.setStackTrace(pe.getStackTrace());
037                                    hash.set(id);
038                                    throw se;
039                            }
040                            
041                    }
042                    catch( FDSignalException fdse){
043                            // do nothing - will be processed by JDI and handled by FD
044                    }
045            }
046            
047            public static String createRuntimeExceptionExpression(PageException pe){
048                    if(!StringUtil.isEmpty(pe.getDetail()))
049                            return pe.getMessage()+" "+pe.getDetail();
050                    return pe.getMessage();
051            }
052            
053            public static List createExceptionStack(PageException pe) {
054                    StackTraceElement[] traces = pe.getStackTrace();
055                    PageContextImpl pc = (PageContextImpl) ThreadLocalPageContext.get();
056                    String template="";
057                    StackTraceElement trace=null;
058                    List list=new ArrayList();
059                    Resource res;
060                    PageSource ps;
061                    FDStackFrameImpl frame;
062                    
063                    
064                    for(int i=traces.length-1;i>=0;i--) {
065                            trace=traces[i];
066                            ps=null;
067                            if(trace.getLineNumber()<=0) continue;
068                            template=trace.getFileName();
069                            if(template==null || ResourceUtil.getExtension(template,"").equals("java")) continue;
070                            
071                            res = ResourceUtil.toResourceNotExisting(pc, template);
072                            ps = pc.toPageSource(res, null);
073                            
074                            frame = new FDStackFrameImpl(null,pc,trace,ps);
075                            if(ASMUtil.isOverfowMethod(trace.getMethodName())) list.set(0,frame);
076                            else list.add(0,frame);
077                            
078                    }
079                    if(pe instanceof TemplateException){
080                            TemplateException te = (TemplateException) pe;
081                            if(te.getPageSource()!=null)
082                                    list.add(0,new FDStackFrameImpl(null,pc,te.getPageSource(),te.getLine()));
083                    }
084                    
085                    return list;
086            }
087    }