Re: ClassCastException after retrieving an Entry

Gregg Wonderly <[email protected]> Thu, 4 Jan 2007 14:39:58 -0600
Newsgroups gmane.comp.java.sun.javaspaces
Message-ID <[email protected]>
Christian Hager wrote:
> Ok I did this
>
>> Class[]cs = entry.getClass().getClasses();
>> for( Class c : cs ) {
>> System.out.println(c.getName()+": loader="+c.getClassLoader() );
>> }
>
> But getClasses() returns an empty array.

Then use getDeclaredClasses() instead.  You need to find out what classloader
loads each of the classes and interfaces that are part of the entry and
specifically where it is finding TaskEntry.  It's that ClassLoader that
represents the bigger issue.

 > Maybe it helps if I explain a
> little more about TaskEntry. TaskEntry is supposed to be a generic task
> which all my special tasks extend. My worker is supposed to be generic
> so it uses TaskEntry as a template to find any task. It excutes the
> taken task and puts the result back in the JavaSpace.

All of this is not part of the problem directly.  What is at issue is where the
classes are being loaded from.  When the entry is deserialized on your client as
you "take()" it, the classes it refers to need to be mapped to class definitions
in your JVM.  The standard classloading mechanisms in the JDK classloaders cause
parent classloaders to be consulted first, before resolving the class to a lower
level loader, so that you can write an application that uses a particular
class/interface, and then download code (with a codebase specified) and have it
deserialize with the classes defined in the same classloader as your application
is using so that the classes will be "equal".

It's not just the name of the class that makes it work.  A class is not the same
class as another class unless both the names are the same AND they are loaded by
the exact same classloader instance.

Gregg Wonderly

===========================================================================
To unsubscribe, send email to [email protected] and include in the body
of the message "signoff JAVASPACES-USERS".  For general help, send email to
[email protected] and include in the body of the message "help".

To view past JAVASPACES-USERS postings, please see:
http://archives.java.sun.com/archives/javaspaces-users.html