001: /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
002: *
003: * ***** BEGIN LICENSE BLOCK *****
004: * Version: MPL 1.1/GPL 2.0
005: *
006: * The contents of this file are subject to the Mozilla Public License Version
007: * 1.1 (the "License"); you may not use this file except in compliance with
008: * the License. You may obtain a copy of the License at
009: * http://www.mozilla.org/MPL/
010: *
011: * Software distributed under the License is distributed on an "AS IS" basis,
012: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
013: * for the specific language governing rights and limitations under the
014: * License.
015: *
016: * The Original Code is Rhino serialization code, released
017: * Sept. 25, 2001.
018: *
019: * The Initial Developer of the Original Code is
020: * Norris Boyd.
021: * Portions created by the Initial Developer are Copyright (C) 2001
022: * the Initial Developer. All Rights Reserved.
023: *
024: * Contributor(s):
025: * Norris Boyd
026: * Igor Bukanov
027: * Attila Szegedi
028: *
029: * Alternatively, the contents of this file may be used under the terms of
030: * the GNU General Public License Version 2 or later (the "GPL"), in which
031: * case the provisions of the GPL are applicable instead of those above. If
032: * you wish to allow use of your version of this file only under the terms of
033: * the GPL and not to allow others to use your version of this file under the
034: * MPL, indicate your decision by deleting the provisions above and replacing
035: * them with the notice and other provisions required by the GPL. If you do
036: * not delete the provisions above, a recipient may use your version of this
037: * file under either the MPL or the GPL.
038: *
039: * ***** END LICENSE BLOCK ***** */
040:
041: // API class
042: package org.mozilla.javascript.serialize;
043:
044: import java.io.*;
045:
046: import org.mozilla.javascript.*;
047:
048: /**
049: * Class ScriptableInputStream is used to read in a JavaScript
050: * object or function previously serialized with a ScriptableOutputStream.
051: * References to names in the exclusion list
052: * replaced with references to the top-level scope specified during
053: * creation of the ScriptableInputStream.
054: *
055: * @author Norris Boyd
056: */
057:
058: public class ScriptableInputStream extends ObjectInputStream {
059:
060: /**
061: * Create a ScriptableInputStream.
062: * @param in the InputStream to read from.
063: * @param scope the top-level scope to create the object in.
064: */
065: public ScriptableInputStream(InputStream in, Scriptable scope)
066: throws IOException {
067: super (in);
068: this .scope = scope;
069: enableResolveObject(true);
070: Context cx = Context.getCurrentContext();
071: if (cx != null) {
072: this .classLoader = cx.getApplicationClassLoader();
073: }
074: }
075:
076: protected Class resolveClass(ObjectStreamClass desc)
077: throws IOException, ClassNotFoundException {
078: String name = desc.getName();
079: if (classLoader != null) {
080: try {
081: return classLoader.loadClass(name);
082: } catch (ClassNotFoundException ex) {
083: // fall through to default loading
084: }
085: }
086: return super .resolveClass(desc);
087: }
088:
089: protected Object resolveObject(Object obj) throws IOException {
090: if (obj instanceof ScriptableOutputStream.PendingLookup) {
091: String name = ((ScriptableOutputStream.PendingLookup) obj)
092: .getName();
093: obj = ScriptableOutputStream.lookupQualifiedName(scope,
094: name);
095: if (obj == Scriptable.NOT_FOUND) {
096: throw new IOException("Object " + name
097: + " not found upon " + "deserialization.");
098: }
099: } else if (obj instanceof UniqueTag) {
100: obj = ((UniqueTag) obj).readResolve();
101: } else if (obj instanceof Undefined) {
102: obj = ((Undefined) obj).readResolve();
103: }
104: return obj;
105: }
106:
107: private Scriptable scope;
108: private ClassLoader classLoader;
109: }
|