001: /*
002: * AllExceptionHandler.java Copyright (c) 2006,07 Swaroop Belur
003: *
004: * This program is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU General Public License
006: * as published by the Free Software Foundation; either version 2
007: * of the License, or (at your option) any later version.
008:
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013:
014: * You should have received a copy of the GNU General Public License
015: * along with this program; if not, write to the Free Software
016: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
017: *
018: */
019: package net.sf.jdec.util;
020:
021: import java.io.IOException;
022:
023: import net.sf.jdec.io.Writer;
024: import net.sf.jdec.exceptions.ApplicationException;
025: import net.sf.jdec.reflection.Behaviour;
026:
027: public class AllExceptionHandler {
028:
029: Throwable t = null;
030:
031: private int codePos = -1;
032:
033: public void setCodePosition(int i) {
034: codePos = i;
035: }
036:
037: public Behaviour getB() {
038: return b;
039: }
040:
041: private Behaviour b = null;
042:
043: public void setBehaviour(Behaviour b) {
044: this .b = b;
045: }
046:
047: /**
048: *
049: */
050: public AllExceptionHandler(Throwable t) {
051: super ();
052: this .t = t;
053: }
054:
055: public AllExceptionHandler(java.lang.String msg) {
056: super ();
057: this .msg = msg;
058: }
059:
060: private java.lang.String msg = "";
061:
062: public AllExceptionHandler(Throwable t,
063: java.lang.String extraMessage) {
064: super ();
065: this .t = t;
066: anyMessage = extraMessage;
067: }
068:
069: private java.lang.String anyMessage = null;
070:
071: public void sendMessage() {
072: java.lang.String s = msg;
073: Writer projectwriter = null;
074: try {
075: java.io.PrintWriter writer = Writer.getWriter("log");
076: projectwriter = (Writer) writer;
077: projectwriter.writeLog(s);
078: projectwriter.flush();
079:
080: } catch (IOException ioe) {
081: throw new ApplicationException(new Throwable(ioe));
082: }
083:
084: }
085:
086: public void reportException() {
087: if (t != null) {
088:
089: StringBuffer exceptionContent = null;
090: String temp = "";
091: temp += "\n\n[ERROR] Exception Report produced by Exception Handler... \n\n";
092: temp += anyMessage + "\n\n";
093: if (b != null) {
094: temp += "[ERROR]\tFaulty Method:\t"
095: + b.getShortDescription() + "\n";
096: temp += "[ERROR]\tBytecode position:\t" + codePos
097: + "\n";
098: }
099: temp += "[ERROR]\tCAUSE:\t" + t.getCause() + "\n";
100: temp += "[ERROR]\tMESSAGE:\t" + t.getMessage() + "\n";
101: temp += "[ERROR]\tCLASS:\t" + t.getClass() + "\n\n";
102:
103: temp += "---EXCEPTION STACK TRACE---\n";
104:
105: exceptionContent = new StringBuffer(temp);
106:
107: Writer projectwriter = null;
108: try {
109: java.io.PrintWriter writer = Writer.getWriter("log");
110: projectwriter = (Writer) writer;
111: projectwriter.writeLog(exceptionContent.toString());
112: t.printStackTrace(writer);
113: projectwriter.flush();
114: } catch (IOException ioe) {
115: throw new ApplicationException(new Throwable(ioe));
116: } finally {
117: temp = null;
118: exceptionContent = null;
119: //Close Log writer objects...
120: //projectwriter.close("log");
121:
122: }
123:
124: } else {
125: throw new ApplicationException(
126: "Exception Handler invoked without Registering any Throwable Object...");
127: }
128:
129: }
130:
131: }
|