Esempio n. 1
0
  /**
   * <code>writeObject</code> for custom serialization.
   *
   * <p>This method writes this object's serialized form for this class as follows:
   *
   * <p>The <code>writeObject</code> method is invoked on <code>out</code> passing this object's
   * unique identifier (a {@link java.rmi.server.UID UID} instance) as the argument.
   *
   * <p>Next, the {@link java.rmi.server.RemoteRef#getRefClass(java.io.ObjectOutput) getRefClass}
   * method is invoked on the activator's <code>RemoteRef</code> instance to obtain its external ref
   * type name. Next, the <code>writeUTF</code> method is invoked on <code>out</code> with the value
   * returned by <code>getRefClass</code>, and then the <code>writeExternal</code> method is invoked
   * on the <code>RemoteRef</code> instance passing <code>out</code> as the argument.
   *
   * @serialData The serialized data for this class comprises a <code>java.rmi.server.UID</code>
   *     (written with <code>ObjectOutput.writeObject</code>) followed by the external ref type name
   *     of the activator's <code>RemoteRef</code> instance (a string written with <code>
   *     ObjectOutput.writeUTF</code>), followed by the external form of the <code>RemoteRef</code>
   *     instance as written by its <code>writeExternal</code> method.
   *     <p>The external ref type name of the <code>RemoteRef</Code> instance is determined using
   *     the definitions of external ref type names specified in the {@link
   *     java.rmi.server.RemoteObject RemoteObject} <code>writeObject</code> method
   *     <b>serialData</b> specification. Similarly, the data written by the <code>writeExternal
   *     </code> method and read by the <code>readExternal</code> method of <code>RemoteRef</code>
   *     implementation classes corresponding to each of the defined external ref type names is
   *     specified in the {@link java.rmi.server.RemoteObject RemoteObject} <code>writeObject</code>
   *     method <b>serialData</b> specification.
   */
  private void writeObject(ObjectOutputStream out) throws IOException, ClassNotFoundException {
    out.writeObject(uid);

    RemoteRef ref;
    if (activator instanceof RemoteObject) {
      ref = ((RemoteObject) activator).getRef();
    } else if (Proxy.isProxyClass(activator.getClass())) {
      InvocationHandler handler = Proxy.getInvocationHandler(activator);
      if (!(handler instanceof RemoteObjectInvocationHandler)) {
        throw new InvalidObjectException("unexpected invocation handler");
      }
      ref = ((RemoteObjectInvocationHandler) handler).getRef();

    } else {
      throw new InvalidObjectException("unexpected activator type");
    }
    out.writeUTF(ref.getRefClass(out));
    ref.writeExternal(out);
  }
Esempio n. 2
0
  /**
   * <code>readObject</code> for custom serialization.
   *
   * <p>This method reads this object's serialized form for this class as follows:
   *
   * <p>The <code>readObject</code> method is invoked on <code>in</code> to read this object's
   * unique identifier (a {@link java.rmi.server.UID UID} instance).
   *
   * <p>Next, the <code>readUTF</code> method is invoked on <code>in</code> to read the external ref
   * type name of the <code>RemoteRef</code> instance for this object's activator. Next, the <code>
   * RemoteRef</code> instance is created of an implementation-specific class corresponding to the
   * external ref type name (returned by <code>readUTF</code>), and the <code>readExternal</code>
   * method is invoked on that <code>RemoteRef</code> instance to read the external form
   * corresponding to the external ref type name.
   *
   * <p>Note: If the external ref type name is <code>"UnicastRef"</code>, <code>"UnicastServerRef"
   * </code>, <code>"UnicastRef2"</code>, <code>"UnicastServerRef2"</code>, or <code>
   * "ActivatableRef"</code>, a corresponding implementation-specific class must be found, and its
   * <code>readExternal</code> method must read the serial data for that external ref type name as
   * specified to be written in the <b>serialData</b> documentation for this class. If the external
   * ref type name is any other string (of non-zero length), a <code>ClassNotFoundException</code>
   * will be thrown, unless the implementation provides an implementation-specific class
   * corresponding to that external ref type name, in which case the <code>RemoteRef</code> will be
   * an instance of that implementation-specific class.
   */
  private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
    uid = (UID) in.readObject();

    try {
      Class<? extends RemoteRef> refClass =
          Class.forName(RemoteRef.packagePrefix + "." + in.readUTF()).asSubclass(RemoteRef.class);
      RemoteRef ref = refClass.newInstance();
      ref.readExternal(in);
      activator =
          (Activator)
              Proxy.newProxyInstance(
                  null, new Class<?>[] {Activator.class}, new RemoteObjectInvocationHandler(ref));

    } catch (InstantiationException e) {
      throw (IOException)
          new InvalidObjectException("Unable to create remote reference").initCause(e);
    } catch (IllegalAccessException e) {
      throw (IOException)
          new InvalidObjectException("Unable to create remote reference").initCause(e);
    }
  }
Esempio n. 3
0
 private static void checkStub(Remote stub, Class<? extends Remote> stubClass) {
   // Check remote stub is from the expected class.
   //
   if (stub.getClass() != stubClass) {
     if (!Proxy.isProxyClass(stub.getClass())) {
       throw new SecurityException("Expecting a " + stubClass.getName() + " stub!");
     } else {
       InvocationHandler handler = Proxy.getInvocationHandler(stub);
       if (handler.getClass() != RemoteObjectInvocationHandler.class) {
         throw new SecurityException(
             "Expecting a dynamic proxy instance with a "
                 + RemoteObjectInvocationHandler.class.getName()
                 + " invocation handler!");
       } else {
         stub = (Remote) handler;
       }
     }
   }
   // Check RemoteRef in stub is from the expected class
   // "sun.rmi.server.UnicastRef2".
   //
   RemoteRef ref = ((RemoteObject) stub).getRef();
   if (ref.getClass() != UnicastRef2.class) {
     throw new SecurityException(
         "Expecting a " + UnicastRef2.class.getName() + " remote reference in stub!");
   }
   // Check RMIClientSocketFactory in stub is from the expected class
   // "javax.rmi.ssl.SslRMIClientSocketFactory".
   //
   LiveRef liveRef = ((UnicastRef2) ref).getLiveRef();
   RMIClientSocketFactory csf = liveRef.getClientSocketFactory();
   if (csf == null || csf.getClass() != SslRMIClientSocketFactory.class) {
     throw new SecurityException(
         "Expecting a "
             + SslRMIClientSocketFactory.class.getName()
             + " RMI client socket factory in stub!");
   }
 }
Esempio n. 4
0
 public boolean equals(Object obj) {
   if (obj instanceof RemoteObject) {
     if (ref == null) {
       return obj == this;
     } else {
       RemoteRef otherRef = ((RemoteObject) obj).getRef();
       return ref.remoteEquals(otherRef);
     }
   } else if (obj != null) {
     return obj.equals(this);
   } else {
     return false;
   }
 }
Esempio n. 5
0
 public int hashCode() {
   if (ref == null) return super.hashCode();
   else return ref.remoteHashCode();
 }
Esempio n. 6
0
 public String toString() {
   if (ref == null) return getClass().getName() + "[<unexported>]";
   else return getClass().getName() + "[" + ref.remoteToString() + "]";
 }