You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
81 lines
3.0 KiB
81 lines
3.0 KiB
13 years ago
|
// serverClassLoader.java
|
||
20 years ago
|
// -----------------------
|
||
17 years ago
|
// (C) by Michael Peter Christen; mc@yacy.net
|
||
20 years ago
|
// first published on http://www.anomic.de
|
||
|
// Frankfurt, Germany, 2004
|
||
|
// last major change: 11.07.2004
|
||
|
//
|
||
|
// This program is free software; you can redistribute it and/or modify
|
||
|
// it under the terms of the GNU General Public License as published by
|
||
|
// the Free Software Foundation; either version 2 of the License, or
|
||
|
// (at your option) any later version.
|
||
|
//
|
||
|
// This program is distributed in the hope that it will be useful,
|
||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
// GNU General Public License for more details.
|
||
|
//
|
||
|
// You should have received a copy of the GNU General Public License
|
||
|
// along with this program; if not, write to the Free Software
|
||
|
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||
|
|
||
12 years ago
|
package net.yacy.server;
|
||
20 years ago
|
|
||
20 years ago
|
import java.io.File;
|
||
|
import java.io.IOException;
|
||
15 years ago
|
import java.util.Map;
|
||
|
import java.util.concurrent.ConcurrentHashMap;
|
||
20 years ago
|
|
||
15 years ago
|
import net.yacy.kelondro.util.FileUtils;
|
||
|
|
||
16 years ago
|
|
||
17 years ago
|
public final class serverClassLoader extends ClassLoader {
|
||
|
/**
|
||
|
* directory of class files
|
||
|
*/
|
||
15 years ago
|
private final Map<File, Class<?>> classes;
|
||
20 years ago
|
|
||
|
public serverClassLoader() {
|
||
18 years ago
|
//super(ClassLoader.getSystemClassLoader());
|
||
|
super(Thread.currentThread().getContextClassLoader());
|
||
15 years ago
|
this.classes = new ConcurrentHashMap<File, Class<?>>(100);
|
||
20 years ago
|
}
|
||
|
|
||
17 years ago
|
public serverClassLoader(final ClassLoader parent) {
|
||
19 years ago
|
super(parent);
|
||
13 years ago
|
this.classes = new ConcurrentHashMap<File, Class<?>>(100);
|
||
20 years ago
|
}
|
||
|
|
||
|
public Package[] packages() {
|
||
19 years ago
|
return super.getPackages();
|
||
20 years ago
|
}
|
||
|
|
||
17 years ago
|
public Class<?> loadClass(final File classfile) throws ClassNotFoundException {
|
||
16 years ago
|
// take the class out of the cache, denoted by the class file
|
||
|
Class<?> c = this.classes.get(classfile);
|
||
|
if (c != null) return c;
|
||
13 years ago
|
|
||
|
final int p = classfile.getName().indexOf('.',0);
|
||
16 years ago
|
if (p < 0) throw new ClassNotFoundException("wrong class name: " + classfile.getName());
|
||
|
final String classname = classfile.getName().substring(0, p);
|
||
19 years ago
|
|
||
16 years ago
|
// load the file from the file system
|
||
|
byte[] b;
|
||
|
try {
|
||
16 years ago
|
//System.out.println("*** DEBUG CLASSLOADER: " + classfile + "; file " + (classfile.exists() ? "exists": "does not exist"));
|
||
16 years ago
|
b = FileUtils.read(classfile);
|
||
16 years ago
|
// make a class out of the stream
|
||
|
c = this.defineClass(null, b, 0, b.length);
|
||
|
resolveClass(c);
|
||
|
this.classes.put(classfile, c);
|
||
|
} catch (final LinkageError ee) {
|
||
|
c = findLoadedClass(classname);
|
||
|
if (c != null) return c;
|
||
16 years ago
|
throw new ClassNotFoundException("linkageError, " + ee.getMessage() + ":" + classfile.toString());
|
||
16 years ago
|
} catch (final IOException ee) {
|
||
16 years ago
|
throw new ClassNotFoundException(ee.getMessage() + ":" + classfile.toString());
|
||
19 years ago
|
}
|
||
16 years ago
|
return c;
|
||
20 years ago
|
}
|
||
|
|
||
|
}
|