KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > ubik > rmi > examples > time > TimeClient


1 package org.sapia.ubik.rmi.examples.time;
2
3 import java.util.Properties JavaDoc;
4
5 import javax.naming.InitialContext JavaDoc;
6 import javax.naming.NamingException JavaDoc;
7
8 import org.sapia.ubik.rmi.Consts;
9 import org.sapia.ubik.rmi.naming.remote.RemoteInitialContextFactory;
10 import org.sapia.ubik.rmi.server.Hub;
11 import org.sapia.ubik.rmi.server.Log;
12
13
14 /**
15  * @
16  */

17 public class TimeClient {
18   private InitialContext JavaDoc _theContext;
19   private TimeServiceIF _theTimeService;
20   private boolean _isLogging;
21
22   public TimeClient() {
23     this(true);
24   }
25
26   public TimeClient(boolean isLogging) {
27     try {
28       Log.setInfo();
29       _isLogging = isLogging;
30
31       Properties JavaDoc props = new Properties JavaDoc();
32
33       props.setProperty(InitialContext.PROVIDER_URL, "ubik://localhost:1099/");
34       props.setProperty(InitialContext.INITIAL_CONTEXT_FACTORY,
35         RemoteInitialContextFactory.class.getName());
36       props.setProperty(Consts.UBIK_DOMAIN_NAME,
37         Consts.DEFAULT_DOMAIN);
38
39       _theContext = new InitialContext JavaDoc(props);
40     } catch (Exception JavaDoc ne) {
41       System.err.println("Error creating the JNDI context");
42       ne.printStackTrace();
43       throw new RuntimeException JavaDoc("Error creating the JNDI context - " +
44         ne.getMessage());
45     }
46   }
47
48   public static void main(String JavaDoc[] args) {
49     try {
50       Log.setWarning();
51       new TimeClient().execute();
52       Hub.shutdown(30000);
53     } catch (Throwable JavaDoc t) {
54       t.printStackTrace();
55     }
56   }
57
58   public void execute() {
59     try {
60       String JavaDoc aTime = getTimeService().getTime();
61
62       if (_isLogging) {
63         System.out.println("Current time --> " + aTime);
64       }
65     } catch (NamingException JavaDoc ne) {
66       System.err.println("Error looking up the time service");
67       ne.printStackTrace();
68     }
69   }
70
71   public TimeServiceIF getTimeService() throws NamingException JavaDoc {
72     if (_theTimeService == null) {
73       Object JavaDoc anObject = _theContext.lookup("util/timeService");
74
75       if (anObject instanceof TimeServiceIF) {
76         _theTimeService = (TimeServiceIF) anObject;
77       } else {
78         throw new NamingException JavaDoc(
79           "The object received is not a TimeServiceIF: " + anObject);
80       }
81     }
82
83     return _theTimeService;
84   }
85 }
86
Popular Tags