home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / RemoteObject.java < prev    next >
Text File  |  1997-05-20  |  4KB  |  131 lines

  1. /*
  2.  * @(#)RemoteObject.java    1.4 96/11/18
  3.  * 
  4.  * Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion 1.1_beta
  20.  */
  21.  
  22. package java.rmi.server;
  23.  
  24. import java.rmi.Remote;
  25. import java.rmi.UnmarshalException;
  26.  
  27. /**
  28.  * The RemoteObject class implements the java.lang.Object behavior for
  29.  * remote objects.  RemoteObject provides the remote semantics of
  30.  * Object by implementing methods for hashCode, equals, and toString.
  31.  */
  32. public abstract class RemoteObject implements Remote, java.io.Serializable {
  33.  
  34.     transient protected RemoteRef ref;
  35.     
  36.     /**
  37.      * Create a remote object.
  38.      */
  39.     protected RemoteObject() {
  40.     ref = null;
  41.     }
  42.     
  43.     /**
  44.      * Create a remote object, initialized with the specified remote reference.
  45.      */
  46.     protected RemoteObject(RemoteRef newref) {
  47.     ref = newref;
  48.     }
  49.  
  50.     /**
  51.      * Returns a hashcode for a remote object.  Two remote object stubs
  52.      * that refer to the same remote object will have the same hash code
  53.      * (in order to support remote objects as keys in hash tables).
  54.      *
  55.      * @see        java.util.Hashtable
  56.      */
  57.     public int hashCode() {
  58.     return (ref == null) ? super.hashCode() : ref.remoteHashCode();
  59.     }
  60.  
  61.     /**
  62.      * Compares two remote objects for equality.
  63.      * Returns a boolean that indicates whether this remote object is
  64.      * equivalent to the specified Object. This method is used when a
  65.      * remote object is stored in a hashtable.
  66.      * @param    obj    the Object to compare with
  67.      * @return    true if these Objects are equal; false otherwise.
  68.      * @see        java.util.Hashtable
  69.      */
  70.     public boolean equals(Object obj) {
  71.     if (ref == null) 
  72.         return obj == this;
  73.  
  74.     if (obj instanceof RemoteObject) {
  75.         return ref.remoteEquals(((RemoteObject)obj).ref);
  76.     }
  77.  
  78.     return false;
  79.     }
  80.  
  81.     /**
  82.      * Returns a String that represents the value of this remote object.
  83.      */
  84.     public String toString()
  85.     {
  86.     String classname = this.getClass().getName();
  87.     return (ref == null) ? classname :
  88.         classname + "[" +ref.remoteToString() + "]";
  89.     }
  90.  
  91.  
  92.     /**
  93.      * writeObject for object serialization. Writes out the class name of
  94.      * the remote reference and delegates to the reference to write out
  95.      * its representation.
  96.      */
  97.     private void writeObject(java.io.ObjectOutputStream out)
  98.     throws java.io.IOException, java.lang.ClassNotFoundException
  99.     {
  100.     if (ref == null) {
  101.         throw new java.rmi.MarshalException("Invalid remote object");
  102.     } else {
  103.         out.writeUTF(ref.getRefClass(out));
  104.         ref.writeExternal(out);
  105.     }
  106.     
  107.     }
  108.  
  109.     /**
  110.      * readObject for object serialization. Reads in the class name of
  111.      * the remote reference and delegates to the reference to read in
  112.      * its representation.
  113.      */
  114.     private void readObject(java.io.ObjectInputStream in) 
  115.     throws java.io.IOException, java.lang.ClassNotFoundException
  116.     {
  117.     try {
  118.         Class refClass = Class.forName(RemoteRef.packagePrefix + "." +
  119.                        in.readUTF());
  120.         ref = (RemoteRef)refClass.newInstance();
  121.         ref.readExternal(in);
  122.     } catch (InstantiationException e) {
  123.         throw new UnmarshalException("Unable to create remote reference",
  124.                      e);
  125.     } catch (IllegalAccessException e) {
  126.         throw new UnmarshalException("Illegal access creating remote reference");
  127.     }
  128.     }
  129.  
  130. }
  131.