1 22 package org.jboss.iiop.rmi.marshal.strategy; 23 24 import java.util.ArrayList ; 25 import java.util.HashMap ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import java.util.Map ; 29 import java.rmi.UnexpectedException ; 30 import javax.rmi.PortableRemoteObject ; 31 32 import org.omg.CORBA.UserException ; 33 import org.omg.CORBA.portable.IDLEntity ; 34 import org.omg.CORBA_2_3.portable.InputStream ; 35 import org.omg.CORBA_2_3.portable.OutputStream ; 36 37 import org.jboss.iiop.rmi.marshal.CDRStream; 38 import org.jboss.iiop.rmi.marshal.CDRStreamReader; 39 import org.jboss.iiop.rmi.marshal.CDRStreamWriter; 40 41 50 public class StubStrategy 51 { 52 54 58 private CDRStreamWriter[] paramWriters; 59 60 63 private List exceptionList; 64 65 68 private Map exceptionMap; 69 70 74 private CDRStreamReader retvalReader; 75 76 81 private Class retvalRemoteInterface; 82 84 104 public static StubStrategy forMethod(String [] paramTypes, 105 String [] excepIds, 106 String [] excepTypes, 107 String retvalType, 108 ClassLoader cl) 109 { 110 115 return new StubStrategy(paramTypes, excepIds, 116 excepTypes, retvalType, cl); 117 } 118 119 120 122 141 private StubStrategy(String [] paramTypes, String [] excepIds, 142 String [] excepTypes, String retvalType, 143 ClassLoader cl) 144 { 145 if (cl == null) { 146 cl = Thread.currentThread().getContextClassLoader(); 147 } 148 149 int len = paramTypes.length; 151 paramWriters = new CDRStreamWriter[len]; 152 for (int i = 0; i < len; i++) { 153 paramWriters[i] = CDRStream.writerFor(paramTypes[i], cl); 154 } 155 156 exceptionList = new ArrayList (); 158 exceptionMap = new HashMap (); 159 len = excepIds.length; 160 for (int i = 0; i < len; i++) { 161 try { 162 Class clz = cl.loadClass(excepTypes[i]); 163 exceptionList.add(clz); 164 ExceptionReader exceptionReader = 165 new ExceptionReader(clz, excepIds[i]); 166 exceptionMap.put(exceptionReader.getReposId(), exceptionReader); 167 } 168 catch (ClassNotFoundException e) { 169 throw new RuntimeException ("Error loading class " 170 + excepTypes[i] + ": " + e); 171 } 172 } 173 174 retvalReader = CDRStream.readerFor(retvalType, cl); 176 177 if (retvalType.charAt(0) == 'R') { 179 try { 180 retvalRemoteInterface = cl.loadClass(retvalType.substring(1)); 181 } 182 catch (ClassNotFoundException e) { 183 throw new RuntimeException ("Error loading class " 184 + retvalType.substring(1) + ": " + e); 185 } 186 } 187 } 188 189 191 197 public void writeParams(OutputStream out, Object [] params) 198 { 199 int len = params.length; 200 201 if (len != paramWriters.length) { 202 throw new RuntimeException ("Cannot marshal parameters: " 203 + "unexpected number of parameters"); 204 } 205 for (int i = 0; i < len; i++ ) { 206 paramWriters[i].write(out, params[i]); 207 } 208 } 209 210 213 public boolean isNonVoid() 214 { 215 return (retvalReader != null); 216 } 217 218 224 public Object readRetval(InputStream in) 225 { 226 return retvalReader.read(in); 227 } 228 229 236 public Exception readException(String id, InputStream in) 237 { 238 ExceptionReader exceptionReader = (ExceptionReader)exceptionMap.get(id); 239 if (exceptionReader == null) { 240 return new UnexpectedException (id); 241 } 242 else { 243 return exceptionReader.read(in); 244 } 245 } 246 247 256 public boolean isDeclaredException(Throwable t) 257 { 258 Iterator iterator = exceptionList.iterator(); 259 while (iterator.hasNext()) { 260 if (((Class )iterator.next()).isInstance(t)) { 261 return true; 262 } 263 } 264 return false; 265 } 266 267 275 public Object convertLocalRetval(Object obj) 276 { 277 if (retvalRemoteInterface == null) 278 return obj; 279 else 280 return PortableRemoteObject.narrow(obj, retvalRemoteInterface); 281 } 282 283 285 289 private static class ExceptionReader 290 { 291 294 private Class clz; 295 296 299 private String reposId; 300 301 307 private java.lang.reflect.Method readMethod = null; 308 309 313 ExceptionReader(Class clz, String reposId) 314 { 315 this.clz = clz; 316 if (IDLEntity .class.isAssignableFrom(clz) 317 && UserException .class.isAssignableFrom(clz)) { 318 319 String helperClassName = clz.getName() + "Helper"; 321 try { 322 Class helperClass = 323 clz.getClassLoader().loadClass(helperClassName); 324 Class [] paramTypes = 325 { org.omg.CORBA.portable.InputStream .class }; 326 readMethod = helperClass.getMethod("read", paramTypes); 327 328 java.lang.reflect.Method idMethod = 331 helperClass.getMethod("id", null); 332 this.reposId = (String )idMethod.invoke(null, null); 333 } 334 catch (ClassNotFoundException e) { 335 throw new RuntimeException ("Error loading class " 336 + helperClassName + ": " + e); 337 } 338 catch (NoSuchMethodException e) { 339 throw new RuntimeException ("No read/id method in helper class " 340 + helperClassName + ": " + e); 341 } 342 catch (IllegalAccessException e) { 343 throw new RuntimeException ("Internal error: " + e); 344 } 345 catch (java.lang.reflect.InvocationTargetException e) { 346 throw new RuntimeException ("Exception in call to " 347 + helperClassName + ": " 348 + e.getTargetException()); 349 } 350 } 351 else { 352 this.reposId = reposId; 355 } 356 } 357 358 public String getReposId() 359 { 360 return reposId; 361 } 362 363 366 public Exception read(InputStream in) 367 { 368 if (readMethod != null) { 369 try { 370 return (Exception )readMethod.invoke(null, new Object [] { in }); 371 } 372 catch (IllegalAccessException e) { 373 throw new RuntimeException ("Internal error: " + e); 374 } 375 catch (java.lang.reflect.InvocationTargetException e) { 376 throw new RuntimeException ("Exception unmarshaling IDLEntity: " 377 + e.getTargetException()); 378 } 379 } 380 else { 381 in.read_string(); return (Exception )in.read_value(clz); 383 } 384 } 385 386 } 388 } 389 | Popular Tags |