KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > impl > presentation > rmi > ExceptionHandlerImpl


1 /*
2  * @(#)ExceptionHandlerImpl.java 1.10 04/06/21
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.corba.se.impl.presentation.rmi ;
9
10 import java.io.Serializable JavaDoc ;
11 import java.io.Externalizable JavaDoc ;
12
13 import javax.rmi.PortableRemoteObject JavaDoc ;
14 import javax.rmi.CORBA.Util JavaDoc ;
15
16 import java.rmi.RemoteException JavaDoc ;
17 import java.rmi.UnexpectedException JavaDoc ;
18
19 import org.omg.CORBA.UserException JavaDoc ;
20
21 import org.omg.CORBA_2_3.portable.InputStream JavaDoc ;
22 import org.omg.CORBA_2_3.portable.OutputStream JavaDoc ;
23 import org.omg.CORBA.portable.ApplicationException JavaDoc ;
24
25 import java.lang.reflect.Method JavaDoc ;
26
27 import com.sun.corba.se.spi.logging.CORBALogDomains ;
28 import com.sun.corba.se.impl.logging.ORBUtilSystemException ;
29
30 public class ExceptionHandlerImpl implements ExceptionHandler
31 {
32     private ExceptionRW[] rws ;
33
34     private final ORBUtilSystemException wrapper ;
35
36 ///////////////////////////////////////////////////////////////////////////////
37
// ExceptionRW interface and implementations.
38
// Used to read and write exceptions.
39
///////////////////////////////////////////////////////////////////////////////
40

41     public interface ExceptionRW
42     {
43     Class JavaDoc getExceptionClass() ;
44
45     String JavaDoc getId() ;
46
47     void write( OutputStream JavaDoc os, Exception JavaDoc ex ) ;
48
49     Exception JavaDoc read( InputStream JavaDoc is ) ;
50     }
51
52     public abstract class ExceptionRWBase implements ExceptionRW
53     {
54     private Class JavaDoc cls ;
55     private String JavaDoc id ;
56
57     public ExceptionRWBase( Class JavaDoc cls )
58     {
59         this.cls = cls ;
60     }
61
62     public Class JavaDoc getExceptionClass()
63     {
64         return cls ;
65     }
66
67     public String JavaDoc getId()
68     {
69         return id ;
70     }
71
72     void setId( String JavaDoc id )
73     {
74         this.id = id ;
75     }
76     }
77
78     public class ExceptionRWIDLImpl extends ExceptionRWBase
79     {
80     private Method JavaDoc readMethod ;
81     private Method JavaDoc writeMethod ;
82
83     public ExceptionRWIDLImpl( Class JavaDoc cls )
84     {
85         super( cls ) ;
86
87         String JavaDoc helperName = cls.getName() + "Helper" ;
88         ClassLoader JavaDoc loader = cls.getClassLoader() ;
89         Class JavaDoc helperClass ;
90
91         try {
92         helperClass = Class.forName( helperName, true, loader ) ;
93         Method JavaDoc idMethod = helperClass.getDeclaredMethod( "id", null ) ;
94         setId( (String JavaDoc)idMethod.invoke( null, null ) ) ;
95         } catch (Exception JavaDoc ex) {
96         throw wrapper.badHelperIdMethod( ex, helperName ) ;
97         }
98
99         try {
100         Class JavaDoc[] argTypes = new Class JavaDoc[] {
101             org.omg.CORBA.portable.OutputStream JavaDoc.class, cls } ;
102         writeMethod = helperClass.getDeclaredMethod( "write",
103             argTypes ) ;
104         } catch (Exception JavaDoc ex) {
105         throw wrapper.badHelperWriteMethod( ex, helperName ) ;
106         }
107
108         try {
109         Class JavaDoc[] argTypes = new Class JavaDoc[] {
110             org.omg.CORBA.portable.InputStream JavaDoc.class } ;
111         readMethod = helperClass.getDeclaredMethod( "read", argTypes ) ;
112         } catch (Exception JavaDoc ex) {
113         throw wrapper.badHelperReadMethod( ex, helperName ) ;
114         }
115     }
116
117     public void write( OutputStream JavaDoc os, Exception JavaDoc ex )
118     {
119         try {
120         Object JavaDoc[] args = new Object JavaDoc[] { os, ex } ;
121         writeMethod.invoke( null, args ) ;
122         } catch (Exception JavaDoc exc) {
123         throw wrapper.badHelperWriteMethod( exc,
124             writeMethod.getDeclaringClass().getName() ) ;
125         }
126     }
127
128     public Exception JavaDoc read( InputStream JavaDoc is )
129     {
130         try {
131         Object JavaDoc[] args = new Object JavaDoc[] { is } ;
132         return (Exception JavaDoc)readMethod.invoke( null, args ) ;
133         } catch (Exception JavaDoc ex) {
134         throw wrapper.badHelperReadMethod( ex,
135             readMethod.getDeclaringClass().getName() ) ;
136         }
137     }
138     }
139
140     public class ExceptionRWRMIImpl extends ExceptionRWBase
141     {
142     public ExceptionRWRMIImpl( Class JavaDoc cls )
143     {
144         super( cls ) ;
145         setId( IDLNameTranslatorImpl.getExceptionId( cls ) ) ;
146     }
147
148     public void write( OutputStream JavaDoc os, Exception JavaDoc ex )
149     {
150         os.write_string( getId() ) ;
151         os.write_value( ex, getExceptionClass() ) ;
152     }
153
154     public Exception JavaDoc read( InputStream JavaDoc is )
155     {
156         is.read_string() ; // read and ignore!
157
return (Exception JavaDoc)is.read_value( getExceptionClass() ) ;
158     }
159     }
160
161 ///////////////////////////////////////////////////////////////////////////////
162

163     public ExceptionHandlerImpl( Class JavaDoc[] exceptions )
164     {
165     wrapper = ORBUtilSystemException.get(
166         CORBALogDomains.RPC_PRESENTATION ) ;
167
168     int count = 0 ;
169     for (int ctr=0; ctr<exceptions.length; ctr++) {
170         Class JavaDoc cls = exceptions[ctr] ;
171         if (!RemoteException JavaDoc.class.isAssignableFrom(cls))
172         count++ ;
173     }
174
175     rws = new ExceptionRW[count] ;
176
177     int index = 0 ;
178     for (int ctr=0; ctr<exceptions.length; ctr++) {
179         Class JavaDoc cls = exceptions[ctr] ;
180         if (!RemoteException JavaDoc.class.isAssignableFrom(cls)) {
181         ExceptionRW erw = null ;
182         if (UserException JavaDoc.class.isAssignableFrom(cls))
183             erw = new ExceptionRWIDLImpl( cls ) ;
184         else
185             erw = new ExceptionRWRMIImpl( cls ) ;
186
187         /* The following check is not performed
188          * in order to maintain compatibility with
189          * rmic. See bug 4989312.
190          
191         // Check for duplicate repository ID
192         String repositoryId = erw.getId() ;
193         int duplicateIndex = findDeclaredException( repositoryId ) ;
194         if (duplicateIndex > 0) {
195             ExceptionRW duprw = rws[duplicateIndex] ;
196             String firstClassName =
197             erw.getExceptionClass().getName() ;
198             String secondClassName =
199             duprw.getExceptionClass().getName() ;
200             throw wrapper.duplicateExceptionRepositoryId(
201             firstClassName, secondClassName, repositoryId ) ;
202         }
203
204         */

