1 17 18 package org.objectweb.jac.aspects.distribution.consistency; 19 20 import java.util.*; 21 import org.aopalliance.intercept.ConstructorInvocation; 22 import org.aopalliance.intercept.MethodInvocation; 23 import org.apache.log4j.Logger; 24 import org.objectweb.jac.core.*; 25 import org.objectweb.jac.core.dist.*; 26 import org.objectweb.jac.core.rtti.ClassRepository; 27 import org.objectweb.jac.util.Log; 28 29 40 41 public class ConsistencyWrapper extends Wrapper { 42 static Logger logger = Logger.getLogger("consistency"); 43 44 45 protected Vector knownReplicas = new Vector(); 46 47 48 Class type = null; 49 50 51 String [] readMethods = null; 52 53 54 String [] writeMethods = null; 55 56 57 String [] callMethods = null; 58 59 String hosts = null; 60 61 62 static protected String visitedReplicas = "visitedReplicas"; 63 64 static { 65 Collaboration.setGlobal(visitedReplicas); 66 } 67 68 69 public static String ALL_METHODS = "ALL"; 70 71 public static String ALL_MODIFIERS = "ALL_MODIFIERS"; 72 73 public static String ALL_GETTERS = "ALL_GETTERS"; 74 75 77 78 public ConsistencyWrapper(AspectComponent ac) { 79 super(ac); 80 this.knownReplicas = null; 81 } 82 83 90 91 public ConsistencyWrapper( 92 AspectComponent ac, 93 Class type, 94 String [] readMethods, 95 String [] writeMethods, 96 String [] callMethods, 97 String hosts) { 98 super(ac); 99 this.type = type; 100 setReadMethods(readMethods); 101 setWriteMethods(writeMethods); 102 setCallMethods(callMethods); 103 this.hosts = hosts; 104 } 105 106 108 109 public static void wrap( 110 Wrappee wrappee, 111 Class wrapperClass, 112 String [] readMethods, 113 String [] writeMethods, 114 String [] callMethods, 115 String hosts) { 116 ConsistencyWrapper wrapper = null; 117 try { 118 wrapper = 119 (ConsistencyWrapper) wrapperClass 120 .getConstructor(new Class [] { AspectComponent.class }) 121 .newInstance(new Object [] { null }); 122 } catch (Exception e) { 123 e.printStackTrace(); 124 return; 125 } 126 wrapper.type = wrappee.getClass(); 127 wrapper.setReadMethods(readMethods); 128 wrapper.setWriteMethods(writeMethods); 129 wrapper.setCallMethods(callMethods); 130 wrapper.hosts = hosts; 131 } 136 137 145 146 public void addMember(RemoteRef newReplica) { 147 148 String [][] wrapped_methods = 149 new String [][] { readMethods, writeMethods, callMethods }; 150 String [] wrapping_methods = 151 new String [] { "whenRead", "whenWrite", "whenCall" }; 152 Class wrapper_type = getClass(); 153 ConsistencyWrapper wrapper = null; 154 155 try { 156 157 wrapper = (ConsistencyWrapper) wrapper_type.newInstance(); 158 159 if (wrapper == null) { 160 throw new InstantiationException (); 161 } 162 163 wrapper.setReadMethods(readMethods); 164 wrapper.setWriteMethods(writeMethods); 165 wrapper.setCallMethods(callMethods); 166 167 } catch (Exception e) { 168 e.printStackTrace(); 169 } 170 171 wrapper.addKnownReplica(newReplica); 172 173 newReplica.invoke( 174 "wrap", 175 new Object [] { wrapper, wrapping_methods, wrapped_methods }); 176 177 whenBindingNewReplica(newReplica); 178 179 188 189 } 190 191 196 197 public void invalidateTopology() { 198 knownReplicas = null; 199 } 200 201 204 205 void calculateKnownReplicas(Wrappee wrappee) { 206 logger.debug("calculing known replicas for " + wrappee); 207 knownReplicas = Topology.getPartialTopology(hosts).getReplicas(wrappee); 208 logger.debug("result for known replicas="+ knownReplicas 209 + "(on topology " + Topology.getPartialTopology(hosts) + ")"); 210 } 211 212 221 222 public void whenBindingNewReplica(RemoteRef newReplica) { 223 } 224 225 235 236 public void whenNewReplicaBounded(RemoteRef remoteReplica) { 237 } 238 239 245 246 public String getVisitedReplicas() { 247 return visitedReplicas; 248 } 249 250 255 256 public String toString() { 257 if (knownReplicas == null) 258 return "Consistency wrapper, no known replicas"; 259 else 260 return "Consistency wrapper, known replicas = " + knownReplicas; 261 } 262 263 268 269 public void setReadMethods(String [] readMethods) { 270 this.readMethods = expandMethods(readMethods); 271 } 272 273 279 280 public void setWriteMethods(String [] writeMethods) { 281 this.writeMethods = expandMethods(writeMethods); 282 } 283 284 290 291 public void setCallMethods(String [] callMethods) { 292 this.callMethods = expandMethods(callMethods); 293 } 294 295 308 309 public Vector getKnownReplicas() { 310 return knownReplicas; 311 } 312 313 318 public void addKnownReplica(RemoteRef newReplica) { 319 knownReplicas.add(newReplica); 320 } 321 322 326 327 public Class getConsistencyWrapperType() { 328 return getClass(); 329 } 330 331 344 345 public void setKnownReplicas(Vector knownReplicas) { 346 this.knownReplicas = knownReplicas; 347 } 348 349 361 362 public Object whenCall(Interaction interaction) { 363 return proceed(interaction); 364 } 365 366 375 376 public Object whenWrite(Interaction interaction) { 377 return proceed(interaction); 378 } 379 380 389 390 public Object whenRead(Interaction interaction) { 391 return proceed(interaction); 392 } 393 394 408 409 public Object acceptRemoteCall(RemoteRef remoteReplica, Object [] data) { 410 return null; 411 } 412 413 427 428 public Object acceptRemoteWrite( 429 Wrappee wrappee, 430 RemoteRef remoteReplica, 431 Object [] data) { 432 return null; 433 } 434 435 449 450 public Object acceptRemoteRead( 451 Wrappee wrappee, 452 RemoteRef remoteReplica, 453 Object [] data) { 454 return null; 455 } 456 457 467 protected String [] expandMethods(String [] methods) { 468 if (methods == null) 469 return null; 470 Vector newVM = new Vector(); 471 for (int i = 0; i < methods.length; i++) { 472 if (methods[i].equals(ALL_METHODS)) { 473 newVM.addAll( 474 Arrays.asList(ClassRepository.getMethodsName(type))); 475 } else if (methods[i].equals(ALL_MODIFIERS)) { 476 newVM.addAll( 477 Arrays.asList(ClassRepository.getModifiersNames(type))); 478 } else if (methods[i].equals(ALL_GETTERS)) { 479 newVM.addAll( 480 Arrays.asList(ClassRepository.getGettersNames(type))); 481 } else { 482 newVM.add(methods[i]); 483 } 484 } 485 String [] newMethods = new String [newVM.size()]; 486 for (int i = 0; i < newMethods.length; i++) { 487 newMethods[i] = (String ) newVM.get(i); 488 } 489 return newMethods; 490 } 491 492 495 public Object invoke(MethodInvocation invocation) throws Throwable { 496 return null; 498 } 499 500 503 public Object construct(ConstructorInvocation invocation) 504 throws Throwable { 505 return null; 507 } 508 509 } 510 | Popular Tags |