import java.util.Map; import java.util.HashMap; import java.util.Set; import java.rmi.Naming; import java.rmi.Remote; import java.rmi.RemoteException; import java.rmi.RMISecurityManager; import java.rmi.registry.Registry; import java.rmi.server.UnicastRemoteObject; /** * Remote Object Registry. This class allows one to register remote objects * using an arbitrary identifier string. Unlike rmiregistry the registry and * service can be on different hosts. One can also have more than one service * registered with the same identifier. *

* This is intended to be example code to give one some idea of how one can * develop a remote object registry. It has not been tested. * @author Ken Baclawski */ @Copyright public class MyRegistry extends UnicastRemoteObject implements Registry { /** * Map from identifiers to sets of remote objects. */ private Map map = new HashMap(); /** * Construct a registry. * @throws RemoteException if an I/O error occurs. */ public MyRegistry() throws RemoteException { } /** * Bind a remote reference to the specified identifier in this registry. * @param id The identifier string to be bound. It can be any string. * @param obj The remote object being bound. */ public void bind(String id, Remote obj) { ServiceGroup group = map.get(id); if (group == null) { group = new ServiceGroup(); map.put(id, group); } group.add(obj); } /** * Get a list of all identifiers currently bound in this registry. * @return The array of bound identifiers. */ public String[] list() { Set identifiers = map.keySet(); int length = identifiers.size(); String[] array = new String[length]; identifiers.toArray(array); return array; } /** * Get a remote reference bound to the specified identifier in this registry. * @param id The identifier of the remote object. * @return A remote object in the service group of the identifier or null * if there is no service group or if there is no remote object in the * service group. */ public Remote lookup(String id) { ServiceGroup group = map.get(id); if (group == null) { return null; } return group.getNext(); } /** * Bind a remote reference to the specified identifier in this registry. * @param identifier The identifier string to be bound. It can be any string. * @param obj The remote object being bound. */ public void rebind(String id, Remote obj) { bind(id, obj); } /** * Remove the binding for the specified id in this registry. * The entire service group is removed. * @param id The identifier string to be unbound. */ public void unbind(String id) { map.remove(id); } /** * Server program for the MyRegistry example. * @param args Only the first command-line argument is used. It specifies * the name to be used for registering MyRegistry with the local * rmiregistry. If it is not specified, then "//localhost/MyRegistry" is * used. */ public static void main (String[] args) { System.setSecurityManager(new RMISecurityManager() { public void checkConnect(String host, int port) {} public void checkAccept(String host, int port) {}}); try { String name = (args.length > 0)? args[0] : "//localhost/MyRegistry"; Naming.rebind(name, new MyRegistry()); System.out.println("MyRegistry service is ready."); } catch (Exception e) { System.out.println("MyRegistry service failed: " + e); } } }