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