@ -43,78 +43,93 @@ package de.anomic.server;
import java.io.File ;
import java.io.File ;
import java.io.IOException ;
import java.io.IOException ;
import java.util.HashMap ;
import java.util.Hashtable ;
import java.util.Hashtable ;
public final class serverClassLoader extends ClassLoader {
public final class serverClassLoader extends ClassLoader {
private final Hash table classes ;
private final Hash Map classes ;
public serverClassLoader ( ) {
public serverClassLoader ( ) {
super ( ClassLoader . getSystemClassLoader ( ) ) ;
super ( ClassLoader . getSystemClassLoader ( ) ) ;
classes = new Hashtable ( ) ;
this . classes = new HashMap ( ) ;
}
}
public serverClassLoader ( ClassLoader parent ) {
public serverClassLoader ( ClassLoader parent ) {
super ( parent ) ;
super ( parent ) ;
classes = new Hashtable ( ) ;
classes = new HashMap ( ) ;
}
}
public Package [ ] packages ( ) {
public Package [ ] packages ( ) {
return super . getPackages ( ) ;
return super . getPackages ( ) ;
}
}
public Class loadClass ( File classfile ) throws ClassNotFoundException {
public Class loadClass ( File classfile ) throws ClassNotFoundException {
// we consider that the classkey can either be only the name of a class, or a partial or
// we consider that the classkey can either be only the name of a class, or a partial or
// complete path to a class file
// complete path to a class file
// normalize classkey: strip off '.class'
// normalize classkey: strip off '.class'
//if (classkey.endsWith(".class")) classkey = classkey.substring(0, classkey.length() - 6);
//if (classkey.endsWith(".class")) classkey = classkey.substring(0, classkey.length() - 6);
// try to find the class in the hashtable
String classFileName = null ;
Class c = ( Class ) classes . get ( classfile ) ;
if ( c ! = null ) return c ;
// consider classkey as a file and extract the file name
//File classfile = new File(classkey);
// this file cannot exist for real, since we stripped off the .class
// we constructed the classfile for the only purpose to strip off the name:
// get the class name out of the classfile
String classname = classfile . getName ( ) ;
int p = classname . indexOf ( "." ) ;
classname = classname . substring ( 0 , p ) ;
// now that we have the name, we can create the real class file
//classfile = new File(classkey + ".class");
// first try: take the class out of the cache, denoted by the classname
try {
try {
c = findLoadedClass ( classname ) ;
classFileName = classfile . getCanonicalPath ( ) ;
if ( c = = null ) {
} catch ( IOException e ) {
// second try: ask the system
throw new ClassNotFoundException ( "Unable to resolve the classfile path" ) ;
c = findSystemClass ( classname ) ;
}
}
if ( c = = null ) {
// try to load the class
// third try
synchronized ( classFileName . intern ( ) ) {
throw new ClassNotFoundException ( "internal trigger" ) ;
// first try: take the class out of the cache, denoted by the classname
}
Class c = ( Class ) this . classes . get ( classfile ) ;
} catch ( ClassNotFoundException e ) {
if ( c ! = null ) return c ;
//System.out.println("INTERNAL ERROR1 in cachedClassLoader: " + e.getMessage());
// third try: load the file from the file system
// consider classkey as a file and extract the file name
byte [ ] b ;
//File classfile = new File(classkey);
try {
// this file cannot exist for real, since we stripped off the .class
// we constructed the classfile for the only purpose to strip off the name:
// get the class name out of the classfile
String classname = classfile . getName ( ) ;
int p = classname . indexOf ( "." ) ;
classname = classname . substring ( 0 , p ) ;
// now that we have the name, we can create the real class file
//classfile = new File(classkey + ".class");
// This code doesn't work properly if there are multiple classes with the same name
// This is because we havn't definded package names in our servlets
//
// try {
// c = findLoadedClass(classname);
// if (c == null) {
// // second try: ask the system
// c = findSystemClass(classname);
// }
// if (c == null) {
// // third try
// throw new ClassNotFoundException("internal trigger");
// }
// } catch (ClassNotFoundException e) {
//System.out.println("INTERNAL ERROR1 in cachedClassLoader: " + e.getMessage());
// third try: load the file from the file system
byte [ ] b ;
try {
b = serverFileUtils . read ( classfile ) ;
b = serverFileUtils . read ( classfile ) ;
// now make a class out of the stream
// now make a class out of the stream
// System.out.println("loading class " + classname + " from file " + classfile.toString());
// System.out.println("loading class " + classname + " from file " + classfile.toString());
c = this . defineClass ( classname , b , 0 , b . length ) ;
c = this . defineClass ( classname , b , 0 , b . length ) ;
resolveClass ( c ) ;
resolveClass ( c ) ;
classes . put ( classfile , c ) ;
this . classes . put ( classfile , c ) ;
} catch ( IOException ee ) {
} catch ( IOException ee ) {
//System.out.println("INTERNAL ERROR2 in cachedClassLoader: " + ee.getMessage());
//System.out.println("INTERNAL ERROR2 in cachedClassLoader: " + ee.getMessage());
throw new ClassNotFoundException ( classfile . toString ( ) ) ;
throw new ClassNotFoundException ( classfile . toString ( ) ) ;
}
}
}
// }
return c ;
return c ;
}
}
}
}
}