1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 package org.coach.tcl; 26 27 import tcl.lang.*; 28 import org.coach.idltree.*; 29 import java.io.*; 30 import java.util.*; 31 import org.omg.Components.*; 32 import org.coach.util.IorPrinter; 33 import org.coach.tracing.service.ThreadContext; 34 import org.coach.tracing.service.ThreadContext; 35 import org.omg.CosNaming.*; 36 import org.omg.CosNaming.NamingContextPackage.*; 37 38 public class TclLib { 39 private Interp tclInterp; 40 private org.omg.CORBA.ORB orb; 41 private org.omg.CosNaming.NamingContext rootContext; 42 43 public TclLib(Interp tclInterp){ 44 orb = org.objectweb.openccm.corba.TheORB.getORB(); 45 try { 46 org.omg.CORBA.Object obj = orb.resolve_initial_references("NameService"); 47 rootContext = org.omg.CosNaming.NamingContextHelper.narrow(obj); 48 } catch (org.omg.CORBA.ORBPackage.InvalidName e1) { 49 } 51 52 this.tclInterp = tclInterp; 53 54 try{ 55 tclInterp.setVar("tclShell", tcl.lang.ReflectObject.newInstance(tclInterp, TclLib.class, this), 0); 56 tclInterp.eval("proc ccmFindObject {objectName} {global tclShell; return [$tclShell findObject $objectName]}"); 57 tclInterp.eval("proc ccmGetFacets {componentNode} {global tclShell; set Facets [$tclShell listFacets $componentNode]; for {set i 0} {$i < [$Facets length]} {incr i} {lappend lstr [$Facets get $i]}; return $lstr}"); 58 tclInterp.eval("proc ccmGetOperations {facetRef} {global tclShell; set Methods [$tclShell listMethods2 $facetRef]; for {set i 0} {$i < [$Methods length]} {incr i} {lappend lstr [$Methods get $i]}; return $lstr}"); 59 tclInterp.eval("proc ccmProvide {componentNode facetName} {global tclShell; return [$tclShell getFacet $componentNode $facetName]}"); 60 tclInterp.eval("proc ccmInvoke {componentNode ccmOperation} {global tclShell; return [$tclShell invoke $componentNode $ccmOperation]}"); 61 tclInterp.eval("proc ccmGetOperation {facetRef operationName} {global tclShell; return [$tclShell getXmlOperation $facetRef $operationName]} "); 62 tclInterp.eval("proc ccmCreateIdlNode {rid} {global tclShell; return [$tclShell newType $rid]} "); 63 tclInterp.eval("proc ccmFromXml {xmlString} {global tclShell; return [$tclShell xmlToType $xmlString]} "); 64 tclInterp.eval("proc ccmToXml {idlNode} {global tclShell; return [$tclShell typeToXml $idlNode]} "); 65 tclInterp.eval("proc ccmGetValue {idlNode {path \"\"}} {global tclShell; $tclShell getValue $idlNode $path} "); 66 tclInterp.eval("proc ccmSetValue {idlNode value {path \"\"}} {global tclShell; $tclShell setValue $idlNode $path $value} "); 67 tclInterp.eval("proc ccmSetNode {idlNode value {path \"\"}} {global tclShell; $tclShell setNode $idlNode $path $value} "); 68 tclInterp.eval("proc ccmGetNode {idlNode path} {global tclShell; $tclShell getNode $idlNode $path} "); 69 tclInterp.eval("proc ccmGetProperty {propName} {global tclShell; return [$tclShell getProperty $propName]} "); 70 tclInterp.eval("proc ccmSetProperty {propName propValue} {global tclShell; return [$tclShell setProperty $propName $propValue]} "); 71 tclInterp.eval("proc ccmFieldValues {node} {global tclShell; return [$tclShell printNode $node]}"); 72 tclInterp.eval("proc ccmFields {node} {global tclShell; return [$tclShell printFields $node]}"); 73 tclInterp.eval("proc ccmValues {node} {global tclShell; return [$tclShell printValues $node]}"); 74 tclInterp.eval("proc ccmFromIor {ior} {global tclShell; return [$tclShell fromIor $ior]} "); 75 tclInterp.eval("proc ccmCreateReply {id method} {global tclShell; return [$tclShell createXmlReply $id $method]}"); 76 tclInterp.eval("proc ccmCreateOperation {id method} {global tclShell; return [$tclShell createXmlOperation $id $method]}"); 77 tclInterp.eval("proc ccmSetTrailLabel {label} {global tclShell; $tclShell setTrailLabel $label} "); 78 tclInterp.eval("proc ccmGetTrailLabel {} {global tclShell; return [$tclShell getTrailLabel]} "); 79 80 tclInterp.eval("proc ccmHelp {} {global tclShell; return [$tclShell getHelp]} "); 81 82 Properties props = System.getProperties(); 83 for (Enumeration e = props.keys(); e.hasMoreElements();) { 84 String key = (String )e.nextElement(); 85 if (System.getProperty(key).endsWith("\\")) { 86 tclInterp.eval("set " + key + " {" + System.getProperty(key) + " }"); 87 } else { 88 tclInterp.eval("set " + key + " {" + System.getProperty(key) + "}"); 89 } 90 } 91 } catch (tcl.lang.TclException e) { 92 System.out.println(tclInterp.getResult()); 93 } 94 } 95 96 public void setOrb(org.omg.CORBA.ORB orb) { 97 if (this.orb == null) { 98 this.orb = orb; 99 } 100 } 101 102 public String getHelp() { 103 String h = "Help for COACH TCL Scripting\n" + 104 " Procedures:\n" + 105 " ccmFindObject {objectName} \n" + 106 " ccmGetFacets {componentNode} \n" + 107 " ccmGetOperations {facetNode} \n" + 108 " ccmProvide {componentNode facetName} \n" + 109 " ccmInvoke {componentNode ccmOperation} \n" + 110 " ccmGetOperation {facetNode operationName} \n" + 111 " ccmCreateOperation {facetId operation} \n" + 112 " ccmCreateReply {facetId operation} \n" + 113 " ccmCreateIdlNode {id} \n" + 114 " ccmFromXml {xmlString} \n" + 115 " ccmToXml {idlNode} \n" + 116 " ccmGetValue {idlNode {path \"\"}} \n" + 117 " ccmSetValue {idlNode value {path \"\"}} \n" + 118 " ccmSetNode {idlNode value {path \"\"}} \n" + 119 " ccmGetNode {idlNode path} \n" + 120 " ccmFieldValues {node} \n" + 121 " ccmFields {node} \n" + 122 " ccmValues {node} \n" + 123 " ccmFromIor {ior} \n" + 124 " ccmSetTrailLabel {label} \n" + 125 " ccmGetTrailLabel {} \n" + 126 " ccmGetProperty {propName} \n" + 127 " ccmSetProperty {propName propValue} "; 128 return h; 129 } 130 131 public void runScript(String src) { 132 try { 133 tclInterp.eval("source " + src); 134 } catch (tcl.lang.TclException e) { 135 System.out.println(tclInterp.getResult()); 136 } 137 } 138 139 public void setTrailLabel(String label) { 140 ThreadContext tc = ThreadContext.getCurrentThreadContext(); 141 tc.setTrailLabel(label); 142 } 143 144 public String getTrailLabel() { 145 ThreadContext tc = ThreadContext.getCurrentThreadContext(); 146 return tc.getTrailLabel(); 147 } 148 149 public IdlInterface findObject(String name) { 150 try { 151 org.omg.CORBA.Object c = null; 152 c = rootContext.resolve(splitName(name)); 153 String ior = orb.object_to_string(c); 154 IorPrinter iorPrinter = new IorPrinter(ior); 155 String id = iorPrinter.getTypeId(); 156 IdlInterface interfaceNode = new IdlInterface(id, false); 157 interfaceNode.setValue(ior); 158 return interfaceNode; 159 } catch (Exception ex) { 160 throw new RuntimeException ("ccmFindObject: " + ex.toString()); 161 } 162 } 163 164 public void tclPuts(String s) { 165 System.out.println(s); 166 } 167 168 public IdlInterface fromIor(String ior) { 169 try { 170 IorPrinter iorPrinter = new IorPrinter(ior); 171 String id = iorPrinter.getTypeId(); 172 IdlInterface interfaceNode = new IdlInterface(id, false); 173 interfaceNode.setValue(ior); 174 return interfaceNode; 175 } catch (Exception ex) { 176 throw new RuntimeException ("ccmFromIor: " + ex.toString()); 177 } 178 } 179 180 public IdlInterface getFacet(IdlNode componentNode, String v) { 181 if (!(componentNode instanceof IdlObject)) { 182 throw new RuntimeException ("Illegal argument: component IdlNode expected"); 183 } 184 try { 185 CCMObject c = null; 186 try { 187 c = CCMObjectHelper.narrow(orb.string_to_object(componentNode.getValue())); 188 } catch (Exception ex) { 189 throw new RuntimeException (componentNode.getId() + " is not a component"); 190 } 191 org.omg.CORBA.Object f = c.provide_facet(v); 192 String ior = orb.object_to_string(f); 193 IorPrinter iorPrinter = new IorPrinter(ior); 194 String id = iorPrinter.getTypeId(); 195 IdlInterface interfaceNode = new IdlInterface(id, false); 196 interfaceNode.setValue(ior); 197 return interfaceNode; 198 } catch (Exception ex) { 199 throw new RuntimeException ("ccmGetFacet: " + ex.toString()); 200 } 201 } 202 203 public String printFields(IdlNode node) { 204 return node.printFields(false); 205 } 206 207 public String printNode(IdlNode node) { 208 return node.printFields(true); 209 } 210 211 public String printValues(IdlNode node) { 212 return node.printValues(); 213 } 214 215 public String getProperty(String propName) { 216 String p = System.getProperty(propName); 217 if (p == null) { 218 throw new RuntimeException ("No such property: " + propName); 219 } else { 220 return p; 221 } 222 } 223 224 public void setProperty(String propName, String propValue) { 225 try { 226 System.setProperty(propName, propValue); 227 } catch (Exception ex) { 228 throw new RuntimeException ("ccmSetProperty: " + ex.toString()); 229 } 230 } 231 232 public String [] listFacets(IdlNode componentNode) { 233 if (!(componentNode instanceof IdlObject)) { 234 throw new RuntimeException ("Illegal argument: component IdlNode expected"); 235 } 236 CCMObject c = null; 237 try { 238 c = CCMObjectHelper.narrow(orb.string_to_object(componentNode.getValue())); 239 } catch (Exception ex) { 240 throw new RuntimeException (componentNode.getId() + " is not a component"); 241 } 242 try { 243 return new String [0]; 246 } catch (Exception ex) { 247 throw new RuntimeException ("ccmGetFacets: " + ex.toString()); 248 } 249 } 250 251 public String [] listMethods(String id) { 252 try { 253 IdlInterface itf = new IdlInterface(id, false); 254 return itf.getOperations(); 255 } catch (Exception ex) { 256 throw new RuntimeException ("ccmGetOperations: " + ex.toString()); 257 } 258 } 259 260 public String [] listMethods2(IdlNode objectNode) { 261 if (!(objectNode instanceof IdlObject)) { 262 throw new RuntimeException ("Illegal argument: object IdlNode expected"); 263 } 264 try { 265 String ior = objectNode.getValue(); 266 IorPrinter iorPrinter = new IorPrinter(ior); 267 String id = iorPrinter.getTypeId(); 268 IdlInterface itf = new IdlInterface(id, false); 269 return itf.getOperations(); 270 } catch (Exception ex) { 271 throw new RuntimeException ("ccmGetOperations: " + ex.toString()); 272 } 273 } 274 275 public IdlOperation getXmlOperation(IdlNode objectNode, String method) { 276 if (!(objectNode instanceof IdlObject)) { 277 throw new RuntimeException ("Illegal argument: object IdlNode expected"); 278 } 279 try { 280 String ior = objectNode.getValue(); 281 IorPrinter iorPrinter = new IorPrinter(ior); 282 String id = iorPrinter.getTypeId(); 283 return new IdlOperation(id, method); 284 } catch (Exception ex) { 285 throw new RuntimeException ("ccmGetOperation: " + ex.toString()); 286 } 287 } 288 289 public IdlReply createXmlReply(String id, String method) { 290 return new IdlReply(id, method); 291 } 292 293 public IdlOperation createXmlOperation(String id, String method) { 294 return new IdlOperation(id, method); 295 } 296 297 public IdlNode newType(String id) { 298 try { 299 return IdlNode.create(XmlNode.type(id)); 300 } catch (Exception ex) { 301 throw new RuntimeException ("ccmNewIdlNode: " + ex.toString()); 302 } 303 } 304 305 public IdlNode xmlToType(String xml) { 306 try { 307 return XmlNode.getIdlNodeXml(xml); 308 } catch (Exception ex) { 309 throw new RuntimeException ("ccmFromXml: " + ex.toString()); 310 } 311 } 312 313 public String typeToXml(IdlNode node) { 314 try { 315 XmlWriter w = new XmlWriter(); 316 XmlNode.write(node, w); 317 return w.toString(); 318 } catch (Exception ex) { 319 throw new RuntimeException ("ccmToXml: " + ex.toString()); 320 } 321 } 322 323 public IdlNode getNode(IdlNode node, String path) { 324 return node.getNode(path); 325 } 326 327 public void setNode(IdlNode node, String path, IdlNode v) { 328 if (path.equals("exception") && node instanceof IdlReply) { 329 ((IdlReply)node).setException(v.toAny()); 330 } else { 331 IdlNode n = node.getNode(path); 332 n.replace(v); 333 } 334 } 335 336 public void setValue(IdlNode node, String path, String v) { 337 IdlNode n = getNode(node, path); 338 if (n.isMutable()) { 339 n.setValue(v); 340 } else { 341 throw new RuntimeException ("IdlNode " + node.toString() + " is not mutable."); 342 } 343 } 344 345 public boolean isLeaf(IdlNode node) { 346 return node.isLeaf(); 347 } 348 349 public String getValue(IdlNode node, String path) { 350 IdlNode n = getNode(node, path); 351 return n.getValue(); 352 } 353 354 public IdlReply invoke(IdlNode targetNode, IdlOperation opr) { 355 if (!(targetNode instanceof IdlObject)) { 356 throw new RuntimeException ("Illegal argument: object IdlNode expected"); 357 } 358 try { 359 return opr.invoke(orb, orb.string_to_object(targetNode.getValue())); 360 } catch (Exception ex) { 361 ex.printStackTrace(); 362 throw new RuntimeException ("ccmInvoke: " + ex.toString()); 363 } 364 } 365 366 private NameComponent[] splitName(String name) { 367 Vector v = new Vector(); 368 StringTokenizer st = new StringTokenizer(name, "/"); 369 while (st.hasMoreTokens()) { 370 String element = st.nextToken(); 371 int index = element.indexOf("."); 372 if (index != -1) { 373 v.addElement(new NameComponent(element.substring(0,index), element.substring(index + 1))); 374 } else { 375 v.addElement(new NameComponent(element, "")); 377 } 378 } 379 NameComponent[] result = new NameComponent[v.size()]; 380 v.copyInto(result); 381 return result; 382 } 383 }
| Popular Tags
|