| 1 package org.sapia.ubik.rmi.server; 2 3 import java.io.Externalizable ; 4 import java.io.IOException ; 5 import java.io.ObjectInput ; 6 import java.io.ObjectOutput ; 7 import java.lang.reflect.Method ; 8 import java.rmi.RemoteException ; 9 import java.util.HashSet ; 10 import java.util.Iterator ; 11 import java.util.Set ; 12 13 import org.sapia.ubik.net.Connection; 14 import org.sapia.ubik.net.ServerAddress; 15 import org.sapia.ubik.rmi.server.transport.Connections; 16 import org.sapia.ubik.rmi.server.transport.TransportManager; 17 18 19 30 public abstract class RemoteRef implements StubInvocationHandler, 31 Externalizable , HealthCheck { 32 static final long serialVersionUID = 1L; 33 protected boolean _callBack; 34 protected OID _oid; 35 protected VmId _vmId = VmId.getInstance(); 36 protected ServerAddress _serverAddress; 37 protected boolean _isFirstVoyage = true; 38 protected transient Connections _pool; 39 protected transient Object _lock = new Object (); 40 41 public RemoteRef() { 42 } 43 44 52 public RemoteRef(OID oid, ServerAddress serverAddress) { 53 this(); 54 _oid = oid; 55 _serverAddress = serverAddress; 56 } 57 58 63 public boolean isCallBack() { 64 return _callBack; 65 } 66 67 73 public OID getOID(){ 74 return _oid; 75 } 76 77 83 protected void setCallBack(boolean callBack) { 84 _callBack = callBack; 85 } 86 87 90 public abstract Object invoke(Object obj, Method toCall, Object [] params) 91 throws Throwable ; 92 93 98 public ServerAddress getServerAddress() { 99 return _serverAddress; 100 } 101 102 107 public OID getOid() { 108 return _oid; 109 } 110 111 117 public boolean isValid() { 118 try { 119 return ((Boolean ) sendCommand(new CommandPing())).booleanValue(); 120 } catch (Throwable t) { 121 return false; 122 } 123 } 124 125 128 public StubContainer toStubContainer(Object proxy) { 129 Set interfaces = new HashSet (); 130 ServerTable.appendInterfaces(proxy.getClass(), interfaces); 131 132 String [] names = new String [interfaces.size()]; 133 int count = 0; 134 135 for (Iterator iter = interfaces.iterator(); iter.hasNext();) { 136 names[count++] = ((Class ) iter.next()).getName(); 137 } 138 139 return new StubContainerBase(names, this); 140 } 141 142 145 public void readExternal(ObjectInput in) 146 throws IOException , ClassNotFoundException { 147 _callBack = in.readBoolean(); 148 _oid = (OID) in.readObject(); 149 _vmId = (VmId) in.readObject(); 150 _serverAddress = (ServerAddress) in.readObject(); 151 _isFirstVoyage = in.readBoolean(); 152 _lock = new Object (); 153 Hub.clientRuntime.gc.register(_serverAddress, _oid, this); 154 155 if (_isFirstVoyage) { 156 _isFirstVoyage = false; 157 } else { 158 Hub.createReference(_serverAddress, _oid); 159 } 160 } 161 162 165 public void writeExternal(ObjectOutput out) throws IOException { 166 out.writeBoolean(_callBack); 167 out.writeObject(_oid); 168 out.writeObject(_vmId); 169 out.writeObject(_serverAddress); 170 out.writeBoolean(_isFirstVoyage); 171 } 172 173 protected Object sendCommand(RMICommand cmd) throws Throwable { 174 synchronized (_lock) { 175 if (_pool == null) { 176 initPool(false); 177 } 178 } 179 180 Connection conn = _pool.acquire(); 181 182 try { 183 try { 184 conn.send(cmd); 185 } catch (RemoteException e) { 186 synchronized (_lock) { 187 _pool.clear(); 188 } 189 190 conn = _pool.acquire(); 191 conn.send(cmd); 192 } 193 194 Object toReturn = conn.receive(); 195 196 if (toReturn == null) { 197 return toReturn; 198 } else if (toReturn instanceof Throwable ) { 199 Throwable err = (Throwable ) toReturn; 200 err.fillInStackTrace(); 201 throw err; 202 } 203 204 return toReturn; 205 } finally { 206 _pool.release(conn); 207 } 208 } 209 210 protected synchronized void initPool(boolean force) 211 throws java.rmi.RemoteException { 212 if (_pool == null) { 213 _pool = TransportManager.getConnectionsFor(_serverAddress); 214 } else if (force) { 215 _pool.clear(); 216 _pool = TransportManager.getConnectionsFor(_serverAddress); 217 } 218 } 219 } 220 | Popular Tags |