author | zgu |
Fri, 12 Apr 2019 09:55:30 -0400 | |
changeset 54524 | 33fda525ad41 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
43211 | 2 |
* Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.rmi; |
|
27 |
||
28 |
import java.io.ByteArrayInputStream; |
|
29 |
import java.io.ByteArrayOutputStream; |
|
30 |
import java.io.IOException; |
|
31 |
import java.io.InputStream; |
|
43211 | 32 |
import java.io.ObjectInputFilter; |
2 | 33 |
import java.io.ObjectInputStream; |
34 |
import java.io.ObjectOutputStream; |
|
35 |
import java.io.ObjectStreamConstants; |
|
36 |
import java.io.OutputStream; |
|
37 |
import java.io.Serializable; |
|
43211 | 38 |
import java.security.AccessController; |
39 |
import java.security.PrivilegedAction; |
|
40 |
||
2 | 41 |
import sun.rmi.server.MarshalInputStream; |
42 |
import sun.rmi.server.MarshalOutputStream; |
|
43 |
||
44 |
/** |
|
45 |
* A <code>MarshalledObject</code> contains a byte stream with the serialized |
|
46 |
* representation of an object given to its constructor. The <code>get</code> |
|
47 |
* method returns a new copy of the original object, as deserialized from |
|
48 |
* the contained byte stream. The contained object is serialized and |
|
49 |
* deserialized with the same serialization semantics used for marshaling |
|
50 |
* and unmarshaling parameters and return values of RMI calls: When the |
|
51 |
* serialized form is created: |
|
52 |
* |
|
53 |
* <ul> |
|
54 |
* <li> classes are annotated with a codebase URL from where the class |
|
55 |
* can be loaded (if available), and |
|
56 |
* <li> any remote object in the <code>MarshalledObject</code> is |
|
57 |
* represented by a serialized instance of its stub. |
|
58 |
* </ul> |
|
59 |
* |
|
60 |
* <p>When copy of the object is retrieved (via the <code>get</code> method), |
|
61 |
* if the class is not available locally, it will be loaded from the |
|
62 |
* appropriate location (specified the URL annotated with the class descriptor |
|
63 |
* when the class was serialized. |
|
64 |
* |
|
65 |
* <p><code>MarshalledObject</code> facilitates passing objects in RMI calls |
|
66 |
* that are not automatically deserialized immediately by the remote peer. |
|
67 |
* |
|
68 |
* @param <T> the type of the object contained in this |
|
69 |
* <code>MarshalledObject</code> |
|
70 |
* |
|
71 |
* @author Ann Wollrath |
|
72 |
* @author Peter Jones |
|
73 |
* @since 1.2 |
|
74 |
*/ |
|
75 |
public final class MarshalledObject<T> implements Serializable { |
|
76 |
/** |
|
77 |
* @serial Bytes of serialized representation. If <code>objBytes</code> is |
|
78 |
* <code>null</code> then the object marshalled was a <code>null</code> |
|
79 |
* reference. |
|
80 |
*/ |
|
81 |
private byte[] objBytes = null; |
|
82 |
||
83 |
/** |
|
84 |
* @serial Bytes of location annotations, which are ignored by |
|
85 |
* <code>equals</code>. If <code>locBytes</code> is null, there were no |
|
86 |
* non-<code>null</code> annotations during marshalling. |
|
87 |
*/ |
|
88 |
private byte[] locBytes = null; |
|
89 |
||
90 |
/** |
|
91 |
* @serial Stored hash code of contained object. |
|
92 |
* |
|
93 |
* @see #hashCode |
|
94 |
*/ |
|
95 |
private int hash; |
|
96 |
||
43211 | 97 |
/** Filter used when creating the instance from a stream; may be null. */ |
98 |
private transient ObjectInputFilter objectInputFilter = null; |
|
99 |
||
2 | 100 |
/** Indicate compatibility with 1.2 version of class. */ |
101 |
private static final long serialVersionUID = 8988374069173025854L; |
|
102 |
||
103 |
/** |
|
104 |
* Creates a new <code>MarshalledObject</code> that contains the |
|
105 |
* serialized representation of the current state of the supplied object. |
|
106 |
* The object is serialized with the semantics used for marshaling |
|
107 |
* parameters for RMI calls. |
|
108 |
* |
|
109 |
* @param obj the object to be serialized (must be serializable) |
|
110 |
* @exception IOException if an <code>IOException</code> occurs; an |
|
111 |
* <code>IOException</code> may occur if <code>obj</code> is not |
|
112 |
* serializable. |
|
113 |
* @since 1.2 |
|
114 |
*/ |
|
115 |
public MarshalledObject(T obj) throws IOException { |
|
116 |
if (obj == null) { |
|
117 |
hash = 13; |
|
118 |
return; |
|
119 |
} |
|
120 |
||
121 |
ByteArrayOutputStream bout = new ByteArrayOutputStream(); |
|
122 |
ByteArrayOutputStream lout = new ByteArrayOutputStream(); |
|
123 |
MarshalledObjectOutputStream out = |
|
124 |
new MarshalledObjectOutputStream(bout, lout); |
|
125 |
out.writeObject(obj); |
|
126 |
out.flush(); |
|
127 |
objBytes = bout.toByteArray(); |
|
128 |
// locBytes is null if no annotations |
|
129 |
locBytes = (out.hadAnnotations() ? lout.toByteArray() : null); |
|
130 |
||
131 |
/* |
|
132 |
* Calculate hash from the marshalled representation of object |
|
133 |
* so the hashcode will be comparable when sent between VMs. |
|
134 |
*/ |
|
135 |
int h = 0; |
|
136 |
for (int i = 0; i < objBytes.length; i++) { |
|
137 |
h = 31 * h + objBytes[i]; |
|
138 |
} |
|
139 |
hash = h; |
|
140 |
} |
|
141 |
||
142 |
/** |
|
43211 | 143 |
* Reads in the state of the object and saves the stream's |
144 |
* serialization filter to be used when the object is deserialized. |
|
145 |
* |
|
146 |
* @param stream the stream |
|
147 |
* @throws IOException if an I/O error occurs |
|
148 |
* @throws ClassNotFoundException if a class cannot be found |
|
149 |
*/ |
|
150 |
private void readObject(ObjectInputStream stream) |
|
151 |
throws IOException, ClassNotFoundException { |
|
152 |
stream.defaultReadObject(); // read in all fields |
|
153 |
objectInputFilter = stream.getObjectInputFilter(); |
|
154 |
} |
|
155 |
||
156 |
/** |
|
2 | 157 |
* Returns a new copy of the contained marshalledobject. The internal |
158 |
* representation is deserialized with the semantics used for |
|
21278 | 159 |
* unmarshaling parameters for RMI calls. |
43211 | 160 |
* If the MarshalledObject was read from an ObjectInputStream, |
161 |
* the filter from that stream is used to deserialize the object. |
|
2 | 162 |
* |
163 |
* @return a copy of the contained object |
|
164 |
* @exception IOException if an <code>IOException</code> occurs while |
|
165 |
* deserializing the object from its internal representation. |
|
166 |
* @exception ClassNotFoundException if a |
|
167 |
* <code>ClassNotFoundException</code> occurs while deserializing the |
|
168 |
* object from its internal representation. |
|
169 |
* could not be found |
|
170 |
* @since 1.2 |
|
171 |
*/ |
|
172 |
public T get() throws IOException, ClassNotFoundException { |
|
173 |
if (objBytes == null) // must have been a null object |
|
174 |
return null; |
|
175 |
||
176 |
ByteArrayInputStream bin = new ByteArrayInputStream(objBytes); |
|
177 |
// locBytes is null if no annotations |
|
178 |
ByteArrayInputStream lin = |
|
179 |
(locBytes == null ? null : new ByteArrayInputStream(locBytes)); |
|
180 |
MarshalledObjectInputStream in = |
|
43211 | 181 |
new MarshalledObjectInputStream(bin, lin, objectInputFilter); |
11117
b6e68b1344d4
7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents:
5506
diff
changeset
|
182 |
@SuppressWarnings("unchecked") |
2 | 183 |
T obj = (T) in.readObject(); |
184 |
in.close(); |
|
185 |
return obj; |
|
186 |
} |
|
187 |
||
188 |
/** |
|
189 |
* Return a hash code for this <code>MarshalledObject</code>. |
|
190 |
* |
|
191 |
* @return a hash code |
|
192 |
*/ |
|
193 |
public int hashCode() { |
|
194 |
return hash; |
|
195 |
} |
|
196 |
||
197 |
/** |
|
198 |
* Compares this <code>MarshalledObject</code> to another object. |
|
199 |
* Returns true if and only if the argument refers to a |
|
200 |
* <code>MarshalledObject</code> that contains exactly the same |
|
201 |
* serialized representation of an object as this one does. The |
|
202 |
* comparison ignores any class codebase annotation, meaning that |
|
203 |
* two objects are equivalent if they have the same serialized |
|
204 |
* representation <i>except</i> for the codebase of each class |
|
205 |
* in the serialized representation. |
|
206 |
* |
|
207 |
* @param obj the object to compare with this <code>MarshalledObject</code> |
|
21278 | 208 |
* @return <code>true</code> if the argument contains an equivalent |
2 | 209 |
* serialized object; <code>false</code> otherwise |
210 |
* @since 1.2 |
|
211 |
*/ |
|
212 |
public boolean equals(Object obj) { |
|
213 |
if (obj == this) |
|
214 |
return true; |
|
215 |
||
216 |
if (obj != null && obj instanceof MarshalledObject) { |
|
11117
b6e68b1344d4
7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents:
5506
diff
changeset
|
217 |
MarshalledObject<?> other = (MarshalledObject<?>) obj; |
2 | 218 |
|
219 |
// if either is a ref to null, both must be |
|
220 |
if (objBytes == null || other.objBytes == null) |
|
221 |
return objBytes == other.objBytes; |
|
222 |
||
223 |
// quick, easy test |
|
224 |
if (objBytes.length != other.objBytes.length) |
|
225 |
return false; |
|
226 |
||
227 |
//!! There is talk about adding an array comparision method |
|
228 |
//!! at 1.2 -- if so, this should be rewritten. -arnold |
|
229 |
for (int i = 0; i < objBytes.length; ++i) { |
|
230 |
if (objBytes[i] != other.objBytes[i]) |
|
231 |
return false; |
|
232 |
} |
|
233 |
return true; |
|
234 |
} else { |
|
235 |
return false; |
|
236 |
} |
|
237 |
} |
|
238 |
||
239 |
/** |
|
240 |
* This class is used to marshal objects for |
|
241 |
* <code>MarshalledObject</code>. It places the location annotations |
|
242 |
* to one side so that two <code>MarshalledObject</code>s can be |
|
243 |
* compared for equality if they differ only in location |
|
244 |
* annotations. Objects written using this stream should be read back |
|
245 |
* from a <code>MarshalledObjectInputStream</code>. |
|
246 |
* |
|
247 |
* @see java.rmi.MarshalledObject |
|
248 |
* @see MarshalledObjectInputStream |
|
249 |
*/ |
|
250 |
private static class MarshalledObjectOutputStream |
|
251 |
extends MarshalOutputStream |
|
252 |
{ |
|
253 |
/** The stream on which location objects are written. */ |
|
254 |
private ObjectOutputStream locOut; |
|
255 |
||
256 |
/** <code>true</code> if non-<code>null</code> annotations are |
|
257 |
* written. |
|
258 |
*/ |
|
259 |
private boolean hadAnnotations; |
|
260 |
||
261 |
/** |
|
262 |
* Creates a new <code>MarshalledObjectOutputStream</code> whose |
|
263 |
* non-location bytes will be written to <code>objOut</code> and whose |
|
264 |
* location annotations (if any) will be written to |
|
265 |
* <code>locOut</code>. |
|
266 |
*/ |
|
267 |
MarshalledObjectOutputStream(OutputStream objOut, OutputStream locOut) |
|
268 |
throws IOException |
|
269 |
{ |
|
270 |
super(objOut); |
|
271 |
this.useProtocolVersion(ObjectStreamConstants.PROTOCOL_VERSION_2); |
|
272 |
this.locOut = new ObjectOutputStream(locOut); |
|
273 |
hadAnnotations = false; |
|
274 |
} |
|
275 |
||
276 |
/** |
|
277 |
* Returns <code>true</code> if any non-<code>null</code> location |
|
278 |
* annotations have been written to this stream. |
|
279 |
*/ |
|
280 |
boolean hadAnnotations() { |
|
281 |
return hadAnnotations; |
|
282 |
} |
|
283 |
||
284 |
/** |
|
285 |
* Overrides MarshalOutputStream.writeLocation implementation to write |
|
286 |
* annotations to the location stream. |
|
287 |
*/ |
|
288 |
protected void writeLocation(String loc) throws IOException { |
|
289 |
hadAnnotations |= (loc != null); |
|
290 |
locOut.writeObject(loc); |
|
291 |
} |
|
292 |
||
293 |
||
294 |
public void flush() throws IOException { |
|
295 |
super.flush(); |
|
296 |
locOut.flush(); |
|
297 |
} |
|
298 |
} |
|
299 |
||
300 |
/** |
|
301 |
* The counterpart to <code>MarshalledObjectOutputStream</code>. |
|
302 |
* |
|
303 |
* @see MarshalledObjectOutputStream |
|
304 |
*/ |
|
305 |
private static class MarshalledObjectInputStream |
|
306 |
extends MarshalInputStream |
|
307 |
{ |
|
308 |
/** |
|
309 |
* The stream from which annotations will be read. If this is |
|
310 |
* <code>null</code>, then all annotations were <code>null</code>. |
|
311 |
*/ |
|
312 |
private ObjectInputStream locIn; |
|
313 |
||
314 |
/** |
|
315 |
* Creates a new <code>MarshalledObjectInputStream</code> that |
|
316 |
* reads its objects from <code>objIn</code> and annotations |
|
317 |
* from <code>locIn</code>. If <code>locIn</code> is |
|
318 |
* <code>null</code>, then all annotations will be |
|
319 |
* <code>null</code>. |
|
320 |
*/ |
|
43211 | 321 |
MarshalledObjectInputStream(InputStream objIn, InputStream locIn, |
322 |
ObjectInputFilter filter) |
|
2 | 323 |
throws IOException |
324 |
{ |
|
325 |
super(objIn); |
|
326 |
this.locIn = (locIn == null ? null : new ObjectInputStream(locIn)); |
|
43211 | 327 |
if (filter != null) { |
328 |
AccessController.doPrivileged((PrivilegedAction<Void>) () -> { |
|
329 |
MarshalledObjectInputStream.this.setObjectInputFilter(filter); |
|
330 |
if (MarshalledObjectInputStream.this.locIn != null) { |
|
331 |
MarshalledObjectInputStream.this.locIn.setObjectInputFilter(filter); |
|
332 |
} |
|
333 |
return null; |
|
334 |
}); |
|
335 |
} |
|
2 | 336 |
} |
337 |
||
338 |
/** |
|
339 |
* Overrides MarshalInputStream.readLocation to return locations from |
|
340 |
* the stream we were given, or <code>null</code> if we were given a |
|
341 |
* <code>null</code> location stream. |
|
342 |
*/ |
|
343 |
protected Object readLocation() |
|
344 |
throws IOException, ClassNotFoundException |
|
345 |
{ |
|
346 |
return (locIn == null ? null : locIn.readObject()); |
|
347 |
} |
|
348 |
} |
|
349 |
||
350 |
} |