public Class findClass(String name) throws ClassNotFoundException {
// The class object that will be returned.
Class c = null;
// Use the cached value, if this class is already loaded into
// this classloader.
ClassCacheEntry entry = (ClassCacheEntry) cache.get(name);
if (entry != null) {
if (log.isDebugEnabled()) {
log.debug("Loaded " + name + " from cache");
}
// Class found in our cache
c = entry.loadedClass;
resolveClass(c);
return c;
}
// Try to load it from each classpath
Iterator it = classpath.iterator();
// Cache entry.
ClassCacheEntry classCache = new ClassCacheEntry();
while (it.hasNext()) {
byte[] classData;
File file = (File) it.next();
try {
if (file.isDirectory()) {
classData = loadClassFromDirectory(file, name, classCache);
} else {
classData = loadClassFromZipfile(file, name, classCache);
}
} catch (IOException ioe) {
// Error while reading in data, consider it as not found
classData = null;
}
if (classData != null) {
// Does the package exist?
String packageName = "";
if (name.lastIndexOf(".") > 0) {
packageName = name.substring(0, name.lastIndexOf("."));
}
if (!packageName.equals("") &&
!packages.containsKey(packageName)) {
packages.put(packageName,
definePackage(packageName, "", "", "", "", "", "", null));
// Define the class
}
c = defineClass(name, classData, 0, classData.length);
// Cache the result;
classCache.loadedClass = c;
// Origin is set by the specific loader
classCache.lastModified = classCache.origin.lastModified();
cache.put(name, classCache);
resolveClass(c);
if (log.isDebugEnabled()) {
log.debug("Loaded " + name +
" adding to cache and returning");
}
return c;
}
}
// If not found in any classpath
throw new ClassNotFoundException(name);
}
|