use of org.apache.cxf.jaxrs.impl.tl.ThreadLocalProxy in project cxf by apache.
the class JAXRSUtilsTest method testSingletonHttpResourceFields.
@SuppressWarnings("unchecked")
@Test
public void testSingletonHttpResourceFields() throws Exception {
ClassResourceInfo cri = new ClassResourceInfo(Customer.class, true);
Customer c = new Customer();
cri.setResourceProvider(new SingletonResourceProvider(c));
Message m = createMessage();
ServletContext servletContextMock = EasyMock.createNiceMock(ServletContext.class);
m.put(AbstractHTTPDestination.HTTP_CONTEXT, servletContextMock);
HttpServletRequest httpRequest = EasyMock.createNiceMock(HttpServletRequest.class);
m.put(AbstractHTTPDestination.HTTP_REQUEST, httpRequest);
HttpServletResponse httpResponse = EasyMock.createMock(HttpServletResponse.class);
m.put(AbstractHTTPDestination.HTTP_RESPONSE, httpResponse);
InjectionUtils.injectContextProxies(cri, cri.getResourceProvider().getInstance(null));
InjectionUtils.injectContextFields(c, cri, m);
assertSame(servletContextMock, ((ThreadLocalProxy<ServletContext>) c.getServletContextResource()).get());
HttpServletRequest currentReq = ((ThreadLocalProxy<HttpServletRequest>) c.getServletRequestResource()).get();
assertSame(httpRequest, ((HttpServletRequestFilter) currentReq).getRequest());
HttpServletResponseFilter filter = (HttpServletResponseFilter) ((ThreadLocalProxy<HttpServletResponse>) c.getServletResponseResource()).get();
assertSame(httpResponse, filter.getResponse());
}
use of org.apache.cxf.jaxrs.impl.tl.ThreadLocalProxy in project cxf by apache.
the class InjectionUtils method createThreadLocalProxy.
// TODO : investigate the possibility of using generic proxies only
@SuppressWarnings("unchecked")
public static <T> ThreadLocalProxy<T> createThreadLocalProxy(Class<T> type) {
ThreadLocalProxy<?> proxy = null;
if (UriInfo.class.isAssignableFrom(type)) {
proxy = new ThreadLocalUriInfo();
} else if (HttpHeaders.class.isAssignableFrom(type)) {
proxy = new ThreadLocalHttpHeaders();
} else if (ProtocolHeaders.class.isAssignableFrom(type)) {
proxy = new ThreadLocalProtocolHeaders();
} else if (SecurityContext.class.isAssignableFrom(type)) {
proxy = new ThreadLocalSecurityContext();
} else if (ContextResolver.class.isAssignableFrom(type)) {
proxy = new ThreadLocalContextResolver<Object>();
} else if (Request.class.isAssignableFrom(type)) {
proxy = new ThreadLocalRequest();
} else if (Providers.class.isAssignableFrom(type)) {
proxy = new ThreadLocalProviders();
} else if (MessageContext.class.isAssignableFrom(type)) {
proxy = new ThreadLocalMessageContext();
}
if (proxy == null && isServletApiContext(type.getName())) {
proxy = createThreadLocalServletApiContext(type.getName());
}
if (proxy == null) {
ProxyClassLoader loader = new ProxyClassLoader(Proxy.class.getClassLoader());
loader.addLoader(type.getClassLoader());
loader.addLoader(ThreadLocalProxy.class.getClassLoader());
return (ThreadLocalProxy<T>) Proxy.newProxyInstance(loader, new Class[] { type, ThreadLocalProxy.class }, new ThreadLocalInvocationHandler<T>());
}
return (ThreadLocalProxy<T>) proxy;
}
use of org.apache.cxf.jaxrs.impl.tl.ThreadLocalProxy in project cxf by apache.
the class ProviderFactory method createProviderFromConstructor.
public static ProviderInfo<? extends Object> createProviderFromConstructor(Constructor<?> c, Map<Class<?>, Object> values, Bus theBus, boolean checkContexts, boolean custom) {
Map<Class<?>, Map<Class<?>, ThreadLocalProxy<?>>> proxiesMap = CastUtils.cast((Map<?, ?>) theBus.getProperty(AbstractResourceInfo.CONSTRUCTOR_PROXY_MAP));
Map<Class<?>, ThreadLocalProxy<?>> existingProxies = null;
if (proxiesMap != null) {
existingProxies = proxiesMap.get(c.getDeclaringClass());
}
Class<?>[] paramTypes = c.getParameterTypes();
Object[] cArgs = ResourceUtils.createConstructorArguments(c, null, false, values);
if (existingProxies != null && existingProxies.size() <= paramTypes.length) {
for (int i = 0; i < paramTypes.length; i++) {
if (cArgs[i] instanceof ThreadLocalProxy) {
cArgs[i] = existingProxies.get(paramTypes[i]);
}
}
}
Object instance = null;
try {
instance = c.newInstance(cArgs);
} catch (Throwable ex) {
throw new RuntimeException("Resource or provider class " + c.getDeclaringClass().getName() + " can not be instantiated");
}
Map<Class<?>, ThreadLocalProxy<?>> proxies = new LinkedHashMap<Class<?>, ThreadLocalProxy<?>>();
for (int i = 0; i < paramTypes.length; i++) {
if (cArgs[i] instanceof ThreadLocalProxy) {
@SuppressWarnings("unchecked") ThreadLocalProxy<Object> proxy = (ThreadLocalProxy<Object>) cArgs[i];
proxies.put(paramTypes[i], proxy);
}
}
boolean isApplication = Application.class.isAssignableFrom(c.getDeclaringClass());
if (isApplication) {
return new ApplicationInfo((Application) instance, proxies, theBus);
}
return new ProviderInfo<Object>(instance, proxies, theBus, checkContexts, custom);
}
Aggregations