1 package org.sapia.ubik.rmi.examples.time; 2 3 import java.util.Properties ; 4 5 import javax.naming.InitialContext ; 6 import javax.naming.NamingException ; 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 17 public class TimeClient { 18 private InitialContext _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 props = new Properties (); 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 (props); 40 } catch (Exception ne) { 41 System.err.println("Error creating the JNDI context"); 42 ne.printStackTrace(); 43 throw new RuntimeException ("Error creating the JNDI context - " + 44 ne.getMessage()); 45 } 46 } 47 48 public static void main(String [] args) { 49 try { 50 Log.setWarning(); 51 new TimeClient().execute(); 52 Hub.shutdown(30000); 53 } catch (Throwable t) { 54 t.printStackTrace(); 55 } 56 } 57 58 public void execute() { 59 try { 60 String aTime = getTimeService().getTime(); 61 62 if (_isLogging) { 63 System.out.println("Current time --> " + aTime); 64 } 65 } catch (NamingException ne) { 66 System.err.println("Error looking up the time service"); 67 ne.printStackTrace(); 68 } 69 } 70 71 public TimeServiceIF getTimeService() throws NamingException { 72 if (_theTimeService == null) { 73 Object anObject = _theContext.lookup("util/timeService"); 74 75 if (anObject instanceof TimeServiceIF) { 76 _theTimeService = (TimeServiceIF) anObject; 77 } else { 78 throw new NamingException ( 79 "The object received is not a TimeServiceIF: " + anObject); 80 } 81 } 82 83 return _theTimeService; 84 } 85 } 86 | Popular Tags |