1 19 package org.objectweb.carol.cmi; 20 21 import java.io.ByteArrayInputStream ; 22 import java.io.ByteArrayOutputStream ; 23 import java.io.IOException ; 24 import java.rmi.Remote ; 25 import java.rmi.RemoteException ; 26 27 31 public class StubData { 32 private ClusterId id; 33 private byte[] serializedStub; 34 private Object stub; 35 private int factor; 36 private double loadIncr; 38 public StubData(ClusterId id, byte[] serStub, int factor) throws RemoteException { 39 if (factor < 1) { 40 throw new RemoteException ("bad load factor : " + factor); 41 } 42 this.id = id; 43 this.serializedStub = serStub; 44 this.stub = null; 45 this.factor = factor; 46 this.loadIncr = 1.0 / (double)factor; 47 } 48 49 public StubData(ClusterId id, Remote stub, int factor) throws RemoteException { 50 if (factor < 1) { 51 throw new RemoteException ("bad load factor : " + factor); 52 } 53 this.id = id; 54 this.serializedStub = null; 55 this.stub = stub; 56 this.factor = factor; 57 this.loadIncr = 1.0 / (double)factor; 58 } 59 60 StubData(Remote stub) { 61 this.id = null; 62 this.serializedStub = null; 63 this.stub = stub; 64 this.factor = Config.DEFAULT_RR_FACTOR; 65 this.loadIncr = 1.0 / (double)Config.DEFAULT_RR_FACTOR; 66 } 67 68 public ClusterId getId() { 69 return id; 70 } 71 72 public byte[] getSerializedStub() throws IOException { 73 if (serializedStub == null) { 74 ByteArrayOutputStream outStream = new ByteArrayOutputStream (); 75 CmiOutputStream out = new CmiOutputStream(outStream); 76 out.writeObject(stub); 77 out.flush(); 78 serializedStub = outStream.toByteArray(); 79 } 80 return serializedStub; 81 } 82 83 public Remote getStub() throws RemoteException { 84 Object s = getStubOrException(); 85 if (s instanceof Remote ) { 86 return (Remote )s; 87 } 88 throw (RemoteException )s; 89 } 90 91 public Object getStubOrException() { 92 if (stub == null) { 93 ByteArrayInputStream inStream = new ByteArrayInputStream (serializedStub); 94 try { 95 CmiInputStream in = new CmiInputStream(inStream); 96 stub = (Remote )in.readObject(); 97 } catch (IOException e) { 98 stub = new RemoteException (e.toString()); 99 } catch (ClassNotFoundException e) { 100 stub = new RemoteException (e.toString()); 101 } 102 } 103 return stub; 104 } 105 106 public double getLoadIncr() { 107 return loadIncr; 108 } 109 110 public int getFactor() { 111 return factor; 112 } 113 114 public String toString() { 115 String str = "[id:" + id + ",stub:"; 116 Object o = getStubOrException(); 117 if (o instanceof Remote ) { 118 return str + o.toString() + "]"; 119 } else { 120 return str + "serialized]"; 121 } 122 } 123 124 public int hashCode() { 125 if (id != null) { 126 return id.hashCode(); 127 } 128 return System.identityHashCode(this); 129 } 130 131 public boolean equals(Object obj) { 132 if (obj == this) return true; 133 if (id == null) return false; 134 if (!(obj instanceof StubData)) return false; 135 StubData sd = (StubData)obj; 136 return id.equals(sd.id); 137 } 138 } 139 | Popular Tags |