1 22 package org.jboss.net.sockets; 23 24 import java.rmi.server.UnicastRemoteObject ; 25 import java.rmi.Remote ; 26 import java.lang.reflect.Proxy ; 27 import java.rmi.server.RMIClientSocketFactory ; 28 import java.rmi.server.RMIServerSocketFactory ; 29 import java.rmi.RemoteException ; 30 import java.lang.reflect.Method ; 31 import java.util.HashMap ; 32 import java.rmi.NoSuchObjectException ; 33 38 public class RMIMultiSocketServer 39 { 40 private static HashMap handlermap = new HashMap (); 41 private static HashMap stubmap = new HashMap (); 42 43 public static Remote exportObject(Remote obj, 44 int port, 45 RMIClientSocketFactory csf, 46 RMIServerSocketFactory ssf, 47 Class [] interfaces, 48 int numSockets) 49 throws RemoteException 50 { 51 Remote [] stubs = new Remote [numSockets]; 52 53 Method [] methods = obj.getClass().getMethods(); 54 55 HashMap invokerMap = new HashMap (); 56 for (int i = 0; i < methods.length; i++) { 57 Long methodkey = new Long (MethodHash.calculateHash(methods[i])); 58 invokerMap.put(methodkey, methods[i]); 59 } 60 61 RMIMultiSocketHandler[] handlers = new RMIMultiSocketHandler[numSockets]; 62 for (int i = 0; i < numSockets; i++) 63 { 64 int theport = (port == 0) ? 0 : port + i; 65 handlers[i] = new RMIMultiSocketHandler(obj, invokerMap); 66 stubs[i] = UnicastRemoteObject.exportObject(handlers[i], theport, csf, ssf); 67 } 68 69 Remote remote = (Remote )Proxy.newProxyInstance( 70 obj.getClass().getClassLoader(), 71 interfaces, 72 new RMIMultiSocketClient(stubs)); 73 stubmap.put(remote, stubs); 74 handlermap.put(remote, handlers); 75 return remote; 76 } 77 78 public static Remote exportObject(Remote obj, 79 int port, 80 RMIClientSocketFactory csf, 81 RMIServerSocketFactory ssf, 82 int numSockets) 83 throws RemoteException 84 { 85 return exportObject(obj, port, csf, ssf, obj.getClass().getInterfaces(), numSockets); 86 } 87 88 public static boolean unexportObject(Remote obj, boolean force) 89 throws NoSuchObjectException 90 { 91 handlermap.remove(obj); 92 Remote [] stubs = (Remote [])stubmap.remove(obj); 93 for (int i = 0; i < stubs.length; i++) 94 { 95 UnicastRemoteObject.unexportObject(stubs[i], force); 96 } 97 98 return true; 99 } 100 } 101 | Popular Tags |