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            public static List test(PageContextImpl pc){
027                    return new FDThreadImpl(null,(CFMLFactoryImpl)pc.getConfig().getFactory(),"test",pc).getStack();
028            }
029    */
030            
031            public FDThreadImpl(FDControllerImpl engine,CFMLFactoryImpl factory, String name, PageContextImpl pc) {
032                    this.engine=engine;
033                    this.factory=factory;
034                    this.name=name;
035                    this.pc=pc;
036            }
037    
038            /**
039             * @see com.intergral.fusiondebug.server.IFDThread#getName()
040             */
041            public String getName() {
042                    return name+":"+pc.getCFID();
043            }
044    
045            /**
046             * @see com.intergral.fusiondebug.server.IFDThread#id()
047             */
048            public int id() {
049                    return pc.getId();
050            }
051            
052            public static int id(PageContext pc) {
053                    return pc.getId();
054            }
055    
056            /**
057             * @see com.intergral.fusiondebug.server.IFDThread#stop()
058             */
059            public void stop() {
060                    pc.getThread().stop();
061            }
062            
063            /**
064             * @see com.intergral.fusiondebug.server.IFDThread#getThread()
065             */
066            public Thread getThread() {
067                    return pc.getThread();
068            }
069    
070            /**
071             * @see com.intergral.fusiondebug.server.IFDThread#getOutputBuffer()
072             */
073            public String getOutputBuffer() {
074                    return pc.getRootOut().toString();
075            }
076    
077    
078            /**
079             * @see com.intergral.fusiondebug.server.IFDThread#getStack()
080             */
081            public List getStackFrames() {
082                    return getStack();
083            }
084            public List getStack() {
085                    List stack = pc.getPageSourceList();
086                    
087                    StackTraceElement[] traces = pc.getThread().getStackTrace();
088                    String template="";
089                    StackTraceElement trace=null;
090                    ArrayList list=new ArrayList();
091                    Resource res;
092                    PageSource ps;
093                    int index=stack.size();
094                    for(int i=traces.length-1;i>=0;i--) {
095                            trace=traces[i];
096                            ps=null;
097                            if(trace.getLineNumber()<=0) continue;
098                            template=trace.getFileName();
099                            if(template==null || ResourceUtil.getExtension(template,"").equals("java")) continue;
100                            
101                            if(index>0)ps=(PageSource) stack.get(--index);
102                            // 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 
103                            if(ps==null || !(ps.getFullClassName().equals(trace.getClassName()) && ps.getPhyscalFile().getAbsolutePath().equals(template))){
104                                    res = ResourceUtil.toResourceNotExisting(pc, template);
105                                    ps = pc.toPageSource(res, null);
106                            }
107                            
108                            FDStackFrameImpl frame = new FDStackFrameImpl(this,pc,trace,ps);
109                            if(ASMUtil.isOverfowMethod(trace.getMethodName())) list.set(0,frame);
110                            else list.add(0,frame);
111                    }
112                    return list;
113            }
114            
115            /**
116             * @see com.intergral.fusiondebug.server.IFDThread#getTopStackFrame()
117             */
118            public IFDStackFrame getTopStack(){
119                    return getTopStackFrame();
120            }
121            public IFDStackFrame getTopStackFrame(){
122                    PageSource ps = pc.getCurrentPageSource();
123                    
124                    StackTraceElement[] traces = pc.getThread().getStackTrace();
125                    String template="";
126                    StackTraceElement trace=null;
127                    Resource res;
128                    
129                    for(int i=0;i<traces.length;i++) {
130                            trace=traces[i];
131                            if(trace.getLineNumber()<=0) continue;
132                            template=trace.getFileName();
133                            if(template==null || ResourceUtil.getExtension(template,"").equals("java")) continue;
134                            
135                            if(ps==null || !(ps.getFullClassName().equals(trace.getClassName()) && ps.getPhyscalFile().getAbsolutePath().equals(template))){
136                                    res = ResourceUtil.toResourceNotExisting(pc, template);
137                                    ps = pc.toPageSource(res, null);
138                            }
139                            break;
140                    }
141                    return new FDStackFrameImpl(this,pc,trace,ps);  
142            }
143            
144            /**
145             * @return the engine
146             */
147            public IFDController getController() {
148                    return engine;
149            }
150    
151    
152    }