1 /* 2 * Copyright (c) 1998-2002 The Jgroup Team. 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU Lesser General Public License version 2 as 6 * published by the Free Software Foundation. 7 * 8 * This program is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU Lesser General Public License for more details. 12 * 13 * You should have received a copy of the GNU Lesser General Public License 14 * along with this program; if not, write to the Free Software 15 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 16 * 17 */ 18 19 package jgroup.relacs.rmi; 20 21 import java.io.IOException; 22 import java.io.ObjectOutputStream; 23 import java.io.OutputStream; 24 import java.rmi.MarshalledObject; 25 import java.rmi.Remote; 26 27 /** 28 * This class overrides the replaceObject method to replace each 29 * exported remote object with its corresponding stub instance. 30 * This is obtained by using the <CODE>java.rmi.MarshalledObject</CODE> 31 * class, that contains a byte stream with the serialized 32 * representation of an object given to its constructor. The 33 * contained object is serialized and deserialized with the same 34 * serialization semantics used for marshaling and unmarshaling 35 * parameters and return values of RMI calls. 36 * 37 * @author Alberto Montresor 38 * @since Jgroup 0.8 39 */ 40 public class MarshalOutputStream 41 extends ObjectOutputStream 42 { 43 44 //////////////////////////////////////////////////////////////////////////////////////////// 45 // Constructors 46 //////////////////////////////////////////////////////////////////////////////////////////// 47 48 /** 49 * Provide a way for subclasses that are completely reimplementing 50 * MarshalOutputStream to not have to allocate private data just used by 51 * this implementation of ObjectOutputStream. 52 * 53 * <p>If there is a security manager installed, this method first calls the 54 * security manager's <code>checkPermission</code> method with a 55 * <code>SerializablePermission("enableSubclassImplementation")</code> 56 * permission to ensure it's ok to enable subclassing. 57 * 58 * @exception IOException Thrown if not called by a subclass. 59 * 60 * @throws SecurityException 61 * if a security manager exists and its 62 * <code>checkPermission</code> method denies 63 * enabling subclassing. 64 * 65 */ 66 protected MarshalOutputStream() 67 throws IOException 68 { 69 super(); 70 enableReplaceObject(true); 71 } 72 73 /** 74 * 75 */ 76 public MarshalOutputStream(OutputStream out) 77 throws IOException 78 { 79 super(out); 80 enableReplaceObject(true); 81 } 82 83 84 //////////////////////////////////////////////////////////////////////////////////////////// 85 // Methods 86 //////////////////////////////////////////////////////////////////////////////////////////// 87 88 /** 89 * If the specified object is a Remote object, substitutes it with 90 * a marshalled form of it. 91 * 92 */ 93 protected Object replaceObject(Object obj) 94 throws IOException 95 { 96 if (obj instanceof Remote) 97 obj = new MarshalledObject(obj); 98 return obj; 99 } 100 101 } // END MarshalOutputStream 102 103 104