01: /*
02: * ExtensionClassLoader.java
03: *
04: * Copyright (C) 1998-2002 Peter Graves
05: * $Id: ExtensionClassLoader.java,v 1.2 2003/06/29 00:19:34 piso Exp $
06: *
07: * This program is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU General Public License
09: * as published by the Free Software Foundation; either version 2
10: * of the License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public License
18: * along with this program; if not, write to the Free Software
19: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20: */
21:
22: package org.armedbear.j;
23:
24: import java.io.DataInputStream;
25:
26: public final class ExtensionClassLoader extends ClassLoader {
27: public Class loadClass(String s, boolean resolve)
28: throws ClassNotFoundException {
29: try {
30: File file = null;
31: String classname = null;
32: Class c = null;
33: if (s.endsWith(".class")) {
34: // String passed in is a file name, not a class name.
35: // By default, extension classes are in ~/.j
36: file = File.getInstance(Directories
37: .getEditorDirectory(), s);
38: } else {
39: // Must be class name.
40: classname = s;
41: }
42: if (classname != null) {
43: c = findLoadedClass(classname);
44: if (c == null) {
45: try {
46: c = findSystemClass(classname);
47: } catch (Exception e) {
48: }
49: }
50: if (c == null) {
51: // We did not find it. Look for a .class file in ~/.j
52: Debug.assertTrue(file == null);
53: file = File.getInstance(Directories
54: .getEditorDirectory(), classname
55: .concat(".class"));
56: }
57: }
58: if (c == null) {
59: Log.debug("looking for extension class in file "
60: + file.getCanonicalPath());
61: if (file.isFile()) {
62: long length = file.length();
63: if (length < Integer.MAX_VALUE) {
64: byte[] classbytes = new byte[(int) length];
65: DataInputStream in = new DataInputStream(file
66: .getInputStream());
67: in.readFully(classbytes);
68: in.close();
69: c = defineClass(classname, classbytes, 0,
70: (int) length);
71: }
72: } else
73: Log.debug("not found " + file.getCanonicalPath());
74: }
75: if (c != null && resolve)
76: resolveClass(c);
77: return c;
78: } catch (Exception e) {
79: throw new ClassNotFoundException(e.toString());
80: }
81: }
82: }
|