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

  1. /*
  2.  * @(#)ActivationID.java    1.11 98/03/18
  3.  *
  4.  * Copyright 1997, 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.activation;
  16.  
  17. import java.rmi.*;
  18. import java.rmi.server.RemoteObject;
  19. import java.rmi.server.RemoteRef;
  20. import java.rmi.server.UID;
  21. import sun.rmi.server.RemoteProxy;
  22.  
  23. /**
  24.  * Activation makes use of special identifiers to denote remote
  25.  * objects that can be activated over time. An activation identifier
  26.  * (an instance of the class <code>ActivationID</code>) contains several
  27.  * pieces of information needed for activating an object: <ul>
  28.  * <li> a remote reference to the object's activator, and
  29.  * <li> a unique identifier for the object. </ul> <p>
  30.  *
  31.  * An activation id for an object can be obtained by registering
  32.  * an object with the activation system. Registration is accomplished
  33.  * in a few ways: <ul>
  34.  * <li>via the <code>Activatable.register</code> method
  35.  * <li>via the first <code>Activatable</code> constructor (that takes
  36.  * three arguments and both registers and exports the object, and
  37.  * <li>via the first <code>Activatable.exportObject</code> method
  38.  * that takes the activation descriptor, object and port as arguments;
  39.  * this method both registers and exports the object. </ul>
  40.  *
  41.  * @author    Ann Wollrath
  42.  * @version    1.11, 03/18/98
  43.  * @see        Activatable
  44.  * @since    JDK1.2
  45.  */
  46. public class ActivationID implements java.io.Serializable
  47. {
  48.     /** the object's activator */
  49.     private Activator activator;
  50.     /** the object's unique id */
  51.     private UID uid = new UID();
  52.  
  53.     /** indicate compatibility with JDK 1.2 version of class */
  54.     private static final long serialVersionUID = -4608673054848209235L;
  55.  
  56.     /**
  57.      * The constructor for <code>ActivationID</code> takes a single
  58.      * argument, activator, that specifies a remote reference to the
  59.      * activator responsible for activating the object associated with
  60.      * this identifier. An instance of <code>ActivationID</code> is globally
  61.      * unique.
  62.      *
  63.      * @param activator reference to the activator responsible for
  64.      * activating the object
  65.      */
  66.    public ActivationID(Activator activator)
  67.     {
  68.     this.activator = activator;
  69.     }
  70.  
  71.     /**
  72.      * Activate the object for this id.
  73.      *
  74.      * @param force if true, forces the activator to contact the group
  75.      * when activating the object (instead of returning a cached reference);
  76.      * if false, returning a cached value is acceptable.
  77.      * @return the reference to the active remote object
  78.      * @exception ActivationException if activation fails
  79.      * @exception UnknownObjectException if the object is unknown
  80.      * @exception RemoteException if remote call fails
  81.      */
  82.     public Remote activate(boolean force)
  83.     throws ActivationException, UnknownObjectException, RemoteException
  84.     {
  85.      try {
  86.          MarshalledObject mobj =
  87.          (MarshalledObject)(activator.activate(this, force));
  88.          return (Remote)mobj.get();
  89.      } catch (UnknownObjectException e) {
  90.          throw e;
  91.      } catch (RemoteException e) {
  92.          throw e;
  93.      } catch (java.io.IOException e) {
  94.          throw new ActivationException("activation failed", e);
  95.      } catch (java.lang.ClassNotFoundException e) {
  96.          throw new ActivationException("activation failed", e);
  97.     }
  98.     
  99.     }
  100.     
  101.     /**
  102.      * Returns a hashcode for the activation id.  Two identifiers that
  103.      * refer to the same remote object will have the same hash code.
  104.      *
  105.      * @see java.util.Hashtable
  106.      */
  107.     public int hashCode() 
  108.     {
  109.     return uid.hashCode();
  110.     }
  111.  
  112.     /**
  113.      * Compares two activation ids for content equality.
  114.      * Returns true if both of the following conditions are true:
  115.      * 1) the unique identifiers equivalent (by content), and
  116.      * 2) the activator specified in each identifier
  117.      *    refers to the same remote object.
  118.      *
  119.      * @param    obj    the Object to compare with
  120.      * @return    true if these Objects are equal; false otherwise.
  121.      * @see        java.util.Hashtable
  122.      */
  123.     public boolean equals(Object obj) 
  124.     {
  125.     if (obj instanceof ActivationID) {
  126.         ActivationID id = (ActivationID)obj;
  127.         return (uid.equals(id.uid) && activator.equals(id.activator));
  128.     } else {
  129.         return false;
  130.     }
  131.     }
  132.     
  133.     /**
  134.      * writeObject for object serialization. Writes out uid and the activator's
  135.      * ref.
  136.      */
  137.     private void writeObject(java.io.ObjectOutputStream out)
  138.     throws java.io.IOException, java.lang.ClassNotFoundException
  139.     {
  140.     out.writeObject(uid);
  141.     RemoteRef ref = ((RemoteObject)activator).getRef();
  142.     
  143.     out.writeUTF(ref.getRefClass(out));
  144.     ref.writeExternal(out);
  145.     }
  146.  
  147.     /**
  148.      * readObject for object serialization. Reads in the uid and activator's
  149.      * remote ref and creates the activator with the reference.
  150.      */
  151.     private void readObject(java.io.ObjectInputStream in) 
  152.     throws java.io.IOException, java.lang.ClassNotFoundException
  153.     {
  154.     uid = (UID)in.readObject();
  155.     
  156.     try {
  157.         Class refClass = Class.forName(RemoteRef.packagePrefix + "." +
  158.                        in.readUTF());
  159.         RemoteRef ref = (RemoteRef)refClass.newInstance();
  160.         ref.readExternal(in);
  161.         activator =
  162.         (Activator)RemoteProxy.getStub(activatorClassName, ref);
  163.         
  164.     } catch (InstantiationException e) {
  165.         throw new UnmarshalException("Unable to create remote reference",
  166.                      e);
  167.     } catch (IllegalAccessException e) {
  168.         throw new UnmarshalException("Illegal access creating remote reference");
  169.     }
  170.     }
  171.  
  172.     private static String activatorClassName;
  173.     
  174.     static 
  175.     {
  176.     try {
  177.         java.security.AccessController.beginPrivileged();
  178.         activatorClassName =
  179.         System.getProperty("java.rmi.activation.activator.class",
  180.                    "sun.rmi.server.Activation$ActivatorImpl");
  181.     } finally {
  182.         java.security.AccessController.endPrivileged();
  183.     }
  184.     }
  185. }
  186.