KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jruby > exceptions > RaiseException


1 /***** BEGIN LICENSE BLOCK *****
2  * Version: CPL 1.0/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Common Public
5  * License Version 1.0 (the "License"); you may not use this file
6  * except in compliance with the License. You may obtain a copy of
7  * the License at http://www.eclipse.org/legal/cpl-v10.html
8  *
9  * Software distributed under the License is distributed on an "AS
10  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11  * implied. See the License for the specific language governing
12  * rights and limitations under the License.
13  *
14  * Copyright (C) 2001 Alan Moore <alan_moore@gmx.net>
15  * Copyright (C) 2001-2004 Jan Arne Petersen <jpetersen@uni-bonn.de>
16  * Copyright (C) 2002 Benoit Cerrina <b.cerrina@wanadoo.fr>
17  * Copyright (C) 2002-2004 Anders Bengtsson <ndrsbngtssn@yahoo.se>
18  * Copyright (C) 2004 Thomas E Enebo <enebo@acm.org>
19  * Copyright (C) 2004 Joey Gibson <joey@joeygibson.com>
20  * Copyright (C) 2004 Stefan Matthias Aust <sma@3plus4.de>
21  * Copyright (C) 2005 Charles O Nutter <headius@headius.com>
22  *
23  * Alternatively, the contents of this file may be used under the terms of
24  * either of the GNU General Public License Version 2 or later (the "GPL"),
25  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26  * in which case the provisions of the GPL or the LGPL are applicable instead
27  * of those above. If you wish to allow use of your version of this file only
28  * under the terms of either the GPL or the LGPL, and not to allow others to
29  * use your version of this file under the terms of the CPL, indicate your
30  * decision by deleting the provisions above and replace them with the notice
31  * and other provisions required by the GPL or the LGPL. If you do not delete
32  * the provisions above, a recipient may use your version of this file under
33  * the terms of any one of the CPL, the GPL or the LGPL.
34  ***** END LICENSE BLOCK *****/

35 package org.jruby.exceptions;
36
37 import java.io.ByteArrayOutputStream JavaDoc;
38 import java.io.PrintStream JavaDoc;
39 import java.io.PrintWriter JavaDoc;
40 import java.io.StringWriter JavaDoc;
41
42 import org.jruby.*;
43 import org.jruby.runtime.Block;
44 import org.jruby.runtime.ThreadContext;
45 import org.jruby.runtime.builtin.IRubyObject;
46
47 public class RaiseException extends JumpException {
48     private static final long serialVersionUID = -7612079169559973951L;
49     
50     private RubyException exception;
51
52     public RaiseException(RubyException actException) {
53         this(actException, false);
54     }
55
56     public RaiseException(Ruby runtime, RubyClass excptnClass, String JavaDoc msg, boolean nativeException) {
57         super(msg, JumpType.RaiseJump);
58         if (msg == null) {
59             msg = "No message available";
60         }
61         setException((RubyException) excptnClass.callMethod(runtime.getCurrentContext(), "new", new IRubyObject[] {excptnClass.getRuntime().newString(msg)}, Block.NULL_BLOCK), nativeException);
62     }
63
64     public RaiseException(RubyException exception, boolean isNativeException) {
65         super(JumpType.RaiseJump);
66         setException(exception, isNativeException);
67     }
68
69     public static RaiseException createNativeRaiseException(Ruby runtime, Throwable JavaDoc cause) {
70         NativeException nativeException = new NativeException(runtime, runtime.getClass(NativeException.CLASS_NAME), cause);
71         return new RaiseException(cause, nativeException);
72     }
73
74     private static String JavaDoc buildMessage(Throwable JavaDoc exception) {
75         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
76         StringWriter JavaDoc stackTrace = new StringWriter JavaDoc();
77         exception.printStackTrace(new PrintWriter JavaDoc(stackTrace));
78     
79         sb.append("Native Exception: '").append(exception.getClass()).append("'; ");
80         sb.append("Message: ").append(exception.getMessage()).append("; ");
81         sb.append("StackTrace: ").append(stackTrace.getBuffer().toString());
82
83         return sb.toString();
84     }
85
86     public RaiseException(Throwable JavaDoc cause, NativeException nativeException) {
87         super(buildMessage(cause), cause, JumpType.RaiseJump);
88         setException(nativeException, false);
89     }
90
91     /**
92      * Gets the exception
93      * @return Returns a RubyException
94      */

95     public RubyException getException() {
96         return exception;
97     }
98
99     /**
100      * Sets the exception
101      * @param newException The exception to set
102      */

103     protected void setException(RubyException newException, boolean nativeException) {
104         Ruby runtime = newException.getRuntime();
105         ThreadContext context = runtime.getCurrentContext();
106
107         if (!context.isWithinDefined()) {
108             runtime.getGlobalVariables().set("$!", newException);
109         }
110
111         if (runtime.getTraceFunction() != null) {
112             runtime.callTraceFunction(context, "return", context.getPosition(),
113                     context.getFrameSelf(), context.getFrameName(), context.getFrameKlazz());
114         }
115
116         this.exception = newException;
117
118         if (runtime.getStackTraces() > 5) {
119             return;
120         }
121
122         runtime.setStackTraces(runtime.getStackTraces() + 1);
123
124         if (newException.callMethod(context, "backtrace").isNil() && context.getSourceFile() != null) {
125             IRubyObject backtrace = context.createBacktrace(0, nativeException);
126             newException.callMethod(context, "set_backtrace", backtrace);
127         }
128
129         runtime.setStackTraces(runtime.getStackTraces() - 1);
130     }
131
132     public Throwable JavaDoc fillInStackTrace() {
133         return originalFillInStackTrace();
134     }
135     
136     public void printStackTrace() {
137         printStackTrace(System.err);
138     }
139     
140     public void printStackTrace(PrintStream JavaDoc ps) {
141         StackTraceElement JavaDoc[] trace = getStackTrace();
142         int externalIndex = 0;
143         for (int i = trace.length - 1; i > 0; i--) {
144             if (trace[i].getClassName().indexOf("org.jruby.evaluator") >= 0) {
145                 break;
146             }
147             externalIndex = i;
148         }
149         IRubyObject backtrace = exception.backtrace();
150         Ruby runtime = backtrace.getRuntime();
151         if (runtime.getNil() != backtrace) {
152             String JavaDoc firstLine = backtrace.callMethod(runtime.getCurrentContext(), "first").callMethod(runtime.getCurrentContext(), "to_s").toString();
153             ps.print(firstLine + ": ");
154         }
155         ps.println(exception.message + " (" + exception.getMetaClass().toString() + ")");
156         exception.printBacktrace(ps);
157         ps.println("\t...internal jruby stack elided...");
158         for (int i = externalIndex; i < trace.length; i++) {
159             ps.println("\tfrom " + trace[i].toString());
160         }
161     }
162     
163     public void printStackTrace(PrintWriter JavaDoc pw) {
164         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
165         printStackTrace(new PrintStream JavaDoc(baos));
166         pw.print(baos.toString());
167     }
168 }
169
Popular Tags