KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > test > sas > Basic_Client


1 package org.jacorb.test.sas;
2
3 import java.io.BufferedReader JavaDoc;
4 import java.io.FileWriter JavaDoc;
5 import java.io.InputStreamReader JavaDoc;
6 import java.io.PrintWriter JavaDoc;
7 import java.net.URL JavaDoc;
8
9 import org.omg.BiDirPolicy.BIDIRECTIONAL_POLICY_TYPE;
10 import org.omg.BiDirPolicy.BOTH;
11 import org.omg.BiDirPolicy.BidirectionalPolicyValueHelper;
12 import org.omg.CORBA.Any JavaDoc;
13 import org.omg.CORBA.ORB JavaDoc;
14 import org.omg.CORBA.Policy JavaDoc;
15 import org.omg.PortableServer.IdAssignmentPolicyValue JavaDoc;
16 import org.omg.PortableServer.ImplicitActivationPolicyValue JavaDoc;
17 import org.omg.PortableServer.LifespanPolicyValue JavaDoc;
18 import org.omg.PortableServer.POA JavaDoc;
19
20 import InterOpTest.Johnson;
21 import InterOpTest.JohnsonHelper;
22 import InterOpTest.Peter;
23 import InterOpTest.PeterHelper;
24 import InterOpTest.PeterPOA;
25
26 /**
27  * This is the client side of the sas demo. It just calls the single
28  * operation "printCert()" of the server. As you can see, sas is fully
29  * transparent.
30  *
31  * @author Nicolas Noffke
32  * @version $Id: Basic_Client.java,v 1.2 2004/03/16 17:55:40 david.robison Exp $
33  */

34
35 public class Basic_Client extends PeterPOA {
36
37     private static ORB JavaDoc orb = null;
38     
39     public static void main(String JavaDoc args[]) {
40         if (args.length != 3) {
41             System.out.println("Usage: <ior_file> <username> <password>");
42             System.exit(1);
43         }
44
45         try {
46             // initialize the ORB.
47
orb = ORB.init(args, null);
48             
49             // initialize the POA
50
POA JavaDoc rootPOA = (POA JavaDoc) orb.resolve_initial_references("RootPOA");
51             Policy JavaDoc[] policies = new Policy JavaDoc[4];
52             policies[0] = rootPOA.create_lifespan_policy(LifespanPolicyValue.TRANSIENT);
53             policies[1] = rootPOA.create_id_assignment_policy(IdAssignmentPolicyValue.SYSTEM_ID);
54             policies[2] = rootPOA.create_implicit_activation_policy( ImplicitActivationPolicyValue.IMPLICIT_ACTIVATION );
55             Any JavaDoc bidirAny = orb.create_any();
56             BidirectionalPolicyValueHelper.insert( bidirAny, BOTH.value );
57             policies[3] = orb.create_policy( BIDIRECTIONAL_POLICY_TYPE.value, bidirAny );
58             POA JavaDoc bidirPOA = rootPOA.create_POA( "BiDirPOA", rootPOA.the_POAManager(), policies );
59             bidirPOA.the_POAManager().activate();
60             
61             // run the ORB
62
Thread JavaDoc orbThread = new Thread JavaDoc(new Runnable JavaDoc() {
63                 public void run() {
64                     runORB();
65                     System.out.println("ORB closed");
66                 }
67             });
68             orbThread.start();
69
70             // create peter and write his IOR
71
Basic_Client server = new Basic_Client();
72             bidirPOA.activate_object(server);
73             Peter peter = PeterHelper.narrow(bidirPOA.servant_to_reference(server));
74             PrintWriter JavaDoc pw = new PrintWriter JavaDoc(new FileWriter JavaDoc("peter.ior"));
75             pw.println(orb.object_to_string(peter));
76             pw.flush();
77             pw.close();
78
79             // get the server
80
URL JavaDoc iorURL = new URL JavaDoc(args[0]);
81             System.out.println("Reading object from ior: "+args[0]);
82             BufferedReader JavaDoc br = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(iorURL.openStream()));//new FileReader(f));
83
org.omg.CORBA.Object JavaDoc obj = orb.string_to_object(br.readLine());
84             br.close();
85             Johnson johnson = JohnsonHelper.narrow(obj);
86             
87             // call
88
System.out.println("CSS1:"+johnson.say_hello("hello from JacORB"));
89             System.out.println("CSS2:"+johnson.say_hello("hello from JacORB"));
90             System.out.println("TSS1:"+johnson.say_hello_from_you(peter, 1));
91             System.out.println("TSS2:"+johnson.say_hello_from_you(peter, 1));
92
93             System.out.println("Call to server succeeded");
94         } catch (Exception JavaDoc ex) {
95             System.out.println("Error: " + ex + ": " + ex.getMessage());
96             ex.printStackTrace();
97         }
98         if (orb != null) orb.shutdown(false);
99     }
100     
101     private static void runORB() {
102         orb.run();
103     }
104
105     /* (non-Javadoc)
106      * @see InterOpTest.PeterOperations#say_hello(java.lang.String)
107      */

108     public String JavaDoc say_hello(String JavaDoc msg) {
109         System.out.println("TSS: "+msg);
110         return "Hello from Peter!\n" + msg;
111     }
112 }
113
Popular Tags