001    package railo.runtime.type.trace;
002    
003    import railo.commons.io.res.util.ResourceUtil;
004    import railo.runtime.PageContext;
005    import railo.runtime.debug.Debugger;
006    import railo.runtime.debug.DebuggerImpl;
007    import railo.runtime.engine.ThreadLocalPageContext;
008    import railo.runtime.type.Array;
009    import railo.runtime.type.QueryPro;
010    import railo.runtime.type.Struct;
011    import railo.runtime.type.UDF;
012    import railo.runtime.type.dt.DateTime;
013    
014    public class TraceObjectSupport implements TraceObject {
015    
016    
017            
018            protected Object o;
019            protected  Debugger debugger;
020            
021            protected int type;
022            protected String category;
023            protected String text;
024    
025            
026            public TraceObjectSupport(Debugger debugger,Object o,int type,String category,String text) {
027                    this.o=o;
028                    //this.log=log;
029                    this.type=type;
030                    this.category=category;
031                    this.text=text;
032                    this.debugger=debugger;
033                    //((DebuggerImpl)debugger).addObjectTrace(this);
034                    
035                    
036            }
037            
038            
039    
040    
041            /**
042             * @see railo.runtime.type.QueryImpl#toString()
043             */
044            
045            public String toString() {
046                    
047                    return o.toString();
048            }
049    
050            public boolean equals(Object obj) {
051                    
052                    return o.equals(obj);
053            }
054            
055    
056            protected void log() {
057                    try{log(debugger,type,category,text,null,null);}catch(Throwable t){}
058            }
059            
060            protected void log(Object varName) {
061                    try{log(debugger,type,category,text,varName.toString(),null);}catch(Throwable t){}
062            }
063            
064            protected void log(Object varName,Object varValue) {
065                    try{log(debugger,type,category,text,varName.toString(),varValue.toString());}catch(Throwable t){}
066            }
067    
068            public synchronized static void log(Debugger debugger,int type,String category,String text,String varName,String varValue) {
069                    
070                    Throwable t=new Exception("Stack trace");
071                    Throwable cause = t.getCause(); 
072                    while(cause!=null){
073                            t=cause;
074                            cause = t.getCause(); 
075                    }
076                    StackTraceElement[] traces = t.getStackTrace();
077                    
078            int line=0;
079                    String template=null;
080                    StackTraceElement trace=null;
081                    for(int i=0;i<traces.length;i++) {
082                            trace=traces[i];
083                            template=trace.getFileName();
084                            if(trace.getLineNumber()<=0 || template==null || ResourceUtil.getExtension(template,"").equals("java") ||
085                                            template.endsWith("Dump.cfc") || template.endsWith("dump.cfm"))// MUST bad impl 
086                                    continue;
087                            line=trace.getLineNumber();
088                            break;
089                    }
090                    //print.e(t);
091                    if(line==0) return;
092                    
093                    /*String type=traces[2].getMethodName();
094                    
095                    int row=qry.addRow();
096                    qry.setAtEL(LABEL, row, label);
097                    qry.setAtEL(ACTION, row, type(type));
098                    qry.setAtEL(PARAMS, row, addional);
099                    */
100                    String action=type(traces[2].getMethodName());
101                    ((DebuggerImpl)debugger).addTrace(type, category, text, template,line,action, varName, varValue);
102                            
103            }       
104            
105    
106            protected static String type(String type) {
107                    if(type.equals("appendEL")) return "append";
108                    if(type.equals("setEL")) return "set";
109                    if(type.equals("removeEL")) return "remove";
110                    if(type.equals("keys")) return "list";
111                    if(type.equals("toDumpData")) return "dump";
112                    
113                    
114                    return type;
115            }
116            
117    
118    
119            protected PageContext pc() {
120                    return ThreadLocalPageContext.get();
121            }
122            
123            public static TraceObject toTraceObject(Debugger debugger,Object obj, int type, String category, String text) {
124                    if(obj instanceof TraceObject)
125                            return (TraceObject) obj;
126                    else if(obj instanceof UDF)
127                            return new TOUDF(debugger,(UDF) obj,type,category,text);
128                    else if(obj instanceof QueryPro)
129                            return new TOQuery(debugger,(QueryPro) obj,type,category,text);
130                    else if(obj instanceof Array)
131                            return new TOArray(debugger,(Array) obj,type,category,text);
132                    else if(obj instanceof Struct)
133                            return new TOStruct(debugger,(Struct) obj,type,category,text);
134                    else if(obj instanceof DateTime)
135                            return new TODateTime(debugger,(DateTime) obj,type,category,text);
136                    
137                    
138                    return new TOObjects(debugger,obj,type,category,text);
139            }
140            
141    }