import net.jini.core.entry.*; import net.jini.core.lookup.*; import net.jini.core.discovery.*; import net.jini.lookup.entry.*; import com.sun.jini.lookup.*; import com.sun.jini.lease.*; import java.io.*; import java.rmi.*; import java.rmi.server.*; import java.applet.*; /*************************************************************************** MyServer -- The Jini Server Class ***************************************************************************/ public class MyServer extends UnicastRemoteObject implements MyServerInterface, ServiceIDListener { public MyServer () throws RemoteException { super (); } public String sayHello () throws RemoteException // MyServiceInterface { System.out.println ("server: The method sayHello() in MyServer was " + "called"); return ("Hello World from MyServer!"); } public void serviceIDNotify (ServiceID sidIn) // ServiceIDListener { System.out.println ("server: received ServiceID = " + sidIn); } public static void main (String[] args) { int i; int iPort; String sHost; MyServer myServer; MyServerApplet myServerApplet; LookupLocator lookup; Entry[] aeAttributes; JoinManager joinmanager; ServiceID id; ServiceMatches matches; ServiceRegistrar registrar; try { /* Setting the security manager to allow the RMI class loader to go to the codebase for classes that are not available locally. ========================================================== */ System.setSecurityManager (new RMISecurityManager ()); /* Create the attributes (an array of entry objects) that describe this server and use it to register this server with the lookup service. Add an instance of MyServerApplet as an attribute of this service. JoinManager finds and registers with the lookup service. =============================================================== */ myServer = new MyServer (); aeAttributes = new Entry[2]; aeAttributes[0] = new Name ("MyServer"); aeAttributes[1] = new MyServerApplet (myServer); joinmanager = new JoinManager ( myServer, aeAttributes, myServer, new LeaseRenewalManager () ); System.out.println (); System.out.println ("server: JoinManager (a) = " + joinmanager); /* Find the Jini lookup service (reggie) and print its location. ============================================================= */ lookup = new LookupLocator ("jini://localhost"); sHost = lookup.getHost (); iPort = lookup.getPort (); System.out.println ("server: LookupLocator = " + lookup); System.out.println ("server: LookupLocator.host = " + sHost); System.out.println ("server: LookupLocator.port = " + iPort); /* Get the lookup service's ServiceRegistrar (the class by which interaction with the lookup service is possible). ============================================================= */ registrar = lookup.getRegistrar (); id = registrar.getServiceID (); System.out.println ("server: ServiceRegistrar = " + registrar); System.out.println ("server: ServiceID = " + id); /* Itemize the service items and the service objects that are available in the lookup service. Note: Sometimes this is executed too quickly for the service that was registered above to show up. ============================================================== */ try {Thread.sleep (2000);} catch (Exception e) {} matches = registrar.lookup ( new ServiceTemplate (null, null, null), 50 ); System.out.println ("server: ServiceMatches = " + matches); System.out.println ("server: num matches = " + matches.totalMatches); for (i = 0; i < matches.totalMatches; i++) { System.out.println ("server: svc item " + i + ": " + matches.items[i]); System.out.println ("server: svc object " + i + ": " + matches.items[i].service); } System.out.println ("*-----------------------------------------*"); } catch (Exception e) { System.out.println("server: MyServer.main(): Exception " + e); } } }