jdk/src/share/classes/sun/rmi/server/MarshalOutputStream.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 51 6fe31bc95bbc
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1996-2004 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.rmi.server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.rmi.Remote;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.rmi.server.RemoteStub;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import sun.rmi.transport.ObjectTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import sun.rmi.transport.Target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * A MarshalOutputStream extends ObjectOutputStream to add functions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * specific to marshaling of remote object references. If it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * necessary to serialize remote objects or objects that contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * references to remote objects a MarshalOutputStream must be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * instead of ObjectOutputStream. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * A new MarshalOutputStream is constructed to serialize remote
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * objects or graphs containing remote objects. Objects are written to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * the stream using the ObjectOutputStream.writeObject method. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * MarshalOutputStream maps remote objects to the corresponding remote
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * stub and embeds the location from which to load the stub
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * classes. The location may be ignored by the client but is supplied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
public class MarshalOutputStream extends ObjectOutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * Creates a marshal output stream with protocol version 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    public MarshalOutputStream(OutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        this(out, ObjectStreamConstants.PROTOCOL_VERSION_1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * Creates a marshal output stream with the given protocol version.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    public MarshalOutputStream(OutputStream out, int protocolVersion)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        super(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        this.useProtocolVersion(protocolVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                                    new java.security.PrivilegedAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                enableReplaceObject(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * Checks for objects that are instances of java.rmi.Remote
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * that need to be serialized as proxy objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    protected final Object replaceObject(Object obj) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        if ((obj instanceof Remote) && !(obj instanceof RemoteStub)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            Target target = ObjectTable.getTarget((Remote) obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            if (target != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                return target.getStub();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        return obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * Serializes a location from which to load the the specified class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    protected void annotateClass(Class<?> cl) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        writeLocation(java.rmi.server.RMIClassLoader.getClassAnnotation(cl));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * Serializes a location from which to load the specified class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    protected void annotateProxyClass(Class<?> cl) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        annotateClass(cl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * Writes the location for the class into the stream.  This method can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * be overridden by subclasses that store this annotation somewhere
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * else than as the next object in the stream, as is done by this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    protected void writeLocation(String location) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        writeObject(location);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
}