1 package org.sapia.ubik.rmi.server; 2 3 import java.io.IOException ; 4 import java.io.ObjectInput ; 5 import java.io.ObjectOutput ; 6 import java.lang.reflect.Method ; 7 import java.lang.reflect.Proxy ; 8 9 import javax.naming.NamingException ; 10 11 import org.sapia.ubik.net.ServerAddress; 12 import org.sapia.ubik.rmi.naming.ServiceLocator; 13 14 15 25 public class RemoteRefReliable extends RemoteRefEx implements HealthCheck { 26 static final long serialVersionUID = 1L; 27 protected String _url; 28 29 32 public RemoteRefReliable() { 33 super(); 34 } 35 36 44 public RemoteRefReliable(OID oid, ServerAddress serverAddress) { 45 super(oid, serverAddress); 46 } 47 48 53 public void setUp(String url) { 54 _url = url; 55 } 56 57 60 public Object invoke(Object obj, Method toCall, Object [] params) 61 throws Throwable { 62 try { 63 return super.invoke(obj, toCall, params); 64 } catch (java.rmi.RemoteException e) { 65 if (_url != null) { 66 return doFailOver(obj, toCall, params, e); 67 } else { 68 throw e; 69 } 70 } 71 } 72 73 76 public void readExternal(ObjectInput in) 77 throws IOException , ClassNotFoundException { 78 super.readExternal(in); 79 _url = (String ) in.readObject(); 80 } 81 82 85 public void writeExternal(ObjectOutput out) throws IOException { 86 super.writeExternal(out); 87 out.writeObject(_url); 88 } 89 90 93 protected Object onShutdown(Object proxy, Method toCall, Object [] params) 94 throws Throwable { 95 return doFailOver(proxy, toCall, params, new ShutdownException()); 96 } 97 98 101 protected synchronized Object doFailOver(Object obj, Method toCall, 102 Object [] params, Throwable err) throws Throwable { 103 try { 104 Object remote = ServiceLocator.lookup(_url); 105 106 if (remote instanceof Stub && Proxy.isProxyClass(remote.getClass())) { 107 try { 108 synchronized (_lock) { 111 ServerAddress newAddr = ((RemoteRef) Proxy.getInvocationHandler(remote)).getServerAddress(); 112 _oid = ((RemoteRef) Proxy.getInvocationHandler(remote)).getOid(); 113 114 if (!newAddr.equals(_serverAddress)) { 115 _serverAddress = newAddr; 116 initPool(true); 117 } else { 118 _pool.clear(); 119 } 120 } 121 122 Hub.clientRuntime.gc.register(_serverAddress, _oid, this); 123 } catch (ClassCastException e) { 124 throw err; 125 } 126 127 return super.invoke(obj, toCall, params); 128 } else { 129 throw err; 130 } 131 } catch (NamingException e) { 132 throw err; 133 } 134 } 135 } 136 | Popular Tags |