01: // The UMLet source code is distributed under the terms of the GPL; see license.txt
02: /*
03: * Created on 10.08.2004
04: *
05: * TODO To change the template for this generated file go to
06: * Window - Preferences - Java - Code Style - Code Templates
07: */
08: package com.umlet.control.io;
09:
10: import java.io.*;
11:
12: import com.umlet.control.Umlet;
13:
14: /**
15: * @author Ludwig
16: *
17: * TODO To change the template for this generated type comment go to
18: * Window - Preferences - Java - Code Style - Code Templates
19: */
20: public class FileClassLoader extends ClassLoader {
21: private String classFilePath; //fully qualified path to the class file (com/umlet/element/base/Note.class)
22:
23: public FileClassLoader(String path) {
24: if (path != null)
25: this .classFilePath = path;
26: else
27: throw new IllegalArgumentException(
28: "UMLet -> FileClassLoader -> FileClassLoader(): path must not be null");
29: }
30:
31: protected Class loadClass(String className, boolean resolveClassName)
32: throws ClassNotFoundException {
33: Class c = null;
34: if (!classFilePath.endsWith(className + ".class")) {
35: if (className.indexOf("umlet") < 0)
36: c = findSystemClass(className); //get system-classes like java.lang.Object
37: }
38: if (c == null) {
39: c = findLoadedClass(className); //cache
40: }
41: if (c == null && !classFilePath.endsWith(className + ".class")) {
42: c = java.lang.Class.forName(className); //get project classes like com.umlet.element.base.Entity
43: }
44: if (c == null) {
45: try {
46: byte data[] = loadClassData();
47: c = defineClass(className, data, 0, data.length);
48: if (c == null)
49: throw new ClassNotFoundException(className);
50: } catch (IOException e) {
51: throw new ClassNotFoundException("Error reading file: "
52: + className + ".class");
53: }
54: }
55: if (resolveClassName)
56: resolveClass(c);
57: return c;
58: }
59:
60: private byte[] loadClassData() throws IOException {
61: File f = new File(classFilePath);
62: byte buff[] = new byte[(int) f.length()];
63: FileInputStream fis = new FileInputStream(f);
64: DataInputStream dis = new DataInputStream(fis);
65: dis.readFully(buff);
66: dis.close();
67: return buff;
68: }
69: }
|