KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > demo > dii > DiiServer


1 package demo.dii;
2
3 import org.omg.CORBA.Any JavaDoc;
4 import org.omg.CosNaming.*;
5
6 public class DiiServer
7     extends org.omg.PortableServer.DynamicImplementation JavaDoc
8 {
9     private String JavaDoc[] ids = {"IDL:dii/server:1.0"};
10
11     // singleton ORB as any factory
12
org.omg.CORBA.ORB JavaDoc orb = null;
13     
14     serverImpl impl = new serverImpl();
15
16     public DiiServer( org.omg.CORBA.ORB JavaDoc orb )
17     {
18         this.orb = orb;
19     }
20
21     /** from Servant */
22
23     public String JavaDoc[] _all_interfaces(org.omg.PortableServer.POA JavaDoc poa,
24                                     byte[] objectId)
25     {
26     return ids;
27     }
28
29
30     public void invoke(org.omg.CORBA.ServerRequest JavaDoc request)
31     {
32     String JavaDoc op = request.operation();
33     try
34     {
35         if( op.equals("_get_long_number"))
36         {
37         Any JavaDoc a = orb.create_any();
38         a.insert_long( impl.long_number());
39         request.set_result( a );
40         }
41         else if( op.equals("_set_long_number"))
42         {
43         /* set up an argument list */
44         org.omg.CORBA.NVList JavaDoc params = orb.create_list(0);
45         /* there is only on argument to this call, i.e. a long */
46         Any JavaDoc numAny = orb.create_any();
47         numAny.type( orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_long));
48         params.add_value( "", numAny, org.omg.CORBA.ARG_IN.value );
49         
50         /* extract the argugments */
51         request.arguments( params );
52
53         /* make the call */
54         impl.long_number( numAny.extract_long());
55
56         /* set up the any for the result */
57         Any JavaDoc s = orb.create_any();
58         s.type( orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_void ));
59         request.set_result( s );
60         }
61         else if( op.equals("writeNumber") )
62         {
63         org.omg.CORBA.NVList JavaDoc params = orb.create_list(0);
64         Any JavaDoc numAny = orb.create_any();
65         numAny.type( orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_long));
66         params.add_value( "", numAny,org.omg.CORBA.ARG_IN.value );
67         request.arguments( params );
68         Any JavaDoc a = orb.create_any();
69         a.insert_string( impl.writeNumber( numAny.extract_long() ));
70         request.set_result( a );
71         }
72         else if( op.equals("add") )
73         {
74         org.omg.CORBA.NVList JavaDoc params = orb.create_list(0);
75         Any JavaDoc argOneAny = orb.create_any();
76         Any JavaDoc argTwoAny = orb.create_any();
77         Any JavaDoc outArgAny = orb.create_any();
78         argOneAny.type( orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_long));
79         argTwoAny.type( orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_long));
80         outArgAny.type( orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_long));
81
82         /* add these anys to the parameter list */
83         params.add_value( "", argOneAny, org.omg.CORBA.ARG_IN.value );
84         params.add_value( "", argTwoAny, org.omg.CORBA.ARG_IN.value );
85         params.add_value( "", outArgAny, org.omg.CORBA.ARG_OUT.value );
86
87         /* read in and inout arguments */
88         request.arguments( params );
89
90         /* do the computation and fill it into the out arg */
91         org.omg.CORBA.IntHolder JavaDoc iHolder = new org.omg.CORBA.IntHolder JavaDoc();
92         impl.add( argOneAny.extract_long(), argTwoAny.extract_long(), iHolder );
93
94         outArgAny.insert_long( iHolder.value);
95
96         Any JavaDoc resultAny = orb.create_any();
97         resultAny.type(orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_void) );
98         request.set_result( resultAny );
99         }
100         else if( op.equals("writeNumberWithEx") )
101         {
102         org.omg.CORBA.NVList JavaDoc params = orb.create_list(0);
103         Any JavaDoc numAny = orb.create_any();
104         numAny.type( orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_long));
105         params.add_value( "", numAny, org.omg.CORBA.ARG_IN.value );
106         request.arguments( params );
107         Any JavaDoc a = orb.create_any();
108         a.insert_string( impl.writeNumberWithEx( numAny.extract_long() ));
109         request.set_result( a );
110         }
111         else if( op.equals("notify") )
112         {
113         org.omg.CORBA.NVList JavaDoc params = orb.create_list(0);
114         Any JavaDoc stringAny = orb.create_any();
115         stringAny.type( orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_string));
116         params.add_value( "", stringAny, org.omg.CORBA.ARG_IN.value );
117         request.arguments( params );
118         impl._notify( stringAny.extract_string() );
119         Any JavaDoc s = orb.create_any();
120         s.type( orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_void ));
121         request.set_result( s );
122         }
123         else if( op.equals("_non_existent") )
124         {
125         Any JavaDoc s = orb.create_any();
126         s.type( orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_boolean ));
127         s.insert_boolean( _non_existent());
128         request.set_result( s );
129         }
130         /**
131          * the following operations would also have to be implemented
132          * by delegating to the superclass DynamicImplementation or Servant
133          * but are omitted here for brevity
134          */

