KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.coach.idltree;
2
3 import org.w3c.dom.Node JavaDoc;
4 import org.omg.CORBA.ORB JavaDoc;
5 import org.omg.CORBA.TypeCode JavaDoc;
6 import org.omg.CORBA.Any JavaDoc;
7 import org.omg.CORBA.TCKind JavaDoc;
8 import java.util.*;
9 import org.coach.tracing.api.*;
10 import java.lang.reflect.*;
11
12 /**
13  * The class IdlReply represents a reply of an operation on an CORBA IDL interface. Instances are created through one
14  * of the create() factory methods in IdlNode.
15  * An IdlReply object has child nodes for each out, inout or return parameter.
16  * An IdlReply object can also contain an exception value as the result of an operation.
17  * The method isException() can be used to determine if the IdlReply object contains an exception value.
18  *
19  * @author <a HREF="mailto:batteram@lucent.com">Harold Batteram</a> <b>Lucent Technologies</b>
20  */

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

51     public IdlReply(String JavaDoc id, String JavaDoc name, Parameter[] p, Any JavaDoc[] 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 JavaDoc[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 JavaDoc[values.length];
72             for (int i = 0; i < values.length; i++) {
73                 types[i] = values[i].type();
74             }
75         } catch (Exception JavaDoc e) {
76             e.printStackTrace();
77         }
78     }
79
80     /**
81      * Constructs an IdlReply node with exceptions.
82      *
83      * The IdlReply node is initialized with IdlNode parameter children from the values array.
84      *
85      * @param id The interface repository id for the interface to which this operation belongs.
86      * @param name The operation name.
87      * @param p The array with parameter name, parameter type and direction information.
88      * @param values The any values used to initialize the operation parameters.
89      * @param exceptions The list of exception names for this reply.
90      * @param exceptionTypes The list of exception typecodes.
91      */

92     public IdlReply(String JavaDoc id, String JavaDoc name, Parameter[] p, Any JavaDoc[] values, String JavaDoc[] exceptions, TypeCode JavaDoc[] exceptionTypes) {
93         this(id, name, p, values);
94         this.exceptions = exceptions;
95         this.exceptionTypes = exceptionTypes;
96         types = new org.omg.CORBA.TypeCode JavaDoc[values.length];
97         for (int i = 0; i < values.length; i++) {
98             types[i] = values[i].type();
99         }
100     }
101
102     /**
103      * Constructs an IdlReply node with an exception value.
104      *
105      * The IdlReply node is initialized with IdlNode parameter children from the values array.
106      *
107      * @param id The interface repository id for the interface to which this operation belongs.
108      * @param name The operation name.
109      * @param exValue The any containing the exception for this reply.
110      */

111     public IdlReply(String JavaDoc id, String JavaDoc name, Any JavaDoc 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 JavaDoc e) {
123             e.printStackTrace();
124         }
125     }
126         
127     /**
128      * Return the name of the operation this IdlReply object represents.
129      *
130      * @return The operation name.
131      */

132     public String JavaDoc getName() {
133         return name;
134     }
135     
136     /**
137      * Return the list of parameter names for this reply.
138      *
139      * @return The list of parameter names.
140      */

141     public String JavaDoc[] getParameterNames() {
142         return names;
143     }
144
145
146     /**
147      * Return the list of parameter descriptions for this reply.
148      *
149      * @return The list of parameter descriptions.
150      */

151     public Parameter[] getParameterInfo() {
152         return params;
153     }
154
155     /**
156      * Return the list of exceptionr names for this reply.
157      *
158      * @return The list of exception names.
159      */

160     public String JavaDoc[] getExceptions() {
161         return exceptions;
162     }
163             
164     /**
165      * Return the IdlNode child for a named parameter.
166      *
167      * @param name The parameter name for which to return the child IdlNode.
168      *
169      * @return The IdlNode child for the parameter indicated name.
170      */

171     public IdlNode getParameter(String JavaDoc 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     /**
181      * Sets the current exception from an index number in the exception list.
182      *
183      * @param i The index number in the exception list.
184      *
185      */

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 JavaDoc t) {
201             t.printStackTrace();
202         }
203     }
204
205     /**
206      * Sets the current exception from the content of an any.
207      *
208      * @param exValue The any contain the exception type.
209      */

