1 19 package org.objectweb.carol.cmi; 20 21 import java.io.IOException ; 22 import java.io.ObjectInput ; 23 import java.io.ObjectOutput ; 24 import java.lang.reflect.Constructor ; 25 import java.rmi.Remote ; 26 import java.rmi.RemoteException ; 27 import java.util.HashMap ; 28 import java.util.HashSet ; 29 import java.util.Iterator ; 30 31 36 public class ClusterStubData { 37 40 private ClusterConfig cfg; 41 42 45 private long randomSeed; 46 47 private boolean stubDebug = false; 48 49 private StubData firstSD; 50 51 private HashMap idMap; 53 private HashSet stubs; 54 private volatile ClusterStub clusterStub; 55 56 private WeakList lbList = new WeakList(); 57 58 59 62 private ClusterStubData() { 63 } 64 65 72 public ClusterStubData(ClusterId serverId, byte[] stubSer, int factor) 73 throws RemoteException { 74 StubData sd = new StubData(serverId, stubSer, factor); 75 firstSD = sd; 76 idMap = new HashMap (); 77 idMap.put(serverId, sd); 78 stubs = new HashSet (); 79 stubs.add(sd); 80 randomSeed = SecureRandom.getLong(); 81 stubDebug = Config.isStubDebug(); 82 } 83 84 89 ClusterStubData(ClusterRegistryInternal stub) throws RemoteException { 90 StubData sd = new StubData(stub); 91 firstSD = sd; 92 idMap = null; 93 stubs = new HashSet (); 94 stubs.add(sd); 95 randomSeed = SecureRandom.getLong(); 96 stubDebug = Config.isStubDebug(); 97 } 98 99 private static Class [] cnstrParams = new Class [] { ClusterStubData.class }; 100 101 public ClusterStub getClusterStub() throws RemoteException { 102 ClusterStub cs = clusterStub; 103 if (cs == null) { 104 Remote stub; 105 stub = firstSD.getStub(); 106 Class clusterStubClass; 107 try { 108 clusterStubClass = 109 ClusterObject.getClusterStubClass(stub.getClass()); 110 } catch (ClassNotFoundException e1) { 111 throw new RemoteException ("No valid cluster stub class for " + stub.getClass().getName()); 112 } 113 Constructor cnstr; 114 try { 115 cnstr = clusterStubClass.getConstructor(cnstrParams); 116 cs = (ClusterStub) cnstr.newInstance(new Object [] { this }); 117 } catch (Exception e) { 118 throw new RemoteException ("Can not instanciate cluster stub" + e.toString()); 119 } 120 clusterStub = cs; 121 } 122 return cs; 123 } 124 125 135 136 144 public boolean setStub(ClusterId serverId, byte[] stubSer, int factor) { 145 if (idMap == null) { 146 return false; 147 } 148 153 StubData sd; 154 try { 155 sd = new StubData(serverId, stubSer, factor); 156 } catch (RemoteException e) { 157 return false; 158 } 159 synchronized (this) { 160 idMap.put(serverId, sd); 161 stubs.add(sd); 162 } 163 return true; 164 } 165 166 173 public boolean setStub(ClusterRegistryInternal stub) { 174 if (idMap != null) { 175 return false; 176 } 177 StubData sd = new StubData(stub); 178 synchronized (this) { 179 stubs.add(sd); 180 } 181 return true; 182 } 183 184 189 public void write(ObjectOutput out) throws IOException { 190 out.writeObject(cfg); 191 synchronized (this) { 192 Iterator it = stubs.iterator(); 193 int l = stubs.size(); 194 if (idMap == null) { 195 out.writeInt(-l); 196 for (int i = 0; i < l; i++) { 197 StubData sd = (StubData) it.next(); 198 out.writeInt(sd.getFactor()); 199 Object o = sd.getStubOrException(); 200 if (o instanceof Remote ) { 201 out.writeObject(o); 202 } else { 203 out.writeObject(sd.getSerializedStub()); 204 } 205 } 206 } else { 207 out.writeInt(l); 208 for (int i = 0; i < l; i++) { 209 StubData sd = (StubData) it.next(); 210 sd.getId().write(out); 211 out.writeInt(sd.getFactor()); 212 Object o = sd.getStubOrException(); 213 if (o instanceof Remote ) { 214 out.writeObject(o); 215 } else { 216 out.writeObject(sd.getSerializedStub()); 217 } 218 } 219 } 220 } 221 out.writeLong(randomSeed); 222 out.writeBoolean(stubDebug); 223 } 224 225 231 public static ClusterStubData read(ObjectInput in, ClusterStub cs) 232 throws IOException , ClassNotFoundException { 233 ClusterStubData csd = new ClusterStubData(); 234 StubData first = null; 235 try { 236 csd.cfg = (ClusterConfig) in.readObject(); 237 int l = in.readInt(); 238 if (l == 0) { 239 throw new IOException ("invalid serialized stub : 0 stubs"); 240 } 241 HashMap idm = null; 242 HashSet stubs = new HashSet (); 243 if (l < 0) { 244 l = -l; 245 for (int i = 0; i < l; i++) { 246 int f = in.readInt(); 247 Object obj = in.readObject(); 248 System.err.println(obj.getClass().getName()); 249 StubData sd; 250 if (obj instanceof Remote ) { 251 sd = new StubData(null, (Remote ) obj, f); 252 } else { 253 sd = new StubData(null, (byte[]) obj, f); 254 } 255 stubs.add(sd); 256 if (first == null) { 257 first = sd; 258 } 259 } 260 } else { 261 idm = new HashMap (l); 262 for (int i = 0; i < l; i++) { 263 ClusterId id = ClusterId.read(in); 264 int f = in.readInt(); 265 266 Object obj = in.readObject(); 267 StubData sd; 268 if (obj instanceof Remote ) { 269 sd = new StubData(id, (Remote ) obj, f); 270 } else { 271 sd = new StubData(id, (byte[]) obj, f); 272 } 273 stubs.add(sd); 274 idm.put(id, sd); 275 if (first == null) { 276 first = sd; 277 } 278 } 279 } 280 csd.idMap = idm; 281 csd.stubs = stubs; 282 SecureRandom.setSeed( 283 csd.randomSeed = in.readLong() ^ System.currentTimeMillis()); 284 csd.stubDebug = in.readBoolean(); 285 } catch (ClassCastException ce) { 286 ce.printStackTrace(); 287 throw new IOException ("invalid serialized stub " + ce.toString()); 288 } 289 csd.clusterStub = cs; 290 csd.firstSD = first; 291 return csd; 292 } 293 294 303 306 public boolean removeStub(ClusterId serverId) { 307 StubData sd; 308 synchronized (this) { 309 sd = (StubData) idMap.remove(serverId); 310 if (sd == null) { 311 return true; 312 } 313 if (idMap.size() == 0) { 314 idMap.put(serverId, sd); 315 return false; 316 } 317 stubs.remove(sd); 318 removeFromLB(sd); 319 } 320 return true; 321 } 322 323 326 public boolean removeStubData(StubData sd) { 327 synchronized (this) { 328 if (!stubs.remove(sd)) { 329 return true; 330 } 331 if (stubs.size() == 0) { 332 stubs.add(sd); 333 return false; 334 } 335 idMap.remove(sd.getId()); 336 removeFromLB(sd); 337 } 338 return true; 339 } 340 341 private void removeFromLB(StubData sd) { 342 Iterator it = lbList.iterator(); 343 while (it.hasNext()) { 344 StubLB lb = (StubLB) it.next(); 345 lb.removeCallback(sd); 346 } 347 } 348 349 public void setClusterConfig(ClusterConfig cfg) { 350 this.cfg = cfg; 351 } 352 353 358 public ClusterConfig getClusterConfig() { 359 return cfg; 360 } 361 362 public StubLB getRoundRobin() { 363 StubLB lb; 364 synchronized (this) { 366 lb = new RoundRobin(this, stubs); 367 } 368 lbList.put(lb); 369 return lb; 370 } 371 372 public StubLB getRandom() { 373 StubLB lb; 374 synchronized (this) { 376 lb = new Random(this, stubs); 377 } 378 lbList.put(lb); 379 return lb; 380 } 381 382 public StubData getLocal() throws NoLocalStubException { 383 synchronized (this) { 384 Iterator it = stubs.iterator(); 385 while (it.hasNext()) { 386 StubData sd = (StubData)it.next(); 387 System.out.println(sd.getStubOrException()); 388 } 389 } 390 throw new NoLocalStubException(); 391 } 392 393 public boolean isStubDebug() { 394 return stubDebug; 395 } 396 397 public void debug(String mesg) { 398 System.out.println("ClusterStub: " + mesg); 399 } 400 401 public String toContentsString() { 402 synchronized (this) { 403 Iterator it = stubs.iterator(); 404 if (!it.hasNext()) { 405 return "[]"; 406 } 407 String str = "[ " + it.next().toString(); 408 while (it.hasNext()) { 409 str = str + ", " + it.next().toString(); 410 } 411 str = str + " ]"; 412 return str; 413 } 414 } 415 416 public String toString() { 417 return this.getClass().getName() + toContentsString(); 418 } 419 } 420 | Popular Tags |