0N/A/*
2362N/A * Copyright (c) 1996, 2003, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.rmi.server;
0N/A
0N/Aimport java.rmi.Remote;
0N/Aimport java.rmi.NoSuchObjectException;
0N/Aimport java.lang.reflect.Proxy;
0N/Aimport sun.rmi.server.Util;
0N/A
0N/A/**
0N/A * The <code>RemoteObject</code> class implements the
0N/A * <code>java.lang.Object</code> behavior for remote objects.
0N/A * <code>RemoteObject</code> provides the remote semantics of Object by
0N/A * implementing methods for hashCode, equals, and toString.
0N/A *
0N/A * @author Ann Wollrath
0N/A * @author Laird Dornin
0N/A * @author Peter Jones
0N/A * @since JDK1.1
0N/A */
0N/Apublic abstract class RemoteObject implements Remote, java.io.Serializable {
0N/A
0N/A /** The object's remote reference. */
0N/A transient protected RemoteRef ref;
0N/A
0N/A /** indicate compatibility with JDK 1.1.x version of class */
0N/A private static final long serialVersionUID = -3215090123894869218L;
0N/A
0N/A /**
0N/A * Creates a remote object.
0N/A */
0N/A protected RemoteObject() {
0N/A ref = null;
0N/A }
0N/A
0N/A /**
0N/A * Creates a remote object, initialized with the specified remote
0N/A * reference.
0N/A * @param newref remote reference
0N/A */
0N/A protected RemoteObject(RemoteRef newref) {
0N/A ref = newref;
0N/A }
0N/A
0N/A /**
0N/A * Returns the remote reference for the remote object.
0N/A *
0N/A * <p>Note: The object returned from this method may be an instance of
0N/A * an implementation-specific class. The <code>RemoteObject</code>
0N/A * class ensures serialization portability of its instances' remote
0N/A * references through the behavior of its custom
0N/A * <code>writeObject</code> and <code>readObject</code> methods. An
0N/A * instance of <code>RemoteRef</code> should not be serialized outside
0N/A * of its <code>RemoteObject</code> wrapper instance or the result may
0N/A * be unportable.
0N/A *
0N/A * @return remote reference for the remote object
0N/A * @since 1.2
0N/A */
0N/A public RemoteRef getRef() {
0N/A return ref;
0N/A }
0N/A
0N/A /**
0N/A * Returns the stub for the remote object <code>obj</code> passed
0N/A * as a parameter. This operation is only valid <i>after</i>
0N/A * the object has been exported.
0N/A * @param obj the remote object whose stub is needed
0N/A * @return the stub for the remote object, <code>obj</code>.
0N/A * @exception NoSuchObjectException if the stub for the
0N/A * remote object could not be found.
0N/A * @since 1.2
0N/A */
0N/A public static Remote toStub(Remote obj) throws NoSuchObjectException {
0N/A if (obj instanceof RemoteStub ||
0N/A (obj != null &&
0N/A Proxy.isProxyClass(obj.getClass()) &&
0N/A Proxy.getInvocationHandler(obj) instanceof
0N/A RemoteObjectInvocationHandler))
0N/A {
0N/A return obj;
0N/A } else {
0N/A return sun.rmi.transport.ObjectTable.getStub(obj);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns a hashcode for a remote object. Two remote object stubs
0N/A * that refer to the same remote object will have the same hash code
0N/A * (in order to support remote objects as keys in hash tables).
0N/A *
0N/A * @see java.util.Hashtable
0N/A */
0N/A public int hashCode() {
0N/A return (ref == null) ? super.hashCode() : ref.remoteHashCode();
0N/A }
0N/A
0N/A /**
0N/A * Compares two remote objects for equality.
0N/A * Returns a boolean that indicates whether this remote object is
0N/A * equivalent to the specified Object. This method is used when a
0N/A * remote object is stored in a hashtable.
0N/A * If the specified Object is not itself an instance of RemoteObject,
0N/A * then this method delegates by returning the result of invoking the
0N/A * <code>equals</code> method of its parameter with this remote object
0N/A * as the argument.
0N/A * @param obj the Object to compare with
0N/A * @return true if these Objects are equal; false otherwise.
0N/A * @see java.util.Hashtable
0N/A */
0N/A public boolean equals(Object obj) {
0N/A if (obj instanceof RemoteObject) {
0N/A if (ref == null) {
0N/A return obj == this;
0N/A } else {
0N/A return ref.remoteEquals(((RemoteObject)obj).ref);
0N/A }
0N/A } else if (obj != null) {
0N/A /*
0N/A * Fix for 4099660: if object is not an instance of RemoteObject,
0N/A * use the result of its equals method, to support symmetry is a
0N/A * remote object implementation class that does not extend
0N/A * RemoteObject wishes to support equality with its stub objects.
0N/A */
0N/A return obj.equals(this);
0N/A } else {
0N/A return false;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns a String that represents the value of this remote object.
0N/A */
0N/A public String toString() {
0N/A String classname = Util.getUnqualifiedName(getClass());
0N/A return (ref == null) ? classname :
0N/A classname + "[" + ref.remoteToString() + "]";
0N/A }
0N/A
0N/A /**
0N/A * <code>writeObject</code> for custom serialization.
0N/A *
0N/A * <p>This method writes this object's serialized form for this class
0N/A * as follows:
0N/A *
0N/A * <p>The {@link RemoteRef#getRefClass(java.io.ObjectOutput) getRefClass}
0N/A * method is invoked on this object's <code>ref</code> field
0N/A * to obtain its external ref type name.
0N/A * If the value returned by <code>getRefClass</code> was
0N/A * a non-<code>null</code> string of length greater than zero,
0N/A * the <code>writeUTF</code> method is invoked on <code>out</code>
0N/A * with the value returned by <code>getRefClass</code>, and then
0N/A * the <code>writeExternal</code> method is invoked on
0N/A * this object's <code>ref</code> field passing <code>out</code>
0N/A * as the argument; otherwise,
0N/A * the <code>writeUTF</code> method is invoked on <code>out</code>
0N/A * with a zero-length string (<code>""</code>), and then
0N/A * the <code>writeObject</code> method is invoked on <code>out</code>
0N/A * passing this object's <code>ref</code> field as the argument.
0N/A *
0N/A * @serialData
0N/A *
0N/A * The serialized data for this class comprises a string (written with
0N/A * <code>ObjectOutput.writeUTF</code>) that is either the external
0N/A * ref type name of the contained <code>RemoteRef</code> instance
0N/A * (the <code>ref</code> field) or a zero-length string, followed by
0N/A * either the external form of the <code>ref</code> field as written by
0N/A * its <code>writeExternal</code> method if the string was of non-zero
0N/A * length, or the serialized form of the <code>ref</code> field as
0N/A * written by passing it to the serialization stream's
0N/A * <code>writeObject</code> if the string was of zero length.
0N/A *
0N/A * <p>If this object is an instance of
0N/A * {@link RemoteStub} or {@link RemoteObjectInvocationHandler}
0N/A * that was returned from any of
0N/A * the <code>UnicastRemoteObject.exportObject</code> methods
0N/A * and custom socket factories are not used,
0N/A * the external ref type name is <code>"UnicastRef"</code>.
0N/A *
0N/A * If this object is an instance of
0N/A * <code>RemoteStub</code> or <code>RemoteObjectInvocationHandler</code>
0N/A * that was returned from any of
0N/A * the <code>UnicastRemoteObject.exportObject</code> methods
0N/A * and custom socket factories are used,
0N/A * the external ref type name is <code>"UnicastRef2"</code>.
0N/A *
0N/A * If this object is an instance of
0N/A * <code>RemoteStub</code> or <code>RemoteObjectInvocationHandler</code>
0N/A * that was returned from any of
0N/A * the <code>java.rmi.activation.Activatable.exportObject</code> methods,
0N/A * the external ref type name is <code>"ActivatableRef"</code>.
0N/A *
0N/A * If this object is an instance of
0N/A * <code>RemoteStub</code> or <code>RemoteObjectInvocationHandler</code>
0N/A * that was returned from
0N/A * the <code>RemoteObject.toStub</code> method (and the argument passed
0N/A * to <code>toStub</code> was not itself a <code>RemoteStub</code>),
0N/A * the external ref type name is a function of how the remote object
0N/A * passed to <code>toStub</code> was exported, as described above.
0N/A *
0N/A * If this object is an instance of
0N/A * <code>RemoteStub</code> or <code>RemoteObjectInvocationHandler</code>
0N/A * that was originally created via deserialization,
0N/A * the external ref type name is the same as that which was read
0N/A * when this object was deserialized.
0N/A *
0N/A * <p>If this object is an instance of
0N/A * <code>java.rmi.server.UnicastRemoteObject</code> that does not
0N/A * use custom socket factories,
0N/A * the external ref type name is <code>"UnicastServerRef"</code>.
0N/A *
0N/A * If this object is an instance of
0N/A * <code>UnicastRemoteObject</code> that does
0N/A * use custom socket factories,
0N/A * the external ref type name is <code>"UnicastServerRef2"</code>.
0N/A *
0N/A * <p>Following is the data that must be written by the
0N/A * <code>writeExternal</code> method and read by the
0N/A * <code>readExternal</code> method of <code>RemoteRef</code>
0N/A * implementation classes that correspond to the each of the
0N/A * defined external ref type names:
0N/A *
0N/A * <p>For <code>"UnicastRef"</code>:
0N/A *
0N/A * <ul>
0N/A *
0N/A * <li>the hostname of the referenced remote object,
0N/A * written by {@link java.io.ObjectOutput#writeUTF(String)}
0N/A *
0N/A * <li>the port of the referenced remote object,
0N/A * written by {@link java.io.ObjectOutput#writeInt(int)}
0N/A *
0N/A * <li>the data written as a result of calling
0N/A * {link java.rmi.server.ObjID#write(java.io.ObjectOutput)}
0N/A * on the <code>ObjID</code> instance contained in the reference
0N/A *
0N/A * <li>the boolean value <code>false</code>,
0N/A * written by {@link java.io.ObjectOutput#writeBoolean(boolean)}
0N/A *
0N/A * </ul>
0N/A *
0N/A * <p>For <code>"UnicastRef2"</code> with a
0N/A * <code>null</code> client socket factory:
0N/A *
0N/A * <ul>
0N/A *
0N/A * <li>the byte value <code>0x00</code>
0N/A * (indicating <code>null</code> client socket factory),
0N/A * written by {@link java.io.ObjectOutput#writeByte(int)}
0N/A *
0N/A * <li>the hostname of the referenced remote object,
0N/A * written by {@link java.io.ObjectOutput#writeUTF(String)}
0N/A *
0N/A * <li>the port of the referenced remote object,
0N/A * written by {@link java.io.ObjectOutput#writeInt(int)}
0N/A *
0N/A * <li>the data written as a result of calling
0N/A * {link java.rmi.server.ObjID#write(java.io.ObjectOutput)}
0N/A * on the <code>ObjID</code> instance contained in the reference
0N/A *
0N/A * <li>the boolean value <code>false</code>,
0N/A * written by {@link java.io.ObjectOutput#writeBoolean(boolean)}
0N/A *
0N/A * </ul>
0N/A *
0N/A * <p>For <code>"UnicastRef2"</code> with a
0N/A * non-<code>null</code> client socket factory:
0N/A *
0N/A * <ul>
0N/A *
0N/A * <li>the byte value <code>0x01</code>
0N/A * (indicating non-<code>null</code> client socket factory),
0N/A * written by {@link java.io.ObjectOutput#writeByte(int)}
0N/A *
0N/A * <li>the hostname of the referenced remote object,
0N/A * written by {@link java.io.ObjectOutput#writeUTF(String)}
0N/A *
0N/A * <li>the port of the referenced remote object,
0N/A * written by {@link java.io.ObjectOutput#writeInt(int)}
0N/A *
0N/A * <li>a client socket factory (object of type
0N/A * <code>java.rmi.server.RMIClientSocketFactory</code>),
0N/A * written by passing it to an invocation of
0N/A * <code>writeObject</code> on the stream instance
0N/A *
0N/A * <li>the data written as a result of calling
0N/A * {link java.rmi.server.ObjID#write(java.io.ObjectOutput)}
0N/A * on the <code>ObjID</code> instance contained in the reference
0N/A *
0N/A * <li>the boolean value <code>false</code>,
0N/A * written by {@link java.io.ObjectOutput#writeBoolean(boolean)}
0N/A *
0N/A * </ul>
0N/A *
0N/A * <p>For <code>"ActivatableRef"</code> with a
0N/A * <code>null</code> nested remote reference:
0N/A *
0N/A * <ul>
0N/A *
0N/A * <li>an instance of
0N/A * <code>java.rmi.activation.ActivationID</code>,
0N/A * written by passing it to an invocation of
0N/A * <code>writeObject</code> on the stream instance
0N/A *
0N/A * <li>a zero-length string (<code>""</code>),
0N/A * written by {@link java.io.ObjectOutput#writeUTF(String)}
0N/A *
0N/A * </ul>
0N/A *
0N/A * <p>For <code>"ActivatableRef"</code> with a
0N/A * non-<code>null</code> nested remote reference:
0N/A *
0N/A * <ul>
0N/A *
0N/A * <li>an instance of
0N/A * <code>java.rmi.activation.ActivationID</code>,
0N/A * written by passing it to an invocation of
0N/A * <code>writeObject</code> on the stream instance
0N/A *
0N/A * <li>the external ref type name of the nested remote reference,
0N/A * which must be <code>"UnicastRef2"</code>,
0N/A * written by {@link java.io.ObjectOutput#writeUTF(String)}
0N/A *
0N/A * <li>the external form of the nested remote reference,
0N/A * written by invoking its <code>writeExternal</code> method
0N/A * with the stream instance
0N/A * (see the description of the external form for
0N/A * <code>"UnicastRef2"</code> above)
0N/A *
0N/A * </ul>
0N/A *
0N/A * <p>For <code>"UnicastServerRef"</code> and
0N/A * <code>"UnicastServerRef2"</code>, no data is written by the
0N/A * <code>writeExternal</code> method or read by the
0N/A * <code>readExternal</code> method.
0N/A */
0N/A private void writeObject(java.io.ObjectOutputStream out)
0N/A throws java.io.IOException, java.lang.ClassNotFoundException
0N/A {
0N/A if (ref == null) {
0N/A throw new java.rmi.MarshalException("Invalid remote object");
0N/A } else {
0N/A String refClassName = ref.getRefClass(out);
0N/A if (refClassName == null || refClassName.length() == 0) {
0N/A /*
0N/A * No reference class name specified, so serialize
0N/A * remote reference.
0N/A */
0N/A out.writeUTF("");
0N/A out.writeObject(ref);
0N/A } else {
0N/A /*
0N/A * Built-in reference class specified, so delegate
0N/A * to reference to write out its external form.
0N/A */
0N/A out.writeUTF(refClassName);
0N/A ref.writeExternal(out);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * <code>readObject</code> for custom serialization.
0N/A *
0N/A * <p>This method reads this object's serialized form for this class
0N/A * as follows:
0N/A *
0N/A * <p>The <code>readUTF</code> method is invoked on <code>in</code>
0N/A * to read the external ref type name for the <code>RemoteRef</code>
0N/A * instance to be filled in to this object's <code>ref</code> field.
0N/A * If the string returned by <code>readUTF</code> has length zero,
0N/A * the <code>readObject</code> method is invoked on <code>in</code>,
0N/A * and than the value returned by <code>readObject</code> is cast to
0N/A * <code>RemoteRef</code> and this object's <code>ref</code> field is
0N/A * set to that value.
0N/A * Otherwise, this object's <code>ref</code> field is set to a
0N/A * <code>RemoteRef</code> instance that is created of an
0N/A * implementation-specific class corresponding to the external ref
0N/A * type name returned by <code>readUTF</code>, and then
0N/A * the <code>readExternal</code> method is invoked on
0N/A * this object's <code>ref</code> field.
0N/A *
0N/A * <p>If the external ref type name is
0N/A * <code>"UnicastRef"</code>, <code>"UnicastServerRef"</code>,
0N/A * <code>"UnicastRef2"</code>, <code>"UnicastServerRef2"</code>,
0N/A * or <code>"ActivatableRef"</code>, a corresponding
0N/A * implementation-specific class must be found, and its
0N/A * <code>readExternal</code> method must read the serial data
0N/A * for that external ref type name as specified to be written
0N/A * in the <b>serialData</b> documentation for this class.
0N/A * If the external ref type name is any other string (of non-zero
0N/A * length), a <code>ClassNotFoundException</code> will be thrown,
0N/A * unless the implementation provides an implementation-specific
0N/A * class corresponding to that external ref type name, in which
0N/A * case this object's <code>ref</code> field will be set to an
0N/A * instance of that implementation-specific class.
0N/A */
0N/A private void readObject(java.io.ObjectInputStream in)
0N/A throws java.io.IOException, java.lang.ClassNotFoundException
0N/A {
0N/A String refClassName = in.readUTF();
0N/A if (refClassName == null || refClassName.length() == 0) {
0N/A /*
0N/A * No reference class name specified, so construct
0N/A * remote reference from its serialized form.
0N/A */
0N/A ref = (RemoteRef) in.readObject();
0N/A } else {
0N/A /*
0N/A * Built-in reference class specified, so delegate to
0N/A * internal reference class to initialize its fields from
0N/A * its external form.
0N/A */
0N/A String internalRefClassName =
0N/A RemoteRef.packagePrefix + "." + refClassName;
0N/A Class refClass = Class.forName(internalRefClassName);
0N/A try {
0N/A ref = (RemoteRef) refClass.newInstance();
0N/A
0N/A /*
0N/A * If this step fails, assume we found an internal
0N/A * class that is not meant to be a serializable ref
0N/A * type.
0N/A */
0N/A } catch (InstantiationException e) {
0N/A throw new ClassNotFoundException(internalRefClassName, e);
0N/A } catch (IllegalAccessException e) {
0N/A throw new ClassNotFoundException(internalRefClassName, e);
0N/A } catch (ClassCastException e) {
0N/A throw new ClassNotFoundException(internalRefClassName, e);
0N/A }
0N/A ref.readExternal(in);
0N/A }
0N/A }
0N/A}