KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > demo > dynany > Client


1 package demo.dynany;
2
3 import java.io.*;
4 import org.omg.CORBA.Any JavaDoc;
5 import org.omg.CORBA.TypeCode JavaDoc;
6 import org.omg.CORBA.TCKind JavaDoc;
7 import org.omg.DynamicAny.*;
8 import org.omg.CosNaming.*;
9
10
11 public class Client
12 {
13     public static AnyServer s = null;
14
15     public static void main( String JavaDoc[] args )
16     {
17     try
18     {
19         org.omg.CORBA.ORB JavaDoc orb = org.omg.CORBA.ORB.init(args, null);
20         DynAnyFactory dynFactory =
21                 DynAnyFactoryHelper.narrow(
22                      orb.resolve_initial_references("DynAnyFactory"));
23
24         NamingContextExt nc =
25         NamingContextExtHelper.narrow(
26                      orb.resolve_initial_references("NameService"));
27
28         AnyServer s =
29         AnyServerHelper.narrow(
30                      nc.resolve(nc.to_name("DynAnyServer.example")));
31
32         // char
33

34         DynAny dyn_any =
35                 dynFactory.create_dyn_any_from_type_code(
36                          orb.get_primitive_tc( TCKind.tk_char ));
37
38         dyn_any.insert_char( 'c' );
39         System.out.println("Passing a char..." +
40                                s.generic( dyn_any.to_any() ));
41
42         // enum
43

44         TypeCode JavaDoc enum_tc =
45                 orb.create_enum_tc("IDL:colors:1.0","colors",
46                                    new String JavaDoc[]{"red",
47                                                 "green",
48                                                 "blue",
49                                                 "mauve",
50                                                 "magenta",
51                                                 "salmon"});
52
53         DynEnum dyn_enum =
54                 (DynEnum)dynFactory.create_dyn_any_from_type_code( enum_tc );
55
56         dyn_enum.set_as_string("salmon");
57         System.out.println("Passing an enum value..." +
58                                s.generic( dyn_enum.to_any() ) );
59
60         // sequence
61

62         TypeCode JavaDoc seq_tc =
63                 orb.create_sequence_tc( 2, orb.create_string_tc(0));
64         DynSequence dyn_seq =
65                 (DynSequence)dynFactory.create_dyn_any_from_type_code( seq_tc );
66
67         Any JavaDoc [] string_sequence = new Any JavaDoc[2];
68         string_sequence[0] = orb.create_any();
69         string_sequence[1] = orb.create_any();
70         string_sequence[0].insert_string("hello");
71         string_sequence[1].insert_string("world");
72         dyn_seq.set_elements( string_sequence );
73
74         System.out.println("Passing a sequence of length " +
75                                dyn_seq.component_count() + "..." +
76                                s.generic( dyn_seq.to_any() ) );
77         dyn_seq.destroy();
78
79             // test the primitive get/set operations for DynSequences
80

81             seq_tc =
82                 orb.create_sequence_tc( 1, orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_boolean));
83
84             dyn_seq = (DynSequence)dynFactory.create_dyn_any_from_type_code( seq_tc );
85
86             dyn_seq.set_length( 1 );
87             dyn_seq.insert_boolean( true );
88         dyn_seq.destroy();
89
90         // array
91

92         TypeCode JavaDoc array_tc =
93                 orb.create_array_tc( 3, orb.create_string_tc(0));
94         DynArray dyn_array =
95                 (DynArray)dynFactory.create_dyn_any_from_type_code( array_tc );
96
97         string_sequence = new Any JavaDoc[3];
98         string_sequence[0] = orb.create_any();
99         string_sequence[1] = orb.create_any();
100         string_sequence[2] = orb.create_any();
101         string_sequence[0].insert_string("hello");
102         string_sequence[1].insert_string("another");
103         string_sequence[2].insert_string("world");
104         dyn_array.set_elements( string_sequence );
105
106         System.out.println("Passing an array of length " +
107                                dyn_array.component_count() +
108                                "..." + s.generic( dyn_array.to_any() ) );
109         dyn_array.destroy();
110
111         // struct
112

113         /*
114              * the hardest part is coming up with a correct call to
115              * create the struct's TypeCode, which is normally
116              * statically generated into the helper class
117              */

118
119         TypeCode JavaDoc struct_tc =
120                 orb.create_struct_tc(
121                     "IDL:Node:1.0",
122                     "Node",
123                     new org.omg.CORBA.StructMember JavaDoc[]
124                     {
125                         new org.omg.CORBA.StructMember JavaDoc( "first",
126                               org.omg.CORBA.ORB.init().create_string_tc(0),
127                                                         null ),
128                         new org.omg.CORBA.StructMember JavaDoc( "second",
129                               org.omg.CORBA.ORB.init().create_sequence_tc(
130                                           0,
131                                           org.omg.CORBA.ORB.init().create_recursive_tc("IDL:Node:1.0")
132                                           ),
133                                                         null)
134                             }
135                     );
136             
137         DynStruct dyn_struct =
138                 (DynStruct)dynFactory.create_dyn_any_from_type_code(
139                         struct_tc );
140
141         // Create a list of name value pairs that holds the struct members.
142
// This struct has two members, so we have 2 array elements
143

144         // the first struct member is a string,
145
// the member name is "first"
146

147         NameValuePair[] nvp_seq = new NameValuePair[2];
148         Any JavaDoc first_member_any = orb.create_any();
149         first_member_any.insert_string("first");
150         nvp_seq[0] = new NameValuePair("first", first_member_any );
151
152         /*
153          the second member is a recursive sequence, ie. the element
154              type of the sequence is that of the enclosing struct, the
155              member name is "second". We'll leave that sequence empty,
156              but we have to provide an initialized any for the empty
157              sequence, so we create a DynAny that initializes an any
158              as an empty sequence
159         */

160
161         DynSequence second_member =
162                 (DynSequence)dynFactory.create_dyn_any_from_type_code(
163                                 struct_tc.member_type(1) );
164         nvp_seq[1] =
165                 new NameValuePair("second", second_member.to_any() );
166
167         // insert the nvp list into the DynStruct
168
dyn_struct.set_members( nvp_seq );
169
170         System.out.println("Passing a struct..." +
171                                s.generic( dyn_struct.to_any() ) );
172
173             // test the primitive get/set operations for DynStruct
174

175             dyn_struct.seek(0);
176             dyn_struct.insert_string( "newly inserted" );
177         System.out.println("Passing the struct again..." +
178                                s.generic( dyn_struct.to_any() ) );
179
180         dyn_struct.destroy();
181         second_member.destroy();
182
183         /* union */
184
185         // setting up TypeCodes gets even more complicated,
186
// but you asked for it...
187

188         org.omg.CORBA.UnionMember JavaDoc[] members =
189                 new org.omg.CORBA.UnionMember JavaDoc[3];
190
191         org.omg.CORBA.Any JavaDoc label_any;
192         label_any = orb.create_any();
193         label_any.insert_octet((byte)0); // 0 byte means default case
194

195         members[0] =
196                 new org.omg.CORBA.UnionMember JavaDoc("s",
197                                               label_any,
198                                               org.omg.CORBA.ORB.init().get_primitive_tc(org.omg.CORBA.TCKind.from_int(2)),
199                                               null);
200
201
202
203             label_any = org.omg.CORBA.ORB.init().create_any();
204             label_any.insert_char('l');
205         members[1] =
206                 new org.omg.CORBA.UnionMember JavaDoc("l",
207                                               label_any,
208                                               org.omg.CORBA.ORB.init().get_primitive_tc(org.omg.CORBA.TCKind.from_int(3)),
209                                               null);
210
211
212             label_any = org.omg.CORBA.ORB.init().create_any();
213             label_any.insert_char('f');
214
215         members[2] =
216                 new org.omg.CORBA.UnionMember JavaDoc("f",
217                                               label_any,
218                                               org.omg.CORBA.ORB.init().get_primitive_tc(org.omg.CORBA.TCKind.from_int(6)),
219                                               null);
220
221         TypeCode JavaDoc union_tc =
222                 org.omg.CORBA.ORB.init().create_union_tc("IDL:Nums:1.0",
223                                                          "Nums",
224                                                          org.omg.CORBA.ORB.init().get_primitive_tc(org.omg.CORBA.TCKind.from_int(9)),
225                                                          members );
226
227         DynUnion dyn_union =
228                 (DynUnion)dynFactory.create_dyn_any_from_type_code( union_tc );
229             
230             Any JavaDoc discriminator_any = orb.create_any();
231             discriminator_any.insert_char('l');
232             DynAny discriminator = dynFactory.create_dyn_any(discriminator_any);
233             
234             //dyn_enum.set_as_string("salmon");
235
// DynAny discriminator = dyn_enum;
236

237         dyn_union.set_discriminator( discriminator );
238         dyn_union.member().insert_long(4711);
239         System.out.println("Passing a union..." +
240                                s.generic( dyn_union.to_any()));
241
242         // setting a different union value
243

244         dyn_union.set_to_default_member();
245         dyn_union.member().insert_short((short)19);
246         System.out.println("Passing the union again..." +
247                                s.generic( dyn_union.to_any()));
248
249     }
250     catch ( Exception JavaDoc e)
251     {
252         e.printStackTrace();
253     }
254     }
255 }
256
257
258
Popular Tags