001: package demo.dynany;
002:
003: import org.omg.DynamicAny.*;
004: import org.omg.CORBA.Any;
005: import org.omg.CORBA.TCKind;
006: import org.omg.CosNaming.*;
007:
008: public class Server extends AnyServerPOA {
009: public static org.omg.CORBA.ORB orb;
010: DynAnyFactory factory;
011:
012: public Server() {
013: try {
014: factory = DynAnyFactoryHelper.narrow(orb
015: .resolve_initial_references("DynAnyFactory"));
016: } catch (Exception e) {
017: e.printStackTrace();
018: }
019: }
020:
021: public java.lang.String generic(Any a) {
022: printAny(a);
023: return "done.";
024: }
025:
026: private void printAny(Any a) {
027: String result = "<empty>";
028: try {
029: switch (a.type().kind().value()) {
030: case TCKind._tk_char:
031: System.out.println("char: " + a.extract_char());
032: break;
033: case TCKind._tk_short:
034: System.out.println("Short: " + a.extract_short());
035: break;
036: case TCKind._tk_long:
037: System.out.println("Long: " + a.extract_long());
038: break;
039: case TCKind._tk_double:
040: System.out.println("Double: " + a.extract_double());
041: break;
042: case TCKind._tk_float:
043: System.out.println("Float: " + a.extract_float());
044: break;
045: case TCKind._tk_string:
046: System.out.println("String: " + a.extract_string());
047: break;
048: case TCKind._tk_enum:
049: DynEnum dynEnum = (DynEnum) factory.create_dyn_any(a);
050: System.out.println("** Enum **");
051: System.out.println(dynEnum.get_as_string());
052: break;
053: case TCKind._tk_struct:
054: DynStruct dynstruct = (DynStruct) factory
055: .create_dyn_any(a);
056: org.omg.DynamicAny.NameValuePair[] members = dynstruct
057: .get_members();
058: System.out.println("** Struct **");
059: for (int i = 0; i < members.length; i++) {
060: System.out.println("name: " + members[i].id
061: + " value: ");
062: printAny(members[i].value);
063: }
064: break;
065: case TCKind._tk_union:
066: System.out.println("** Union **");
067: DynUnion dynunion = (DynUnion) factory
068: .create_dyn_any(a);
069: System.out.println("member name "
070: + dynunion.member_name());
071: printAny(dynunion.member().to_any());
072: break;
073: case TCKind._tk_sequence:
074: DynSequence dynseq = (DynSequence) factory
075: .create_dyn_any(a);
076: Any[] contents = dynseq.get_elements();
077: System.out.println("** Sequence of length "
078: + contents.length + " **");
079: for (int i = 0; i < contents.length; i++)
080: printAny(contents[i]);
081: break;
082: case TCKind._tk_array:
083: DynArray dynarray = (DynArray) factory
084: .create_dyn_any(a);
085: Any[] array_contents = dynarray.get_elements();
086: System.out.println("** Array **");
087: for (int i = 0; i < array_contents.length; i++)
088: printAny(array_contents[i]);
089: break;
090: default:
091: System.out.println("Unknown, kind "
092: + a.type().kind().value());
093:
094: }
095: } catch (Exception e) {
096: e.printStackTrace();
097: }
098: }
099:
100: public static void main(String[] args) {
101: orb = org.omg.CORBA.ORB.init(args, null);
102: try {
103: org.omg.PortableServer.POA poa = org.omg.PortableServer.POAHelper
104: .narrow(orb.resolve_initial_references("RootPOA"));
105:
106: poa.the_POAManager().activate();
107:
108: org.omg.CORBA.Object o = poa
109: .servant_to_reference(new Server());
110:
111: NamingContextExt nc = NamingContextExtHelper.narrow(orb
112: .resolve_initial_references("NameService"));
113:
114: nc.bind(nc.to_name("DynAnyServer.example"), o);
115: } catch (Exception e) {
116: e.printStackTrace();
117: }
118: orb.run();
119: }
120: }
|