135         else if( op.equals("_all_interfaces") )
136         {
137         throw new org.omg.CORBA.BAD_OPERATION JavaDoc("Object reference operations not implemented in example!");
138         }
139         else if( op.equals("_get_interface") )
140         {
141         throw new org.omg.CORBA.BAD_OPERATION JavaDoc("Object reference operations not implemented in example!");
142         }
143         else if( op.equals("_is_a") )
144         {
145         throw new org.omg.CORBA.BAD_OPERATION JavaDoc("Object reference operations not implemented in example");
146         }
147         else
148         {
149         throw new org.omg.CORBA.BAD_OPERATION JavaDoc(op + " not found.");
150         }
151     }
152     catch ( org.omg.CORBA.UserException JavaDoc e )
153     {
154             System.out.println("Caught: " + e );
155             
156         Any JavaDoc exceptAny = orb.create_any();
157         try
158         {
159         Class JavaDoc helperClass = Class.forName( e.getClass().getName() + "Helper");
160         Class JavaDoc anyClass = Class.forName("org.omg.CORBA.Any");
161         java.lang.reflect.Method JavaDoc insert =
162             helperClass.getDeclaredMethod("insert",
163                           new Class JavaDoc[] { anyClass, e.getClass() });
164
165         insert.invoke( null, new java.lang.Object JavaDoc[]{exceptAny, e});
166         }
167         catch ( Exception JavaDoc sfe )
168         {
169         sfe.printStackTrace();
170         }
171         request.set_exception( exceptAny );
172     }
173     catch ( org.omg.CORBA.SystemException JavaDoc e )
174     {
175         try
176             {
177         ((org.jacorb.orb.dsi.ServerRequest)request).setSystemException( e );
178         }
179         catch ( Exception JavaDoc sfe )
180         {
181         sfe.printStackTrace();
182         }
183     }
184     catch ( Exception JavaDoc e )
185     {
186         e.printStackTrace();
187     }
188     }
189
190
191     public static void main( String JavaDoc[] args )
192     {
193     try
194     {
195         org.omg.CORBA.ORB JavaDoc orb = org.omg.CORBA.ORB.init(args, null);
196         org.omg.PortableServer.POA JavaDoc poa =
197         org.omg.PortableServer.POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
198
199         poa.the_POAManager().activate();
200
201         org.omg.CORBA.Object JavaDoc o =
202                 poa.servant_to_reference(new DiiServer( orb ));
203
204         // register server with naming context
205
NamingContextExt nc =
206                 NamingContextExtHelper.narrow(orb.resolve_initial_references("NameService"));
207         nc.bind( nc.to_name("dii.example"), o);
208         orb.run();
209     }
210     catch ( Exception JavaDoc e )
211     {
212         e.printStackTrace();
213     }
214     }
215 }
216
217
218
Popular Tags