Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Equinox » Classloader problem when deserializing
Classloader problem when deserializing [message #707526] Mon, 01 August 2011 19:18 Go to next message
stormweaver  is currently offline stormweaver Friend
Messages: 3
Registered: August 2011
Junior Member
Hi there.
I'm relatively new to OSGi and have been facing a problem, which I assume has to do with the classloaders. Here's my situation:

My goal is to serialize and deserialize a message(object), where the code for deserialization is on a different bundle than the one containing the class being serialized.

I have two bundles B1 and B2. The class to be (de)serialized is in B2.
A class (say C1) in B1 has the code for deserialization:
C1.received(byte[] buf, int offset, int length)

This class is extended in B2, to derive another class C2.

In B2, I have a object of type C2 for receiving serialized buffer, and I called
c2.received(buf, offset, length)

I get a ClassNotFound error inside that method while trying to deserialize, eventhough this method call is being made in B2.

When I moved the code for deserialization from the abstract class C1 to C2, it worked fine. Hence, I've come to the conclusion that when calling methods from superclasses that are on a different bundle, a "context switch" or something like that occurs (I'm not sure about the term used)

However, I need the deserialization code to be in the superclass C1 in bundle B1, to avoid duplicating it into all subclasses. How can I get this to work?

I tried using Eclipse-BuddyPolicy: dependent in B1's manifest, but that didn't do it either. Or do I need to start some bundle to get BuddyPolicy to work? Currently I'm only using the org.eclipse.osgi jar and my two jar files B1 and B2.

Any suggestions please Smile
Re: Classloader problem when deserializing [message #707882 is a reply to message #707526] Tue, 02 August 2011 06:37 Go to previous messageGo to next message
Gunnar Wagenknecht is currently offline Gunnar WagenknechtFriend
Messages: 486
Registered: July 2009
Location: San Francisco ✈ Germany
Senior Member

Am 01.08.2011 21:18, schrieb stormweaver:
> However, I need the deserialization code to be in the superclass C1
> in bundle B1, to avoid duplicating it into all subclasses. How can I
> get this to work?

Are you using vanilla Java serialization/de-serialization (i.e.
ObjectInputStream)? If yes then one possible way is to extend
ObjectInputStream with an implementation that overrides
"resolveClass(ObjectStreamClass)". In that method you should delegate to
the appropriate ClassLoader or to Bundle.loadClass(String) (likely the
class loader/bundle of your subclass).

There is such an implementation available in Apache Commons IO.
org.apache.commons.io.input.ClassLoaderObjectInputStream

Buddy-ClassLoading is another way but that is Equinox only.

-Gunnar


--
Gunnar Wagenknecht
gunnar@wagenknecht.org
http://wagenknecht.org/
Re: Classloader problem when deserializing [message #708393 is a reply to message #707882] Tue, 02 August 2011 19:13 Go to previous messageGo to next message
stormweaver  is currently offline stormweaver Friend
Messages: 3
Registered: August 2011
Junior Member
Quote:

There is such an implementation available in Apache Commons IO.
org.apache.commons.io.input.ClassLoaderObjectInputStream


I tried ClassLoaderObjectInputStream and now it works!! Thanks a million!! Smile
Re: Classloader problem when deserializing [message #708412 is a reply to message #708393] Tue, 02 August 2011 19:42 Go to previous message
stormweaver  is currently offline stormweaver Friend
Messages: 3
Registered: August 2011
Junior Member
However... after using the Apache Commons IO bundle, i just found out that my bundles now run fine with KnopflerFish, but not with eclipse osgi.

With the eclipse OSGI, I get the following error:
java.lang.NoClassDefFoundError: org/apache/commons/io/input/ClassLoaderObjectInputStream


But the required bundles are all loaded, and the manifest has the necessary imports.
id      State       Bundle
0       ACTIVE      org.eclipse.osgi_3.7.0.v20110613
1       ACTIVE      teletutor.core_1.0.0
3       ACTIVE      TeleConsumer_1.0.0
4       ACTIVE      com.springsource.org.apache.commons.io_1.4.0


I don't understand why it's working on one framework and not on the other...
Previous Topic:ConditionalPermissionAdmin and equinox.ds
Next Topic:Content Assist
Goto Forum:
  


Current Time: Tue Apr 16 14:22:47 GMT 2024

Powered by FUDForum. Page generated in 2.41861 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top