205
206         rws[index++] = erw ;
207         }
208     }
209     }
210
211     private int findDeclaredException( Class JavaDoc cls )
212     {
213         for (int ctr = 0; ctr < rws.length; ctr++) {
214             Class JavaDoc next = rws[ctr].getExceptionClass() ;
215             if (next.isAssignableFrom(cls))
216         return ctr ;
217         }
218
219         return -1 ;
220     }
221
222     private int findDeclaredException( String JavaDoc repositoryId )
223     {
224     for (int ctr=0; ctr<rws.length; ctr++) {
225         // This may occur when rws has not been fully
226
// populated, in which case the search should just fail.
227
if (rws[ctr]==null)
228         return -1 ;
229
230         String JavaDoc rid = rws[ctr].getId() ;
231         if (repositoryId.equals( rid ))
232         return ctr ;
233     }
234
235     return -1 ;
236     }
237
238     public boolean isDeclaredException( Class JavaDoc cls )
239     {
240     return findDeclaredException( cls ) >= 0 ;
241     }
242
243     public void writeException( OutputStream JavaDoc os, Exception JavaDoc ex )
244     {
245     int index = findDeclaredException( ex.getClass() ) ;
246     if (index < 0)
247         throw wrapper.writeUndeclaredException( ex,
248         ex.getClass().getName() ) ;
249
250     rws[index].write( os, ex ) ;
251     }
252
253     public Exception JavaDoc readException( ApplicationException JavaDoc ae )
254     {
255     // Note that the exception ID is present in both ae
256
// and in the input stream from ae. The exception
257
// reader must actually read the exception ID from
258
// the stream.
259
InputStream JavaDoc is = (InputStream JavaDoc)ae.getInputStream() ;
260     String JavaDoc excName = ae.getId() ;
261     int index = findDeclaredException( excName ) ;
262     if (index < 0) {
263         excName = is.read_string() ;
264         Exception JavaDoc res = new UnexpectedException JavaDoc( excName ) ;
265         res.initCause( ae ) ;
266         return res ;
267     }
268
269     return rws[index].read( is ) ;
270     }
271
272     // This is here just for the dynamicrmiiiop test
273
public ExceptionRW getRMIExceptionRW( Class JavaDoc cls )
274     {
275     return new ExceptionRWRMIImpl( cls ) ;
276     }
277 }
278
279
Popular Tags