01: /*
02: * Copyright 2004,2004 The Apache Software Foundation.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.apache.bsf.util;
18:
19: import java.io.File;
20: import java.io.FileInputStream;
21: import java.util.Hashtable;
22:
23: /**
24: * This class loader knows to load a class from the tempDir dir
25: * of the environment of the given manager.
26: *
27: * @author Sanjiva Weerawarana
28: */
29: class BSFClassLoader extends ClassLoader {
30: Hashtable cache = new Hashtable();
31: String tempDir = ".";
32:
33: // note the non-public constructor - this is only avail within
34: // this package.
35: BSFClassLoader() {
36: }
37:
38: public synchronized Class loadClass(String name, boolean resolve)
39: throws ClassNotFoundException {
40: Class c = (Class) cache.get(name);
41: if (c == null) {
42: // is it a system class
43: try {
44: c = findSystemClass(name);
45: cache.put(name, c);
46: return c;
47: } catch (ClassNotFoundException e) {
48: // nope
49: }
50: try {
51: byte[] data = loadClassData(name);
52: c = defineClass(name, data, 0, data.length);
53: cache.put(name, c);
54: } catch (Exception e) {
55: e.printStackTrace();
56: throw new ClassNotFoundException(
57: "unable to resolve class '" + name + "'");
58: }
59: }
60: if (resolve)
61: resolveClass(c);
62: return c;
63: }
64:
65: private byte[] loadClassData(String name) throws Exception {
66: String fileName = tempDir + File.separatorChar + name
67: + ".class";
68: FileInputStream fi = new FileInputStream(fileName);
69: byte[] data = new byte[fi.available()];
70: fi.read(data);
71: fi.close();
72: return data;
73: }
74:
75: public void setTempDir(String tempDir) {
76: this.tempDir = tempDir;
77: }
78: }
|