KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgap > util > MultiClassLoader


1 /*
2  * This file is part of JGAP.
3  *
4  * JGAP offers a dual license model containing the LGPL as well as the MPL.
5  *
6  * For licencing information please see the file license.txt included with JGAP
7  * or have a look at the top of class org.jgap.Chromosome which representatively
8  * includes the JGAP license policy applicable for any file delivered with JGAP.
9  */

10 package org.jgap.util;
11
12 import java.util.*;
13
14 /**
15  * A simple test class loader capable of loading from
16  * multiple sources, such as local files or a URL.
17  *
18  * This class is derived from an article by Chuck McManis
19  * http://www.javaworld.com/javaworld/jw-10-1996/indepth.src.html
20  * with large modifications.
21  *
22  * Note that this has been updated to use the non-deprecated version of
23  * defineClass() -- JDM.
24  *
25  * @author Jack Harich - 8/18/97
26  * @author John D. Mitchell - 99.03.04
27  * @author Klaus Meffert (integrated into JGAP)
28  *
29  * @since 3.2
30  */

31 public abstract class MultiClassLoader
32     extends ClassLoader JavaDoc {
33   /** String containing the CVS revision. Read out via reflection!*/
34   private final static String JavaDoc CVS_REVISION = "$Revision: 1.1 $";
35
36 //---------- Fields --------------------------------------
37
private Hashtable classes = new Hashtable();
38
39   private char classNameReplacementChar;
40
41   protected boolean monitorOn = false;
42
43   protected boolean sourceMonitorOn = true;
44
45   public MultiClassLoader() {
46   }
47
48   /**
49    * This is a simple version for external clients since they
50    * will always want the class resolved before it is returned
51    * to them.
52    */

53   public Class JavaDoc loadClass(String JavaDoc className)
54       throws ClassNotFoundException JavaDoc {
55     return (loadClass(className, true));
56   }
57
58   public synchronized Class JavaDoc loadClass(String JavaDoc className,
59                                       boolean resolveIt)
60       throws ClassNotFoundException JavaDoc {
61     Class JavaDoc result;
62     byte[] classBytes;
63     monitor(">> MultiClassLoader.loadClass(" + className + ", " + resolveIt +
64             ")");
65     //----- Check our local cache of classes
66
result = (Class JavaDoc) classes.get(className);
67     if (result != null) {
68       monitor(">> returning cached result.");
69       return result;
70     }
71     //----- Check with the primordial class loader
72
try {
73       result = super.findSystemClass(className);
74       monitor(">> returning system class (in CLASSPATH).");
75       return result;
76     } catch (ClassNotFoundException JavaDoc e) {
77       monitor(">> Not a system class.");
78     }
79     //----- Try to load it from preferred source
80
// Note loadClassBytes() is an abstract method
81
classBytes = loadClassBytes(className);
82     if (classBytes == null) {
83       throw new ClassNotFoundException JavaDoc();
84     }
85     //----- Define it (parse the class file)
86
result = defineClass(className, classBytes, 0, classBytes.length);
87     if (result == null) {
88       throw new ClassFormatError JavaDoc();
89     }
90     //----- Resolve if necessary
91
if (resolveIt)
92       resolveClass(result);
93     // Done
94
classes.put(className, result);
95     monitor(">> Returning newly loaded class.");
96     return result;
97   }
98
99   /**
100    * This optional call allows a class name such as
101    * "COM.test.Hello" to be changed to "COM_test_Hello",
102    * which is useful for storing classes from different
103    * packages in the same retrival directory.
104    * In the above example the char would be '_'.
105    */

106   public void setClassNameReplacementChar(char replacement) {
107     classNameReplacementChar = replacement;
108   }
109
110   protected abstract byte[] loadClassBytes(String JavaDoc className);
111
112   protected String JavaDoc formatClassName(String JavaDoc className) {
113     if (classNameReplacementChar == '\u0000') {
114       // '/' is used to map the package to the path
115
return className.replace('.', '/') + ".class";
116     }
117     else {
118       // Replace '.' with custom char, such as '_'
119
return className.replace('.',
120                                classNameReplacementChar) + ".class";
121     }
122   }
123
124   protected void monitor(String JavaDoc text) {
125     if (monitorOn)
126       print(text);
127   }
128
129   protected static void print(String JavaDoc text) {
130     System.out.println(text);
131   }
132 }
133
Popular Tags