KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > demo > dynany > Server


1 package demo.dynany;
2
3 import org.omg.DynamicAny.*;
4 import org.omg.CORBA.Any JavaDoc;
5 import org.omg.CORBA.TCKind JavaDoc;
6 import org.omg.CosNaming.*;
7
8 public class Server
9     extends AnyServerPOA
10 {
11     public static org.omg.CORBA.ORB JavaDoc orb;
12     DynAnyFactory factory;
13
14     public Server()
15     {
16     try
17     {
18         factory = DynAnyFactoryHelper.narrow( orb.resolve_initial_references("DynAnyFactory"));
19     }
20     catch( Exception JavaDoc e )
21     {
22         e.printStackTrace();
23     }
24     }
25     public java.lang.String JavaDoc generic(Any a)
26     {
27     printAny(a);
28     return "done.";
29     }
30
31     private void printAny(Any a)
32     {
33     String JavaDoc result = "<empty>";
34     try
35     {
36         switch( a.type().kind().value() )
37         {
38         case TCKind._tk_char:
39         System.out.println("char: " + a.extract_char());
40         break;
41         case TCKind._tk_short:
42         System.out.println("Short: " + a.extract_short());
43         break;
44         case TCKind._tk_long:
45         System.out.println("Long: " + a.extract_long());
46         break;
47         case TCKind._tk_double:
48         System.out.println("Double: " + a.extract_double());
49         break;
50         case TCKind._tk_float:
51         System.out.println("Float: " + a.extract_float());
52         break;
53         case TCKind._tk_string:
54         System.out.println("String: " + a.extract_string());
55         break;
56         case TCKind._tk_enum:
57         DynEnum dynEnum = (DynEnum)factory.create_dyn_any(a);
58         System.out.println("** Enum **");
59         System.out.println( dynEnum.get_as_string());
60         break;
61         case TCKind._tk_struct:
62         DynStruct dynstruct = (DynStruct)factory.create_dyn_any( a );
63         org.omg.DynamicAny.NameValuePair JavaDoc[] members = dynstruct.get_members();
64         System.out.println("** Struct **");
65         for( int i = 0; i < members.length; i++ )
66         {
67             System.out.println("name: " + members[i].id + " value: " );
68             printAny( members[i].value);
69         }
70         break;
71         case TCKind._tk_union:
72         System.out.println("** Union **");
73         DynUnion dynunion = (DynUnion)factory.create_dyn_any( a );
74         System.out.println("member name " + dynunion.member_name());
75         printAny(dynunion.member().to_any());
76         break;
77         case TCKind._tk_sequence:
78         DynSequence dynseq = (DynSequence)factory.create_dyn_any( a );
79         Any[] contents = dynseq.get_elements();
80         System.out.println("** Sequence of length " + contents.length + " **");
81         for( int i = 0; i < contents.length; i++)
82             printAny( contents[i]);
83         break;
84         case TCKind._tk_array:
85         DynArray dynarray = (DynArray)factory.create_dyn_any( a );
86         Any[] array_contents = dynarray.get_elements();
87         System.out.println("** Array **");
88         for( int i = 0; i < array_contents.length; i++)
89             printAny( array_contents[i]);
90         break;
91         default:
92         System.out.println("Unknown, kind " + a.type().kind().value());
93         
94         }
95     }
96     catch ( Exception JavaDoc e )
97     {
98         e.printStackTrace();
99     }
100     }
101
102     public static void main( String JavaDoc[] args )
103     {
104     orb = org.omg.CORBA.ORB.init(args, null);
105     try
106     {
107         org.omg.PortableServer.POA JavaDoc poa =
108         org.omg.PortableServer.POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
109
110         poa.the_POAManager().activate();
111         
112         org.omg.CORBA.Object JavaDoc o = poa.servant_to_reference(new Server());
113
114         NamingContextExt nc =
115         NamingContextExtHelper.narrow(orb.resolve_initial_references("NameService"));
116
117         nc.bind(nc.to_name("DynAnyServer.example"), o);
118     }
119     catch ( Exception JavaDoc e )
120     {
121         e.printStackTrace();
122     }
123     orb.run();
124     }
125 }
126
127
128
Popular Tags