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.InputStream;
23 import java.io.ObjectInputStream;
24 import java.rmi.MarshalException;
25 import java.rmi.MarshalledObject;
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 MarshalInputStream
41 extends ObjectInputStream
42 {
43
44 ////////////////////////////////////////////////////////////////////////////////////////////
45 // Constructors
46 ////////////////////////////////////////////////////////////////////////////////////////////
47
48 /**
49 * Provide a way for subclasses that are completely reimplementing
50 * MarshalInputStream to not have to allocate private data just used by
51 * this implementation of ObjectInputStream.
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 MarshalInputStream()
67 throws IOException
68 {
69 super();
70 enableResolveObject(true);
71 }
72
73 /**
74 *
75 */
76 public MarshalInputStream(InputStream out)
77 throws IOException
78 {
79 super(out);
80 enableResolveObject(true);
81 }
82
83
84 ////////////////////////////////////////////////////////////////////////////////////////////
85 // Methods
86 ////////////////////////////////////////////////////////////////////////////////////////////
87
88 /**
89 * If the specified object is a marshalled object, extracts the
90 * contained object from it.
91 */
92 protected Object resolveObject(Object obj)
93 throws IOException
94 {
95 try {
96 return ((MarshalledObject) obj).get();
97 } catch (ClassCastException e) {
98 return obj;
99 } catch (ClassNotFoundException e) {
100 throw new MarshalException("resolveObject: ", e);
101 }
102 }
103
104 } // END MarshalInputStream
105
106
107