KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > demo > policies > Client


1 package demo.policies;
2
3 import java.io.*;
4 import org.omg.CORBA.*;
5 import org.omg.Messaging.*;
6
7 /**
8  * A simple demo that shows how to use CORBA QoS policies at the level
9  * of individual objects, or ORB-wide. This is a variaton on hello
10  * world and relies on IOR files rather than the naming service.
11  *
12  * @author Gerald Brose
13  */

14
15 public class Client
16 {
17     public static final int MSEC_FACTOR = 10000;
18     public static final int ONE_SECOND = 1000 * MSEC_FACTOR;
19
20     public static void main( String JavaDoc args[] )
21     {
22         if( args.length != 1 )
23     {
24             System.out.println( "Usage: jaco demo.policies.Client <ior_file>" );
25             System.exit( 1 );
26         }
27
28         try
29     {
30             // read IOR from file
31
File f = new File( args[ 0 ] );
32             if( ! f.exists() )
33             {
34                 System.out.println("File " + args[0] +
35                                    " does not exist.");
36                 System.exit( -1 );
37             }
38             
39             //check if args[0] points to a directory
40
if( f.isDirectory() )
41             {
42                 System.out.println("File " + args[0] +
43                                    " is a directory.");
44                 System.exit( -1 );
45             }
46             BufferedReader br =
47                 new BufferedReader( new FileReader( f ));
48
49             // initialize the ORB
50
ORB orb = ORB.init( args, null );
51
52             // get object reference from command-line argument file
53
org.omg.CORBA.Object JavaDoc obj =
54                 orb.string_to_object( br.readLine() );
55
56             br.close();
57
58             // and narrow it to HelloWorld.GoodDay
59
// if this fails, a BAD_PARAM will be thrown
60
GoodDay goodDay = GoodDayHelper.narrow( obj );
61
62             // get PolicyManager and create policies ....
63

64             PolicyManager policyManager =
65                 PolicyManagerHelper.narrow( orb.resolve_initial_references("ORBPolicyManager"));
66
67             // create an timeout value of 1 sec. The unit is a time
68
// step of 100 nano secs., so 10000 of these make up a
69
// micro second.
70
Any rrtPolicyAny = orb.create_any();
71             rrtPolicyAny.insert_ulonglong( ONE_SECOND );
72
73             // create a relative roundtrip timeout policy and set this
74
// policy ORB-wide
75
Policy rrtPolicy =
76                 orb.create_policy( RELATIVE_RT_TIMEOUT_POLICY_TYPE.value,
77                                    rrtPolicyAny );
78
79             policyManager.set_policy_overrides( new Policy[] {rrtPolicy},
80                                           SetOverrideType.ADD_OVERRIDE);
81
82             // create a sync scope policy
83
Any syncPolicyAny = orb.create_any();
84             syncPolicyAny.insert_short( SYNC_WITH_SERVER.value );
85             Policy syncPolicy =
86                 orb.create_policy( SYNC_SCOPE_POLICY_TYPE.value, syncPolicyAny );
87             
88             // set the sync scope policy on an object reference
89
goodDay._set_policy_override( new Policy[] {syncPolicy},
90                                           SetOverrideType.ADD_OVERRIDE);
91
92             // try to invoke the operation and print the result: this
93
// should result in a timeout exception because the server
94
// will sleep longer than the timeout (sleep is int msecs.)
95
System.out.println( "Should see a TIMEOUT exception soon...");
96             try
97             {
98                 System.out.println( goodDay.hello( 2000 ) );
99                 System.out.println( "... should not be here, no exception!");
100             }
101             catch ( org.omg.CORBA.TIMEOUT JavaDoc t )
102             {
103                 System.out.println( "here it is: " + t.getMessage());
104             }
105
106             // create another timeout poliy
107
Any objRrtPolicyAny = orb.create_any();
108             objRrtPolicyAny.insert_ulonglong( 4 * ONE_SECOND );
109
110             Policy objRrtPolicy =
111                 orb.create_policy( RELATIVE_RT_TIMEOUT_POLICY_TYPE.value,
112                                    objRrtPolicyAny );
113
114             goodDay._set_policy_override( new Policy[] {objRrtPolicy},
115                                           SetOverrideType.ADD_OVERRIDE);
116
117             // see what the effective policy is now...
118
Policy objPol =
119                 goodDay._get_policy( RELATIVE_RT_TIMEOUT_POLICY_TYPE.value );
120
121             if ( objPol != null)
122             {
123                 long timoutValue = ((RelativeRoundtripTimeoutPolicy)objPol).relative_expiry();
124                 System.out.println("Object-level timeout is " + timoutValue + " timesteps (100ns)");
125
126                 // try the invocation again, in this case the override
127
// policy's new timeout should be sufficient to let
128
// the call come back
129
System.out.println( goodDay.hello( 100 ));
130             }
131             else
132             {
133                 System.out.println("ERROR, no Object-level timeout found");
134             }
135         }
136         catch( Exception JavaDoc ex )
137     {
138             ex.printStackTrace();
139         }
140     }
141 }
142
143
Popular Tags