home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / rmi / server / RemoteObject.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  5.0 KB  |  167 lines

  1. /*
  2.  * @(#)RemoteObject.java    1.13 98/03/18
  3.  *
  4.  * Copyright 1996-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.rmi.server;
  16.  
  17. import java.rmi.Remote;
  18. import java.rmi.UnmarshalException;
  19. import java.rmi.NoSuchObjectException;
  20.  
  21. /**
  22.  * The RemoteObject class implements the java.lang.Object behavior for
  23.  * remote objects.  RemoteObject provides the remote semantics of
  24.  * Object by implementing methods for hashCode, equals, and toString.
  25.  */
  26. public abstract class RemoteObject implements Remote, java.io.Serializable {
  27.  
  28.     transient protected RemoteRef ref;
  29.     
  30.     /* indicate compatibility with JDK 1.1.x version of class */
  31.     private static final long serialVersionUID = -3215090123894869218L;
  32.  
  33.     /**
  34.      * Create a remote object.
  35.      */
  36.     protected RemoteObject() {
  37.     ref = null;
  38.     }
  39.     
  40.     /**
  41.      * Create a remote object, initialized with the specified remote reference.
  42.      */
  43.     protected RemoteObject(RemoteRef newref) {
  44.     ref = newref;
  45.     }
  46.  
  47.     /**
  48.      * Returns the remote reference for the remote object.
  49.      */
  50.     public RemoteRef getRef() 
  51.     {
  52.     return ref;
  53.     }
  54.     
  55.     /**
  56.      * Returns the stub for the remote object <code>obj</code> passed
  57.      * as a parameter. This operation is only valid <i>after</i>
  58.      * the object has been exported.
  59.  
  60.      * @return the stub for the remote object, <code>obj</code>.
  61.      * @exception NoSuchObjectException if the stub for the
  62.      * remote object could not be found.
  63.      */
  64.     public static Remote toStub(Remote obj) throws NoSuchObjectException
  65.     {
  66.     if (obj instanceof RemoteStub) {
  67.         return (RemoteStub)obj; 
  68.     } else {
  69.         return sun.rmi.transport.ObjectTable.getStub(obj);
  70.     }
  71.     }
  72.  
  73.     /**
  74.      * Returns a hashcode for a remote object.  Two remote object stubs
  75.      * that refer to the same remote object will have the same hash code
  76.      * (in order to support remote objects as keys in hash tables).
  77.      *
  78.      * @see        java.util.Hashtable
  79.      */
  80.     public int hashCode() {
  81.     return (ref == null) ? super.hashCode() : ref.remoteHashCode();
  82.     }
  83.  
  84.     /**
  85.      * Compares two remote objects for equality.
  86.      * Returns a boolean that indicates whether this remote object is
  87.      * equivalent to the specified Object. This method is used when a
  88.      * remote object is stored in a hashtable.
  89.      * If the specified Object is not itself an instance of RemoteObject,
  90.      * then this method delegates by returning the result of invoking the
  91.      * <code>equals</code> method of its parameter with this remote object
  92.      * as the argument.
  93.      * @param    obj    the Object to compare with
  94.      * @return    true if these Objects are equal; false otherwise.
  95.      * @see        java.util.Hashtable
  96.      */
  97.     public boolean equals(Object obj) {
  98.     if (obj instanceof RemoteObject) {
  99.         if (ref == null) {
  100.         return obj == this;
  101.         } else {
  102.         return ref.remoteEquals(((RemoteObject)obj).ref);
  103.         }
  104.     } else if (obj != null) {
  105.         /*
  106.          * Fix for 4099660: if object is not an instance of RemoteObject,
  107.          * use the result of its equals method, to support symmetry is a
  108.          * remote object implementation class that does not extend
  109.          * RemoteObject wishes to support equality with its stub objects.
  110.          */
  111.         return obj.equals(this);
  112.     } else {
  113.         return false;
  114.     }
  115.     }
  116.  
  117.     /**
  118.      * Returns a String that represents the value of this remote object.
  119.      */
  120.     public String toString()
  121.     {
  122.     String classname = this.getClass().getName();
  123.     return (ref == null) ? classname :
  124.         classname + "[" +ref.remoteToString() + "]";
  125.     }
  126.  
  127.  
  128.     /**
  129.      * writeObject for object serialization. Writes out the class name of
  130.      * the remote reference and delegates to the reference to write out
  131.      * its representation.
  132.      */
  133.     private void writeObject(java.io.ObjectOutputStream out)
  134.     throws java.io.IOException, java.lang.ClassNotFoundException
  135.     {
  136.     if (ref == null) {
  137.         throw new java.rmi.MarshalException("Invalid remote object");
  138.     } else {
  139.         out.writeUTF(ref.getRefClass(out));
  140.         ref.writeExternal(out);
  141.     }
  142.     
  143.     }
  144.  
  145.     /**
  146.      * readObject for object serialization. Reads in the class name of
  147.      * the remote reference and delegates to the reference to read in
  148.      * its representation.
  149.      */
  150.     private void readObject(java.io.ObjectInputStream in) 
  151.     throws java.io.IOException, java.lang.ClassNotFoundException
  152.     {
  153.     try {
  154.         Class refClass = Class.forName(RemoteRef.packagePrefix + "." +
  155.                        in.readUTF());
  156.         ref = (RemoteRef)refClass.newInstance();
  157.         ref.readExternal(in);
  158.     } catch (InstantiationException e) {
  159.         throw new UnmarshalException("Unable to create remote reference",
  160.                      e);
  161.     } catch (IllegalAccessException e) {
  162.         throw new UnmarshalException("Illegal access creating remote reference");
  163.     }
  164.     }
  165.  
  166. }
  167.