01: /* ***** BEGIN LICENSE BLOCK *****
02: * Version: MPL 1.1/GPL 2.0
03: *
04: * The contents of this file are subject to the Mozilla Public License Version
05: * 1.1 (the "License"); you may not use this file except in compliance with
06: * the License. You may obtain a copy of the License at
07: * http://www.mozilla.org/MPL/
08: *
09: * Software distributed under the License is distributed on an "AS IS" basis,
10: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11: * for the specific language governing rights and limitations under the
12: * License.
13: *
14: * The Original Code is Rhino code, released
15: * May 6, 1999.
16: *
17: * The Initial Developer of the Original Code is
18: * Netscape Communications Corporation.
19: * Portions created by the Initial Developer are Copyright (C) 1997-1999
20: * the Initial Developer. All Rights Reserved.
21: *
22: * Contributor(s):
23: * Norris Boyd
24: * Roger Lawrence
25: * Patrick Beard
26: * Igor Bukanov
27: *
28: * Alternatively, the contents of this file may be used under the terms of
29: * the GNU General Public License Version 2 or later (the "GPL"), in which
30: * case the provisions of the GPL are applicable instead of those above. If
31: * you wish to allow use of your version of this file only under the terms of
32: * the GPL and not to allow others to use your version of this file under the
33: * MPL, indicate your decision by deleting the provisions above and replacing
34: * them with the notice and other provisions required by the GPL. If you do
35: * not delete the provisions above, a recipient may use your version of this
36: * file under either the MPL or the GPL.
37: *
38: * ***** END LICENSE BLOCK ***** */
39:
40: package org.mozilla.javascript;
41:
42: /**
43: * Load generated classes.
44: *
45: * @author Norris Boyd
46: */
47: public class DefiningClassLoader extends ClassLoader implements
48: GeneratedClassLoader {
49: public DefiningClassLoader() {
50: this .parentLoader = getClass().getClassLoader();
51: }
52:
53: public DefiningClassLoader(ClassLoader parentLoader) {
54: this .parentLoader = parentLoader;
55: }
56:
57: public Class defineClass(String name, byte[] data) {
58: // Use our own protection domain for the generated classes.
59: // TODO: we might want to use a separate protection domain for classes
60: // compiled from scripts, based on where the script was loaded from.
61: return super .defineClass(name, data, 0, data.length,
62: SecurityUtilities.getProtectionDomain(getClass()));
63: }
64:
65: public void linkClass(Class cl) {
66: resolveClass(cl);
67: }
68:
69: public Class loadClass(String name, boolean resolve)
70: throws ClassNotFoundException {
71: Class cl = findLoadedClass(name);
72: if (cl == null) {
73: if (parentLoader != null) {
74: cl = parentLoader.loadClass(name);
75: } else {
76: cl = findSystemClass(name);
77: }
78: }
79: if (resolve) {
80: resolveClass(cl);
81: }
82: return cl;
83: }
84:
85: private final ClassLoader parentLoader;
86: }
|