restlet/restlet-framework-java — Issue #6: ClientResource.wrap() in osgi
Author: carrot-garden
Created: 2012-02-26 16:29:17+00:00
State: open

Jerome, hi;

1) as Pierre reported here:
http://restlet-discuss.1400322.n2.nabble.com/Problem-with-wrap-td6290637.html

2) ClientResource.wrap() in osgi does not work when crossing the bundle boundary:

https://github.com/restlet/restlet-framework-java/blob/master/modules/org.restlet/src/org/restlet/resource/ClientResource.java

public T wrap(Class resourceInterface) {
T result = null;

// Create the client resource proxy
java.lang.reflect.InvocationHandler h = new org.restlet.engine.resource.ClientInvocationHandler(
this, resourceInterface);

// Instantiate our dynamic proxy
result = (T) java.lang.reflect.Proxy.newProxyInstance(
org.restlet.engine.Engine.getInstance().getClassLoader(),
new Class[] { ClientProxy.class, resourceInterface }, h);

return result;
}

due to Engine.getInstance().getClassLoader() used as default;

3) the following are workarounds :

a) make change in wrap():
Engine.getInstance().getClassLoader() -> resourceInterface.getClass().getClassLoader()

b) "wrap" the wrap invocation:

Engine engine = Engine.getInstance();
ClassLoader loaderOld = engine.getUserClassLoader();
ClassLoader loaderNew = resourceInterface.getClass().getClassLoader();
try {
engine.setUserClassLoader(loaderNew);
clientResource.wrap(resourceInterface);
}
finally {
engine.setUserClassLoader(loaderOld);
}

4) I am curious if change should be made in wrap()
or there is a need for one more wiki page which explains
restlet class loader best use practices, especially for osgi?

thank you;

Andrei