2
|
1 |
/*
|
|
2 |
* Copyright 1996-2004 Sun Microsystems, Inc. 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. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any 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() {
|
|
68 |
public Object 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 |
protected final Object replaceObject(Object obj) throws IOException {
|
|
80 |
if ((obj instanceof Remote) && !(obj instanceof RemoteStub)) {
|
|
81 |
Target target = ObjectTable.getTarget((Remote) obj);
|
|
82 |
if (target != null) {
|
|
83 |
return target.getStub();
|
|
84 |
}
|
|
85 |
}
|
|
86 |
return obj;
|
|
87 |
}
|
|
88 |
|
|
89 |
/**
|
|
90 |
* Serializes a location from which to load the the specified class.
|
|
91 |
*/
|
|
92 |
protected void annotateClass(Class<?> cl) throws IOException {
|
|
93 |
writeLocation(java.rmi.server.RMIClassLoader.getClassAnnotation(cl));
|
|
94 |
}
|
|
95 |
|
|
96 |
/**
|
|
97 |
* Serializes a location from which to load the specified class.
|
|
98 |
*/
|
|
99 |
protected void annotateProxyClass(Class<?> cl) throws IOException {
|
|
100 |
annotateClass(cl);
|
|
101 |
}
|
|
102 |
|
|
103 |
/**
|
|
104 |
* Writes the location for the class into the stream. This method can
|
|
105 |
* be overridden by subclasses that store this annotation somewhere
|
|
106 |
* else than as the next object in the stream, as is done by this class.
|
|
107 |
*/
|
|
108 |
protected void writeLocation(String location) throws IOException {
|
|
109 |
writeObject(location);
|
|
110 |
}
|
|
111 |
}
|