210     public void setException(org.omg.CORBA.Any JavaDoc 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 JavaDoc 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     /**
233      * Tests if the IdlReply contains an exception value.
234      *
235      * @return True is the IdlReply contains an exception, false otherwise.
236      */

237     public boolean isException() {
238         return isException;
239     }
240
241     /**
242      * Tests if the IdlReply has a return value.
243      *
244      * @return True is the IdlReply has a return value, false otherwise.
245      */

246     public boolean hasReturn() {
247         return hasReturn;
248     }
249             
250     /**
251      * Return the exception value of the IdlReply as an IdlNode.
252      *
253      * @return The exception value of the IdlReply as an IdlNode.
254      */

255     public IdlNode getException() {
256         if (isException && getChildCount() == 1) {
257             return (IdlNode)getChildAt(0);
258         }
259         return null;
260     }
261     
262     /**
263      * Return all IdlNode child nodes.
264      *
265      * @return An IdlNode array containing all in and inout parameter values.
266      */

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     /**
276      * Returns a string representation for this IdlReply node.
277      *
278      * @return The string representation for this IdlReply node.
279      */

280     public String JavaDoc toString() {
281         return "reply " + id + " " + name;
282     }
283         
284     /**
285      * Fills the given any array with parameter values.
286      *
287      * The content of the values array is filled with the parameter
288      * child nodes converted to any values. The given array length must
289      * match the number of in and inout parameter values for this reply.
290      */

291     public void getValues(Any JavaDoc[] values) {
292         if (values.length != getChildCount()) {
293             throw new RuntimeException JavaDoc("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     /**
301      * Sets the IdlReply values from the content of the given any array with parameter values.
302      *
303      * The content of the values array is used to initialize the child nodes with the parameter
304      * child nodes converted from any values. The given array length must
305      * match the number of in and inout parameter values for this reply.
306      */

307     public void setValues(Any JavaDoc[] values) {
308         if (values.length != params.length) {
309             throw new RuntimeException JavaDoc("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     /**
318      * Reads all inout, out and return value from an InputStream.
319      *
320      * The result is returned as an Any array.
321      *
322      * @param in The org.omg.CORBA.portable.InputStream to read the values from.
323      *
324      * @return An any array with inout, out and return values read from the inputstream.
325      */

326     public Any JavaDoc[] readValues(org.omg.CORBA.portable.InputStream JavaDoc in) {
327         IdlNode[] replyParameters = getParameters();
328         Any JavaDoc[] outVal = new Any JavaDoc[replyParameters.length];
329
330         try {
331             if (hasReturn) {
332                 // The return value is the first value in the stream but the last value
333
// in the parameter array.
334
org.omg.CORBA.TypeCode JavaDoc 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 JavaDoc ptc = replyParameters[i].getTypeCode();
346                     outVal[i].read_value(in, ptc);
347                 }
348             }
349         } catch (Throwable JavaDoc t) {
350             t.printStackTrace();
351         }
352         setValues(outVal);
353         
354         return outVal;
355     }
356
357     /**
358      * Writes the value to a CORBA outputstream.
359      *
360      * @param is The outputstream to write to.
361      */

362     public void write(org.omg.CORBA.portable.OutputStream JavaDoc os) {
363         for(int i = 0; i < getChildCount(); i++) {
364             ((IdlParameter)getChildAt(i)).write(os);
365         }
366     }
367
368     /**
369      * Reads the value from a CORBA inputstream.
370      *
371      * @param is The inputstream to read from.
372      */

373     public void read(org.omg.CORBA.portable.InputStream JavaDoc is) {
374         for(int i = 0; i < getChildCount(); i++) {
375             ((IdlParameter)getChildAt(i)).read(is);
376         }
377     }
378         
379     // XML section
380

381     /**
382      * Create an IdlReply from an Xml representation.
383      *
384      * The XML format example
385      * <pre>
386      * &lt;reply id="IDL:example/MyInterface:1.0"&gt;
387      * &lt;inout name="p2"&gt;
388      * &lt;long&gt;100&lt;/long&gt;
389      * &lt;/inout&gt;
390      * &lt;return name="p2"&gt;
391      * &lt;string&gt;my return value&lt;/string&gt;
392      * &lt;/return&gt;
393      * &lt;/reply&gt;
394      * </pre>
395      *
396      * @param xml The XML string from which to create an IdlReply instance.
397      */

398     public IdlReply(String JavaDoc xml) {
399         this(XmlNode.getNode(xml));
400     }
401
402     IdlReply(Node JavaDoc 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 JavaDoc[] 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 JavaDoc("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 JavaDoc e) {
426             e.printStackTrace();
427             throw new RuntimeException JavaDoc(e.toString());
428         }
429     }
430
431     /**
432      * Constructs an intialized IdlReply node from an interface repository id and operation name.
433      *
434      * @param id The interface repository id.
435      * @param name The operation name.
436      */

437     public IdlReply(String JavaDoc id, String JavaDoc name) {
438         this();
439         try {
440             // We use reflection here and rely on the presence of the HelperExt class.
441
// Alternatively the IR server can be used.
442
this.id = id;
443             this.name = name;
444             setField(name);
445
446             initParameters(id);
447             
448         } catch (Exception JavaDoc e) {
449             e.printStackTrace();
450         }
451     }
452
453     private void initParameters(String JavaDoc 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 JavaDoc[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             // The names and params array only have vales for non void return, out and inout parameters
477
int idx = getChildCount();
478             names = new String JavaDoc[idx];
479             params = new Parameter[idx];
480             types = new org.omg.CORBA.TypeCode JavaDoc[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 JavaDoc e) {
494             e.printStackTrace();
495         }
496     }
497
498     /**
499      * Write the current value to an IdlWriter object.
500      *
501      * @param w The IdlWriter object to write the current value to.
502      */

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