KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > DxLib > DxException


1 // You can redistribute this software and/or modify it under the terms of
2
// the Ozone Library License version 1 published by ozone-db.org.
3
//
4
// The original code and portions created by SMB are
5
// Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
6
//
7
// $Id: DxException.java,v 1.1 2001/12/18 10:31:30 per_nyfelt Exp $
8

9 package org.ozoneDB.DxLib;
10
11 import java.lang.reflect.*;
12 import java.io.*;
13
14 /**
15  * Diese Klasse dient dazu, ein Java-Exception in einem Stream zu schreiben
16  * bzw. daraus zu lesen. Das ist z.B. fuer Socket-Verbindungen nuetzlich.
17  *
18  *
19  * @author <a HREF="http://www.softwarebuero.de/">SMB</a>
20  * @version $Revision: 1.1 $Date: 2001/12/18 10:31:30 $
21  */

22 public class DxException extends DxObject implements Externalizable {
23
24     final static long serialVersionUID = 1L;
25
26     Throwable JavaDoc exc;
27     String JavaDoc msg;
28
29
30     public DxException() {
31         msg = new String JavaDoc();
32     }
33
34
35     public DxException( Throwable JavaDoc e ) {
36         exc = e;
37         msg = new String JavaDoc( e.toString() );
38     }
39
40
41     public Throwable JavaDoc toExc() {
42         return exc;
43     }
44
45
46     public String JavaDoc toString() {
47         return msg;
48     }
49
50
51     public void writeExternal( ObjectOutput out ) throws IOException {
52         try {
53             if (exc != null) {
54                 out.writeUTF( exc.getClass().getName() );
55             } else {
56                 out.writeUTF( "null" );
57             }
58             out.writeUTF( msg );
59         } catch (Exception JavaDoc e) {
60             throw new IOException( e.toString() );
61         }
62     }
63
64
65     public void readExternal( ObjectInput in ) throws IOException {
66         try {
67             String JavaDoc e = in.readUTF();
68             msg = in.readUTF();
69             if (e.compareTo( "null" ) != 0) {
70                 Class JavaDoc[] classes = new Class JavaDoc[1];
71                 classes[0] = e.getClass();
72
73                 //Constructor constr = Class.forName( e ).getConstructor( classes );
74
// todo: uncommented until verified that it works
75
// todo: maybe we should use ClassManager here instead ot Thread.currentThread...?
76
Constructor constr = Thread.currentThread().getContextClassLoader().loadClass( e ).getConstructor( classes );
77
78                 if (constr != null) {
79                     Object JavaDoc[] args = new Object JavaDoc[1];
80                     args[0] = msg;
81                     System.out.println( args[0] );
82                     exc = (Throwable JavaDoc)constr.newInstance( args );
83                 }
84             }
85         } catch (Exception JavaDoc e) {
86             throw new IOException( e.toString() );
87         }
88     }
89 }
90
Popular Tags