1 package demo.dynany; 2 3 import java.io.*; 4 import org.omg.CORBA.Any ; 5 import org.omg.CORBA.TypeCode ; 6 import org.omg.CORBA.TCKind ; 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 [] args ) 16 { 17 try 18 { 19 org.omg.CORBA.ORB 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 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 44 TypeCode enum_tc = 45 orb.create_enum_tc("IDL:colors:1.0","colors", 46 new String []{"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 62 TypeCode 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 [] string_sequence = new Any [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 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 92 TypeCode 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 [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 113 118 119 TypeCode struct_tc = 120 orb.create_struct_tc( 121 "IDL:Node:1.0", 122 "Node", 123 new org.omg.CORBA.StructMember [] 124 { 125 new org.omg.CORBA.StructMember ( "first", 126 org.omg.CORBA.ORB.init().create_string_tc(0), 127 null ), 128 new org.omg.CORBA.StructMember ( "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 144 147 NameValuePair[] nvp_seq = new NameValuePair[2]; 148 Any 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 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 dyn_struct.set_members( nvp_seq ); 169 170 System.out.println("Passing a struct..." + 171 s.generic( dyn_struct.to_any() ) ); 172 173 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 184 185 188 org.omg.CORBA.UnionMember [] members = 189 new org.omg.CORBA.UnionMember [3]; 190 191 org.omg.CORBA.Any label_any; 192 label_any = orb.create_any(); 193 label_any.insert_octet((byte)0); 195 members[0] = 196 new org.omg.CORBA.UnionMember ("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 ("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 ("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 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 discriminator_any = orb.create_any(); 231 discriminator_any.insert_char('l'); 232 DynAny discriminator = dynFactory.create_dyn_any(discriminator_any); 233 234 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 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 e) 251 { 252 e.printStackTrace(); 253 } 254 } 255 } 256 257 258 | Popular Tags |