1 17 18 package org.objectweb.jac.core.dist; 19 20 import java.io.Serializable ; 21 import java.lang.reflect.Constructor ; 22 import java.lang.reflect.InvocationTargetException ; 23 import java.lang.reflect.Method ; 24 import java.util.Arrays ; 25 import org.apache.log4j.Logger; 26 import org.objectweb.jac.core.ACManager; 27 import org.objectweb.jac.core.Collaboration; 28 import org.objectweb.jac.core.JacLoader; 29 import org.objectweb.jac.core.JacPropLoader; 30 import org.objectweb.jac.core.NameRepository; 31 import org.objectweb.jac.core.ObjectRepository; 32 import org.objectweb.jac.core.SerializedJacObject; 33 import org.objectweb.jac.core.Wrappee; 34 import org.objectweb.jac.core.Wrapping; 35 import org.objectweb.jac.core.rtti.ClassRepository; 36 import org.objectweb.jac.util.ExtArrays; 37 import org.objectweb.jac.util.WrappedThrowableException; 38 39 50 51 public class RemoteContainer implements Serializable { 52 static Logger logger = Logger.getLogger("dist"); 53 static Logger loggerSerial = Logger.getLogger("serialization"); 54 55 57 public static boolean verbose = false; 58 59 60 public String name = "theContainerWithNoName"; 61 62 63 65 66 68 73 74 public static RemoteContainer bindNewContainer(String name) { 75 76 logger.debug("binding to a new container "+name); 77 RemoteContainer result = null; 78 result=Topology.get().getContainer(name); 80 if (result != null) { 81 logger.debug("container already bound"); 82 return result; 83 } else { 84 result = RemoteContainer.resolve(name); 85 if (result == null) { 86 throw new RuntimeException ("container "+name+" does not exist"); 87 } 88 } 89 90 RemoteRef remoteTopology = result.bindTo("JAC_topology"); 92 93 RemoteContainer[] rcs = Topology.get().getContainers(); 95 remoteTopology.invoke("addContainers", new Object [] {rcs}); 96 97 rcs = (RemoteContainer[])remoteTopology.invoke("getContainers",ExtArrays.emptyObjectArray); 99 Topology.get().addContainers(rcs); 100 return result; 101 } 102 103 108 public RemoteContainer(boolean verbose) { 109 RemoteContainer.verbose = verbose; 110 } 111 112 118 public RemoteContainer(String className, boolean verbose) { 119 try { 120 Class.forName(className).newInstance(); 121 } catch (Exception e) { 122 logger.error("RemoteContainer "+className,e); 123 } 124 RemoteContainer.verbose = verbose; 125 } 126 127 134 public RemoteContainer() {} 135 136 141 public String getName() { 142 return name; 143 } 144 145 146 159 160 public void setName(String name) { 161 this.name = Distd.getFullHostName(name); 162 } 163 164 172 public static boolean isLocal(String name) { 173 RemoteContainer rc = RemoteContainer.resolve(name); 174 if (rc==null) 175 return false; 176 return rc.isLocal(); 177 } 178 179 191 public static RemoteContainer resolve(String name) 192 { 193 logger.debug("resolving remote container "+name); 194 195 RemoteContainer remoteContainer = null; 196 197 198 199 String namingClassName = JacPropLoader.namingClassName; 200 207 if (namingClassName == null) { 208 namingClassName = JacPropLoader.namingClassDefaultName; 209 } 210 211 212 213 try { 214 Class namingClass = Class.forName(namingClassName); 215 216 Method meth = 217 namingClass.getMethod( "resolve", new Class []{String .class} ); 218 219 remoteContainer = 220 (RemoteContainer) meth.invoke(null, new Object []{name}); 221 222 } catch (Exception e) { 223 logger.error("resolve "+name,e); 224 throw new RuntimeException (e.toString()); 225 } 226 227 logger.debug("resolve is returning " + remoteContainer); 228 229 return remoteContainer; 230 } 231 232 233 242 243 public boolean isLocal() { 244 return Distd.containsContainer( this ); 245 } 246 247 269 public int instantiates(String name, String className, Object [] args, 270 String [] fields, byte[] state, byte[] collaboration) 271 { 272 273 Collaboration.set( 274 (Collaboration)SerializedJacObject.deserialize(collaboration)); 275 276 logger.debug("remote instantiation of "+className); 277 278 Object substance = null; 279 280 try { 281 282 283 284 Class substanceClass = null; 285 286 try { 287 288 291 substanceClass = Class.forName(className); 292 293 if (args == null) { 294 Constructor c = substanceClass.getConstructor ( 295 new Class [] {}); 296 substance = c.newInstance(new Object [] {}); 297 } else { 298 throw new InstantiationException ( 299 "No arguments allowed when remote instantiation"); 300 } 301 302 303 306 } catch(Exception e) { 307 logger.error("Instantiates "+name+" "+className+Arrays.asList(args),e); 310 return -1; 311 } 312 313 314 315 if (fields != null && state != null) { 316 Object [] dstate = (Object []) 317 SerializedJacObject.deserialize(state); 318 319 ObjectState.setState( 320 substance,new Object [] { fields, dstate } 321 ); 322 loggerSerial.debug("deserialized fields="+ 323 Arrays.asList(fields)+"; state="+Arrays.asList(dstate)); 324 } else { 325 loggerSerial.debug("deserialize: transmitted state is empty"); 326 } 327 328 if (verbose) { 329 System.out.println( 330 "--- A " + className + " instance has been created (" + 331 ObjectRepository.getMemoryObjectIndex(substance) + ") ---" 332 ); 333 } 334 335 } catch (Exception e) { 336 logger.error("Instantiates "+name+" "+className+Arrays.asList(args),e); 337 return -1; 338 } 339 340 341 ((ACManager) ACManager.get()).whenRemoteInstantiation( (Wrappee) substance, name ); 342 343 return ObjectRepository.getMemoryObjectIndex(substance); 344 } 345 346 353 354 public void copy(String name, int index, String [] fields, byte[] state, 355 byte[] collaboration) { 356 357 358 Collaboration.set((Collaboration)SerializedJacObject.deserialize(collaboration)); 359 ObjectState.setState(ObjectRepository.getMemoryObject(index), new Object [] { 360 fields, (Object []) SerializedJacObject.deserialize(state) } ); 361 362 363 } 364 365 366 377 378 public byte[] invoke(int index, String methodName, byte[] args, 379 byte[] collaboration) { 380 381 if (args != null) 382 Distd.inputCount += args.length; 383 384 Object [] methodArgs = 385 (Object [])SerializedJacObject.deserializeArgs(args); 386 387 Object ret = null; 388 389 try { 390 Object substance = ObjectRepository.getMemoryObject(index); 391 Class substanceClass = substance.getClass(); 392 393 394 Collaboration.set((Collaboration)SerializedJacObject. 395 deserialize(collaboration)); 396 397 logger.debug("remote invocation of " + methodName + " on "+ 398 substance + "("+ 399 NameRepository.get().getName(substance)+")"); 400 401 ret = ClassRepository.invokeDirect(substanceClass, methodName, 402 substance, methodArgs); 403 404 } catch (InvocationTargetException e) { 405 if (e.getTargetException() instanceof WrappedThrowableException) { 406 throw (RuntimeException )e.getTargetException(); 407 } else { 408 logger.error("invoke "+index+"."+methodName+ 409 Arrays.asList(methodArgs),e); 410 } 411 } catch (Exception e) { 412 logger.error("invoke "+index+"."+methodName+ 413 Arrays.asList(methodArgs),e); 414 } 415 423 424 byte[] sret = SerializedJacObject.serialize(ret); 425 426 if (sret != null) 427 Distd.outputCount += sret.length; 428 logger.debug("remote invocation ok, returning "+ret); 429 return sret; 430 } 431 432 public byte[] invokeRoleMethod(int index, String methodName, byte[] args, 433 byte[] collaboration) { 434 435 if (args != null) 436 Distd.inputCount += args.length; 437 438 Object [] methodArgs = 439 (Object []) 440 SerializedJacObject.deserialize(args); 441 442 Object ret = null; 443 444 try { 445 446 Object substance = ObjectRepository.getMemoryObject(index); 447 Class substanceClass = substance.getClass(); 448 449 450 451 logger.debug(Collaboration.get().toString()); 452 453 Collaboration.set((Collaboration)SerializedJacObject 454 .deserialize(collaboration)); 455 456 457 logger.debug("remote invocation of role method " + methodName + " on "+ 458 substance + "("+ 459 NameRepository.get().getName(substance)+")"+ 460 " - "+this); 461 logger.debug(Collaboration.get().toString()); 462 463 ret=Wrapping.invokeRoleMethod((Wrappee)substance,methodName,methodArgs); 464 465 logger.debug(Collaboration.get().toString()); 466 } catch(Exception e) { 467 if (e instanceof WrappedThrowableException) { 468 throw (RuntimeException )e; 469 } else { 470 logger.error("invokeRoleMethod "+index+"."+methodName+ 471 Arrays.asList(methodArgs),e); 472 } 473 } 474 475 byte[] sret = SerializedJacObject.serialize(ret); 476 477 if (sret != null) 478 Distd.outputCount += sret.length; 479 480 logger.debug("remote invocation ok, returning "+ret); 481 482 return sret; 483 } 484 485 486 497 498 public byte[] getByteCodeFor(String className) { 499 ClassLoader cl = getClass().getClassLoader(); 500 if (cl instanceof JacLoader) { 501 logger.debug("getByteCodeFor "+className+" bootstraping"); 502 503 try { 504 if (Wrappee.class.isAssignableFrom(Class.forName(className))) { 505 return null; 507 } 508 } catch ( Exception e ) { 509 logger.error("Failed to fetch bytecode for "+className,e); 510 } 511 } else if (cl instanceof DistdClassLoader) { 512 logger.debug("getByteCodeFor "+className+" distd"); 513 ((DistdClassLoader)cl).getByteCode(className); 514 } 515 logger.debug("getByteCodeFor "+className+" -> null"); 516 return null; 517 } 518 519 530 531 public RemoteRef bindTo(String name) { 532 Class nrc = null; 533 Method m = null; 534 Object nr = null; 535 Object o = null; 536 537 logger.debug("client is binding to "+name); 538 539 o = NameRepository.get().getObject(name); 540 if (o == null) { 541 logger.debug("object "+name+" not found on this container"); 542 return null; 543 } 544 545 RemoteContainer rc = RemoteContainer.resolve(this.name); 546 int index = ObjectRepository.getMemoryObjectIndex(o); 547 548 RemoteRef rr = RemoteRef.create(name, rc, index); 549 550 logger.debug("returning remote reference "+rr); 551 552 return rr; 553 } 554 555 561 562 public boolean equals(Object container) { 563 if (!(container instanceof RemoteContainer)) 564 return false; 565 RemoteContainer c = (RemoteContainer)container; 566 567 return name.equals(c.getName()); 568 } 569 570 574 575 public String toString() { 576 return "Container " + getName(); 577 } 578 579 582 583 public void launchRemoteGUI() { 584 RemoteContainer rc = RemoteContainer.resolve( name ); 585 logger.debug("launch remote GUI: container = " + rc ); 586 RemoteRef remoteTopology = rc.bindTo( "JAC_topology" ); 587 logger.debug("remote topology = " + remoteTopology ); 588 589 598 remoteTopology.invoke( "launchGUI", ExtArrays.emptyObjectArray ); 599 } 600 601 } 602 | Popular Tags |