1 7 8 package com.sun.corba.se.impl.presentation.rmi ; 9 10 import java.io.Serializable ; 11 import java.io.Externalizable ; 12 13 import javax.rmi.PortableRemoteObject ; 14 import javax.rmi.CORBA.Util ; 15 16 import java.rmi.RemoteException ; 17 import java.rmi.UnexpectedException ; 18 19 import org.omg.CORBA.UserException ; 20 21 import org.omg.CORBA_2_3.portable.InputStream ; 22 import org.omg.CORBA_2_3.portable.OutputStream ; 23 import org.omg.CORBA.portable.ApplicationException ; 24 25 import java.lang.reflect.Method ; 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 41 public interface ExceptionRW 42 { 43 Class getExceptionClass() ; 44 45 String getId() ; 46 47 void write( OutputStream os, Exception ex ) ; 48 49 Exception read( InputStream is ) ; 50 } 51 52 public abstract class ExceptionRWBase implements ExceptionRW 53 { 54 private Class cls ; 55 private String id ; 56 57 public ExceptionRWBase( Class cls ) 58 { 59 this.cls = cls ; 60 } 61 62 public Class getExceptionClass() 63 { 64 return cls ; 65 } 66 67 public String getId() 68 { 69 return id ; 70 } 71 72 void setId( String id ) 73 { 74 this.id = id ; 75 } 76 } 77 78 public class ExceptionRWIDLImpl extends ExceptionRWBase 79 { 80 private Method readMethod ; 81 private Method writeMethod ; 82 83 public ExceptionRWIDLImpl( Class cls ) 84 { 85 super( cls ) ; 86 87 String helperName = cls.getName() + "Helper" ; 88 ClassLoader loader = cls.getClassLoader() ; 89 Class helperClass ; 90 91 try { 92 helperClass = Class.forName( helperName, true, loader ) ; 93 Method idMethod = helperClass.getDeclaredMethod( "id", null ) ; 94 setId( (String )idMethod.invoke( null, null ) ) ; 95 } catch (Exception ex) { 96 throw wrapper.badHelperIdMethod( ex, helperName ) ; 97 } 98 99 try { 100 Class [] argTypes = new Class [] { 101 org.omg.CORBA.portable.OutputStream .class, cls } ; 102 writeMethod = helperClass.getDeclaredMethod( "write", 103 argTypes ) ; 104 } catch (Exception ex) { 105 throw wrapper.badHelperWriteMethod( ex, helperName ) ; 106 } 107 108 try { 109 Class [] argTypes = new Class [] { 110 org.omg.CORBA.portable.InputStream .class } ; 111 readMethod = helperClass.getDeclaredMethod( "read", argTypes ) ; 112 } catch (Exception ex) { 113 throw wrapper.badHelperReadMethod( ex, helperName ) ; 114 } 115 } 116 117 public void write( OutputStream os, Exception ex ) 118 { 119 try { 120 Object [] args = new Object [] { os, ex } ; 121 writeMethod.invoke( null, args ) ; 122 } catch (Exception exc) { 123 throw wrapper.badHelperWriteMethod( exc, 124 writeMethod.getDeclaringClass().getName() ) ; 125 } 126 } 127 128 public Exception read( InputStream is ) 129 { 130 try { 131 Object [] args = new Object [] { is } ; 132 return (Exception )readMethod.invoke( null, args ) ; 133 } catch (Exception 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 cls ) 143 { 144 super( cls ) ; 145 setId( IDLNameTranslatorImpl.getExceptionId( cls ) ) ; 146 } 147 148 public void write( OutputStream os, Exception ex ) 149 { 150 os.write_string( getId() ) ; 151 os.write_value( ex, getExceptionClass() ) ; 152 } 153 154 public Exception read( InputStream is ) 155 { 156 is.read_string() ; return (Exception )is.read_value( getExceptionClass() ) ; 158 } 159 } 160 161 163 public ExceptionHandlerImpl( Class [] 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 cls = exceptions[ctr] ; 171 if (!RemoteException .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 cls = exceptions[ctr] ; 180 if (!RemoteException .class.isAssignableFrom(cls)) { 181 ExceptionRW erw = null ; 182 if (UserException .class.isAssignableFrom(cls)) 183 erw = new ExceptionRWIDLImpl( cls ) ; 184 else 185 erw = new ExceptionRWRMIImpl( cls ) ; 186 187 205 206 rws[index++] = erw ; 207 } 208 } 209 } 210 211 private int findDeclaredException( Class cls ) 212 { 213 for (int ctr = 0; ctr < rws.length; ctr++) { 214 Class next = rws[ctr].getExceptionClass() ; 215 if (next.isAssignableFrom(cls)) 216 return ctr ; 217 } 218 219 return -1 ; 220 } 221 222 private int findDeclaredException( String repositoryId ) 223 { 224 for (int ctr=0; ctr<rws.length; ctr++) { 225 if (rws[ctr]==null) 228 return -1 ; 229 230 String rid = rws[ctr].getId() ; 231 if (repositoryId.equals( rid )) 232 return ctr ; 233 } 234 235 return -1 ; 236 } 237 238 public boolean isDeclaredException( Class cls ) 239 { 240 return findDeclaredException( cls ) >= 0 ; 241 } 242 243 public void writeException( OutputStream os, Exception 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 readException( ApplicationException ae ) 254 { 255 InputStream is = (InputStream )ae.getInputStream() ; 260 String excName = ae.getId() ; 261 int index = findDeclaredException( excName ) ; 262 if (index < 0) { 263 excName = is.read_string() ; 264 Exception res = new UnexpectedException ( excName ) ; 265 res.initCause( ae ) ; 266 return res ; 267 } 268 269 return rws[index].read( is ) ; 270 } 271 272 public ExceptionRW getRMIExceptionRW( Class cls ) 274 { 275 return new ExceptionRWRMIImpl( cls ) ; 276 } 277 } 278 279
| Popular Tags
|