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

  1. /*
  2.  * @(#)UnicastRemoteObject.java    1.10 96/12/16
  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. package java.rmi.server;
  22.  
  23. import java.rmi.*;
  24.  
  25. /**
  26.  * The UnicastRemoteObject class defines a non-replicated remote
  27.  * object whose references are valid only while the server process is
  28.  * alive.  The UnicastRemoteObject class provides support for
  29.  * point-to-point active object references (invocations, parameters,
  30.  * and results) using TCP streams.  <p>
  31.  *
  32.  * Objects that require remote behavior should extend RemoteObject,
  33.  * typically via UnicastRemoteObject. If UnicastRemoteObject is not
  34.  * extended, the implementation class must then assume the
  35.  * responsibility for the correct semantics of the hashCode, equals,
  36.  * and toString methods inherited from the Object class, so that they
  37.  * behave appropriately for remote objects.
  38.  */
  39. public class UnicastRemoteObject extends RemoteServer {
  40.  
  41.  
  42.     /**
  43.      * Create and export a new UnicastRemoteObject object using an
  44.      * anonymous port.
  45.      */
  46.     protected UnicastRemoteObject() throws RemoteException
  47.     {
  48.     exportObject((Remote)this);
  49.     }
  50.  
  51.     /**
  52.      * Re-export the remote object when it is deserialized.
  53.      */
  54.     private void readObject(java.io.ObjectInputStream in) 
  55.     throws java.io.IOException, java.lang.ClassNotFoundException
  56.     {
  57.     exportObject((Remote)this);
  58.     }
  59.     
  60.     /**
  61.      * Returns a clone of the remote object that is distinct from
  62.      * the original.
  63.      *
  64.      * @exception CloneNotSupportedException if clone failed due to
  65.      * a RemoteException.
  66.      * @return the new remote object
  67.      */
  68.     public Object clone() throws CloneNotSupportedException
  69.     {
  70.     try {
  71.         UnicastRemoteObject remote = (UnicastRemoteObject)super.clone();
  72.         exportObject(remote);
  73.         return remote;
  74.     } catch (RemoteException e) {
  75.         throw new ServerCloneException("Clone failed", e);
  76.     }
  77.     }
  78.  
  79.     /** 
  80.      * Export the remote object to make it available to receive incoming calls.
  81.      * @param obj the remote object to be exported
  82.      * @exception RemoteException if export fails
  83.      */
  84.     public static RemoteStub exportObject(Remote obj)
  85.     throws RemoteException
  86.     {
  87.     /* Server ref must be created and assigned before remote object 
  88.      * can be exported.
  89.      */
  90.     try {
  91.         Class refClass = Class.forName(RemoteRef.packagePrefix +
  92.                        ".UnicastServerRef");
  93.         Object refObj = refClass.newInstance();
  94.         if (refObj instanceof ServerRef) {
  95.         ServerRef serverRef = (ServerRef)refObj;
  96.         if (obj instanceof UnicastRemoteObject)
  97.             ((UnicastRemoteObject)obj).ref = serverRef;
  98.         return serverRef.exportObject(obj, null);
  99.         } else {
  100.         throw new ExportException("Reference is not a java.rmi.server.ServerRef");
  101.         }
  102.     } catch (Exception e) {
  103.         throw new ExportException("Unable to create remote reference", e);
  104.     }
  105.     }
  106. }
  107.