CONTENTS | PREV | NEXT | Java Remote Method Invocation |
RemoteObject
Class
Thejava.rmi.server.RemoteObject
class implements thejava.lang.Object
behavior for remote objects. ThehashCode
andequals
methods are implemented to allow remote object references to be stored in hashtables and compared. Theequals
method returnstrue
if twoRemote
objects refer to the same remote object. It compares the remote object references of the remote objects.The
toString
method returns a string describing the remote object. The contents and syntax of this string is implementation-specific and can vary.All of the other methods of
java.lang.Object
retain their original implementations.
package java.rmi.server;public abstract class RemoteObject implements java.rmi.Remote, java.io.Serializable { protected transient RemoteRef ref;
protected RemoteObject(); protected RemoteObject(RemoteRef ref);
public RemoteRef getRef(); public static Remote toStub(java.rmi.Remote obj) throws java.rmi.NoSuchObjectException; public int hashCode(); public boolean equals(Object obj); public String toString(); }
Since theRemoteObject
class is abstract, it cannot be instantiated. Therefore, one ofRemoteObject
's constructors must be called from a subclass implementation. The firstRemoteObject
constructor creates aRemoteObject
with anull
remote reference. The secondRemoteObject
constructor creates aRemoteObject
with the given remote reference, ref.The
getRef
method returns the remote reference for the remote object.The
toStub
method returns a stub for the remote object, obj, passed as a parameter. This operation is only valid after the remote object implementation has been exported. If the stub for the remote object could not be found, then the method throwsNoSuchObjectException
.
RemoteObject
Class
The default implementations in thejava.lang.Object
class for theequals
,hashCode
, andtoString
methods are not appropriate for remote objects. Therefore, theRemoteObject
class provides implementations for these methods that have semantics more appropriate for remote objects.
equals
and hashCode
methods
In order for a remote object to be used as a key in a hash table, the methodsequals
andhashCode
need to be overridden in the remote object implementation. These methods are overridden by the classjava.rmi.server.RemoteObject
:
- The
java.rmi.server.RemoteObject
class's implementation of theequals
method determines whether two object references are equal, not whether the contents of the two objects are equal. This is because determining equality based on content requires a remote method invocation, and the signature ofequals
does not allow a remote exception to be thrown.- The
java.rmi.server.RemoteObject
class's implementation of thehashCode
method returns the same value for all remote references that refer to the same underlying remote object (because references to the same object are considered equal).
toString
method
ThetoString
method is defined to return a string which represents the remote reference of the object. The contents of the string are specific to the remote reference type. The current implementation for singleton (unicast) objects includes an object identifier and other information about the object that is specific to the transport layer (such as host name and port number).
clone
method
Objects are only clonable using the Java programming language's default mechanism if they support thejava.lang.Cloneable
interface. Stubs for remote objects generated by thermic
compiler are declared final and do not implement theCloneable
interface. Therefore, cloning a stub is not possible.
TheRemoteObject
class implements the special (private)writeObject
andreadObject
methods called by the object serialization mechanism to handle serializing data to ajava.io.ObjectOutputStream
.RemoteObject
's serialized form is written using the method:
private void writeObject(java.io.ObjectOutputStream out) throws java.io.IOException, java.lang.ClassNotFoundException;
- If
RemoteObject
's remote reference field, ref, isnull
, then the method throwsjava.rmi.MarshalException
.- If the remote reference, ref, is non-
null
: ref's class is obtained via a call to itsgetRefClass
method, which typically returns the non-package qualified name of the remote reference's class.
- If the class name returned is non-
null
:
- ref's class name is written to the stream, out, in UTF.
- ref's
writeExternal
method is called passing it the stream, out, so that ref can write its external representation to the stream.- If the class name returned by
ref.getRefClass
isnull
:A
RemoteObject
's state is reconstructed from its serialized form using this method called by theObjectInputStream
during deserialization:
private void readObject(java.io.ObjectInputStream in) throws java.io.IOException, java.lang.ClassNotFoundException;
- First, the ref's class name, a UTF string, is read from the stream in.
- Given the unqualified class name read from the stream:
- The ref's full class name is constructed by concatenating the value of the string
java.rmi.server.RemoteRef.packagePrefix
and "." with the class name read from the stream.- An instance of the ref's class is created (from the full class name); if an instance cannot be created because of an
InstantiationException
or anIllegalAccessException
, ajava.rmi.UnmarshalException
will be thrown.- The new instance (which becomes the ref field) reads its external form from the stream, in.
CONTENTS | PREV | NEXT
Copyright © 1997-1999 Sun Microsystems, Inc. All Rights Reserved.