1 package org.coach.idltree; 2 3 import org.w3c.dom.Node ; 4 import org.omg.DynamicAny.*; 5 import org.omg.CORBA.ORB ; 6 import org.omg.CORBA.TypeCode ; 7 import org.omg.CORBA.Any ; 8 import org.omg.CORBA.TCKind ; 9 import org.coach.tracing.api.*; 10 import java.util.*; 11 import java.lang.reflect.*; 12 13 20 public class IdlOperation extends IdlNode implements IdlWritable { 21 private String [] allTypes; 22 private String [] allNames; 23 private String [] allDirections; 24 private String [] directions; 25 26 protected String [] names = new String [0]; 27 protected String [] exceptions = new String [0]; 28 protected Parameter[] params = new Parameter[0]; 29 protected String [] typeNames = new String [0]; 30 protected transient TypeCode [] types = new TypeCode [0]; 31 protected transient TypeCode [] exceptionTypes = new TypeCode [0]; 32 33 protected String name; 34 protected boolean isOneway; 35 36 protected IdlOperation() { 37 isLeaf = false; 38 isMutable = false; 39 setUserObject(this); 40 type = "operation"; 41 } 42 43 54 public IdlOperation(String id, String name, Parameter[] p, org.omg.CORBA.Any [] values, boolean isOneway) { 55 this(); 56 try { 57 this.isOneway = isOneway; 58 this.id = id; 59 this.name = name; 60 if (p != null) { 61 params = p; 62 names = new String [p.length]; 63 for (int i = 0; i < p.length; i++) { 64 names[i] = p[i].name; 65 add(new IdlParameter(p[i].dir, p[i].name, values[i])); 66 } 67 } 68 } catch (Exception e) { 69 e.printStackTrace(); 70 } 71 } 72 73 78 public String getName() { 79 return name; 80 } 81 82 87 public String [] getParameterNames() { 88 return names; 89 } 90 91 98 public IdlNode getParameter(String name) { 99 for (int i = 0; i < names.length; i++) { 100 if (names[i].equals(name)) { 101 return ((IdlParameter)getChildAt(i)).getParameter(); 102 } 103 } 104 return null; 105 } 106 107 112 public IdlNode[] getParameters() { 113 IdlNode[] members = new IdlNode[getChildCount()]; 114 for(int i = 0; i < members.length; i++) { 115 members[i] = ((IdlParameter)getChildAt(i)).getParameter(); 116 } 117 return members; 118 } 119 120 125 public String toString() { 126 return "operation " + id + " " + name; 127 } 128 129 136 public void getValues(org.omg.CORBA.Any [] values) { 137 if (values.length != getChildCount()) { 138 System.out.println("Invalid array length for any values"); 139 return; 140 } 141 for(int i = 0; i < getChildCount(); i++) { 142 values[i] = ((IdlParameter)getChildAt(i)).getParameter().toAny(); 143 } 144 } 145 146 148 165 public IdlOperation(String xml) { 166 this(XmlNode.getNode(xml)); 167 } 168 169 175 public IdlOperation(String id, String name) { 176 this(); 177 try { 178 this.id = id; 179 this.name = name; 180 initParameters(id); 181 } catch (Exception e) { 182 e.printStackTrace(); 183 } 184 } 185 186 IdlOperation(Node n) { 187 this(); 188 try { 189 id = XmlNode.getId(n); 190 name = XmlNode.getName(n); 191 192 initParameters(id); 193 194 Node [] nodes = XmlNode.childElements(n); 195 196 removeAllChildren(); 197 for (int i = 0; i < nodes.length; i++) { 198 if (!names[i].equals(XmlNode.getName(nodes[i]))) { 199 throw new RuntimeException ("Unknown parameter name: " + XmlNode.getName(nodes[i]) + " for " + id + " " + name + " expected name: " + names[i]); 200 } 201 add(new IdlParameter(nodes[i])); 202 } 203 } catch (Exception e) { 204 throw new RuntimeException (e.toString()); 205 } 206 } 207 208 214 private void initParameters(String id) { 215 try { 216 allDirections = XmlNode.getParameterDirections(id, name); 217 allTypes = XmlNode.getParameterTypes(id, name); 218 allNames = XmlNode.getParameterNames(id, name); 219 exceptions = XmlNode.getParameterExceptions(id, name); 220 221 for (int i = 0; i < allDirections.length; i++) { 222 if (allDirections[i].equals("oneway")) { 223 isOneway = true; 224 } 225 } 226 227 exceptionTypes = new org.omg.CORBA.TypeCode [exceptions.length]; 228 if (exceptions.length > 0) { 229 isMutable = true; 230 value = "no exception"; 231 for (int i = 0; i < exceptions.length; i++) { 232 exceptionTypes[i] = XmlNode.type(exceptions[i]); 233 } 234 } 235 236 removeAllChildren(); 237 for (int i = 0; i < allNames.length; i++) { 238 if (allDirections[i].startsWith("in")) { 239 add(new IdlParameter(allDirections[i], allNames[i], XmlNode.type(allTypes[i]))); 240 } 241 } 242 243 int idx = getChildCount(); 245 names = new String [idx]; 246 typeNames = new String [idx]; 247 directions = new String [idx]; 248 params = new Parameter[idx]; 249 types = new org.omg.CORBA.TypeCode [idx]; 250 idx = 0; 251 for (int i = 0; i < allNames.length; i++) { 252 if (allDirections[i].startsWith("in")) { 253 params[idx] = new Parameter(allDirections[i], allTypes[i], allNames[i]); 254 names[idx] = allNames[i]; 255 types[idx] = XmlNode.type(allTypes[i]); 256 typeNames[idx] = allTypes[i]; 257 directions[idx] = allDirections[i]; 258 idx++; 259 } 260 } 261 } catch (Exception e) { 262 e.printStackTrace(); 263 } 264 } 265 266 271 public String [] getNames() { 272 return names; 273 } 274 275 280 public String [] getTypes() { 281 return typeNames; 282 } 283 284 289 public String [] getDirections() { 290 return directions; 291 } 292 293 298 public void write(IdlWriter w) { 299 write(this, w); 300 } 301 302 public static void write(IdlOperation n, IdlWriter w) { 303 w.write_start_operation(n.getName(), n.getId()); 304 for(int i = 0; i < n.getChildCount(); i++) { 305 XmlNode.write((IdlNode)n.getChildAt(i), w); 306 } 307 w.write_end_operation(); 308 } 309 310 public IdlReply invoke(org.omg.CORBA.ORB orb, org.omg.CORBA.Object target) { 311 return dii_invoke(orb, target); 312 } 313 314 322 public IdlReply object_invoke(org.omg.CORBA.ORB orb, org.omg.CORBA.Object target) { 323 org.omg.CORBA.portable.ObjectImpl objImpl = null; 324 org.omg.CORBA.portable.OutputStream out = null; 325 org.omg.CORBA.portable.InputStream in = null; 326 327 try { 328 330 objImpl = (org.omg.CORBA.portable.ObjectImpl )target; 331 String oprName = name; 332 Parameter[] inPar = null; 333 Parameter[] outPar = null; 334 org.omg.CORBA.Any [] inVal = null; 335 org.omg.CORBA.Any [] outVal = null; 336 337 out = objImpl._request(oprName, !isOneway); 338 inPar = new Parameter[params.length]; 339 inVal = new org.omg.CORBA.Any [params.length]; 340 for (int i = 0; i < names.length; i++) { 341 IdlParameter p = (IdlParameter)getChildAt(i); 342 IdlNode n = p.getParameter(); 343 inPar[i] = new Parameter(directions[i], typeNames[i], names[i]); 344 inVal[i] = n.toAny(); 345 } 346 347 for (int i = 0; i < inVal.length; i++) { 348 inVal[i].write_value(out); 349 } 350 System.err.println("++++++++++++++ Before invoke: " + org.coach.tracing.service.ThreadContext.getEventCounter()); 351 try { 352 in = objImpl._invoke(out); 353 } catch(org.omg.CORBA.portable.ApplicationException aex) { 354 in = aex.getInputStream(); 355 String aex_id = aex.getId(); 356 org.omg.CORBA.Any aex_value = orb.create_any(); 357 org.omg.CORBA.TypeCode etc = XmlNode.type(aex_id); 358 aex_value.read_value(in, etc); 359 IdlReply exReply = new IdlReply(id, name); 360 exReply.setException(aex_value); 361 return exReply; 362 } 363 System.err.println("++++++++++++++ After invoke: " + org.coach.tracing.service.ThreadContext.getEventCounter()); 364 365 IdlReply reply = new IdlReply(id, name); 366 if (!isOneway) { 367 outPar = reply.getParameterInfo(); 368 outVal = reply.readValues(in); 369 } 370 System.err.println("++++++++++++++ After reply: " + org.coach.tracing.service.ThreadContext.getEventCounter()); 371 372 return reply; 373 } catch (Throwable t) { 374 t.printStackTrace(); 375 javax.swing.JOptionPane.showMessageDialog(null, t.toString()); 376 } 377 378 return null; 379 } 380 381 389 public IdlReply dii_invoke(org.omg.CORBA.ORB orb, org.omg.CORBA.Object target) { 390 org.omg.CORBA.Request request = target._request(name); 391 392 IdlNode[] nodes = getParameters(); 393 Vector v = new Vector(); 395 int ni = 0; 396 for (int i = 0; i < allDirections.length; i++) { 397 if (allDirections[i].equals("in")) { 398 Any any = nodes[ni++].toAny(); 399 request.add_named_in_arg(allNames[i]).read_value(any.create_input_stream(), any.type()); 400 } else if (allDirections[i].equals("inout")) { 401 Any any = nodes[ni++].toAny(); 402 Any inout = request.add_named_inout_arg(allNames[i]); 403 inout.read_value(any.create_input_stream(), any.type()); 404 v.add(inout); 405 } else if (allDirections[i].equals("out")) { 406 Any out = request.add_named_out_arg(allNames[i]); 407 out.type(XmlNode.type(allTypes[i])); 408 v.add(out); 409 } 410 } 411 412 414 request.set_return_type(XmlNode.type(allTypes[allTypes.length - 1])); 415 for (int i = 0; i < exceptionTypes.length; i++) { 416 request.exceptions().add(exceptionTypes[i]); 417 } 418 request.invoke(); 419 Exception ex = request.env().exception(); 420 421 IdlReply reply = new IdlReply(id, name); 422 if (ex != null) { 423 org.omg.CORBA.UnknownUserException uex = (org.omg.CORBA.UnknownUserException )ex; 424 reply.setException(uex.except); 425 return reply; 426 } 427 428 if (reply.hasReturn()) { 429 v.add(request.result().value()); 430 } 431 432 Any[] replyValues = new Any[v.size()]; 433 v.toArray(replyValues); 434 435 reply.setValues(replyValues); 436 437 return reply; 438 } 439 } | Popular Tags |