KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > demo > dii > Client


1 package demo.dii;
2
3 /**
4  * An example for using the Dynamic Invocation Interface
5  */

6 import org.omg.CosNaming.*;
7
8 public class Client
9 {
10     public static void main( String JavaDoc[] args )
11     {
12         org.omg.CORBA.ORB JavaDoc orb = null;
13
14         try
15         {
16         orb = org.omg.CORBA.ORB.init(args,null);
17
18         NamingContextExt nc = NamingContextExtHelper.narrow(
19                     orb.resolve_initial_references("NameService"));
20     
21         org.omg.CORBA.Object JavaDoc s =
22                     nc.resolve( nc.to_name("dii.example"));
23
24         // a simple request
25
org.omg.CORBA.Request JavaDoc r = s._request("_get_long_number");
26         
27         r.set_return_type(
28                     orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_long));
29
30         r.invoke();
31
32         if( r.env().exception() != null )
33                 {
34             throw r.env().exception();
35                 }
36         else
37                 {
38             System.out.println("0: " + r.return_value() );
39                 }
40     
41         // a request with out args
42
org.omg.CORBA.Request JavaDoc r_out = s._request("add");
43         
44         r_out.add_in_arg().insert_long( 3 );
45         r_out.add_in_arg().insert_long( 4 );
46
47         org.omg.CORBA.Any JavaDoc out_arg = r_out.add_out_arg();
48         out_arg.type( orb.get_primitive_tc(
49                     org.omg.CORBA.TCKind.tk_long) );
50
51         r_out.set_return_type( orb.get_primitive_tc(
52                     org.omg.CORBA.TCKind.tk_void));
53
54         r_out.invoke();
55
56         if( r_out.env().exception() != null )
57                 {
58             throw r_out.env().exception();
59                 }
60         else
61                 {
62             System.out.println("1: " + out_arg.extract_long() );
63                 }
64
65         // another simple request with a string argumente, oneway
66
org.omg.CORBA.Request JavaDoc r1 = s._request("notify");
67         r1.add_in_arg().insert_string("hallo");
68         r1.send_oneway();
69
70         // a request with a return value
71
org.omg.CORBA.Request JavaDoc r2 = s._request("writeNumber");
72         r2.add_in_arg().insert_long( 5 );
73         r2.set_return_type( orb.get_primitive_tc( org.omg.CORBA.TCKind.tk_string ));
74         r2.invoke();
75         if( r2.env().exception() != null )
76                 {
77             throw r2.env().exception();
78                 }
79         else
80                 {
81             System.out.println("2: " + r2.return_value() );
82                 }
83             
84         // deferred asynchronous operation, synchronize with result
85
org.omg.CORBA.Request JavaDoc r3 = s._request("writeNumber");
86         r3.add_in_arg().insert_long( 5 );
87         r3.set_return_type( orb.get_primitive_tc(
88                     org.omg.CORBA.TCKind.tk_string ));
89
90         r3.send_deferred();
91         r3.get_response();
92         if( r3.env().exception() != null )
93                 {
94             throw r3.env().exception();
95                 }
96         else
97                 {
98             System.out.println("3: " + r3.return_value() );
99                 }
100
101         // polling until response is there
102
org.omg.CORBA.Request JavaDoc r4 = s._request("writeNumber");
103         r4.add_in_arg().insert_long( 5 );
104         r4.set_return_type( orb.get_primitive_tc(
105                     org.omg.CORBA.TCKind.tk_string ));
106         
107         r4.send_deferred();
108         
109         while( ! r4.poll_response() )
110         {
111             /* we could be doing s.th. useful here instead of
112                        sleeping...*/

113             try
114             {
115             Thread.currentThread().sleep(10);
116             }
117             catch ( InterruptedException JavaDoc i){}
118             System.out.print(".");
119         }
120
121         if( r4.env().exception() != null )
122                 {
123             throw r4.env().exception();
124                 }
125         else
126                 {
127             System.out.println("4: " + r2.return_value() );
128                 }
129
130                 //send a request which throws an exception
131
org.omg.CORBA.Request JavaDoc r5 = s._request("writeNumberWithEx");
132         r5.add_in_arg().insert_long( 5 );
133                 org.omg.CORBA.ExceptionList JavaDoc exceptions = r5.exceptions();
134                 org.omg.CORBA.TypeCode JavaDoc tc =
135                     orb.create_exception_tc(
136                         "IDL:dii/server/e:1.0",
137                         "e",
138                         new org.omg.CORBA.StructMember JavaDoc[]{
139                             new org.omg.CORBA.StructMember JavaDoc(
140                                 "why",
141                                 orb.create_string_tc(0),
142                                 null)
143                                 }
144                         );
145                 exceptions.add( tc );
146
147         r5.invoke();
148         if( r5.env().exception() != null )
149                 {
150                     System.out.println("5: Got exception " +
151                                        r5.env().exception());
152                     //Hint: what you get here is a
153
//org.omg.CORBA.portable.ApplicationException
154
//which contains an any containing the real
155
//exception.
156
}
157         }
158         catch ( Exception JavaDoc e)
159         {
160         e.printStackTrace();
161         }
162         orb.shutdown( false );
163         }
164 }
165
166
167
168
Popular Tags