KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > coach > idltree > IdlOperation


1 package org.coach.idltree;
2
3 import org.w3c.dom.Node JavaDoc;
4 import org.omg.DynamicAny.*;
5 import org.omg.CORBA.ORB JavaDoc;
6 import org.omg.CORBA.TypeCode JavaDoc;
7 import org.omg.CORBA.Any JavaDoc;
8 import org.omg.CORBA.TCKind JavaDoc;
9 import org.coach.tracing.api.*;
10 import java.util.*;
11 import java.lang.reflect.*;
12
13 /**
14  * The class IdlOperation represents an operation on an CORBA IDL interface. Instances are created through one
15  * of the create() factory methods in IdlNode.
16  * An IdlOperation object has child nodes for each in and inout parameter.
17  *
18  * @author <a HREF="mailto:batteram@lucent.com">Harold Batteram</a> <b>Lucent Technologies</b>
19  */

20 public class IdlOperation extends IdlNode implements IdlWritable {
21     private String JavaDoc[] allTypes;
22     private String JavaDoc[] allNames;
23     private String JavaDoc[] allDirections;
24     private String JavaDoc[] directions;
25
26     protected String JavaDoc[] names = new String JavaDoc[0];
27     protected String JavaDoc[] exceptions = new String JavaDoc[0];
28     protected Parameter[] params = new Parameter[0];
29     protected String JavaDoc[] typeNames = new String JavaDoc[0];
30     protected transient TypeCode JavaDoc[] types = new TypeCode JavaDoc[0];
31     protected transient TypeCode JavaDoc[] exceptionTypes = new TypeCode JavaDoc[0];
32
33     protected String JavaDoc name;
34     protected boolean isOneway;
35
36     protected IdlOperation() {
37         isLeaf = false;
38         isMutable = false;
39         setUserObject(this);
40         type = "operation";
41     }
42
43     /**
44      * Constructs an IdlOperation node.
45      *
46      * The IdlOperertion node is initialized with IdlNode parameter children from the values array.
47      *
48      * @param id The interface repository id for the interface to which this operation belongs.
49      * @param name The operation name.
50      * @param p The array with parameter name, parameter type and direction information.
51      * @param values The any values used to initialize the operation parameters.
52      * @param isOneway Flag to indicate if this is a oneway operation.
53      */

54     public IdlOperation(String JavaDoc id, String JavaDoc name, Parameter[] p, org.omg.CORBA.Any JavaDoc[] 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 JavaDoc[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 JavaDoc e) {
69             e.printStackTrace();
70         }
71     }
72
73     /**
74      * Return the name of the operation this IdlOperation object represents.
75      *
76      * @return The operation name.
77      */

78     public String JavaDoc getName() {
79         return name;
80     }
81
82     /**
83      * Return the list of parameter names for this operation.
84      *
85      * @return The list of parameter names.
86      */

87     public String JavaDoc[] getParameterNames() {
88         return names;
89     }
90
91     /**
92      * Return the IdlNode child for a named parameter.
93      *
94      * @param name The parameter name for which to return the child IdlNode.
95      *
96      * @return The IdlNode child for the parameter indicated name.
97      */

98     public IdlNode getParameter(String JavaDoc 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     /**
108      * Return all IdlNode child nodes.
109      *
110      * @return An IdlNode array containing all in and inout parameter values.
111      */

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     /**
121      * Returns a string representation for this IdlOperation node.
122      *
123      * @return The string representation for this IdlOperation node.
124      */

125     public String JavaDoc toString() {
126         return "operation " + id + " " + name;
127     }
128
129     /**
130      * Fills the given any array with parameter values.
131      *
132      * The content of the values array is filled with the parameter
133      * child nodes converted to any values. The given array length must
134      * match the number of in and inout parameter values for this operation.
135      */

136     public void getValues(org.omg.CORBA.Any JavaDoc[] 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     // XML section
147

148     /**
149      * Create an IdlOperation from an Xml representation.
150      *
151      * The XML format example
152      * <pre>
153      * &lt;operation id="IDL:example/MyInterface:1.0"&gt;
154      * &lt;in name="p1"&gt;
155      * &lt;string&gt;my parameter value&lt;/string&gt;
156      * &lt;/in&gt;
157      * &lt;inout name="p2"&gt;
158      * &lt;long&gt;100&lt;/long&gt;
159      * &lt;/inout&gt;
160      * &lt;/operation&gt;
161      * </pre>
162      *
163      * @param xml The XML string from which to create an IdlOperation instance.
164      */

165     public IdlOperation(String JavaDoc xml) {
166         this(XmlNode.getNode(xml));
167     }
168
169     /**
170      * Constructs a IdlOperation instance from a interface repository id and an operation name.
171      *
172      * The constructed IdlOperation node has intizialized child node members which correspond
173      * to the in and inout parameters for this operation.
174      */

175     public IdlOperation(String JavaDoc id, String JavaDoc name) {
176         this();
177         try {
178             this.id = id;
179             this.name = name;
180             initParameters(id);
181         } catch (Exception JavaDoc e) {
182             e.printStackTrace();
183         }
184     }
185
186     IdlOperation(Node JavaDoc n) {
187         this();
188         try {
189             id = XmlNode.getId(n);
190             name = XmlNode.getName(n);
191
192             initParameters(id);
193
194             Node JavaDoc[] 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 JavaDoc("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 JavaDoc e) {
204             throw new RuntimeException JavaDoc(e.toString());
205         }
206     }
207
208     /**
209      * Intitializes parameter information data structures.
210      * This is currently based on the presence of an extended helper class generated
211      * by the DscGen idl compiler.
212      * Alternativly, an interface repository server could be used.
213      */

214     private void initParameters(String JavaDoc 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 JavaDoc[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             // The names and params array only have vales for non void return, out and inout parameters
244
int idx = getChildCount();
245             names = new String JavaDoc[idx];
246             typeNames = new String JavaDoc[idx];
247             directions = new String JavaDoc[idx];
248             params = new Parameter[idx];
249             types = new org.omg.CORBA.TypeCode JavaDoc[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 JavaDoc e) {
262             e.printStackTrace();
263         }
264     }
265
266     /**
267      * Return the list of parameter names for this operation.
268      *
269      * @return The list of parameter names.
270      */

271     public String JavaDoc[] getNames() {
272         return names;
273     }
274
275     /**
276      * Return the list of parameter type names for this operation.
277      *
278      * @return The list of parameter type names.
279      */

280     public String JavaDoc[] getTypes() {
281         return typeNames;
282     }
283
284     /**
285      * Return the list of parameter directions for this operation.
286      *
287      * @return The list of parameter directions.
288      */

289     public String JavaDoc[] getDirections() {
290         return directions;
291     }
292
293     /**
294      * Write the current value to an IdlWriter object.
295      *
296      * @param w The IdlWriter object to write the current value to.
297      */

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 JavaDoc orb, org.omg.CORBA.Object JavaDoc target) {
311         return dii_invoke(orb, target);
312     }
313         
314     /**
315      * Invokes the operation on a target CORBA object and returns an XmlReply object.
316      *
317      * @param orb The CORBA ORB reference.
318      * @param target The target CORBA object on which to invoke the operation.
319      *
320      * @return An IdlReply object representing the result of the operation.
321      */

322     public IdlReply object_invoke(org.omg.CORBA.ORB JavaDoc orb, org.omg.CORBA.Object JavaDoc target) {
323         org.omg.CORBA.portable.ObjectImpl JavaDoc objImpl = null;
324         org.omg.CORBA.portable.OutputStream JavaDoc out = null;
325         org.omg.CORBA.portable.InputStream JavaDoc in = null;
326         
327         try {
328 // Object tmp = (Object)(((org.omg.CORBA_2_3.ORB)orb).lookup_value_factory(org.omg.Components.ComponentPortDescriptionHelper.id()));
329

330             objImpl = (org.omg.CORBA.portable.ObjectImpl JavaDoc)target;
331             String JavaDoc oprName = name;
332             Parameter[] inPar = null;
333             Parameter[] outPar = null;
334             org.omg.CORBA.Any JavaDoc[] inVal = null;
335             org.omg.CORBA.Any JavaDoc[] outVal = null;
336             
337             out = objImpl._request(oprName, !isOneway);
338             inPar = new Parameter[params.length];
339             inVal = new org.omg.CORBA.Any JavaDoc[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 JavaDoc aex) {
354                 in = aex.getInputStream();
355                 String JavaDoc aex_id = aex.getId();
356                 org.omg.CORBA.Any JavaDoc aex_value = orb.create_any();
357                 org.omg.CORBA.TypeCode JavaDoc 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 JavaDoc t) {
374             t.printStackTrace();
375             javax.swing.JOptionPane.showMessageDialog(null, t.toString());
376         }
377
378         return null;
379     }
380
381     /**
382      * Invokes the operation on a target CORBA object and returns an XmlReply object.
383      *
384      * @param orb The CORBA ORB reference.
385      * @param target The target CORBA object on which to invoke the operation.
386      *
387      * @return An IdlReply object representing the result of the operation.
388      */

389     public IdlReply dii_invoke(org.omg.CORBA.ORB JavaDoc orb, org.omg.CORBA.Object JavaDoc target) {
390         org.omg.CORBA.Request JavaDoc request = target._request(name);
391
392         IdlNode[] nodes = getParameters();
393         // Vector to hold inout and out values for the request
394
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 // IdlReply reply = new IdlReply(id, name);
413

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 JavaDoc ex = request.env().exception();
420         
421         IdlReply reply = new IdlReply(id, name);
422         if (ex != null) {
423             org.omg.CORBA.UnknownUserException JavaDoc uex = (org.omg.CORBA.UnknownUserException JavaDoc)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