KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > coach > tcl > TclLib


1 /***************************************************************************/
2 /* COACH: Component Based Open Source Architecture for */
3 /* Distributed Telecom Applications */
4 /* See: http://www.objectweb.org/ */
5 /* */
6 /* Copyright (C) 2003 Lucent Technologies Nederland BV */
7 /* Bell Labs Advanced Technologies - EMEA */
8 /* */
9 /* Initial developer(s): Harold Batteram */
10 /* */
11 /* This library is free software; you can redistribute it and/or */
12 /* modify it under the terms of the GNU Lesser General Public */
13 /* License as published by the Free Software Foundation; either */
14 /* version 2.1 of the License, or (at your option) any later version. */
15 /* */
16 /* This library is distributed in the hope that it will be useful, */
17 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
18 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU */
19 /* Lesser General Public License for more details. */
20 /* */
21 /* You should have received a copy of the GNU Lesser General Public */
22 /* License along with this library; if not, write to the Free Software */
23 /* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
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 JavaDoc orb;
41     private org.omg.CosNaming.NamingContext JavaDoc rootContext;
42
43     public TclLib(Interp tclInterp){
44         orb = org.objectweb.openccm.corba.TheORB.getORB();
45         try {
46             org.omg.CORBA.Object JavaDoc obj = orb.resolve_initial_references("NameService");
47             rootContext = org.omg.CosNaming.NamingContextHelper.narrow(obj);
48         } catch (org.omg.CORBA.ORBPackage.InvalidName JavaDoc e1) {
49             // NS is not started
50
}
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 JavaDoc key = (String JavaDoc)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 JavaDoc orb) {
97         if (this.orb == null) {
98             this.orb = orb;
99         }
100     }
101
102     public String JavaDoc getHelp() {
103         String JavaDoc 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 JavaDoc 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 JavaDoc label) {
140         ThreadContext tc = ThreadContext.getCurrentThreadContext();
141         tc.setTrailLabel(label);
142     }
143
144     public String JavaDoc getTrailLabel() {
145         ThreadContext tc = ThreadContext.getCurrentThreadContext();
146         return tc.getTrailLabel();
147     }
148         
149     public IdlInterface findObject(String JavaDoc name) {
150         try {
151             org.omg.CORBA.Object JavaDoc c = null;
152             c = rootContext.resolve(splitName(name));
153             String JavaDoc ior = orb.object_to_string(c);
154             IorPrinter iorPrinter = new IorPrinter(ior);
155             String JavaDoc id = iorPrinter.getTypeId();
156             IdlInterface interfaceNode = new IdlInterface(id, false);
157             interfaceNode.setValue(ior);
158             return interfaceNode;
159         } catch (Exception JavaDoc ex) {
160             throw new RuntimeException JavaDoc("ccmFindObject: " + ex.toString());
161         }
162     }
163
164     public void tclPuts(String JavaDoc s) {
165         System.out.println(s);
166     }
167     
168     public IdlInterface fromIor(String JavaDoc ior) {
169         try {
170             IorPrinter iorPrinter = new IorPrinter(ior);
171             String JavaDoc id = iorPrinter.getTypeId();
172             IdlInterface interfaceNode = new IdlInterface(id, false);
173             interfaceNode.setValue(ior);
174             return interfaceNode;
175         } catch (Exception JavaDoc ex) {
176             throw new RuntimeException JavaDoc("ccmFromIor: " + ex.toString());
177         }
178     }
179     
180     public IdlInterface getFacet(IdlNode componentNode, String JavaDoc v) {
181         if (!(componentNode instanceof IdlObject)) {
182             throw new RuntimeException JavaDoc("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 JavaDoc ex) {
189                 throw new RuntimeException JavaDoc(componentNode.getId() + " is not a component");
190             }
191             org.omg.CORBA.Object JavaDoc f = c.provide_facet(v);
192             String JavaDoc ior = orb.object_to_string(f);
193             IorPrinter iorPrinter = new IorPrinter(ior);
194             String JavaDoc id = iorPrinter.getTypeId();
195             IdlInterface interfaceNode = new IdlInterface(id, false);
196             interfaceNode.setValue(ior);
197             return interfaceNode;
198         } catch (Exception JavaDoc ex) {
199             throw new RuntimeException JavaDoc("ccmGetFacet: " + ex.toString());
200         }
201     }
202
203     public String JavaDoc printFields(IdlNode node) {
204         return node.printFields(false);
205     }
206
207     public String JavaDoc printNode(IdlNode node) {
208         return node.printFields(true);
209     }
210
211     public String JavaDoc printValues(IdlNode node) {
212         return node.printValues();
213     }
214         
215     public String JavaDoc getProperty(String JavaDoc propName) {
216         String JavaDoc p = System.getProperty(propName);
217         if (p == null) {
218             throw new RuntimeException JavaDoc("No such property: " + propName);
219         } else {
220             return p;
221         }
222     }
223
224     public void setProperty(String JavaDoc propName, String JavaDoc propValue) {
225         try {
226             System.setProperty(propName, propValue);
227         } catch (Exception JavaDoc ex) {
228             throw new RuntimeException JavaDoc("ccmSetProperty: " + ex.toString());
229         }
230     }
231                     
232     public String JavaDoc[] listFacets(IdlNode componentNode) {
233         if (!(componentNode instanceof IdlObject)) {
234             throw new RuntimeException JavaDoc("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 JavaDoc ex) {
240             throw new RuntimeException JavaDoc(componentNode.getId() + " is not a component");
241         }
242         try {
243             //ToDo:
244
// FacetDescriptions facetDescriptions = c.get_all_facets();
245
return new String JavaDoc[0];
246         } catch (Exception JavaDoc ex) {
247             throw new RuntimeException JavaDoc("ccmGetFacets: " + ex.toString());
248         }
249     }
250     
251     public String JavaDoc[] listMethods(String JavaDoc id) {
252         try {
253             IdlInterface itf = new IdlInterface(id, false);
254             return itf.getOperations();
255         } catch (Exception JavaDoc ex) {
256             throw new RuntimeException JavaDoc("ccmGetOperations: " + ex.toString());
257         }
258     }
259
260     public String JavaDoc[] listMethods2(IdlNode objectNode) {
261         if (!(objectNode instanceof IdlObject)) {
262             throw new RuntimeException JavaDoc("Illegal argument: object IdlNode expected");
263         }
264         try {
265             String JavaDoc ior = objectNode.getValue();
266             IorPrinter iorPrinter = new IorPrinter(ior);
267             String JavaDoc id = iorPrinter.getTypeId();
268             IdlInterface itf = new IdlInterface(id, false);
269             return itf.getOperations();
270         } catch (Exception JavaDoc ex) {
271             throw new RuntimeException JavaDoc("ccmGetOperations: " + ex.toString());
272         }
273     }
274  
275     public IdlOperation getXmlOperation(IdlNode objectNode, String JavaDoc method) {
276         if (!(objectNode instanceof IdlObject)) {
277             throw new RuntimeException JavaDoc("Illegal argument: object IdlNode expected");
278         }
279         try {
280             String JavaDoc ior = objectNode.getValue();
281             IorPrinter iorPrinter = new IorPrinter(ior);
282             String JavaDoc id = iorPrinter.getTypeId();
283             return new IdlOperation(id, method);
284         } catch (Exception JavaDoc ex) {
285             throw new RuntimeException JavaDoc("ccmGetOperation: " + ex.toString());
286         }
287     }
288
289     public IdlReply createXmlReply(String JavaDoc id, String JavaDoc method) {
290         return new IdlReply(id, method);
291     }
292
293     public IdlOperation createXmlOperation(String JavaDoc id, String JavaDoc method) {
294         return new IdlOperation(id, method);
295     }
296         
297     public IdlNode newType(String JavaDoc id) {
298         try {
299             return IdlNode.create(XmlNode.type(id));
300         } catch (Exception JavaDoc ex) {
301             throw new RuntimeException JavaDoc("ccmNewIdlNode: " + ex.toString());
302         }
303     }
304
305     public IdlNode xmlToType(String JavaDoc xml) {
306         try {
307             return XmlNode.getIdlNodeXml(xml);
308         } catch (Exception JavaDoc ex) {
309             throw new RuntimeException JavaDoc("ccmFromXml: " + ex.toString());
310         }
311     }
312
313     public String JavaDoc typeToXml(IdlNode node) {
314         try {
315           XmlWriter w = new XmlWriter();
316           XmlNode.write(node, w);
317           return w.toString();
318         } catch (Exception JavaDoc ex) {
319             throw new RuntimeException JavaDoc("ccmToXml: " + ex.toString());
320         }
321     }
322                 
323     public IdlNode getNode(IdlNode node, String JavaDoc path) {
324         return node.getNode(path);
325     }
326     
327     public void setNode(IdlNode node, String JavaDoc 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 JavaDoc path, String JavaDoc v) {
337         IdlNode n = getNode(node, path);
338         if (n.isMutable()) {
339             n.setValue(v);
340         } else {
341             throw new RuntimeException JavaDoc("IdlNode " + node.toString() + " is not mutable.");
342         }
343     }
344
345     public boolean isLeaf(IdlNode node) {
346         return node.isLeaf();
347     }
348     
349     public String JavaDoc getValue(IdlNode node, String JavaDoc 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 JavaDoc("Illegal argument: object IdlNode expected");
357         }
358         try {
359             return opr.invoke(orb, orb.string_to_object(targetNode.getValue()));
360         } catch (Exception JavaDoc ex) {
361 ex.printStackTrace();
362             throw new RuntimeException JavaDoc("ccmInvoke: " + ex.toString());
363         }
364     }
365                 
366     private NameComponent[] splitName(String JavaDoc name) {
367         Vector v = new Vector();
368         StringTokenizer st = new StringTokenizer(name, "/");
369         while (st.hasMoreTokens()) {
370             String JavaDoc 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                 // HJB: allow names without a kind field
376
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