1 package org.coach.idltree; 2 3 import org.w3c.dom.Node ; 4 import org.omg.CORBA.ORB ; 5 import org.omg.CORBA.TypeCode ; 6 import org.omg.CORBA.Any ; 7 import org.omg.CORBA.TCKind ; 8 import java.util.*; 9 import org.coach.tracing.api.*; 10 import java.lang.reflect.*; 11 12 21 public class IdlReply extends IdlNode implements IdlWritable { 22 private String [] allTypes; 23 private String [] allNames; 24 private String [] allDirections; 25 protected boolean isException; 26 protected String name; 27 protected String [] names = new String [0]; 28 protected String [] exceptions = new String [0]; 29 protected Parameter[] params = new Parameter[0]; 30 protected transient TypeCode [] types = new TypeCode [0]; 31 protected transient TypeCode [] exceptionTypes = new TypeCode [0]; 32 protected boolean hasReturn; 33 34 protected IdlReply() { 35 isLeaf = false; 36 isMutable = false; 37 setUserObject(this); 38 type = "reply"; 39 } 40 41 51 public IdlReply(String id, String name, Parameter[] p, Any [] values) { 52 this(); 53 try { 54 this.id = id; 55 this.name = name; 56 setField(name); 57 if (p != null) { 58 params = p; 59 names = new String [p.length]; 60 for (int i = 0; i < p.length; i++) { 61 if (p[i].dir.equals("return")) { 62 p[i].name = "return"; 63 if(!p[i].type.equals("void")) { 64 hasReturn = true; 65 } 66 } 67 names[i] = p[i].name; 68 add(new IdlParameter(p[i].dir, p[i].name, values[i])); 69 } 70 } 71 types = new org.omg.CORBA.TypeCode [values.length]; 72 for (int i = 0; i < values.length; i++) { 73 types[i] = values[i].type(); 74 } 75 } catch (Exception e) { 76 e.printStackTrace(); 77 } 78 } 79 80 92 public IdlReply(String id, String name, Parameter[] p, Any [] values, String [] exceptions, TypeCode [] exceptionTypes) { 93 this(id, name, p, values); 94 this.exceptions = exceptions; 95 this.exceptionTypes = exceptionTypes; 96 types = new org.omg.CORBA.TypeCode [values.length]; 97 for (int i = 0; i < values.length; i++) { 98 types[i] = values[i].type(); 99 } 100 } 101 102 111 public IdlReply(String id, String name, Any exValue) { 112 this(); 113 try { 114 this.id = id; 115 this.name = name; 116 setField(name); 117 this.isException = true; 118 IdlNode n = new IdlException(exValue); 119 n.field = "exception"; 120 hasReturn = false; 121 add(n); 122 } catch (Exception e) { 123 e.printStackTrace(); 124 } 125 } 126 127 132 public String getName() { 133 return name; 134 } 135 136 141 public String [] getParameterNames() { 142 return names; 143 } 144 145 146 151 public Parameter[] getParameterInfo() { 152 return params; 153 } 154 155 160 public String [] getExceptions() { 161 return exceptions; 162 } 163 164 171 public IdlNode getParameter(String name) { 172 for (int i = 0; i < names.length; i++) { 173 if (names[i].equals(name)) { 174 return ((IdlParameter)getChildAt(i)).getParameter(); 175 } 176 } 177 return null; 178 } 179 180 186 public void setException(int i) { 187 try { 188 if (i < 0) { 189 value = "no exception"; 190 setDefaults(); 191 } else { 192 isException = true; 193 removeAllChildren(); 194 IdlNode n = new IdlException(exceptionTypes[i]); 195 n.field = "exception"; 196 value = exceptions[i]; 197 hasReturn = false; 198 add(n); 199 } 200 } catch (Throwable t) { 201 t.printStackTrace(); 202 } 203 } 204 205 210 public void setException(org.omg.CORBA.Any exValue) { 211 try { 212 IdlNode n = new IdlException(exValue); 213 n.field = "exception"; 214 value = exValue.type().id(); 215 removeAllChildren(); 216 isException = true; 217 hasReturn = false; 218 add(n); 219 } catch (Throwable t) { 220 t.printStackTrace(); 221 } 222 } 223 224 protected void setDefaults() { 225 removeAllChildren(); 226 isException = false; 227 for (int i = 0; i < params.length; i++) { 228 add(new IdlParameter(params[i].dir, params[i].name, create(types[i]).toAny())); 229 } 230 } 231 232 237 public boolean isException() { 238 return isException; 239 } 240 241 246 public boolean hasReturn() { 247 return hasReturn; 248 } 249 250 255 public IdlNode getException() { 256 if (isException && getChildCount() == 1) { 257 return (IdlNode)getChildAt(0); 258 } 259 return null; 260 } 261 262 267 public IdlNode[] getParameters() { 268 IdlNode[] members = new IdlNode[getChildCount()]; 269 for(int i = 0; i < members.length; i++) { 270 members[i] = ((IdlParameter)getChildAt(i)).getParameter(); 271 } 272 return members; 273 } 274 275 280 public String toString() { 281 return "reply " + id + " " + name; 282 } 283 284 291 public void getValues(Any [] values) { 292 if (values.length != getChildCount()) { 293 throw new RuntimeException ("Invalid array length for any values"); 294 } 295 for(int i = 0; i < getChildCount(); i++) { 296 values[i] = ((IdlParameter)getChildAt(i)).getParameter().toAny(); 297 } 298 } 299 300 307 public void setValues(Any [] values) { 308 if (values.length != params.length) { 309 throw new RuntimeException ("Value array length (" + values.length + ") does not match number of parameters (" + params.length + ")"); 310 } 311 removeAllChildren(); 312 for (int i = 0; i < params.length; i++) { 313 add(new IdlParameter(params[i].dir, params[i].name, values[i])); 314 } 315 } 316 317 326 public Any [] readValues(org.omg.CORBA.portable.InputStream in) { 327 IdlNode[] replyParameters = getParameters(); 328 Any [] outVal = new Any [replyParameters.length]; 329 330 try { 331 if (hasReturn) { 332 org.omg.CORBA.TypeCode ptc = replyParameters[replyParameters.length - 1].getTypeCode(); 335 outVal[replyParameters.length - 1] = orb.create_any(); 336 outVal[replyParameters.length - 1].read_value(in, ptc); 337 for (int i = 0; i < replyParameters.length - 1; i++) { 338 outVal[i] = orb.create_any(); 339 ptc = replyParameters[i].getTypeCode(); 340 outVal[i].read_value(in, ptc); 341 } 342 } else { 343 for (int i = 0; i < replyParameters.length; i++) { 344 outVal[i] = orb.create_any(); 345 org.omg.CORBA.TypeCode ptc = replyParameters[i].getTypeCode(); 346 outVal[i].read_value(in, ptc); 347 } 348 } 349 } catch (Throwable t) { 350 t.printStackTrace(); 351 } 352 setValues(outVal); 353 354 return outVal; 355 } 356 357 362 public void write(org.omg.CORBA.portable.OutputStream os) { 363 for(int i = 0; i < getChildCount(); i++) { 364 ((IdlParameter)getChildAt(i)).write(os); 365 } 366 } 367 368 373 public void read(org.omg.CORBA.portable.InputStream is) { 374 for(int i = 0; i < getChildCount(); i++) { 375 ((IdlParameter)getChildAt(i)).read(is); 376 } 377 } 378 379 381 398 public IdlReply(String xml) { 399 this(XmlNode.getNode(xml)); 400 } 401 402 IdlReply(Node n) { 403 this(); 404 try { 405 id = XmlNode.getId(n); 406 name = XmlNode.getName(n); 407 setField(name); 408 409 initParameters(id); 410 411 removeAllChildren(); 412 413 Node [] nodes = XmlNode.childElements(n); 414 if (nodes.length == 1 && nodes[0].getNodeName().toUpperCase().equals("EXCEPTION")) { 415 add(XmlNode.getIdlNode(nodes[0])); 416 hasReturn = false; 417 } else { 418 for (int i = 0; i < nodes.length; i++) { 419 if (!names[i].equals("return") && !names[i].equals(XmlNode.getName(nodes[i]))) { 420 throw new RuntimeException ("Unknown parameter name: " + XmlNode.getName(nodes[i]) + " for " + id + " " + name + " expected name: " + names[i]); 421 } 422 add(new IdlParameter(nodes[i])); 423 } 424 } 425 } catch (Exception e) { 426 e.printStackTrace(); 427 throw new RuntimeException (e.toString()); 428 } 429 } 430 431 437 public IdlReply(String id, String name) { 438 this(); 439 try { 440 this.id = id; 443 this.name = name; 444 setField(name); 445 446 initParameters(id); 447 448 } catch (Exception e) { 449 e.printStackTrace(); 450 } 451 } 452 453 private void initParameters(String id) { 454 try { 455 allDirections = XmlNode.getParameterDirections(id, name); 456 allTypes = XmlNode.getParameterTypes(id, name); 457 allNames = XmlNode.getParameterNames(id, name); 458 exceptions = XmlNode.getParameterExceptions(id, name); 459 460 exceptionTypes = new org.omg.CORBA.TypeCode [exceptions.length]; 461 if (exceptions.length > 0) { 462 isMutable = true; 463 value = "no exception"; 464 for (int i = 0; i < exceptions.length; i++) { 465 exceptionTypes[i] = XmlNode.type(exceptions[i]); 466 } 467 } 468 469 removeAllChildren(); 470 for (int i = 0; i < allNames.length; i++) { 471 if (allDirections[i].endsWith("out") || (allDirections[i].equals("return") && !allTypes[i].equals("void"))) { 472 add(new IdlParameter(allDirections[i], allNames[i], XmlNode.type(allTypes[i]))); 473 } 474 } 475 476 int idx = getChildCount(); 478 names = new String [idx]; 479 params = new Parameter[idx]; 480 types = new org.omg.CORBA.TypeCode [idx]; 481 idx = 0; 482 for (int i = 0; i < allNames.length; i++) { 483 if (allDirections[i].endsWith("out") || (allDirections[i].equals("return") && !allTypes[i].equals("void"))) { 484 params[idx] = new Parameter(allDirections[i], allTypes[i], allNames[i]); 485 names[idx] = allNames[i]; 486 types[idx] = XmlNode.type(allTypes[i]); 487 idx++; 488 } 489 if (allDirections[i].equals("return") && !allTypes[i].equals("void")) { 490 hasReturn = true; 491 } 492 } 493 } catch (Exception e) { 494 e.printStackTrace(); 495 } 496 } 497 498 503 public void write(IdlWriter w) { 504 write(this, w); 505 } 506 507 public static void write(IdlReply n, IdlWriter w) { 508 w.write_start_reply(n.getName(), n.getId()); 509 for(int i = 0; i < n.getChildCount(); i++) { 510 XmlNode.write((IdlNode)n.getChildAt(i), w); 511 } 512 w.write_end_reply(); 513 } 514 } | Popular Tags |