001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.intergral.fusiondebug.server;
020
021import java.util.ArrayList;
022import java.util.List;
023
024import lucee.commons.io.res.Resource;
025import lucee.commons.io.res.util.ResourceUtil;
026import lucee.commons.lang.StringUtil;
027import lucee.runtime.PageContextImpl;
028import lucee.runtime.PageSource;
029import lucee.runtime.engine.ThreadLocalPageContext;
030import lucee.runtime.exp.NativeException;
031import lucee.runtime.exp.PageException;
032import lucee.runtime.exp.TemplateException;
033import lucee.runtime.op.Caster;
034import lucee.transformer.bytecode.util.ASMUtil;
035
036import com.intergral.fusiondebug.server.FDSignalException;
037
038public class FDSignal {
039        private static ThreadLocal hash=new ThreadLocal();
040
041        public static void signal(PageException pe, boolean caught) {
042                try {
043                        String id = pe.hashCode()+":"+caught;
044                        if(Caster.toString(hash.get(),"").equals(id)) return;
045                        
046                        List stack = createExceptionStack(pe);
047                        if(stack.size()>0){
048                                FDSignalException se = new FDSignalException(); 
049                                se.setExceptionStack(stack);
050                                se.setRuntimeExceptionCaughtStatus(caught);
051                                se.setRuntimeExceptionExpression(createRuntimeExceptionExpression(pe));
052                                if(pe instanceof NativeException) se.setRuntimeExceptionType("native");
053                                else se.setRuntimeExceptionType(pe.getTypeAsString());
054                                se.setStackTrace(pe.getStackTrace());
055                                hash.set(id);
056                                throw se;
057                        }
058                        
059                }
060                catch( FDSignalException fdse){
061                        // do nothing - will be processed by JDI and handled by FD
062                }
063        }
064        
065        public static String createRuntimeExceptionExpression(PageException pe){
066                if(!StringUtil.isEmpty(pe.getDetail()))
067                        return pe.getMessage()+" "+pe.getDetail();
068                return pe.getMessage();
069        }
070        
071        public static List createExceptionStack(PageException pe) {
072                StackTraceElement[] traces = pe.getStackTrace();
073                PageContextImpl pc = (PageContextImpl) ThreadLocalPageContext.get();
074                String template="";
075                StackTraceElement trace=null;
076                List list=new ArrayList();
077                Resource res;
078                PageSource ps;
079                FDStackFrameImpl frame;
080                
081                
082                for(int i=traces.length-1;i>=0;i--) {
083                        trace=traces[i];
084                        ps=null;
085                        if(trace.getLineNumber()<=0) continue;
086                        template=trace.getFileName();
087                        if(template==null || ResourceUtil.getExtension(template,"").equals("java")) continue;
088                        
089                        res = ResourceUtil.toResourceNotExisting(pc, template);
090                        ps = pc.toPageSource(res, null);
091                        
092                        frame = new FDStackFrameImpl(null,pc,trace,ps);
093                        if(ASMUtil.isOverfowMethod(trace.getMethodName())) list.set(0,frame);
094                        else list.add(0,frame);
095                        
096                }
097                if(pe instanceof TemplateException){
098                        TemplateException te = (TemplateException) pe;
099                        if(te.getPageSource()!=null)
100                                list.add(0,new FDStackFrameImpl(null,pc,te.getPageSource(),te.getLine()));
101                }
102                
103                return list;
104        }
105}