"Understanding the Java Classloading Mechanism"
Vol. 8, Issue 8, p. 16
Listing 1
1. public class CustomClassLoader
2. extends ClassLoader {
3. //search repository
4. private List classRepository;
5. public CustomClassLoader
6. (ClassLoader parent,String searchPath)
7. {S}
8. protected Class findClass(String
9. className) throws
10. ClassNotFoundException {
11. byte[] classBytes =
12. loadFromCustomRepository(className);
13. if(classBytes != null) {
14. return defineClass
15. (className,classBytes,0,classBytes.length);
16. }
17. //else
18. throw new ClassNotFoundException(className);
19. }
20. }
Listing 2
1. public Class loadClass(String name)
2. throws ClassNotFoundException
3. {
4. //check if the class is already loaded
5. Class loadedClass = findLoadedClass(name);
6. if (loadedClass == null) {
7. //search for class in local
8. //repository before delegating
9. .......
10. //if class not found delegate to parent
11. loadedClass =
13. this.getClass().getClassLoader().loadClass(name);
13. }
14. return loadedClass;
15. }
|