KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > demo > imr > Server


1 package demo.imr;
2
3 import java.io.*;
4
5 import org.omg.CORBA.*;
6 import org.omg.PortableServer.*;
7
8 import org.jacorb.util.*;
9
10 public class Server
11     extends SomeIfPOA
12 {
13     public Server()
14     {
15     }
16
17     public void op()
18     {
19         System.out.println("Server: Received call from client");
20     }
21         
22     public static void main(String JavaDoc[] args)
23     {
24         if( args.length != 2 )
25     {
26             System.out.println(
27                 "Usage: jaco demo.imr.Server <ior_file> <timeout in secs>");
28             System.exit( 1 );
29         }
30
31         System.setProperty( "jacorb.implname", "imr_demo" );
32         System.setProperty( "jacorb.use_imr", "on" );
33
34         try
35         {
36             long timeout = Integer.parseInt( args[1] ) * 1000;
37             //init ORB
38
ORB orb = ORB.init( args, null );
39
40             //get root POA
41
org.omg.PortableServer.POA JavaDoc root_poa =
42         org.omg.PortableServer.POAHelper.narrow(
43                     orb.resolve_initial_references("RootPOA"));
44
45             //create necessary policies
46
org.omg.CORBA.Policy JavaDoc[] policies = new org.omg.CORBA.Policy JavaDoc[2];
47
48         policies[0] = root_poa.create_lifespan_policy(LifespanPolicyValue.PERSISTENT);
49         policies[1] = root_poa.create_id_assignment_policy(IdAssignmentPolicyValue.USER_ID);
50             
51             //create user POA with these policies
52
POA demo_poa = root_poa.create_POA( "ImRDemoServerPOA",
53                                                 root_poa.the_POAManager(),
54                                                 policies );
55             
56             //destroy policies
57
for (int i=0; i<policies.length; i++)
58         policies[i].destroy();
59
60             //instanciate implementation
61
Server s = new Server();
62
63             //create object id
64
byte[] id = "imr_demo".getBytes();
65
66             //activate object
67
demo_poa.activate_object_with_id( id, s );
68
69             //make POA accept requests
70
root_poa.the_POAManager().activate();
71
72             
73             // create the object reference
74
org.omg.CORBA.Object JavaDoc obj =
75                 demo_poa.servant_to_reference( s );
76
77             PrintWriter pw =
78                 new PrintWriter( new FileWriter( args[ 0 ] ));
79
80             // print stringified object reference to file
81
pw.println( orb.object_to_string( obj ));
82             
83             pw.flush();
84             pw.close();
85     
86             // wait for requests
87
Thread.sleep( timeout );
88
89             orb.shutdown( true );
90         }
91         catch( Exception JavaDoc e )
92         {
93             System.out.println( e );
94         }
95     }
96 }
97
98
99
Popular Tags