01: /**
02: * Based on code from The Java(TM) Developers Almanac 1.4,
03: * Volume 1: Examples and Quick Reference (4th Edition)
04: * by Patrick Chan
05: *
06: * @author garethc
07: * 11/11/2002 12:06:56
08: */package vqwiki;
09:
10: import org.apache.log4j.Logger;
11:
12: import java.io.File;
13: import java.io.FileInputStream;
14: import java.io.IOException;
15: import java.io.InputStream;
16:
17: public class PluginClassLoader extends ClassLoader {
18:
19: private static final Logger logger = Logger
20: .getLogger(PluginClassLoader.class);
21: private String pluginsDir;
22:
23: /**
24: *
25: */
26: public PluginClassLoader(String pluginsDir) {
27: this .pluginsDir = pluginsDir;
28: }
29:
30: /**
31: *
32: */
33: protected Class findClass(String name)
34: throws ClassNotFoundException {
35: byte[] b = new byte[0];
36: try {
37: b = loadClassData(name);
38: } catch (Exception e) {
39: logger.warn(e);
40: }
41: return defineClass(name, b, 0, b.length);
42: }
43:
44: /**
45: *
46: */
47: private byte[] loadClassData(String name) throws Exception {
48: File file = new File(this .pluginsDir, name + ".class");
49: InputStream is = new FileInputStream(file);
50: // Get the size of the file
51: long length = file.length();
52: // You cannot create an array using a long type.
53: // It needs to be an int type.
54: // Before converting to an int type, check
55: // to ensure that file is not larger than Integer.MAX_VALUE.
56: if (length > Integer.MAX_VALUE) {
57: // File is too large
58: }
59: // Create the byte array to hold the data
60: byte[] bytes = new byte[(int) length];
61: // Read in the bytes
62: int offset = 0;
63: int numRead = 0;
64: while (offset < bytes.length
65: && (numRead = is.read(bytes, offset, bytes.length
66: - offset)) >= 0) {
67: offset += numRead;
68: }
69: // Ensure all the bytes have been read in
70: if (offset < bytes.length) {
71: throw new IOException("Could not completely read file "
72: + file.getName());
73: }
74: // Close the input stream and return bytes
75: is.close();
76: return bytes;
77: }
78: }
79:
80: // $Log$
81: // Revision 1.6 2006/04/23 07:52:28 wrh2
82: // Coding style updates (VQW-73).
83: //
84: // Revision 1.5 2003/10/05 05:07:30 garethc
85: // fixes and admin file encoding option + merge with contributions
86: //
87: // Revision 1.4 2003/04/15 08:41:31 mrgadget4711
88: // ADD: Lucene search
89: // ADD: RSS Stream
90: //
91: // Revision 1.3 2003/01/07 03:11:52 garethc
92: // beginning of big cleanup, taglibs etc
93: //
94: // Revision 1.2 2002/12/08 20:58:58 garethc
95: // 2.3.6 almost ready
96: //
97: // Revision 1.1 2002/11/10 23:53:02 garethc
98: // manual and plugins
99: //
|