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.runtime.CFMLFactoryImpl;
009    import railo.runtime.PageContext;
010    import railo.runtime.PageContextImpl;
011    import railo.runtime.PageSource;
012    import railo.transformer.bytecode.util.ASMUtil;
013    
014    import com.intergral.fusiondebug.server.IFDController;
015    import com.intergral.fusiondebug.server.IFDStackFrame;
016    import com.intergral.fusiondebug.server.IFDThread;
017    
018    public class FDThreadImpl implements IFDThread {
019            
020            
021            private PageContextImpl pc;
022            private String name;
023            private FDControllerImpl engine;
024            //private CFMLFactoryImpl factory;
025    
026            
027            public FDThreadImpl(FDControllerImpl engine,CFMLFactoryImpl factory, String name, PageContextImpl pc) {
028                    this.engine=engine;
029                    //this.factory=factory;
030                    this.name=name;
031                    this.pc=pc;
032            }
033    
034            @Override
035            public String getName() {
036                    return name+":"+pc.getCFID();
037            }
038    
039            @Override
040            public int id() {
041                    return pc.getId();
042            }
043            
044            public static int id(PageContext pc) {
045                    return pc.getId();
046            }
047    
048            @Override
049            public void stop() {
050                    pc.getThread().stop();
051            }
052            
053            @Override
054            public Thread getThread() {
055                    return pc.getThread();
056            }
057    
058            @Override
059            public String getOutputBuffer() {
060                    return pc.getRootOut().toString();
061            }
062    
063    
064            public List getStackFrames() {
065                    return getStack();
066            }
067            public List getStack() {
068                    List stack = pc.getPageSourceList();
069                    
070                    StackTraceElement[] traces = pc.getThread().getStackTrace();
071                    String template="";
072                    StackTraceElement trace=null;
073                    ArrayList list=new ArrayList();
074                    Resource res;
075                    PageSource ps;
076                    int index=stack.size();
077                    for(int i=traces.length-1;i>=0;i--) {
078                            trace=traces[i];
079                            ps=null;
080                            if(trace.getLineNumber()<=0) continue;
081                            template=trace.getFileName();
082                            if(template==null || ResourceUtil.getExtension(template,"").equals("java")) continue;
083                            
084                            if(index>0)ps=(PageSource) stack.get(--index);
085                            // inside the if is the old way, that only work when the cfm is inside the mapping, but i'm not shure woth the new way 
086                            if(ps==null || !(ps.getFullClassName().equals(trace.getClassName()) && ps.getDisplayPath().equals(template))){
087                                    res = ResourceUtil.toResourceNotExisting(pc, template);
088                                    ps = pc.toPageSource(res, null);
089                            }
090                            
091                            FDStackFrameImpl frame = new FDStackFrameImpl(this,pc,trace,ps);
092                            if(ASMUtil.isOverfowMethod(trace.getMethodName())) list.set(0,frame);
093                            else list.add(0,frame);
094                    }
095                    return list;
096            }
097            
098            public IFDStackFrame getTopStack(){
099                    return getTopStackFrame();
100            }
101            
102            @Override
103        public IFDStackFrame getTopStackFrame(){
104                    PageSource ps = pc.getCurrentPageSource();
105                    
106                    StackTraceElement[] traces = pc.getThread().getStackTrace();
107                    String template="";
108                    StackTraceElement trace=null;
109                    Resource res;
110                    
111                    for(int i=0;i<traces.length;i++) {
112                            trace=traces[i];
113                            if(trace.getLineNumber()<=0) continue;
114                            template=trace.getFileName();
115                            if(template==null || ResourceUtil.getExtension(template,"").equals("java")) continue;
116                            
117                            if(ps==null || !(ps.getFullClassName().equals(trace.getClassName()) && ps.getResource().getAbsolutePath().equals(template))){
118                                    res = ResourceUtil.toResourceNotExisting(pc, template);
119                                    ps = pc.toPageSource(res, null);
120                            }
121                            break;
122                    }
123                    return new FDStackFrameImpl(this,pc,trace,ps);  
124            }
125            
126            /**
127             * @return the engine
128             */
129            public IFDController getController() {
130                    return engine;
131            }
132    
133    
134    }