KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > es > ESException


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.es;
30
31 import com.caucho.java.LineMap;
32 import com.caucho.util.CharBuffer;
33
34 import java.io.CharArrayWriter JavaDoc;
35 import java.io.IOException JavaDoc;
36 import java.io.OutputStream JavaDoc;
37 import java.io.PrintWriter JavaDoc;
38
39 /**
40  * JavaScript exception, filtered to get the line numbers right.
41  */

42 public class ESException extends Exception JavaDoc {
43   public ESException() {}
44   public ESException(String JavaDoc name) { super(name); }
45   public ESException(Throwable JavaDoc e) { super(e); }
46   
47   public static void staticPrintESTrace(Exception JavaDoc e, OutputStream JavaDoc os)
48   {
49     CharArrayWriter JavaDoc writer = new CharArrayWriter JavaDoc();
50     PrintWriter JavaDoc pw = new PrintWriter JavaDoc(writer);
51     
52     e.printStackTrace(pw);
53
54     pw.close();
55     char []array = writer.toCharArray();
56
57     CharBuffer cb = filter(array);
58
59     if (os != null) {
60       byte []b = cb.toString().getBytes();
61
62       try {
63     os.write(b, 0, b.length);
64       } catch (IOException JavaDoc e1) {
65       }
66     } else
67       System.out.println(cb);
68   }
69
70   public static void staticPrintESTrace(Exception JavaDoc e, PrintWriter JavaDoc os)
71   {
72     CharArrayWriter JavaDoc writer = new CharArrayWriter JavaDoc();
73     PrintWriter JavaDoc pw = new PrintWriter JavaDoc(writer);
74     
75     e.printStackTrace(pw);
76
77     pw.close();
78     char []array = writer.toCharArray();
79
80     CharBuffer cb = filter(array);
81
82     if (os != null) {
83       os.print(cb.toString());
84     } else
85       System.out.println(cb);
86   }
87
88   public void printESStackTrace(OutputStream JavaDoc os)
89   {
90     staticPrintESTrace(this, os);
91   }
92
93   public void printESStackTrace()
94   {
95     printESStackTrace(System.out);
96   }
97
98   public void printESStackTrace(PrintWriter JavaDoc out)
99   {
100     CharArrayWriter JavaDoc writer = new CharArrayWriter JavaDoc();
101     PrintWriter JavaDoc pw = new PrintWriter JavaDoc(writer);
102     
103     printStackTrace(pw);
104
105     pw.close();
106     char []array = writer.toCharArray();
107
108     CharBuffer cb = filter(array);
109
110     out.print(cb.toString());
111   }
112
113   /**
114    * Filter the exception trace to convert *.java line numbers back to
115    * the *.js name.
116    */

117   private static CharBuffer filter(char []array)
118   {
119     CharBuffer buf = new CharBuffer();
120     CharBuffer fun = new CharBuffer();
121     CharBuffer file = new CharBuffer();
122
123     boolean hasJavaScript = false;
124     int i = 0;
125     while (i < array.length) {
126       fun.clear();
127       file.clear();
128       int start = i;
129       int end;
130       for (end = i; end < array.length && array[end] != '\n'; end++) {
131       }
132
133       for (; i < end && Character.isWhitespace(array[i]); i++) {
134     fun.append(array[i]);
135       }
136
137       // skip 'at'
138
for (; i < end && ! Character.isWhitespace(array[i]); i++) {
139     fun.append(array[i]);
140       }
141
142       if (! fun.endsWith("at")) {
143     for (i = start; i < end; i++) {
144       buf.append(array[i]);
145     }
146     i = end + 1;
147
148     buf.append('\n');
149     
150     continue;
151       }
152
153       for (; i < end && Character.isWhitespace(array[i]); i++) {
154       }
155
156       fun.clear();
157       for (; i < end && ! Character.isWhitespace(array[i]) &&
158          array[i] != '('; i++) {
159     fun.append(array[i]);
160       }
161
162       if (fun.startsWith("com.caucho.es.")) {
163         i = end + 1;
164     continue;
165       }
166
167       /*
168       if (! fun.startsWith("_js.")) {
169     if (hasJavaScript) {
170       i = end + 1;
171       continue;
172     }
173
174     for (i = start; i < end; i++) {
175       buf.append(array[i]);
176     }
177     i = end + 1;
178
179     buf.append('\n');
180     
181     continue;
182       }
183       */

184
185       if (i < end && array[i] == '(')
186     i++;
187
188       for (; i < end && array[i] != ')'; i++) {
189     file.append(array[i]);
190       }
191
192       i = end + 1;
193
194       if (fun.endsWith(".call"))
195     continue;
196
197       int p = fun.lastIndexOf('.');
198       String JavaDoc className;
199       String JavaDoc function;
200       if (p > 0) {
201         className = fun.substring(0, p);
202         function = fun.substring(p + 1);
203       }
204       else {
205         className = "";
206         function = fun.toString();
207       }
208
209       Global global = Global.getGlobalProto();
210       LineMap lineMap = global != null ? global.getLineMap(className) : null;
211       String JavaDoc line = file.toString();
212
213       if (lineMap != null) {
214         p = file.indexOf(':');
215         if (p > 0) {
216           try {
217             String JavaDoc filename = file.substring(0, p);
218             int lineNo = Integer.parseInt(file.substring(p + 1));
219             line = lineMap.convertLine(filename, lineNo);
220           } catch (Exception JavaDoc e) {
221           }
222         }
223         else
224           line = lineMap.convertLine(file.toString(), 1);
225       }
226
227       buf.append("\tat ");
228       buf.append(fun);
229       buf.append("(");
230       buf.append(line);
231       buf.append(")");
232       buf.append("\n");
233
234       hasJavaScript = true;
235     }
236
237     return buf;
238   }
239 }
240
241
242
243
Popular Tags