src/share/classes/com/sun/corba/se/impl/presentation/rmi/ExceptionHandlerImpl.java

Thu, 31 Aug 2017 18:10:36 +0800

author
aoqi
date
Thu, 31 Aug 2017 18:10:36 +0800
changeset 748
6845b95cba6b
parent 240
f90b3e014e83
parent 0
7ef37b2cdcad
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package com.sun.corba.se.impl.presentation.rmi ;
aoqi@0 27
aoqi@0 28 import java.io.Serializable ;
aoqi@0 29 import java.io.Externalizable ;
aoqi@0 30
aoqi@0 31 import javax.rmi.PortableRemoteObject ;
aoqi@0 32 import javax.rmi.CORBA.Util ;
aoqi@0 33
aoqi@0 34 import java.rmi.RemoteException ;
aoqi@0 35 import java.rmi.UnexpectedException ;
aoqi@0 36
aoqi@0 37 import org.omg.CORBA.UserException ;
aoqi@0 38
aoqi@0 39 import org.omg.CORBA_2_3.portable.InputStream ;
aoqi@0 40 import org.omg.CORBA_2_3.portable.OutputStream ;
aoqi@0 41 import org.omg.CORBA.portable.ApplicationException ;
aoqi@0 42
aoqi@0 43 import java.lang.reflect.Method ;
aoqi@0 44
aoqi@0 45 import com.sun.corba.se.spi.logging.CORBALogDomains ;
aoqi@0 46 import com.sun.corba.se.impl.logging.ORBUtilSystemException ;
aoqi@0 47
aoqi@0 48 public class ExceptionHandlerImpl implements ExceptionHandler
aoqi@0 49 {
aoqi@0 50 private ExceptionRW[] rws ;
aoqi@0 51
aoqi@0 52 private final ORBUtilSystemException wrapper ;
aoqi@0 53
aoqi@0 54 ///////////////////////////////////////////////////////////////////////////////
aoqi@0 55 // ExceptionRW interface and implementations.
aoqi@0 56 // Used to read and write exceptions.
aoqi@0 57 ///////////////////////////////////////////////////////////////////////////////
aoqi@0 58
aoqi@0 59 public interface ExceptionRW
aoqi@0 60 {
aoqi@0 61 Class getExceptionClass() ;
aoqi@0 62
aoqi@0 63 String getId() ;
aoqi@0 64
aoqi@0 65 void write( OutputStream os, Exception ex ) ;
aoqi@0 66
aoqi@0 67 Exception read( InputStream is ) ;
aoqi@0 68 }
aoqi@0 69
aoqi@0 70 public abstract class ExceptionRWBase implements ExceptionRW
aoqi@0 71 {
aoqi@0 72 private Class cls ;
aoqi@0 73 private String id ;
aoqi@0 74
aoqi@0 75 public ExceptionRWBase( Class cls )
aoqi@0 76 {
aoqi@0 77 this.cls = cls ;
aoqi@0 78 }
aoqi@0 79
aoqi@0 80 public Class getExceptionClass()
aoqi@0 81 {
aoqi@0 82 return cls ;
aoqi@0 83 }
aoqi@0 84
aoqi@0 85 public String getId()
aoqi@0 86 {
aoqi@0 87 return id ;
aoqi@0 88 }
aoqi@0 89
aoqi@0 90 void setId( String id )
aoqi@0 91 {
aoqi@0 92 this.id = id ;
aoqi@0 93 }
aoqi@0 94 }
aoqi@0 95
aoqi@0 96 public class ExceptionRWIDLImpl extends ExceptionRWBase
aoqi@0 97 {
aoqi@0 98 private Method readMethod ;
aoqi@0 99 private Method writeMethod ;
aoqi@0 100
aoqi@0 101 public ExceptionRWIDLImpl( Class cls )
aoqi@0 102 {
aoqi@0 103 super( cls ) ;
aoqi@0 104
aoqi@0 105 String helperName = cls.getName() + "Helper" ;
aoqi@0 106 ClassLoader loader = cls.getClassLoader() ;
aoqi@0 107 Class helperClass ;
aoqi@0 108
aoqi@0 109 try {
aoqi@0 110 helperClass = Class.forName( helperName, true, loader ) ;
aoqi@0 111 Method idMethod = helperClass.getDeclaredMethod( "id", (Class[])null ) ;
aoqi@0 112 setId( (String)idMethod.invoke( null, (Object[])null ) ) ;
aoqi@0 113 } catch (Exception ex) {
aoqi@0 114 throw wrapper.badHelperIdMethod( ex, helperName ) ;
aoqi@0 115 }
aoqi@0 116
aoqi@0 117 try {
aoqi@0 118 Class[] argTypes = new Class[] {
aoqi@0 119 org.omg.CORBA.portable.OutputStream.class, cls } ;
aoqi@0 120 writeMethod = helperClass.getDeclaredMethod( "write",
aoqi@0 121 argTypes ) ;
aoqi@0 122 } catch (Exception ex) {
aoqi@0 123 throw wrapper.badHelperWriteMethod( ex, helperName ) ;
aoqi@0 124 }
aoqi@0 125
aoqi@0 126 try {
aoqi@0 127 Class[] argTypes = new Class[] {
aoqi@0 128 org.omg.CORBA.portable.InputStream.class } ;
aoqi@0 129 readMethod = helperClass.getDeclaredMethod( "read", argTypes ) ;
aoqi@0 130 } catch (Exception ex) {
aoqi@0 131 throw wrapper.badHelperReadMethod( ex, helperName ) ;
aoqi@0 132 }
aoqi@0 133 }
aoqi@0 134
aoqi@0 135 public void write( OutputStream os, Exception ex )
aoqi@0 136 {
aoqi@0 137 try {
aoqi@0 138 Object[] args = new Object[] { os, ex } ;
aoqi@0 139 writeMethod.invoke( null, args ) ;
aoqi@0 140 } catch (Exception exc) {
aoqi@0 141 throw wrapper.badHelperWriteMethod( exc,
aoqi@0 142 writeMethod.getDeclaringClass().getName() ) ;
aoqi@0 143 }
aoqi@0 144 }
aoqi@0 145
aoqi@0 146 public Exception read( InputStream is )
aoqi@0 147 {
aoqi@0 148 try {
aoqi@0 149 Object[] args = new Object[] { is } ;
aoqi@0 150 return (Exception)readMethod.invoke( null, args ) ;
aoqi@0 151 } catch (Exception ex) {
aoqi@0 152 throw wrapper.badHelperReadMethod( ex,
aoqi@0 153 readMethod.getDeclaringClass().getName() ) ;
aoqi@0 154 }
aoqi@0 155 }
aoqi@0 156 }
aoqi@0 157
aoqi@0 158 public class ExceptionRWRMIImpl extends ExceptionRWBase
aoqi@0 159 {
aoqi@0 160 public ExceptionRWRMIImpl( Class cls )
aoqi@0 161 {
aoqi@0 162 super( cls ) ;
aoqi@0 163 setId( IDLNameTranslatorImpl.getExceptionId( cls ) ) ;
aoqi@0 164 }
aoqi@0 165
aoqi@0 166 public void write( OutputStream os, Exception ex )
aoqi@0 167 {
aoqi@0 168 os.write_string( getId() ) ;
aoqi@0 169 os.write_value( ex, getExceptionClass() ) ;
aoqi@0 170 }
aoqi@0 171
aoqi@0 172 public Exception read( InputStream is )
aoqi@0 173 {
aoqi@0 174 is.read_string() ; // read and ignore!
aoqi@0 175 return (Exception)is.read_value( getExceptionClass() ) ;
aoqi@0 176 }
aoqi@0 177 }
aoqi@0 178
aoqi@0 179 ///////////////////////////////////////////////////////////////////////////////
aoqi@0 180
aoqi@0 181 public ExceptionHandlerImpl( Class[] exceptions )
aoqi@0 182 {
aoqi@0 183 wrapper = ORBUtilSystemException.get(
aoqi@0 184 CORBALogDomains.RPC_PRESENTATION ) ;
aoqi@0 185
aoqi@0 186 int count = 0 ;
aoqi@0 187 for (int ctr=0; ctr<exceptions.length; ctr++) {
aoqi@0 188 Class cls = exceptions[ctr] ;
aoqi@0 189 if (!RemoteException.class.isAssignableFrom(cls))
aoqi@0 190 count++ ;
aoqi@0 191 }
aoqi@0 192
aoqi@0 193 rws = new ExceptionRW[count] ;
aoqi@0 194
aoqi@0 195 int index = 0 ;
aoqi@0 196 for (int ctr=0; ctr<exceptions.length; ctr++) {
aoqi@0 197 Class cls = exceptions[ctr] ;
aoqi@0 198 if (!RemoteException.class.isAssignableFrom(cls)) {
aoqi@0 199 ExceptionRW erw = null ;
aoqi@0 200 if (UserException.class.isAssignableFrom(cls))
aoqi@0 201 erw = new ExceptionRWIDLImpl( cls ) ;
aoqi@0 202 else
aoqi@0 203 erw = new ExceptionRWRMIImpl( cls ) ;
aoqi@0 204
aoqi@0 205 /* The following check is not performed
aoqi@0 206 * in order to maintain compatibility with
aoqi@0 207 * rmic. See bug 4989312.
aoqi@0 208
aoqi@0 209 // Check for duplicate repository ID
aoqi@0 210 String repositoryId = erw.getId() ;
aoqi@0 211 int duplicateIndex = findDeclaredException( repositoryId ) ;
aoqi@0 212 if (duplicateIndex > 0) {
aoqi@0 213 ExceptionRW duprw = rws[duplicateIndex] ;
aoqi@0 214 String firstClassName =
aoqi@0 215 erw.getExceptionClass().getName() ;
aoqi@0 216 String secondClassName =
aoqi@0 217 duprw.getExceptionClass().getName() ;
aoqi@0 218 throw wrapper.duplicateExceptionRepositoryId(
aoqi@0 219 firstClassName, secondClassName, repositoryId ) ;
aoqi@0 220 }
aoqi@0 221
aoqi@0 222 */
aoqi@0 223
aoqi@0 224 rws[index++] = erw ;
aoqi@0 225 }
aoqi@0 226 }
aoqi@0 227 }
aoqi@0 228
aoqi@0 229 private int findDeclaredException( Class cls )
aoqi@0 230 {
aoqi@0 231 for (int ctr = 0; ctr < rws.length; ctr++) {
aoqi@0 232 Class next = rws[ctr].getExceptionClass() ;
aoqi@0 233 if (next.isAssignableFrom(cls))
aoqi@0 234 return ctr ;
aoqi@0 235 }
aoqi@0 236
aoqi@0 237 return -1 ;
aoqi@0 238 }
aoqi@0 239
aoqi@0 240 private int findDeclaredException( String repositoryId )
aoqi@0 241 {
aoqi@0 242 for (int ctr=0; ctr<rws.length; ctr++) {
aoqi@0 243 // This may occur when rws has not been fully
aoqi@0 244 // populated, in which case the search should just fail.
aoqi@0 245 if (rws[ctr]==null)
aoqi@0 246 return -1 ;
aoqi@0 247
aoqi@0 248 String rid = rws[ctr].getId() ;
aoqi@0 249 if (repositoryId.equals( rid ))
aoqi@0 250 return ctr ;
aoqi@0 251 }
aoqi@0 252
aoqi@0 253 return -1 ;
aoqi@0 254 }
aoqi@0 255
aoqi@0 256 public boolean isDeclaredException( Class cls )
aoqi@0 257 {
aoqi@0 258 return findDeclaredException( cls ) >= 0 ;
aoqi@0 259 }
aoqi@0 260
aoqi@0 261 public void writeException( OutputStream os, Exception ex )
aoqi@0 262 {
aoqi@0 263 int index = findDeclaredException( ex.getClass() ) ;
aoqi@0 264 if (index < 0)
aoqi@0 265 throw wrapper.writeUndeclaredException( ex,
aoqi@0 266 ex.getClass().getName() ) ;
aoqi@0 267
aoqi@0 268 rws[index].write( os, ex ) ;
aoqi@0 269 }
aoqi@0 270
aoqi@0 271 public Exception readException( ApplicationException ae )
aoqi@0 272 {
aoqi@0 273 // Note that the exception ID is present in both ae
aoqi@0 274 // and in the input stream from ae. The exception
aoqi@0 275 // reader must actually read the exception ID from
aoqi@0 276 // the stream.
aoqi@0 277 InputStream is = (InputStream)ae.getInputStream() ;
aoqi@0 278 String excName = ae.getId() ;
aoqi@0 279 int index = findDeclaredException( excName ) ;
aoqi@0 280 if (index < 0) {
aoqi@0 281 excName = is.read_string() ;
aoqi@0 282 Exception res = new UnexpectedException( excName ) ;
aoqi@0 283 res.initCause( ae ) ;
aoqi@0 284 return res ;
aoqi@0 285 }
aoqi@0 286
aoqi@0 287 return rws[index].read( is ) ;
aoqi@0 288 }
aoqi@0 289
aoqi@0 290 // This is here just for the dynamicrmiiiop test
aoqi@0 291 public ExceptionRW getRMIExceptionRW( Class cls )
aoqi@0 292 {
aoqi@0 293 return new ExceptionRWRMIImpl( cls ) ;
aoqi@0 294 }
aoqi@0 295 }

mercurial