1 package org.sapia.ubik.rmi.replication; 2 3 import org.sapia.ubik.net.ServerAddress; 4 import org.sapia.ubik.rmi.server.Hub; 5 import org.sapia.ubik.rmi.server.invocation.InvokeCommand; 6 7 import java.io.IOException ; 8 import java.io.ObjectInput ; 9 import java.io.ObjectOutput ; 10 11 import java.rmi.RemoteException ; 12 13 import java.util.HashSet ; 14 import java.util.Set ; 15 16 17 30 public abstract class ReplicatedCommand extends InvokeCommand { 31 private Set _visited = new HashSet (); 32 private Set _targets; 33 private ReplicatedInvoker _invoker; 34 private boolean _executed; 35 private boolean _synchronous; 36 private boolean _disabled; 37 38 39 public ReplicatedCommand() { 40 } 41 42 49 public ReplicatedCommand(InvokeCommand cmd, Set targets, 50 ReplicatedInvoker invoker, boolean synchronous) { 51 super(cmd.getOID(), cmd.getMethodName(), cmd.getParams(), 52 cmd.getParameterTypes(), null); 53 _targets = targets; 54 _invoker = invoker; 55 _synchronous = synchronous; 56 } 57 58 72 public Object execute() throws Throwable { 73 if (_disabled) { 74 return super.execute(); 75 } 76 77 Object toReturn; 78 Hub.serverRuntime.dispatchEvent(new ReplicationEvent(this)); 79 80 Set siblings = _invoker.getSiblings(); 81 ReplicationStrategy strat = new ReplicationStrategy(_visited, _targets, 82 siblings); 83 ServerAddress addr; 84 ServerAddress current = getServerAddress(); 85 86 if (_executed) { 87 convertParams(_invoker.getClass().getClassLoader()); 88 toReturn = _invoker.invoke(super.getMethodName(), 89 super.getParameterTypes(), super.getParams()); 90 } else { 91 if (_targets != null) { 92 if (_targets.contains(current)) { 93 toReturn = super.execute(); 94 _executed = true; 95 _targets.remove(current); 96 } else { 97 _executed = true; 98 toReturn = send(strat.selectNextSibling()); 99 100 return toReturn; 101 } 102 } else { 103 toReturn = super.execute(); 104 _executed = true; 105 } 106 107 if ((addr = strat.selectNextSibling()) != null) { 108 if (!_disabled) { 109 send(addr); 110 } 111 } 112 } 113 114 return toReturn; 115 } 116 117 120 public ReplicatedInvoker getReplicatedInvoker() { 121 return _invoker; 122 } 123 124 128 public void disable() { 129 _disabled = true; 130 } 131 132 135 public void readExternal(ObjectInput in) 136 throws IOException , ClassNotFoundException { 137 super.readExternal(in); 138 _visited = (Set ) in.readObject(); 139 _targets = (Set ) in.readObject(); 140 _invoker = (ReplicatedInvoker) in.readObject(); 141 _executed = in.readBoolean(); 142 _synchronous = in.readBoolean(); 143 _disabled = in.readBoolean(); 144 } 145 146 149 public void writeExternal(ObjectOutput out) throws IOException { 150 super.writeExternal(out); 151 out.writeObject(_visited); 152 out.writeObject(_targets); 153 out.writeObject(_invoker); 154 out.writeBoolean(_executed); 155 out.writeBoolean(_synchronous); 156 out.writeBoolean(_disabled); 157 } 158 159 167 protected Object send(ServerAddress next) throws RemoteException { 168 SendHelper helper = new SendHelper(this, next, _synchronous); 169 170 if (_synchronous) { 171 try { 172 return helper.send(); 173 } catch (Throwable t) { 174 if (t instanceof RemoteException ) { 175 throw (RemoteException ) t; 176 } 177 178 throw new RemoteException ("Exception caught replicating command", t); 179 } 180 } else { 181 try { 182 helper.send(); 183 } catch (Throwable t) { 184 if (t instanceof RemoteException ) { 185 throw (RemoteException ) t; 186 } 187 188 throw new RemoteException ("Exception caught replicating command", t); 189 } 190 191 return null; 192 } 193 } 194 } 195 | Popular Tags |