1 22 package org.jboss.iiop.rmi.marshal.strategy; 23 24 import java.lang.reflect.Method ; 25 26 import java.rmi.RemoteException ; 27 28 import org.omg.CORBA.UserException ; 29 import org.omg.CORBA.portable.IDLEntity ; 30 import org.omg.CORBA.portable.UnknownException ; 31 import org.omg.CORBA_2_3.portable.InputStream ; 32 import org.omg.CORBA_2_3.portable.OutputStream ; 33 34 import org.jboss.iiop.rmi.ExceptionAnalysis; 35 import org.jboss.iiop.rmi.RMIIIOPViolationException; 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 SkeletonStrategy 51 { 52 56 private CDRStreamReader[] paramReaders; 57 58 61 private Method m; 62 63 69 private ExceptionWriter[] excepWriters; 70 71 75 private CDRStreamWriter retvalWriter; 76 77 79 82 public SkeletonStrategy(Method m) 83 { 84 this.m = m; 86 87 Class [] paramTypes = m.getParameterTypes(); 89 int len = paramTypes.length; 90 paramReaders = new CDRStreamReader[len]; 91 for (int i = 0; i < len; i++) { 92 paramReaders[i] = CDRStream.readerFor(paramTypes[i]); 93 } 94 95 Class [] excepTypes = m.getExceptionTypes(); 97 len = excepTypes.length; 98 int n = 0; 99 for (int i = 0; i < len; i++) { 100 if (!RemoteException .class.isAssignableFrom(excepTypes[i])) { 101 n++; 102 } 103 } 104 excepWriters = new ExceptionWriter[n]; 105 int j = 0; 106 for (int i = 0; i < len; i++) { 107 if (!RemoteException .class.isAssignableFrom(excepTypes[i])) { 108 excepWriters[j++] = new ExceptionWriter(excepTypes[i]); 109 } 110 } 111 ExceptionWriter.arraysort(excepWriters); 112 113 retvalWriter = CDRStream.writerFor(m.getReturnType()); 115 } 116 117 123 public Object [] readParams(InputStream in) 124 { 125 int len = paramReaders.length; 126 Object [] params = new Object [len]; 127 for (int i = 0; i < len; i++ ) { 128 params[i] = paramReaders[i].read(in); 129 } 130 return params; 131 } 132 133 136 public Method getMethod() 137 { 138 return m; 139 } 140 141 145 public boolean isNonVoid() 146 { 147 return (retvalWriter != null); 148 } 149 150 156 public void writeRetval(OutputStream out, Object retVal) 157 { 158 retvalWriter.write(out, retVal); 159 } 160 161 167 public void writeException(OutputStream out, Exception e) 168 { 169 int len = excepWriters.length; 170 for (int i = 0; i < len; i++) { 171 if (excepWriters[i].getExceptionClass().isInstance(e)) { 172 excepWriters[i].write(out, e); 173 return; 174 } 175 } 176 throw new UnknownException (e); 177 } 178 179 181 185 private static class ExceptionWriter 186 implements CDRStreamWriter 187 { 188 191 private Class clz; 192 193 199 private java.lang.reflect.Method writeMethod = null; 200 201 207 private String reposId; 208 209 213 ExceptionWriter(Class clz) 214 { 215 this.clz = clz; 216 if (IDLEntity .class.isAssignableFrom(clz) 217 && UserException .class.isAssignableFrom(clz)) { 218 219 String helperClassName = clz.getName() + "Helper"; 221 try { 222 Class helperClass = 223 clz.getClassLoader().loadClass(helperClassName); 224 Class [] paramTypes = 225 { org.omg.CORBA.portable.OutputStream .class, clz }; 226 writeMethod = helperClass.getMethod("write", paramTypes); 227 } 228 catch (ClassNotFoundException e) { 229 throw new RuntimeException ("Error loading class " 230 + helperClassName + ": " + e); 231 } 232 catch (NoSuchMethodException e) { 233 throw new RuntimeException ("No write method in helper class " 234 + helperClassName + ": " + e); 235 } 236 237 } 238 else { 239 try { 242 this.reposId = ExceptionAnalysis.getExceptionAnalysis(clz) 243 .getExceptionRepositoryId(); 244 } 245 catch (RMIIIOPViolationException e) { 246 throw new RuntimeException ("Cannot obtain " 247 + "exception repository id for " 248 + clz.getName() + ":\n" + e); 249 } 250 } 251 } 252 253 256 Class getExceptionClass() 257 { 258 return clz; 259 } 260 261 264 public void write(OutputStream out, Object excep) 265 { 266 if (writeMethod != null) { 267 try { 268 writeMethod.invoke(null, new Object [] { out, excep }); 269 } 270 catch (IllegalAccessException e) { 271 throw new RuntimeException ("Internal error: " + e); 272 } 273 catch (java.lang.reflect.InvocationTargetException e) { 274 throw new RuntimeException ("Exception marshaling IDLEntity: " 275 + e.getTargetException()); 276 } 277 } 278 else { 279 out.write_string(reposId); 280 out.write_value((Exception )excep, clz); 281 } 282 } 283 284 288 static void arraysort(ExceptionWriter[] a) 289 { 290 int len = a.length; 291 292 for (int i = 0; i < len - 1; i++) { 293 for (int j = i + 1; j < len; j++) { 294 if (a[i].clz.isAssignableFrom(a[j].clz)) { 295 ExceptionWriter tmp = a[i]; 296 297 a[i] = a[j]; 298 a[j] = tmp; 299 } 300 } 301 } 302 } 303 304 } 306 } 307 | Popular Tags |