1 17 18 package org.objectweb.jac.core.dist; 19 20 import java.io.Serializable ; 21 import java.lang.reflect.Constructor ; 22 import java.util.Arrays ; 23 import org.apache.log4j.Logger; 24 import org.objectweb.jac.core.Collaboration; 25 import org.objectweb.jac.core.JacPropLoader; 26 import org.objectweb.jac.core.ObjectRepository; 27 import org.objectweb.jac.core.SerializedJacObject; 28 import org.objectweb.jac.util.WrappedThrowableException; 29 30 46 public class RemoteRef implements Serializable { 47 static Logger logger = Logger.getLogger("dist"); 48 static Logger loggerSerial = Logger.getLogger("serialization"); 49 50 51 protected RemoteContainer remCont; 52 53 54 protected int remIndex; 55 56 58 protected String name = null; 59 60 61 63 64 66 78 public static RemoteRef create(String name) { 79 80 RemoteRef remoteRef = null; 81 String remoteRefClassName = JacPropLoader.remoteRefClassName; 82 83 try { 84 Class remoteRefClass = Class.forName(remoteRefClassName); 85 remoteRef = (RemoteRef) remoteRefClass.newInstance(); 86 } catch (Exception e) { 87 logger.error("create "+name,e); 88 } 89 90 remoteRef.setName(name); 91 92 return remoteRef; 93 } 94 95 111 public static RemoteRef create(String name, 112 RemoteContainer remCont, 113 int remIndex) 114 { 115 logger.debug("creating remote ref " + name + ", " + 116 remCont + ", " + remIndex); 117 118 RemoteRef remoteRef = null; 119 String remoteRefClassName = JacPropLoader.remoteRefClassName; 120 131 try { 132 Class remoteRefClass = Class.forName(remoteRefClassName); 133 134 Constructor c = 135 remoteRefClass.getConstructor( 136 new Class [] { RemoteContainer.class, int.class } 137 ); 138 remoteRef = 139 (RemoteRef)c.newInstance( 140 new Object [] { remCont, new Integer (remIndex) }); 141 } catch(Exception e) { 142 logger.error("create "+name+","+remCont+","+remIndex,e); 143 } 144 145 remoteRef.setName(name); 146 logger.debug("returning remote ref " + remoteRef); 147 148 return remoteRef; 149 } 150 151 158 159 public static RemoteRef create(String name, Object localObject) { 160 logger.debug("creating remote ref "+name+" for "+localObject); 161 return RemoteRef.create( 162 name, RemoteContainer.resolve(Distd.getLocalContainerName()), 163 ObjectRepository.getMemoryObjectIndex(localObject)); 164 } 165 166 172 173 public RemoteRef(RemoteContainer remCont, int remIndex) { 174 this.remCont = remCont; 175 this.remIndex = remIndex; 176 } 177 178 179 185 186 public RemoteRef(String remCont, int remIndex) { 187 this.remCont = resolve(remCont); 188 this.remIndex = remIndex; 189 } 190 191 192 199 200 protected RemoteRef() {} 201 202 203 211 212 public RemoteContainer getRemCont() { return remCont; } 213 214 215 219 220 public void setName(String name) { 221 this.name = name; 222 } 223 224 228 229 public String getName() { 230 return name; 231 } 232 233 240 241 public int getRemIndex() { return remIndex; } 242 243 256 public RemoteContainer resolve(String contName) { return null; } 257 258 273 public RemoteContainer reresolve() { return null; } 274 275 276 277 278 final protected static String toAdaptProp = "org.objectweb.jac.toAdapt"; 279 280 281 289 290 public void remoteNew(String host, String clName) { 291 remoteNewWithCopy(host, clName, null, null, null); 292 } 293 294 303 304 public void remoteNew(String host, String clName, Object [] args) { 305 remoteNewWithCopy(host, clName, args, null, null); 306 } 307 308 320 321 public void remoteNewWithCopy(String host, String clName, Object src) { 322 remoteNewWithCopy(host, clName, null, src, null); 323 } 324 325 338 339 public void remoteNewWithCopy(String host, 340 String clName, 341 Object [] args, 342 Object src) { 343 remoteNewWithCopy(host, clName, args, src, null); 344 } 345 346 347 360 361 public void remoteNewWithCopy(String host, 362 String clName, 363 Object src, 364 String [] fieldsName) { 365 remoteNewWithCopy(host, clName, null, src, fieldsName); 366 } 367 368 369 383 384 public void remoteNewWithCopy(String host, 385 String clName, 386 Object [] args, 387 Object src, 388 String [] fieldsName) 389 { 390 395 396 remCont = resolve(host); 397 398 399 400 Object [] fieldsValue = null; 401 402 if (src!=null) { 403 if ( fieldsName == null ) { 404 Object [] state = ObjectState.getState(src); 405 fieldsName = (String [])state[0]; 406 fieldsValue = (Object [])state[1]; 407 } 408 else { 409 Object [] state = ObjectState.getState(src,fieldsName ); 410 fieldsName = (String [])state[0]; 411 fieldsValue = (Object [])state[1]; 412 } 413 } 414 415 if (fieldsName!=null) 416 loggerSerial.debug( 417 "serializing fields "+Arrays.asList(fieldsName)+ 418 " values = "+Arrays.asList(fieldsValue)); 419 420 byte[] sfieldsValue = SerializedJacObject.serialize(fieldsValue); 421 if (sfieldsValue!=null) 422 Distd.outputCount += sfieldsValue.length; 423 424 425 426 remIndex = 427 remCont.instantiates( 428 name, clName, args, fieldsName, 429 sfieldsValue, 430 SerializedJacObject.serialize(Collaboration.get()) 431 ); 432 433 } 434 435 443 public void remoteCopy(Object src) { 444 445 Object [] state = ObjectState.getState(src); 446 byte[] sstate = SerializedJacObject.serialize( (Object []) state[1] ); 447 448 if ( sstate != null ) Distd.outputCount += sstate.length; 449 450 451 remCont.copy( 452 name, remIndex, 453 (String []) state[0], 454 sstate, 455 SerializedJacObject.serialize(Collaboration.get()) 456 ); 457 } 458 459 460 469 public void remoteCopy(Object src, String [] fieldsName) { 470 471 Object [] state = ObjectState.getState(src,fieldsName ); 472 byte[] sstate = SerializedJacObject.serialize( (Object []) state[1] ); 473 474 if ( sstate != null ) Distd.outputCount += sstate.length; 475 476 477 478 remCont.copy( 479 name, 480 remIndex, 481 (String []) state[0], 482 sstate, 483 SerializedJacObject.serialize(Collaboration.get()) 484 ); 485 } 486 487 494 public Object invoke(String methodName, Object [] methodArgs) { 495 return invoke(methodName,methodArgs,null); 496 } 497 498 505 public Object invoke(String methodName, Object [] methodArgs, 506 Boolean [] refs) 507 { 508 logger.debug("invoking "+methodName+" on "+this); 509 510 byte[] ret = null; 511 byte[] args = SerializedJacObject.serializeArgs(methodArgs,refs); 512 513 if (args != null) 514 Distd.outputCount += args.length; 515 516 try { 518 ret = remCont.invoke( 519 remIndex, 520 methodName, 521 args, 522 SerializedJacObject.serialize(Collaboration.get()) 523 ); 524 } catch (Exception e) { 525 if (e instanceof WrappedThrowableException) { 526 throw (RuntimeException ) e; 527 } 528 logger.error("Failed to remotely invoke "+methodName+": "+e); 529 } 530 531 if ( ret != null ) Distd.inputCount += ret.length; 532 533 return SerializedJacObject.deserialize( ret ); 534 } 535 536 543 public Object invokeRoleMethod(String methodName,Object [] methodArgs) { 544 545 logger.debug("invoking role method "+methodName+" on "+ 546 this+"-"+remCont); 547 548 byte[] ret = null; 549 byte[] args = SerializedJacObject.serialize(methodArgs); 550 551 if (args != null) Distd.outputCount += args.length; 552 553 try { 554 ret = remCont.invokeRoleMethod( 555 remIndex, 556 methodName, 557 args, 558 SerializedJacObject.serialize(Collaboration.get()) 559 ); 560 } catch ( Exception e ) { 561 if ( e instanceof WrappedThrowableException ) { 562 throw (RuntimeException ) e; 563 } 564 logger.error("Failed to remotely invoke "+methodName+": "+e); 565 } 566 567 if ( ret != null ) Distd.inputCount += ret.length; 568 569 return SerializedJacObject.deserialize(ret); 570 } 571 572 577 public String toString() { 578 return ( "#" + getRemCont().getName() + "/" + 579 name + "[" + getRemIndex() + "]#" ); 580 } 581 582 588 public boolean equals(Object o) { 589 if ( ! (o instanceof RemoteRef) ) return false; 590 RemoteRef r = (RemoteRef) o; 591 return ( r.getRemIndex() == remIndex ) 592 && ( r.getRemCont().equals (remCont) ); 593 } 594 595 } 596 | Popular Tags |