| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
package org.mozilla.javascript.serialize; |
| 10 |
|
| 11 |
import java.io.*; |
| 12 |
|
| 13 |
import org.mozilla.javascript.*; |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
public class ScriptableInputStream extends ObjectInputStream { |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
public ScriptableInputStream(InputStream in, Scriptable scope) |
| 33 |
throws IOException |
| 34 |
{ |
| 35 |
super(in); |
| 36 |
this.scope = scope; |
| 37 |
enableResolveObject(true); |
| 38 |
Context cx = Context.getCurrentContext(); |
| 39 |
if (cx != null) { |
| 40 |
this.classLoader = cx.getApplicationClassLoader(); |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
@Override |
| 45 |
protected Class<?> resolveClass(ObjectStreamClass desc) |
| 46 |
throws IOException, ClassNotFoundException |
| 47 |
{ |
| 48 |
String name = desc.getName(); |
| 49 |
if (classLoader != null) { |
| 50 |
try { |
| 51 |
return classLoader.loadClass(name); |
| 52 |
} catch (ClassNotFoundException ex) { |
| 53 |
|
| 54 |
} |
| 55 |
} |
| 56 |
return super.resolveClass(desc); |
| 57 |
} |
| 58 |
|
| 59 |
@Override |
| 60 |
protected Object resolveObject(Object obj) |
| 61 |
throws IOException |
| 62 |
{ |
| 63 |
if (obj instanceof ScriptableOutputStream.PendingLookup) { |
| 64 |
String name = ((ScriptableOutputStream.PendingLookup)obj).getName(); |
| 65 |
obj = ScriptableOutputStream.lookupQualifiedName(scope, name); |
| 66 |
if (obj == Scriptable.NOT_FOUND) { |
| 67 |
throw new IOException("Object " + name + " not found upon " + |
| 68 |
"deserialization."); |
| 69 |
} |
| 70 |
}else if (obj instanceof UniqueTag) { |
| 71 |
obj = ((UniqueTag)obj).readResolve(); |
| 72 |
}else if (obj instanceof Undefined) { |
| 73 |
obj = ((Undefined)obj).readResolve(); |
| 74 |
} |
| 75 |
return obj; |
| 76 |
} |
| 77 |
|
| 78 |
private Scriptable scope; |
| 79 |
private ClassLoader classLoader; |
| 80 |
} |