views:

34

answers:

1

Hello,

I'm trying to send objects from bundles over a dedicated communication bundle to an other framework. For communication I use Java standard serialization (with ObjectStreams) over TCP/IP.

Communication flow is the following: A sender-bundle will pass serializable objects to a transmit-sender which will serialize the objects and send them via TCP/IP to a transmit-receiver. The receiver will then deserialize the received objects and pass them to the receiver-bundle.

As the OSGi classloading architecture is different than the native one, I have to make a littel hack with the classloader: As I assume that the receiver-bundle should know the classes which he receives (= has them imported or otherwise accessible by its classloader), I use the classloader of the receiver instead of the transmit-receiver to load the class. (Via the Bundle.loadClass(..) method). This works fine for custom classes, however, this does not work for arrays of custom types. (Which are not known to the transmit-receiver classloader but to the receiver-bundle.)

Edit: ObjectInputStream.readObject(...) throws an ClassNotFoundException, if It tries to deserialize an array. (I assume that this exception origins in the Bundle.loadClass(...) method of the receiver-bundle. )

It does work with java.util.List or other Serializable classes. It also works for custom types which contains fields of other custom types as long as they are not arrays.

So the question is: Is there any difference in the way arrays are de/serialized? Or are they loaded differently?

+1  A: 

I suspect you've implemented ObjectInputStream.resolveClass using Bundle.loadClass directly. If so, you're hitting this problem: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6516909

Basically, ClassLoader.loadClass was never intended to be used for array classes. The easiest way to load array classes is to use Class.forName. Unfortunately, there is no easy way to do that with OSGi. You'll either need a Bundle ClassLoader proxy, or you'll need to do some pre-processing on the class names to ensure that you don't pass arrays directly to loadClass:

public static Class<?> bundleClassForName(Bundle bundle, String name) throws ClassNotFoundException {
    int length = name.length();
    if (length > 0 && name.charAt(0) == '[') {
        int pos = 1;
        while (pos < length && name.charAt(pos) == '[') {
            pos++;
        }

        if (pos < name.length()) {
            if (name.charAt(pos) != 'L') {
                return Class.forName(name);
            }

            if (name.charAt(length - 1) == ';') {
                String componentName = name.substring(pos + 1, length - 1);
                Class<?> klass = bundle.loadClass(componentName);
                ClassLoader loader = klass.getClassLoader();
                return Class.forName(name, false, loader);
            }
        }
    }

    return bundle.loadClass(name);
}
bkail
That's true, we're running in this bug here....a dirty-workaround we have tried is by using Dynamic-Import. However, currently we just avoid using arrays.
ManuelFuenfrocken