1 package org.sapia.ubik.rmi.examples.replication; 2 3 import org.sapia.ubik.net.Connection; 4 import org.sapia.ubik.net.ServerAddress; 5 import org.sapia.ubik.rmi.examples.Foo; 6 import org.sapia.ubik.rmi.replication.ReplicatedCommand; 7 import org.sapia.ubik.rmi.replication.ReplicatedInvoker; 8 import org.sapia.ubik.rmi.server.transport.Connections; 9 import org.sapia.ubik.rmi.server.transport.TransportManager; 10 11 import java.io.IOException ; 12 13 import java.lang.reflect.Method ; 14 15 import java.rmi.RemoteException ; 16 17 import java.util.Set ; 18 19 20 28 public class ReplicatedInvokerImpl implements ReplicatedInvoker { 29 private transient Foo _server; 30 private transient Set _siblings; 31 32 public ReplicatedInvokerImpl() { 33 } 34 35 38 public Object invoke(String methodName, Class [] sig, Object [] params) 39 throws Throwable { 40 System.out.println("Invoking: " + methodName); 41 42 Method m = _server.getClass().getMethod(methodName, sig); 43 44 return m.invoke(_server, params); 45 } 46 47 50 public Set getSiblings() { 51 return _siblings; 52 } 53 54 void setTargetInstance(Foo server) { 55 _server = server; 56 } 57 58 void setSiblings(Set siblings) { 59 _siblings = siblings; 60 } 61 62 65 public void dispatch(ReplicatedCommand cmd, ServerAddress addr) 66 throws RemoteException { 67 System.out.println("Dispatching replicated command"); 68 69 Connections conns = TransportManager.getConnectionsFor(addr); 70 Connection conn = conns.acquire(); 71 Object toReturn; 72 73 try { 74 conn.send(cmd); 75 toReturn = conn.receive(); 76 } catch (RemoteException e) { 77 conn.close(); 78 throw e; 79 } catch (IOException e) { 80 RemoteException re = new RemoteException ("Could not send replicated command", 81 e); 82 conn.close(); 83 throw re; 84 } catch (ClassNotFoundException e) { 85 RemoteException re = new RemoteException ("Could not receive response for replicated command", 86 e); 87 conn.close(); 88 throw re; 89 } 90 91 conns.release(conn); 92 93 if (toReturn instanceof Throwable ) { 94 if (toReturn instanceof RemoteException ) { 95 throw (RemoteException ) toReturn; 96 } 97 98 throw new RemoteException ("Exception caught dispatching command", 99 (Throwable ) toReturn); 100 } 101 } 102 } 103 | Popular Tags |