Description
Problem: by injection of ResourceInfo in resource class or in provider in OSGi environment CXF throws following exception:
Caused by: java.lang.IllegalArgumentException: interface org.apache.cxf.jaxrs.impl.tl.ThreadLocalProxy is not visible from class loader at java.lang.reflect.Proxy.getProxyClass0(Proxy.java:487)[:1.7.0_40] at java.lang.reflect.Proxy.newProxyInstance(Proxy.java:722)[:1.7.0_40] at org.apache.cxf.jaxrs.utils.InjectionUtils.createThreadLocalProxy(InjectionUtils.java:962)
Reason of the problem: there is no dedicated thread local context for ResourceInfo and CXF instantiates it using java reflection proxy in InjectionUtils:
return (ThreadLocalProxy<T>)Proxy.newProxyInstance(type.getClassLoader(), new Class[] {type, ThreadLocalProxy.class }, new ThreadLocalInvocationHandler<T>());
The problem is that classloader in first argument is taken from type class, but proxy have to implement two interfaces: type and ThreadLocalProxy (second argument). It works fine in standalone environment, but in OSGi classloader of ResourceInfo bundle doesn't know nothing about ThreadLocalProxy interface in CXF JAX-RS bundle. Of course, the ThreadLocalProxy cannot be found.
Solution: possible solution is use the classloader of current class (InjectionUtils) instead of type class. As far as InjectionUtils classloader knows type class as well it should work in standalone as well as in OSGi environments.