KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > appserv > management > util > misc > ThrowableMapper


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.appserv.management.util.misc;
24
25 import java.util.Set JavaDoc;
26 import java.lang.reflect.Constructor JavaDoc;
27
28 import com.sun.appserv.management.util.misc.GSetUtil;
29
30
31
32 /**
33     Maps a Throwable to another one in order to avoid the transfer
34     of non-standard (proprietary) Exception types, which could result in
35     ClassNotFoundException on remote clients.
36     <p>
37     Any Throwable which either is, or contains,
38     a Throwable which is not in the allowed packages is converted.
39  */

40 public final class ThrowableMapper
41 {
42     final Throwable JavaDoc mOriginal;
43     
44     final Set JavaDoc<String JavaDoc> mOKPackages;
45     
46     /**
47         By default, any Throwable whose package does not start with one
48         of these packages must be mapped to something standard.
49      */

50     protected final static Set JavaDoc<String JavaDoc> OK_PACKAGES =
51             GSetUtil.newUnmodifiableStringSet( "java.", "javax." );
52     
53         public
54     ThrowableMapper( final Throwable JavaDoc t )
55     {
56         mOriginal = t;
57         mOKPackages = OK_PACKAGES;
58     }
59     
60         private static boolean
61     shouldMap( final Throwable JavaDoc t )
62     {
63         final String JavaDoc tClass = t.getClass().getName();
64         
65         boolean shouldMap = true;
66         
67         for( final String JavaDoc prefix : OK_PACKAGES )
68         {
69             if ( tClass.startsWith( prefix ) )
70             {
71                 shouldMap = false;
72                 break;
73             }
74         }
75         
76         return( shouldMap );
77     }
78     
79     
80         public static Throwable JavaDoc
81     map( final Throwable JavaDoc t )
82     {
83         Throwable JavaDoc result = t;
84         
85         if ( t != null )
86         {
87             final Throwable JavaDoc tCause = t.getCause();
88             final Throwable JavaDoc tCauseMapped = map( tCause );
89             
90             // if either this Exception or its cause needs/was mapped,
91
// then we must form a new Exception
92

93             if ( shouldMap( t ) )
94             {
95                 // the Throwable itself needs to be mapped
96
final String JavaDoc msg =t.getMessage();
97                 
98                 if ( t instanceof Error JavaDoc )
99                 {
100                     result = new Error JavaDoc( msg, tCauseMapped );
101                 }
102                 else if ( t instanceof RuntimeException JavaDoc )
103                 {
104                     result = new RuntimeException JavaDoc( msg, tCauseMapped );
105                 }
106                 else if ( t instanceof Exception JavaDoc )
107                 {
108                     result = new Exception JavaDoc( msg, tCauseMapped );
109                 }
110                 else
111                 {
112                     result = new Throwable JavaDoc( msg, tCauseMapped );
113                 }
114                 
115                 result.setStackTrace( t.getStackTrace() );
116             }
117             else if ( tCauseMapped != tCause )
118             {
119                 // the Throwable doesn't need mapping, but its Cause does
120
// create a Throwable of the same class, and insert its
121
// cause and stack trace.
122
try
123                 {
124                     final Constructor JavaDoc<? extends Throwable JavaDoc> c =
125                         t.getClass().getConstructor( String JavaDoc.class, Throwable JavaDoc.class );
126                     result = c.newInstance( t.getMessage(), tCauseMapped);
127                 }
128                 catch( final Throwable JavaDoc t1 )
129                 {
130                     try
131                     {
132                         final Constructor JavaDoc<? extends Throwable JavaDoc> c =
133                             t.getClass().getConstructor( String JavaDoc.class );
134                         result = c.newInstance( t.getMessage() );
135                         result.initCause( tCauseMapped );
136                     }
137                     catch( final Throwable JavaDoc t2 )
138                     {
139                         result = new Throwable JavaDoc( t.getMessage(), tCauseMapped );
140                     }
141                 }
142                 
143                 result.setStackTrace( tCause.getStackTrace() );
144             }
145             else
146             {
147                 result = t;
148             }
149         }
150
151         return( result );
152     }
153     
154     /**
155         Map the original Throwable to one that is non-proprietary (standard).
156         Possible results include java.lang.Exception, java.lang.RuntimeException,
157         java.lang.Error. The original stack trace and exception chain is
158         preserved, each element in that chain being mapped if necessary.
159         
160         @return a Throwable which uses only standard classes
161      */

162         public Throwable JavaDoc
163     map()
164     {
165         return( map( mOriginal ) );
166     }
167 }
168
169
170
171
172
173
174
175
176
Popular Tags