Search code examples
javajsonosgiremoteobjectknopflerfish

JSONObjects not working with osgi registration, not as function argument, neaither as return


I'm working with a knopflerfish environment where I am trying to use functions from a remote object using ServiceRegistration. I will explain as an example. I have created two bundles, A and B. Bundle B has a class C where I have implemented two methods:

public void printIt(JSONObject toPrint) {
    logger.debug(toPrint);
}

public void printIt(String toPrint) {
    logger.debug(toPrint);
}

public JSONObject returnedJ() {
    return (new JSONObject());
}

public String returnedM() {
    return "hello";
}

This class C is being initializated in B's activator and registered using ServiceRegistration so it can be used as a remote object from bundle A. A's activator just gets the registrated object C in order to use it's methods and here is where the problem appears. The remote object can be used but only methods that use String are working the JSONObject ones don't. Has this happened to anyone? Which could be the explaination?

I have tested this behaviour debuging and using try/catch and no errors are shown. Also have to say that when using JSONObject methods bundle A gets frozen and there is no signal in the method from class C. For now I will be parsing the objects from JSONObject to String in bundle A and from String to JSONObjects in bundle B but this is still annoying me.

PD: im using org.codehaus.jettison.json.JSONObject.

Thanks in advance.


Solution

  • In comments, questioner clarified that the library providing the org.codehaus.jettison.json.JSONObject type had been copied/embedded into both bundles.

    Embedding can be used for libraries that are used entirely within a bundle, but never for types that might be shared between bundles, e.g. as part of an API. This is because, in Java, the identity of a type is the combination of its fully qualified name and the classloader that defined it. Therefore, the type JSONObject defined by one bundle is different from the type JSONObject defined by another bundle – even if they identical in terms of bytecode. If you were to try to load an instance of one into a variable that is expecting the other, you would see a ClassCastException with a message of the form "JSONObject cannot be cast to JSONObject".

    The solution is to ensure that the library is only present and exported in one bundle, and imported by all other bundles that use it.