001: package demo.dynany;
002:
003: import java.io.*;
004: import org.omg.CORBA.Any;
005: import org.omg.CORBA.TypeCode;
006: import org.omg.CORBA.TCKind;
007: import org.omg.DynamicAny.*;
008: import org.omg.CosNaming.*;
009:
010: public class Client {
011: public static AnyServer s = null;
012:
013: public static void main(String[] args) {
014: try {
015: org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args, null);
016: DynAnyFactory dynFactory = DynAnyFactoryHelper.narrow(orb
017: .resolve_initial_references("DynAnyFactory"));
018:
019: NamingContextExt nc = NamingContextExtHelper.narrow(orb
020: .resolve_initial_references("NameService"));
021:
022: AnyServer s = AnyServerHelper.narrow(nc.resolve(nc
023: .to_name("DynAnyServer.example")));
024:
025: // char
026:
027: DynAny dyn_any = dynFactory
028: .create_dyn_any_from_type_code(orb
029: .get_primitive_tc(TCKind.tk_char));
030:
031: dyn_any.insert_char('c');
032: System.out.println("Passing a char..."
033: + s.generic(dyn_any.to_any()));
034:
035: // enum
036:
037: TypeCode enum_tc = orb.create_enum_tc("IDL:colors:1.0",
038: "colors", new String[] { "red", "green", "blue",
039: "mauve", "magenta", "salmon" });
040:
041: DynEnum dyn_enum = (DynEnum) dynFactory
042: .create_dyn_any_from_type_code(enum_tc);
043:
044: dyn_enum.set_as_string("salmon");
045: System.out.println("Passing an enum value..."
046: + s.generic(dyn_enum.to_any()));
047:
048: // sequence
049:
050: TypeCode seq_tc = orb.create_sequence_tc(2, orb
051: .create_string_tc(0));
052: DynSequence dyn_seq = (DynSequence) dynFactory
053: .create_dyn_any_from_type_code(seq_tc);
054:
055: Any[] string_sequence = new Any[2];
056: string_sequence[0] = orb.create_any();
057: string_sequence[1] = orb.create_any();
058: string_sequence[0].insert_string("hello");
059: string_sequence[1].insert_string("world");
060: dyn_seq.set_elements(string_sequence);
061:
062: System.out.println("Passing a sequence of length "
063: + dyn_seq.component_count() + "..."
064: + s.generic(dyn_seq.to_any()));
065: dyn_seq.destroy();
066:
067: // test the primitive get/set operations for DynSequences
068:
069: seq_tc = orb.create_sequence_tc(1, orb
070: .get_primitive_tc(org.omg.CORBA.TCKind.tk_boolean));
071:
072: dyn_seq = (DynSequence) dynFactory
073: .create_dyn_any_from_type_code(seq_tc);
074:
075: dyn_seq.set_length(1);
076: dyn_seq.insert_boolean(true);
077: dyn_seq.destroy();
078:
079: // array
080:
081: TypeCode array_tc = orb.create_array_tc(3, orb
082: .create_string_tc(0));
083: DynArray dyn_array = (DynArray) dynFactory
084: .create_dyn_any_from_type_code(array_tc);
085:
086: string_sequence = new Any[3];
087: string_sequence[0] = orb.create_any();
088: string_sequence[1] = orb.create_any();
089: string_sequence[2] = orb.create_any();
090: string_sequence[0].insert_string("hello");
091: string_sequence[1].insert_string("another");
092: string_sequence[2].insert_string("world");
093: dyn_array.set_elements(string_sequence);
094:
095: System.out.println("Passing an array of length "
096: + dyn_array.component_count() + "..."
097: + s.generic(dyn_array.to_any()));
098: dyn_array.destroy();
099:
100: // struct
101:
102: /*
103: * the hardest part is coming up with a correct call to
104: * create the struct's TypeCode, which is normally
105: * statically generated into the helper class
106: */
107:
108: TypeCode struct_tc = orb
109: .create_struct_tc(
110: "IDL:Node:1.0",
111: "Node",
112: new org.omg.CORBA.StructMember[] {
113: new org.omg.CORBA.StructMember(
114: "first",
115: org.omg.CORBA.ORB
116: .init()
117: .create_string_tc(0),
118: null),
119: new org.omg.CORBA.StructMember(
120: "second",
121: org.omg.CORBA.ORB
122: .init()
123: .create_sequence_tc(
124: 0,
125: org.omg.CORBA.ORB
126: .init()
127: .create_recursive_tc(
128: "IDL:Node:1.0")),
129: null) });
130:
131: DynStruct dyn_struct = (DynStruct) dynFactory
132: .create_dyn_any_from_type_code(struct_tc);
133:
134: // Create a list of name value pairs that holds the struct members.
135: // This struct has two members, so we have 2 array elements
136:
137: // the first struct member is a string,
138: // the member name is "first"
139:
140: NameValuePair[] nvp_seq = new NameValuePair[2];
141: Any first_member_any = orb.create_any();
142: first_member_any.insert_string("first");
143: nvp_seq[0] = new NameValuePair("first", first_member_any);
144:
145: /*
146: the second member is a recursive sequence, ie. the element
147: type of the sequence is that of the enclosing struct, the
148: member name is "second". We'll leave that sequence empty,
149: but we have to provide an initialized any for the empty
150: sequence, so we create a DynAny that initializes an any
151: as an empty sequence
152: */
153:
154: DynSequence second_member = (DynSequence) dynFactory
155: .create_dyn_any_from_type_code(struct_tc
156: .member_type(1));
157: nvp_seq[1] = new NameValuePair("second", second_member
158: .to_any());
159:
160: // insert the nvp list into the DynStruct
161: dyn_struct.set_members(nvp_seq);
162:
163: System.out.println("Passing a struct..."
164: + s.generic(dyn_struct.to_any()));
165:
166: // test the primitive get/set operations for DynStruct
167:
168: dyn_struct.seek(0);
169: dyn_struct.insert_string("newly inserted");
170: System.out.println("Passing the struct again..."
171: + s.generic(dyn_struct.to_any()));
172:
173: dyn_struct.destroy();
174: second_member.destroy();
175:
176: /* union */
177:
178: // setting up TypeCodes gets even more complicated,
179: // but you asked for it...
180: org.omg.CORBA.UnionMember[] members = new org.omg.CORBA.UnionMember[3];
181:
182: org.omg.CORBA.Any label_any;
183: label_any = orb.create_any();
184: label_any.insert_octet((byte) 0); // 0 byte means default case
185:
186: members[0] = new org.omg.CORBA.UnionMember("s", label_any,
187: org.omg.CORBA.ORB.init().get_primitive_tc(
188: org.omg.CORBA.TCKind.from_int(2)), null);
189:
190: label_any = org.omg.CORBA.ORB.init().create_any();
191: label_any.insert_char('l');
192: members[1] = new org.omg.CORBA.UnionMember("l", label_any,
193: org.omg.CORBA.ORB.init().get_primitive_tc(
194: org.omg.CORBA.TCKind.from_int(3)), null);
195:
196: label_any = org.omg.CORBA.ORB.init().create_any();
197: label_any.insert_char('f');
198:
199: members[2] = new org.omg.CORBA.UnionMember("f", label_any,
200: org.omg.CORBA.ORB.init().get_primitive_tc(
201: org.omg.CORBA.TCKind.from_int(6)), null);
202:
203: TypeCode union_tc = org.omg.CORBA.ORB.init()
204: .create_union_tc(
205: "IDL:Nums:1.0",
206: "Nums",
207: org.omg.CORBA.ORB.init().get_primitive_tc(
208: org.omg.CORBA.TCKind.from_int(9)),
209: members);
210:
211: DynUnion dyn_union = (DynUnion) dynFactory
212: .create_dyn_any_from_type_code(union_tc);
213:
214: Any discriminator_any = orb.create_any();
215: discriminator_any.insert_char('l');
216: DynAny discriminator = dynFactory
217: .create_dyn_any(discriminator_any);
218:
219: //dyn_enum.set_as_string("salmon");
220: // DynAny discriminator = dyn_enum;
221:
222: dyn_union.set_discriminator(discriminator);
223: dyn_union.member().insert_long(4711);
224: System.out.println("Passing a union..."
225: + s.generic(dyn_union.to_any()));
226:
227: // setting a different union value
228:
229: dyn_union.set_to_default_member();
230: dyn_union.member().insert_short((short) 19);
231: System.out.println("Passing the union again..."
232: + s.generic(dyn_union.to_any()));
233:
234: } catch (Exception e) {
235: e.printStackTrace();
236: }
237: }
238: }
|