import net.jini.core.entry.*; import net.jini.core.lookup.*; import net.jini.core.discovery.*; import net.jini.discovery.*; import net.jini.lookup.entry.*; import java.rmi.*; /*************************************************************************** MyClient -- Uses Discovery to Find the Lookup Server ***************************************************************************/ class MyClient implements DiscoveryListener { LookupDiscovery discovery; ServiceRegistrar[] registrars; MyClient () { try { discovery = new LookupDiscovery (LookupDiscovery.ALL_GROUPS); discovery.addDiscoveryListener (this); System.out.println ("client: searching for lookups..."); } catch (Exception e) { System.out.println ("MyClient(): " + e); } } public void discovered (DiscoveryEvent evt) // DiscoveryListener { int i; LookupLocator ll; Entry[] aeAttributes; ServiceTemplate template; MyServerInterface myServerInterface; /* Each cycle of this loop attempts to find MyServerInterface in one of the registrars that was passed to this method. ========================================================== */ registrars = evt.getRegistrars (); for (i = 0; i < registrars.length; i++) { try { ll = registrars[i].getLocator (); System.out.println ("client: got proxy1 to a lookup [" + ll + "]"); aeAttributes = new Entry[1]; aeAttributes[0] = new Name ("MyServer"); template = new ServiceTemplate (null, null, aeAttributes); myServerInterface = (MyServerInterface) registrars[i].lookup (template); System.out.println ("client: requested proxy2 by name [" + ((Name) aeAttributes[0]).name + "]"); System.out.println ("client: got proxy2 [" + myServerInterface.getClass().getName() + "]"); /* If the correct object was returned, call one of its methods then exit the program. =========================================================== */ if (myServerInterface instanceof MyServerInterface) { System.out.println ("client: called proxy2.sayHello() --->" + myServerInterface.sayHello () + "<---"); System.out.println ("*-----------------------------------------" + "--------------------------------*\n"); System.exit (0); } } catch (Exception e) { System.out.println ("client: exception " + e); } } } public void discarded (DiscoveryEvent evt) // DiscoveryListener { int i; registrars = evt.getRegistrars (); try { for (i = 0; i < registrars.length; i++) { System.out.println ("client: discarded lookup [" + registrars[i].getLocator () + "]"); } } catch (Exception e) { System.out.println ("client: exception " + e); } } public static void main (String[] args) { MyClient myClient; try { System.setSecurityManager (new RMISecurityManager ()); System.out.println ("*-----------------------------------------" + "--------------------------------*"); myClient = new MyClient (); Thread.currentThread().join (); myClient.discovery.terminate (); } catch (Exception e) { System.out.println ("main(): " + e); } } }