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