1 7 8 package java.rmi.server; 9 10 import java.rmi.Remote ; 11 import java.rmi.NoSuchObjectException ; 12 import java.lang.reflect.Proxy ; 13 import sun.rmi.server.Util; 14 15 27 public abstract class RemoteObject implements Remote , java.io.Serializable { 28 29 30 transient protected RemoteRef ref; 31 32 33 private static final long serialVersionUID = -3215090123894869218L; 34 35 38 protected RemoteObject() { 39 ref = null; 40 } 41 42 47 protected RemoteObject(RemoteRef newref) { 48 ref = newref; 49 } 50 51 66 public RemoteRef getRef() { 67 return ref; 68 } 69 70 80 public static Remote toStub(Remote obj) throws NoSuchObjectException { 81 if (obj instanceof RemoteStub || 82 (obj != null && 83 Proxy.isProxyClass(obj.getClass()) && 84 Proxy.getInvocationHandler(obj) instanceof 85 RemoteObjectInvocationHandler )) 86 { 87 return obj; 88 } else { 89 return sun.rmi.transport.ObjectTable.getStub(obj); 90 } 91 } 92 93 100 public int hashCode() { 101 return (ref == null) ? super.hashCode() : ref.remoteHashCode(); 102 } 103 104 117 public boolean equals(Object obj) { 118 if (obj instanceof RemoteObject ) { 119 if (ref == null) { 120 return obj == this; 121 } else { 122 return ref.remoteEquals(((RemoteObject )obj).ref); 123 } 124 } else if (obj != null) { 125 131 return obj.equals(this); 132 } else { 133 return false; 134 } 135 } 136 137 140 public String toString() { 141 String classname = Util.getUnqualifiedName(getClass()); 142 return (ref == null) ? classname : 143 classname + "[" + ref.remoteToString() + "]"; 144 } 145 146 343 private void writeObject(java.io.ObjectOutputStream out) 344 throws java.io.IOException , java.lang.ClassNotFoundException 345 { 346 if (ref == null) { 347 throw new java.rmi.MarshalException ("Invalid remote object"); 348 } else { 349 String refClassName = ref.getRefClass(out); 350 if (refClassName == null || refClassName.length() == 0) { 351 355 out.writeUTF(""); 356 out.writeObject(ref); 357 } else { 358 362 out.writeUTF(refClassName); 363 ref.writeExternal(out); 364 } 365 } 366 } 367 368 404 private void readObject(java.io.ObjectInputStream in) 405 throws java.io.IOException , java.lang.ClassNotFoundException 406 { 407 String refClassName = in.readUTF(); 408 if (refClassName == null || refClassName.length() == 0) { 409 413 ref = (RemoteRef ) in.readObject(); 414 } else { 415 420 String internalRefClassName = 421 RemoteRef.packagePrefix + "." + refClassName; 422 Class refClass = Class.forName(internalRefClassName); 423 try { 424 ref = (RemoteRef ) refClass.newInstance(); 425 426 431 } catch (InstantiationException e) { 432 throw new ClassNotFoundException (internalRefClassName, e); 433 } catch (IllegalAccessException e) { 434 throw new ClassNotFoundException (internalRefClassName, e); 435 } catch (ClassCastException e) { 436 throw new ClassNotFoundException (internalRefClassName, e); 437 } 438 ref.readExternal(in); 439 } 440 } 441 } 442 | Popular Tags |