jdk/src/java.rmi/share/classes/sun/rmi/server/MarshalOutputStream.java
author martin
Thu, 30 Oct 2014 07:31:41 -0700
changeset 28059 e576535359cc
parent 27805 e63666f0f666
permissions -rw-r--r--
8067377: My hobby: caning, then then canning, the the can-can Summary: Fix ALL the stutters! Reviewed-by: rriggs, mchung, lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
27805
e63666f0f666 8066632: Suppress deprecation warnings in java.rmi module
darcy
parents: 25859
diff changeset
     2
 * Copyright (c) 1996, 2014, Oracle and/or its affiliates. All rights reserved.
2
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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    23
 * questions.
2
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(
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    67
            new java.security.PrivilegedAction<Void>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    68
                public Void run() {
2
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
     */
27805
e63666f0f666 8066632: Suppress deprecation warnings in java.rmi module
darcy
parents: 25859
diff changeset
    79
    @SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    protected final Object replaceObject(Object obj) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        if ((obj instanceof Remote) && !(obj instanceof RemoteStub)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            Target target = ObjectTable.getTarget((Remote) obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            if (target != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                return target.getStub();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        return obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /**
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 27805
diff changeset
    91
     * Serializes a location from which to load the specified class.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    protected void annotateClass(Class<?> cl) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        writeLocation(java.rmi.server.RMIClassLoader.getClassAnnotation(cl));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * Serializes a location from which to load the specified class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    protected void annotateProxyClass(Class<?> cl) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        annotateClass(cl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * Writes the location for the class into the stream.  This method can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * be overridden by subclasses that store this annotation somewhere
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * else than as the next object in the stream, as is done by this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    protected void writeLocation(String location) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        writeObject(location);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
}