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 / UnicastRemoteObject.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  6.8 KB  |  223 lines

  1. /*
  2.  * @(#)UnicastRemoteObject.java    1.17 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. package java.rmi.server;
  15.  
  16. import java.rmi.*;
  17.  
  18. /**
  19.  * The UnicastRemoteObject class defines a non-replicated remote
  20.  * object whose references are valid only while the server process is
  21.  * alive.  The UnicastRemoteObject class provides support for
  22.  * point-to-point active object references (invocations, parameters,
  23.  * and results) using TCP streams.  <p>
  24.  *
  25.  * Objects that require remote behavior should extend RemoteObject,
  26.  * typically via UnicastRemoteObject. If UnicastRemoteObject is not
  27.  * extended, the implementation class must then assume the
  28.  * responsibility for the correct semantics of the hashCode, equals,
  29.  * and toString methods inherited from the Object class, so that they
  30.  * behave appropriately for remote objects.
  31.  */
  32. public class UnicastRemoteObject extends RemoteServer {
  33.  
  34.     /* port number on which to export object */
  35.     private int port = 0;
  36.  
  37.     /* socket type to use when exporting object, if any */
  38.     private SocketType socketType = null;
  39.  
  40.     /* indicate compatibility with JDK 1.1.x version of class */
  41.     private static final long serialVersionUID = 4974527148936298033L;
  42.  
  43.     /**
  44.      * Create and export a new UnicastRemoteObject object using an
  45.      * anonymous port.
  46.      */
  47.     protected UnicastRemoteObject() throws RemoteException
  48.     {
  49.     this(0);
  50.     }
  51.  
  52.     /**
  53.      * Create and export a new UnicastRemoteObject object using the
  54.      * particular supplied port.
  55.      * @param port the port to export the object on
  56.      */
  57.     protected UnicastRemoteObject(int port) throws RemoteException
  58.     {
  59.     this.port = port;
  60.     exportObject((Remote)this, port);
  61.     }
  62.  
  63.     /**
  64.      * Create and export a new UnicastRemoteObject object using the
  65.      * particular supplied port and socket type.
  66.      */
  67.     protected UnicastRemoteObject(int port, SocketType socketType)
  68.     throws RemoteException
  69.     {
  70.     this.port = port;
  71.     this.socketType = socketType;
  72.     exportObject((Remote)this, port, socketType);
  73.     }
  74.  
  75.     /**
  76.      * Re-export the remote object when it is deserialized.
  77.      */
  78.     private void readObject(java.io.ObjectInputStream in) 
  79.     throws java.io.IOException, java.lang.ClassNotFoundException
  80.     {
  81.     in.defaultReadObject();
  82.     reexport();
  83.     }
  84.     
  85.     /**
  86.      * Returns a clone of the remote object that is distinct from
  87.      * the original.
  88.      *
  89.      * @exception CloneNotSupportedException if clone failed due to
  90.      * a RemoteException.
  91.      * @return the new remote object
  92.      */
  93.     public Object clone() throws CloneNotSupportedException
  94.     {
  95.     try {
  96.         UnicastRemoteObject cloned = (UnicastRemoteObject)super.clone();
  97.         cloned.reexport();
  98.         return cloned;
  99.     } catch (RemoteException e) {
  100.         throw new ServerCloneException("Clone failed", e);
  101.     }
  102.     }
  103.  
  104.     /*
  105.      * Export this UnicastRemoteObject using its initialized fields because
  106.      * its creation bypassed running its constructors (via deserialization
  107.      * or cloning, for example).
  108.      */
  109.     private void reexport() throws RemoteException
  110.     {
  111.     if (socketType == null)
  112.         exportObject((Remote)this, port);
  113.     else
  114.         exportObject((Remote)this, port, socketType);
  115.     }
  116.  
  117.     /** 
  118.      * Export the remote object to make it available to receive incoming calls,
  119.      * using an anonymous port.
  120.      * @param obj the remote object to be exported
  121.      * @exception RemoteException if export fails
  122.      */
  123.     public static RemoteStub exportObject(Remote obj)
  124.     throws RemoteException
  125.     {
  126.     return (RemoteStub)exportObject(obj, 0);
  127.     }
  128.  
  129.     /* parameter types for server ref constructor invocation used below */
  130.     private static Class[] portClass = {
  131.     int.class
  132.     };
  133.  
  134.     /** 
  135.      * Export the remote object to make it available to receive incoming calls,
  136.      * using the particular supplied port.
  137.      * @param obj the remote object to be exported
  138.      * @param port the port to export the object on
  139.      * @exception RemoteException if export fails
  140.      * @since JDK1.2
  141.      */
  142.     public static Remote exportObject(Remote obj, int port)
  143.     throws RemoteException
  144.     {
  145.     // prepare arguments for server ref constructor
  146.     Object[] args = new Object[1];
  147.     args[0] = new Integer(port);
  148.  
  149.     return exportObject(obj, "UnicastServerRef", portClass, args);
  150.     }
  151.  
  152.     /* parameter types for server ref constructor invocation used below */
  153.     private static Class[] portTypeClass = {
  154.     int.class, SocketType.class
  155.     };
  156.  
  157.     /**
  158.      * Export the remote object to make it available to receive incoming calls,
  159.      * using a transport specified by the given socket type.
  160.      * @param obj the remote object to be exported
  161.      * @param port the port to export the object on
  162.      * @param socketType the socket type to export object with
  163.      * @exception RemoteException if export fails
  164.      * @since JDK1.2
  165.      */
  166.     public static Remote exportObject(Remote obj, int port,
  167.                       SocketType socketType)
  168.     throws RemoteException
  169.     {
  170.     // prepare arguments for server ref constructor
  171.     Object[] args = new Object[2];
  172.     args[0] = new Integer(port);
  173.     args[1] = socketType;
  174.     
  175.     return exportObject(obj, "UnicastServerRef2", portTypeClass, args);
  176.     }
  177.  
  178.     /*
  179.      * Create instnace of given server ref type with constructor chosen
  180.      * by indicated paramters and supplied with given arguements, and
  181.      * export remote object with it.
  182.      */
  183.     private static Remote exportObject(Remote obj, String refType,
  184.                        Class[] params, Object[] args)
  185.     throws RemoteException
  186.     {
  187.     // compose name of server ref class and find it
  188.     String refClassName = RemoteRef.packagePrefix + "." + refType;
  189.     Class refClass;
  190.     try {
  191.         refClass = Class.forName(refClassName);
  192.     } catch (ClassNotFoundException e) {
  193.         throw new ExportException(
  194.         "No class found for server ref type: " + refType);
  195.     }
  196.  
  197.     if (!ServerRef.class.isAssignableFrom(refClass)) {
  198.         throw new ExportException(
  199.         "Server ref class not instance of " +
  200.         ServerRef.class.getName() + ": " + refClass.getName());
  201.     }
  202.  
  203.     // create server ref instance using given constructor and arguments
  204.     ServerRef serverRef;
  205.     try {
  206.         java.lang.reflect.Constructor cons =
  207.         refClass.getConstructor(params);
  208.         serverRef = (ServerRef) cons.newInstance(args);
  209.         // if impl does extends UnicastRemoteObject, set its ref
  210.         if (obj instanceof UnicastRemoteObject)
  211.         ((UnicastRemoteObject)obj).ref = serverRef;
  212.  
  213.     } catch (Exception e) {
  214.         throw new ExportException(
  215.         "Exception creating instance of server ref class: " +
  216.         refClass.getName(), e);
  217.     }
  218.  
  219.     return serverRef.exportObject(obj, null);
  220.     }
  221. }
  222.  
  223.