You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by rm...@apache.org on 2014/08/06 00:12:27 UTC
svn commit: r1616022 - in /tomee/tomee/trunk/server/openejb-cxf-rs/src:
main/java/org/apache/openejb/server/cxf/rs/CxfRsHttpListener.java
test/java/org/apache/openejb/server/cxf/rs/CDIProviderTest.java
Author: rmannibucau
Date: Tue Aug 5 22:12:27 2014
New Revision: 1616022
URL: http://svn.apache.org/r1616022
Log:
TOMEE-1285 @Provider can be CDI beans now
Added:
tomee/tomee/trunk/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/CDIProviderTest.java
- copied, changed from r1615960, tomee/tomee/trunk/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/CDIApplicationTest.java
Modified:
tomee/tomee/trunk/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/CxfRsHttpListener.java
Modified: tomee/tomee/trunk/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/CxfRsHttpListener.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/CxfRsHttpListener.java?rev=1616022&r1=1616021&r2=1616022&view=diff
==============================================================================
--- tomee/tomee/trunk/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/CxfRsHttpListener.java (original)
+++ tomee/tomee/trunk/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/CxfRsHttpListener.java Tue Aug 5 22:12:27 2014
@@ -62,7 +62,9 @@ import org.apache.openejb.util.LogCatego
import org.apache.openejb.util.Logger;
import org.apache.openejb.util.proxy.ProxyEJB;
import org.apache.webbeans.config.WebBeansContext;
+import org.apache.webbeans.container.BeanManagerImpl;
+import javax.enterprise.inject.spi.Bean;
import javax.management.ObjectName;
import javax.management.openmbean.TabularData;
import javax.naming.Context;
@@ -211,12 +213,14 @@ public class CxfRsHttpListener implement
}
@Override
+ @Deprecated // we could drop it now I think
public void deploySingleton(final String contextRoot, final String fullContext, final Object o, final Application appInstance,
final Collection<Object> additionalProviders, final ServiceConfiguration configuration) {
- deploy(contextRoot, o.getClass(), fullContext, new SingletonResourceProvider(o), o, appInstance, null, additionalProviders, configuration);
+ deploy(contextRoot, o.getClass(), fullContext, new SingletonResourceProvider(o), o, appInstance, null, additionalProviders, configuration, null);
}
@Override
+ @Deprecated // we could drop it now I think
public void deployPojo(final ClassLoader loader,
final String contextRoot,
final String fullContext,
@@ -228,10 +232,11 @@ public class CxfRsHttpListener implement
final Collection<Object> additionalProviders,
final ServiceConfiguration configuration) {
deploy(contextRoot, loadedClazz, fullContext, new OpenEJBPerRequestPojoResourceProvider(loader, loadedClazz, injections, context, owbCtx),
- null, app, null, additionalProviders, configuration);
+ null, app, null, additionalProviders, configuration, owbCtx);
}
@Override
+ @Deprecated // we could drop it now I think
public void deployEJB(final String contextRoot,
final String fullContext,
final BeanContext beanContext,
@@ -239,16 +244,18 @@ public class CxfRsHttpListener implement
final ServiceConfiguration configuration) {
final Object proxy = ProxyEJB.subclassProxy(beanContext);
- deploy(contextRoot, beanContext.getBeanClass(), fullContext, new NoopResourceProvider(beanContext.getBeanClass(), proxy), proxy, null, new OpenEJBEJBInvoker(Collections.singleton(beanContext)), additionalProviders, configuration);
+ deploy(contextRoot, beanContext.getBeanClass(), fullContext, new NoopResourceProvider(beanContext.getBeanClass(), proxy),
+ proxy, null, new OpenEJBEJBInvoker(Collections.singleton(beanContext)), additionalProviders, configuration, null);
}
private void deploy(final String contextRoot, final Class<?> clazz, final String address, final ResourceProvider rp, final Object serviceBean,
- final Application app, final Invoker invoker, final Collection<Object> additionalProviders, final ServiceConfiguration configuration) {
+ final Application app, final Invoker invoker, final Collection<Object> additionalProviders, final ServiceConfiguration configuration,
+ final WebBeansContext webBeansContext) {
final ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(CxfUtil.initBusLoader());
try {
final JAXRSServerFactoryBean factory = newFactory(address);
- configureFactory(additionalProviders, configuration, factory);
+ configureFactory(additionalProviders, configuration, factory, webBeansContext);
factory.setResourceClasses(clazz);
context = contextRoot;
if (context == null) {
@@ -282,11 +289,26 @@ public class CxfRsHttpListener implement
}
}
- private Collection<Object> providers(final Collection<ServiceInfo> services, final Collection<Object> additionalProviders) {
+ private Collection<Object> providers(final Collection<ServiceInfo> services, final Collection<Object> additionalProviders, final WebBeansContext ctx) {
final Collection<Object> instances = new ArrayList<>();
+ final BeanManagerImpl bm = ctx.getBeanManagerImpl();
for (final Object o : additionalProviders) {
if (o instanceof Class<?>) {
final Class<?> clazz = (Class<?>) o;
+
+ if (bm.isInUse()) {
+ try {
+ final Set<Bean<?>> beans = bm.getBeans(clazz);
+ if (beans != null && !beans.isEmpty()) {
+ final Bean<?> bean = bm.resolve(beans);
+ instances.add(bm.getReference(bean, clazz, bm.createCreationalContext(bean)));
+ continue;
+ }
+ } catch (final Throwable th) {
+ LOGGER.info("Can't use CDI to create provider " + clazz.getName());
+ }
+ }
+
final Collection<Object> instance = ServiceInfos.resolve(services, new String[]{clazz.getName()}, ProviderFactory.INSTANCE);
if (instance != null && !instance.isEmpty()) {
instances.add(instance.iterator().next());
@@ -348,7 +370,7 @@ public class CxfRsHttpListener implement
Thread.currentThread().setContextClassLoader(CxfUtil.initBusLoader());
try {
final JAXRSServerFactoryBean factory = newFactory(prefix);
- configureFactory(additionalProviders, serviceConfiguration, factory);
+ configureFactory(additionalProviders, serviceConfiguration, factory, owbCtx);
factory.setApplication(application);
final List<Class<?>> classes = new ArrayList<>();
@@ -533,7 +555,10 @@ public class CxfRsHttpListener implement
return factory;
}
- private void configureFactory(final Collection<Object> givenAdditionalProviders, final ServiceConfiguration serviceConfiguration, final JAXRSServerFactoryBean factory) {
+ private void configureFactory(final Collection<Object> givenAdditionalProviders,
+ final ServiceConfiguration serviceConfiguration,
+ final JAXRSServerFactoryBean factory,
+ final WebBeansContext ctx) {
CxfUtil.configureEndpoint(factory, serviceConfiguration, CXF_JAXRS_PREFIX);
final Collection<ServiceInfo> services = serviceConfiguration.getAvailableServices();
@@ -600,13 +625,13 @@ public class CxfRsHttpListener implement
if (providersConfig != null) {
providers = ServiceInfos.resolve(services, providersConfig.toArray(new String[providersConfig.size()]), ProviderFactory.INSTANCE);
if (providers != null && additionalProviders != null && !additionalProviders.isEmpty()) {
- providers.addAll(providers(services, additionalProviders));
+ providers.addAll(providers(services, additionalProviders, ctx));
}
}
if (providers == null) {
providers = new ArrayList<>(4);
if (additionalProviders != null && !additionalProviders.isEmpty()) {
- providers.addAll(providers(services, additionalProviders));
+ providers.addAll(providers(services, additionalProviders, ctx));
} else {
providers.addAll(defaultProviders());
}
Copied: tomee/tomee/trunk/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/CDIProviderTest.java (from r1615960, tomee/tomee/trunk/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/CDIApplicationTest.java)
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/CDIProviderTest.java?p2=tomee/tomee/trunk/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/CDIProviderTest.java&p1=tomee/tomee/trunk/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/CDIApplicationTest.java&r1=1615960&r2=1616022&rev=1616022&view=diff
==============================================================================
--- tomee/tomee/trunk/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/CDIApplicationTest.java (original)
+++ tomee/tomee/trunk/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/CDIProviderTest.java Tue Aug 5 22:12:27 2014
@@ -19,7 +19,6 @@ package org.apache.openejb.server.cxf.rs
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.openejb.jee.WebApp;
import org.apache.openejb.junit.ApplicationComposer;
-import org.apache.openejb.server.cxf.rs.beans.MyFirstRestClass;
import org.apache.openejb.testing.Classes;
import org.apache.openejb.testing.Configuration;
import org.apache.openejb.testing.EnableServices;
@@ -31,17 +30,25 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import javax.inject.Inject;
-import javax.ws.rs.core.Application;
-import java.util.HashSet;
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.ext.MessageBodyWriter;
+import javax.ws.rs.ext.Provider;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
import java.util.Properties;
-import java.util.Set;
import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
@EnableServices("jax-rs")
@RunWith(ApplicationComposer.class)
-public class CDIApplicationTest {
+public class CDIProviderTest {
private static int port = -1;
@@ -56,40 +63,57 @@ public class CDIApplicationTest {
}
@Module
- @Classes(cdi = true, value = {MyCdiRESTApplication.class, MyFirstRestClass.class, ACdiBeanInjectedInApp.class})
+ @Classes(cdi = true, value = {Helper.class, MyPro.class, Res.class})
public WebApp war() {
return new WebApp()
- .contextRoot("foo")
- .addServlet("REST Application", Application.class.getName())
- .addInitParam("REST Application", "javax.ws.rs.Application", MyCdiRESTApplication.class.getName());
+ .contextRoot("foo");
}
@Test
public void isCdi() {
- assertTrue(MyCdiRESTApplication.injection);
- assertEquals("Hi from REST World!", WebClient.create("http://localhost:" + port + "/foo/").path("/first/hi").get(String.class));
+ assertEquals("Oh Yeah!", WebClient.create("http://localhost:" + port + "/foo").accept("provider/type").path("res").get(String.class));
}
- public static class ACdiBeanInjectedInApp {
+ @Path("res")
+ public static class Res {
+ @GET
+ @Produces("provider/type")
+ public String f() {
+ return "failed";
+ }
}
- public static class MyCdiRESTApplication extends Application {
- public static boolean injection = false;
-
+ @Provider
+ @Produces("provider/type")
+ public static class MyPro implements MessageBodyWriter<String> {
@Inject
- private ACdiBeanInjectedInApp cdi;
+ private Helper helper;
+
+ @Override
+ public boolean isWriteable(final Class<?> type, final Type genericType,
+ final Annotation[] annotations, final MediaType mediaType) {
+ return true;
+ }
- public Set<Class<?>> getClasses() {
- injection = cdi != null;
+ @Override
+ public long getSize(final String s, final Class<?> type, final Type genericType,
+ final Annotation[] annotations, final MediaType mediaType) {
+ return -1;
+ }
+
+ @Override
+ public void writeTo(final String s, final Class<?> type, final Type genericType,
+ final Annotation[] annotations,
+ final MediaType mediaType,
+ final MultivaluedMap<String, Object> httpHeaders,
+ final OutputStream entityStream) throws IOException, WebApplicationException {
+ entityStream.write(helper.data().getBytes());
+ }
+ }
- if (cdi == null) {
- throw new NullPointerException();
- }
-
- // if no class are returned we use scanning, since we don't test rest deployment we put a single class
- final Set<Class<?>> clazz = new HashSet<Class<?>>();
- clazz.add(MyFirstRestClass.class);
- return clazz;
+ public static class Helper {
+ String data() {
+ return "Oh Yeah!";
}
}
}