author | lana |
Thu, 26 Dec 2013 12:04:16 -0800 | |
changeset 23010 | 6dadb192ad81 |
parent 16872 | f5a07dbe9b1a |
child 23333 | b0af2c7c8c91 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
23010
6dadb192ad81
8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents:
16872
diff
changeset
|
2 |
* Copyright (c) 1996, 2013, 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 sun.rmi.server; |
|
27 |
||
28 |
import java.io.IOException; |
|
29 |
import java.io.InputStream; |
|
30 |
import java.io.ObjectInputStream; |
|
31 |
import java.io.ObjectStreamClass; |
|
32 |
import java.io.StreamCorruptedException; |
|
33 |
import java.net.URL; |
|
34 |
import java.util.*; |
|
35 |
import java.security.AccessControlException; |
|
36 |
import java.security.Permission; |
|
37 |
||
38 |
import java.rmi.server.RMIClassLoader; |
|
39 |
||
40 |
/** |
|
41 |
* MarshalInputStream is an extension of ObjectInputStream. When resolving |
|
42 |
* a class, it reads an object from the stream written by a corresponding |
|
43 |
* MarshalOutputStream. If the class to be resolved is not available |
|
44 |
* locally, from the first class loader on the execution stack, or from the |
|
45 |
* context class loader of the current thread, it will attempt to load the |
|
46 |
* class from the location annotated by the sending MarshalOutputStream. |
|
47 |
* This location object must be a string representing a path of URLs. |
|
48 |
* |
|
49 |
* A new MarshalInputStream should be created to deserialize remote objects or |
|
50 |
* graphs containing remote objects. Objects are created from the stream |
|
51 |
* using the ObjectInputStream.readObject method. |
|
52 |
* |
|
53 |
* @author Peter Jones |
|
54 |
*/ |
|
55 |
public class MarshalInputStream extends ObjectInputStream { |
|
56 |
||
57 |
/** |
|
16872 | 58 |
* Value of "java.rmi.server.useCodebaseOnly" property, |
2 | 59 |
* as cached at class initialization time. |
16872 | 60 |
* |
61 |
* The default value is true. That is, the value is true |
|
62 |
* if the property is absent or is not equal to "false". |
|
63 |
* The value is only false when the property is present |
|
64 |
* and is equal to "false". |
|
2 | 65 |
*/ |
66 |
private static final boolean useCodebaseOnlyProperty = |
|
16872 | 67 |
! java.security.AccessController.doPrivileged( |
68 |
new sun.security.action.GetPropertyAction( |
|
69 |
"java.rmi.server.useCodebaseOnly", "true")) |
|
70 |
.equalsIgnoreCase("false"); |
|
2 | 71 |
|
72 |
/** table to hold sun classes to which access is explicitly permitted */ |
|
51 | 73 |
protected static Map<String, Class<?>> permittedSunClasses |
12040
558b0e0d5910
7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents:
10799
diff
changeset
|
74 |
= new HashMap<>(3); |
2 | 75 |
|
76 |
/** if true, don't try superclass first in resolveClass() */ |
|
77 |
private boolean skipDefaultResolveClass = false; |
|
78 |
||
79 |
/** callbacks to make when done() called: maps Object to Runnable */ |
|
51 | 80 |
private final Map<Object, Runnable> doneCallbacks |
12040
558b0e0d5910
7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents:
10799
diff
changeset
|
81 |
= new HashMap<>(3); |
2 | 82 |
|
83 |
/** |
|
84 |
* if true, load classes (if not available locally) only from the |
|
85 |
* URL specified by the "java.rmi.server.codebase" property. |
|
86 |
*/ |
|
87 |
private boolean useCodebaseOnly = useCodebaseOnlyProperty; |
|
88 |
||
89 |
/* |
|
90 |
* Fix for 4179055: The remote object services inside the |
|
91 |
* activation daemon use stubs that are in the package |
|
92 |
* sun.rmi.server. Classes for these stubs should be loaded from |
|
93 |
* the classpath by RMI system code and not by the normal |
|
94 |
* unmarshalling process as applications should not need to have |
|
95 |
* permission to access the sun implementation classes. |
|
96 |
* |
|
97 |
* Note: this fix should be redone when API changes may be |
|
98 |
* integrated |
|
99 |
* |
|
100 |
* During parameter unmarshalling RMI needs to explicitly permit |
|
101 |
* access to three sun.* stub classes |
|
102 |
*/ |
|
103 |
static { |
|
104 |
try { |
|
105 |
String system = |
|
106 |
"sun.rmi.server.Activation$ActivationSystemImpl_Stub"; |
|
107 |
String registry = "sun.rmi.registry.RegistryImpl_Stub"; |
|
108 |
||
109 |
permittedSunClasses.put(system, Class.forName(system)); |
|
110 |
permittedSunClasses.put(registry, Class.forName(registry)); |
|
111 |
||
112 |
} catch (ClassNotFoundException e) { |
|
113 |
throw new NoClassDefFoundError("Missing system class: " + |
|
114 |
e.getMessage()); |
|
115 |
} |
|
116 |
} |
|
117 |
||
118 |
/** |
|
119 |
* Create a new MarshalInputStream object. |
|
120 |
*/ |
|
121 |
public MarshalInputStream(InputStream in) |
|
122 |
throws IOException, StreamCorruptedException |
|
123 |
{ |
|
124 |
super(in); |
|
125 |
} |
|
126 |
||
127 |
/** |
|
128 |
* Returns a callback previously registered via the setDoneCallback |
|
129 |
* method with given key, or null if no callback has yet been registered |
|
130 |
* with that key. |
|
131 |
*/ |
|
132 |
public Runnable getDoneCallback(Object key) { |
|
51 | 133 |
return doneCallbacks.get(key); // not thread-safe |
2 | 134 |
} |
135 |
||
136 |
/** |
|
137 |
* Registers a callback to make when this stream's done() method is |
|
138 |
* invoked, along with a key for retrieving the same callback instance |
|
139 |
* subsequently from the getDoneCallback method. |
|
140 |
*/ |
|
141 |
public void setDoneCallback(Object key, Runnable callback) { |
|
142 |
//assert(!doneCallbacks.contains(key)); |
|
143 |
doneCallbacks.put(key, callback); // not thread-safe |
|
144 |
} |
|
145 |
||
146 |
/** |
|
147 |
* Indicates that the user of this MarshalInputStream is done reading |
|
148 |
* objects from it, so all callbacks registered with the setDoneCallback |
|
149 |
* method should now be (synchronously) executed. When this method |
|
150 |
* returns, there are no more callbacks registered. |
|
151 |
* |
|
152 |
* This method is implicitly invoked by close() before it delegates to |
|
153 |
* the superclass's close method. |
|
154 |
*/ |
|
155 |
public void done() { |
|
51 | 156 |
Iterator<Runnable> iter = doneCallbacks.values().iterator(); |
2 | 157 |
while (iter.hasNext()) { // not thread-safe |
51 | 158 |
Runnable callback = iter.next(); |
2 | 159 |
callback.run(); |
160 |
} |
|
161 |
doneCallbacks.clear(); |
|
162 |
} |
|
163 |
||
164 |
/** |
|
165 |
* Closes this stream, implicitly invoking done() first. |
|
166 |
*/ |
|
167 |
public void close() throws IOException { |
|
168 |
done(); |
|
169 |
super.close(); |
|
170 |
} |
|
171 |
||
172 |
/** |
|
173 |
* resolveClass is extended to acquire (if present) the location |
|
174 |
* from which to load the specified class. |
|
175 |
* It will find, load, and return the class. |
|
176 |
*/ |
|
12040
558b0e0d5910
7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents:
10799
diff
changeset
|
177 |
protected Class<?> resolveClass(ObjectStreamClass classDesc) |
2 | 178 |
throws IOException, ClassNotFoundException |
179 |
{ |
|
180 |
/* |
|
181 |
* Always read annotation written by MarshalOutputStream |
|
182 |
* describing where to load class from. |
|
183 |
*/ |
|
184 |
Object annotation = readLocation(); |
|
185 |
||
186 |
String className = classDesc.getName(); |
|
187 |
||
188 |
/* |
|
189 |
* Unless we were told to skip this consideration, choose the |
|
190 |
* "default loader" to simulate the default ObjectInputStream |
|
191 |
* resolveClass mechanism (that is, choose the first non-null |
|
192 |
* loader on the execution stack) to maximize the likelihood of |
|
193 |
* type compatibility with calling code. (This consideration |
|
194 |
* is skipped during server parameter unmarshalling using the 1.2 |
|
195 |
* stub protocol, because there would never be a non-null class |
|
196 |
* loader on the stack in that situation anyway.) |
|
197 |
*/ |
|
198 |
ClassLoader defaultLoader = |
|
199 |
skipDefaultResolveClass ? null : latestUserDefinedLoader(); |
|
200 |
||
201 |
/* |
|
202 |
* If the "java.rmi.server.useCodebaseOnly" property was true or |
|
203 |
* useCodebaseOnly() was called or the annotation is not a String, |
|
204 |
* load from the local loader using the "java.rmi.server.codebase" |
|
205 |
* URL. Otherwise, load from a loader using the codebase URL in |
|
206 |
* the annotation. |
|
207 |
*/ |
|
208 |
String codebase = null; |
|
209 |
if (!useCodebaseOnly && annotation instanceof String) { |
|
210 |
codebase = (String) annotation; |
|
211 |
} |
|
212 |
||
213 |
try { |
|
214 |
return RMIClassLoader.loadClass(codebase, className, |
|
215 |
defaultLoader); |
|
216 |
} catch (AccessControlException e) { |
|
217 |
return checkSunClass(className, e); |
|
218 |
} catch (ClassNotFoundException e) { |
|
219 |
/* |
|
220 |
* Fix for 4442373: delegate to ObjectInputStream.resolveClass() |
|
221 |
* to resolve primitive classes. |
|
222 |
*/ |
|
223 |
try { |
|
224 |
if (Character.isLowerCase(className.charAt(0)) && |
|
225 |
className.indexOf('.') == -1) |
|
226 |
{ |
|
227 |
return super.resolveClass(classDesc); |
|
228 |
} |
|
229 |
} catch (ClassNotFoundException e2) { |
|
230 |
} |
|
231 |
throw e; |
|
232 |
} |
|
233 |
} |
|
234 |
||
235 |
/** |
|
236 |
* resolveProxyClass is extended to acquire (if present) the location |
|
237 |
* to determine the class loader to define the proxy class in. |
|
238 |
*/ |
|
12040
558b0e0d5910
7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents:
10799
diff
changeset
|
239 |
protected Class<?> resolveProxyClass(String[] interfaces) |
2 | 240 |
throws IOException, ClassNotFoundException |
241 |
{ |
|
242 |
/* |
|
243 |
* Always read annotation written by MarshalOutputStream. |
|
244 |
*/ |
|
245 |
Object annotation = readLocation(); |
|
246 |
||
247 |
ClassLoader defaultLoader = |
|
248 |
skipDefaultResolveClass ? null : latestUserDefinedLoader(); |
|
249 |
||
250 |
String codebase = null; |
|
251 |
if (!useCodebaseOnly && annotation instanceof String) { |
|
252 |
codebase = (String) annotation; |
|
253 |
} |
|
254 |
||
255 |
return RMIClassLoader.loadProxyClass(codebase, interfaces, |
|
256 |
defaultLoader); |
|
257 |
} |
|
258 |
||
259 |
/* |
|
260 |
* Returns the first non-null class loader up the execution stack, or null |
|
261 |
* if only code from the null class loader is on the stack. |
|
262 |
*/ |
|
10797
f51518b30c84
7104209: Cleanup and remove librmi (native library)
chegar
parents:
5506
diff
changeset
|
263 |
private static ClassLoader latestUserDefinedLoader() { |
f51518b30c84
7104209: Cleanup and remove librmi (native library)
chegar
parents:
5506
diff
changeset
|
264 |
return sun.misc.VM.latestUserDefinedLoader(); |
f51518b30c84
7104209: Cleanup and remove librmi (native library)
chegar
parents:
5506
diff
changeset
|
265 |
} |
2 | 266 |
|
267 |
/** |
|
268 |
* Fix for 4179055: Need to assist resolving sun stubs; resolve |
|
269 |
* class locally if it is a "permitted" sun class |
|
270 |
*/ |
|
12040
558b0e0d5910
7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents:
10799
diff
changeset
|
271 |
private Class<?> checkSunClass(String className, AccessControlException e) |
2 | 272 |
throws AccessControlException |
273 |
{ |
|
274 |
// ensure that we are giving out a stub for the correct reason |
|
275 |
Permission perm = e.getPermission(); |
|
276 |
String name = null; |
|
277 |
if (perm != null) { |
|
278 |
name = perm.getName(); |
|
279 |
} |
|
280 |
||
51 | 281 |
Class<?> resolvedClass = permittedSunClasses.get(className); |
2 | 282 |
|
283 |
// if class not permitted, throw the SecurityException |
|
284 |
if ((name == null) || |
|
285 |
(resolvedClass == null) || |
|
286 |
((!name.equals("accessClassInPackage.sun.rmi.server")) && |
|
287 |
(!name.equals("accessClassInPackage.sun.rmi.registry")))) |
|
288 |
{ |
|
289 |
throw e; |
|
290 |
} |
|
291 |
||
292 |
return resolvedClass; |
|
293 |
} |
|
294 |
||
295 |
/** |
|
296 |
* Return the location for the class in the stream. This method can |
|
297 |
* be overridden by subclasses that store this annotation somewhere |
|
298 |
* else than as the next object in the stream, as is done by this class. |
|
299 |
*/ |
|
300 |
protected Object readLocation() |
|
301 |
throws IOException, ClassNotFoundException |
|
302 |
{ |
|
303 |
return readObject(); |
|
304 |
} |
|
305 |
||
306 |
/** |
|
307 |
* Set a flag to indicate that the superclass's default resolveClass() |
|
308 |
* implementation should not be invoked by our resolveClass(). |
|
309 |
*/ |
|
310 |
void skipDefaultResolveClass() { |
|
311 |
skipDefaultResolveClass = true; |
|
312 |
} |
|
313 |
||
314 |
/** |
|
315 |
* Disable code downloading except from the URL specified by the |
|
316 |
* "java.rmi.server.codebase" property. |
|
317 |
*/ |
|
318 |
void useCodebaseOnly() { |
|
319 |
useCodebaseOnly = true; |
|
320 |
} |
|
321 |
} |