001: /*
002: * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.es;
030:
031: import com.caucho.java.LineMap;
032: import com.caucho.util.CharBuffer;
033:
034: import java.io.CharArrayWriter;
035: import java.io.IOException;
036: import java.io.OutputStream;
037: import java.io.PrintWriter;
038:
039: /**
040: * JavaScript exception, filtered to get the line numbers right.
041: */
042: public class ESException extends Exception {
043: public ESException() {
044: }
045:
046: public ESException(String name) {
047: super (name);
048: }
049:
050: public ESException(Throwable e) {
051: super (e);
052: }
053:
054: public static void staticPrintESTrace(Exception e, OutputStream os) {
055: CharArrayWriter writer = new CharArrayWriter();
056: PrintWriter pw = new PrintWriter(writer);
057:
058: e.printStackTrace(pw);
059:
060: pw.close();
061: char[] array = writer.toCharArray();
062:
063: CharBuffer cb = filter(array);
064:
065: if (os != null) {
066: byte[] b = cb.toString().getBytes();
067:
068: try {
069: os.write(b, 0, b.length);
070: } catch (IOException e1) {
071: }
072: } else
073: System.out.println(cb);
074: }
075:
076: public static void staticPrintESTrace(Exception e, PrintWriter os) {
077: CharArrayWriter writer = new CharArrayWriter();
078: PrintWriter pw = new PrintWriter(writer);
079:
080: e.printStackTrace(pw);
081:
082: pw.close();
083: char[] array = writer.toCharArray();
084:
085: CharBuffer cb = filter(array);
086:
087: if (os != null) {
088: os.print(cb.toString());
089: } else
090: System.out.println(cb);
091: }
092:
093: public void printESStackTrace(OutputStream os) {
094: staticPrintESTrace(this , os);
095: }
096:
097: public void printESStackTrace() {
098: printESStackTrace(System.out);
099: }
100:
101: public void printESStackTrace(PrintWriter out) {
102: CharArrayWriter writer = new CharArrayWriter();
103: PrintWriter pw = new PrintWriter(writer);
104:
105: printStackTrace(pw);
106:
107: pw.close();
108: char[] array = writer.toCharArray();
109:
110: CharBuffer cb = filter(array);
111:
112: out.print(cb.toString());
113: }
114:
115: /**
116: * Filter the exception trace to convert *.java line numbers back to
117: * the *.js name.
118: */
119: private static CharBuffer filter(char[] array) {
120: CharBuffer buf = new CharBuffer();
121: CharBuffer fun = new CharBuffer();
122: CharBuffer file = new CharBuffer();
123:
124: boolean hasJavaScript = false;
125: int i = 0;
126: while (i < array.length) {
127: fun.clear();
128: file.clear();
129: int start = i;
130: int end;
131: for (end = i; end < array.length && array[end] != '\n'; end++) {
132: }
133:
134: for (; i < end && Character.isWhitespace(array[i]); i++) {
135: fun.append(array[i]);
136: }
137:
138: // skip 'at'
139: for (; i < end && !Character.isWhitespace(array[i]); i++) {
140: fun.append(array[i]);
141: }
142:
143: if (!fun.endsWith("at")) {
144: for (i = start; i < end; i++) {
145: buf.append(array[i]);
146: }
147: i = end + 1;
148:
149: buf.append('\n');
150:
151: continue;
152: }
153:
154: for (; i < end && Character.isWhitespace(array[i]); i++) {
155: }
156:
157: fun.clear();
158: for (; i < end && !Character.isWhitespace(array[i])
159: && array[i] != '('; i++) {
160: fun.append(array[i]);
161: }
162:
163: if (fun.startsWith("com.caucho.es.")) {
164: i = end + 1;
165: continue;
166: }
167:
168: /*
169: if (! fun.startsWith("_js.")) {
170: if (hasJavaScript) {
171: i = end + 1;
172: continue;
173: }
174:
175: for (i = start; i < end; i++) {
176: buf.append(array[i]);
177: }
178: i = end + 1;
179:
180: buf.append('\n');
181:
182: continue;
183: }
184: */
185:
186: if (i < end && array[i] == '(')
187: i++;
188:
189: for (; i < end && array[i] != ')'; i++) {
190: file.append(array[i]);
191: }
192:
193: i = end + 1;
194:
195: if (fun.endsWith(".call"))
196: continue;
197:
198: int p = fun.lastIndexOf('.');
199: String className;
200: String function;
201: if (p > 0) {
202: className = fun.substring(0, p);
203: function = fun.substring(p + 1);
204: } else {
205: className = "";
206: function = fun.toString();
207: }
208:
209: Global global = Global.getGlobalProto();
210: LineMap lineMap = global != null ? global
211: .getLineMap(className) : null;
212: String line = file.toString();
213:
214: if (lineMap != null) {
215: p = file.indexOf(':');
216: if (p > 0) {
217: try {
218: String filename = file.substring(0, p);
219: int lineNo = Integer.parseInt(file
220: .substring(p + 1));
221: line = lineMap.convertLine(filename, lineNo);
222: } catch (Exception e) {
223: }
224: } else
225: line = lineMap.convertLine(file.toString(), 1);
226: }
227:
228: buf.append("\tat ");
229: buf.append(fun);
230: buf.append("(");
231: buf.append(line);
232: buf.append(")");
233: buf.append("\n");
234:
235: hasJavaScript = true;
236: }
237:
238: return buf;
239: }
240: }
|