Changes to attempt to silence bcc64x
[ACE_TAO.git] / TAO / examples / Simple / time / Time_Client.java
bloba3f1892300cd9a1a058ed6a5c9faf640aa478676
1 // ============================================================================
2 //
3 // = LIBRARY
4 // TAO/examples/Simple/time
5 //
6 // = FILENAME
7 // Time_Client.java
8 //
9 // = DESCRIPTION
10 // This class implements a Java client interface to the Time example.
12 // = AUTHOR
13 // Martin Krumpolec <krumpo@pobox.sk>
15 // ============================================================================
17 import JACE.Misc.GetOpt;
18 import org.omg.CosNaming.*;
19 import org.omg.CORBA.*;
20 import java.io.*;
22 public class Time_Client
24 private ORB orb = null;
25 private org.omg.CORBA.Object timeRef = null;
26 private boolean shutdown = false;
27 private boolean use_naming_service = false;
28 private String ior_file = null;
29 private String ior = null;
31 Time_Client (String args [])
33 orb = ORB.init (args, null);
34 parse_args (args);
37 private void parse_args (String args[])
39 GetOpt getopt = new GetOpt (args, "k:f:xn");
41 int ch = -1;
43 while ((ch = getopt.next ()) != -1)
45 switch (ch)
47 case 'k':
48 ior = getopt.opt_arg () ();
49 break;
50 case 'f':
51 ior_file = getopt.opt_arg () ();
52 try
54 java.io.FileInputStream file =
55 new FileInputStream(ior_file);
56 java.io.BufferedReader myInput =
57 new BufferedReader
58 (new InputStreamReader (file));
59 ior = myInput.readLine();
61 catch (Exception e)
63 System.out.println ("Error: " + e);
65 break;
66 case 'x':
67 shutdown = true;
68 break;
69 case 'n':
70 use_naming_service = true;
71 break;
72 default:
73 break;
77 return;
80 private Time get_time_object ()
82 if (ior != null)
84 org.omg.CORBA.Object objref = orb.string_to_object (ior);
85 return TimeHelper.narrow (objref);
87 else
89 if (use_naming_service)
91 // Use multicast to obtain NS handle.
92 NS_Resolve ns_resolve =
93 new NS_Resolve (null);
94 org.omg.CORBA.Object objRef =
95 ns_resolve.resolve_name_service (orb);
97 // Get the root naming context.
98 NamingContext ncRef =
99 NamingContextHelper.narrow (objRef);
100 System.out.println ("got initial NS reference");
102 // Resolve the Object Reference in Naming.
103 NameComponent nc =
104 new NameComponent ("Time", "");
105 NameComponent path[] = {nc};
108 return TimeHelper.narrow (ncRef.resolve (path));
110 catch (Exception e)
112 System.out.println ("Error: " + e);
116 return null;
119 public static void main (String args[])
123 Time_Client client = new Time_Client (args);
124 Time timeRef = client.get_time_object ();
126 if (timeRef == null)
128 System.out.println ("Unable to resolve Time.");
129 System.exit (-1);
132 // Call the Time server object and print results.
133 int time = timeRef.current_time ();
135 java.util.Date date =
136 new java.util.Date (((long) time) * 1000);
138 System.out.println ("string time is " + date);
140 if (client.shutdown)
142 timeRef.shutdown ();
143 System.out.println ("Shutting down server ...");
146 catch (Exception e)
148 System.out.println ("ERROR : " + e) ;
149 e.printStackTrace (System.out);