KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > ExtensionClassLoader


1 /*
2  * ExtensionClassLoader.java
3  *
4  * Copyright (C) 1998-2002 Peter Graves
5  * $Id: ExtensionClassLoader.java,v 1.2 2003/06/29 00:19:34 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * 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 JavaDoc;
25
26 public final class ExtensionClassLoader extends ClassLoader JavaDoc
27 {
28     public Class JavaDoc loadClass(String JavaDoc s, boolean resolve) throws ClassNotFoundException JavaDoc
29     {
30         try {
31             File file = null;
32             String JavaDoc classname = null;
33             Class JavaDoc c = null;
34             if (s.endsWith(".class")) {
35                 // String passed in is a file name, not a class name.
36
// By default, extension classes are in ~/.j
37
file = File.getInstance(Directories.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                     }
48                     catch (Exception JavaDoc e) {}
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.getEditorDirectory(),
54                                             classname.concat(".class"));
55                 }
56             }
57             if (c == null) {
58                 Log.debug("looking for extension class in file " + file.getCanonicalPath());
59                 if (file.isFile()) {
60                     long length = file.length();
61                     if (length < Integer.MAX_VALUE) {
62                         byte[] classbytes = new byte[(int)length];
63                         DataInputStream JavaDoc in = new DataInputStream JavaDoc(file.getInputStream());
64                         in.readFully(classbytes);
65                         in.close();
66                         c = defineClass(classname, classbytes, 0, (int) length);
67                     }
68                 } else
69                     Log.debug("not found " + file.getCanonicalPath());
70             }
71             if (c != null && resolve)
72                 resolveClass(c);
73             return c;
74         }
75         catch (Exception JavaDoc e) {
76             throw new ClassNotFoundException JavaDoc(e.toString());
77         }
78     }
79 }
80
Popular Tags