As long as the Solver class is loaded from the system class loader and its
subclasses are loaded from the URLClassLoader, I think you'll be ok:
// Here's some sample code inside your custom URLClassLoader's
loadClass(String className) method.
// All classes referenced in your subclass will be loaded using the
loadClass() method in your custom
// URLClassLoader as well, including its superclass Solver.
// But, Solver will be loaded via the findSystemClass() method, as will
all of the core java methods (like
// String, Vector, etc.)
// In other words the abstract class Solver must be available on both the
Server and the client.
public Class loadClass(String className, boolean resolve)
throws ClassNotFoundException {
Class c = null ;
c = findSystemClass(className) ;
if (c == null) {
// load the class via the URL
}
if (resolve) resolveClass(c) ;
return c ;
}
Give it a try,
Zol Heyman.
-----Original Message-----
From: John Brecht [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, May 04, 1999 4:18 PM
To: [EMAIL PROTECTED]
Subject: class casting from servlet
Hello -
I originally posted this on comp.lang.java.programmer, but
the responses
I got seem to indicate that the problem has more to do with
this being a
servlet, so I repost it here. (I'm brand new to this list. I
tried
searching the archives, but the searcher just hung. Is there
an FAQ
somewhere?)
I am writing a servlet that acts as a dispatcher between
applets and
classes on the server whose methods those applets need to
call. (These
classes on the server should never get downloaded to the
client for
security reasons.) All of these classes on the server extend
the same
class, Solver. Solver has the method, checkAttempt(). Any
subclass of
Solver is meant to override this method.
My servlet uses a URLClassLoader to load the appropriate
class based on
a string passed to it from the applet. I am able to get the
class
loaded, but when I try to cast it to Solver, so I can call
it's
checkAttempt() method, I get a ClassCastException. Here is
how I do the
class loading:
private Solver getSolver(String solverName) {
Class solverClass;
Solver theSolver = null;
try {
log("about to load class... "+solverName);
solverClass = loader.loadClass(solverName);
log("loaded class: "+solverClass);
log("parent:"+solverClass.getSuperclass());
log("about to instantiate...");
Object theInstance = solverClass.newInstance();
log("instantiated");
if(theInstance instanceof Solver ) {
log("about to cast as Solver...");
theSolver = (Solver)(theInstance);
log("cast solver:"+theSolver);
} else {
log("couldn't cast, not a Solver!");
}
} catch (Exception e) {
log(""+e);
}
return theSolver;
}
"loader" is a URLClassLoader prepared with the appropriate
codebase upon
initilization of the servlet. A check of the logs shows
that the class I'm
trying to instantiate has Solver as a Superclass, but then
"theInstance"
somehow does not end up being an actual instance of Solver,
and thus not
castable to Solver. How can this be? What am I doing
wrong?
---
John Brecht
Department of Physics and Astronomy
Michigan State University
[EMAIL PROTECTED]
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and
include in the body
of the message "signoff SERVLET-INTEREST".
Archives:
http://archives.java.sun.com/archives/servlet-interest.html
Resources:
http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html