openwebbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From strub...@apache.org
Subject svn commit: r1812080 - in /openwebbeans/meecrowave/trunk/meecrowave-core/src: main/java/org/apache/meecrowave/cxf/MeecrowaveBus.java test/java/org/apache/meecrowave/ContextInProxiedInstancesTest.java
Date Fri, 13 Oct 2017 08:32:47 GMT
Author: struberg
Date: Fri Oct 13 08:32:47 2017
New Revision: 1812080

URL: http://svn.apache.org/viewvc?rev=1812080&view=rev
Log:
MEECROWAVE-75 properly unwrap interface proxies

e.g. for producer methods 

Modified:
    openwebbeans/meecrowave/trunk/meecrowave-core/src/main/java/org/apache/meecrowave/cxf/MeecrowaveBus.java
    openwebbeans/meecrowave/trunk/meecrowave-core/src/test/java/org/apache/meecrowave/ContextInProxiedInstancesTest.java

Modified: openwebbeans/meecrowave/trunk/meecrowave-core/src/main/java/org/apache/meecrowave/cxf/MeecrowaveBus.java
URL: http://svn.apache.org/viewvc/openwebbeans/meecrowave/trunk/meecrowave-core/src/main/java/org/apache/meecrowave/cxf/MeecrowaveBus.java?rev=1812080&r1=1812079&r2=1812080&view=diff
==============================================================================
--- openwebbeans/meecrowave/trunk/meecrowave-core/src/main/java/org/apache/meecrowave/cxf/MeecrowaveBus.java
(original)
+++ openwebbeans/meecrowave/trunk/meecrowave-core/src/main/java/org/apache/meecrowave/cxf/MeecrowaveBus.java
Fri Oct 13 08:32:47 2017
@@ -14,6 +14,9 @@ import org.apache.johnzon.jaxrs.JsrMessa
 import org.apache.johnzon.jaxrs.JsrMessageBodyWriter;
 import org.apache.johnzon.jaxrs.jsonb.jaxrs.JsonbJaxrsProvider;
 import org.apache.meecrowave.Meecrowave;
+import org.apache.webbeans.config.WebBeansContext;
+import org.apache.webbeans.proxy.InterceptorDecoratorProxyFactory;
+import org.apache.webbeans.proxy.NormalScopeProxyFactory;
 
 import javax.enterprise.context.ApplicationScoped;
 import javax.inject.Inject;
@@ -42,6 +45,8 @@ import static java.util.stream.Collector
 @ApplicationScoped
 public class MeecrowaveBus implements Bus {
     private final Bus delegate = new ExtensionManagerBus();
+    private NormalScopeProxyFactory normalScopeProxyFactory;
+    private InterceptorDecoratorProxyFactory interceptorDecoratorProxyFactory;
 
     protected MeecrowaveBus() {
         // no-op: for proxies
@@ -49,13 +54,11 @@ public class MeecrowaveBus implements Bu
 
     @Inject
     public MeecrowaveBus(final ServletContext context) {
-        setProperty(ClassUnwrapper.class.getName(), (ClassUnwrapper) o -> {
-            final Class<?> aClass = o.getClass();
-            if (aClass.getName().contains("$$")) {
-                return aClass.getSuperclass();
-            }
-            return aClass;
-        });
+        WebBeansContext webBeansContext = WebBeansContext.currentInstance();
+        normalScopeProxyFactory = webBeansContext.getNormalScopeProxyFactory();
+        interceptorDecoratorProxyFactory = webBeansContext.getInterceptorDecoratorProxyFactory();
+
+        setProperty(ClassUnwrapper.class.getName(), (ClassUnwrapper) this::getRealClass);
 
         final Meecrowave.Builder builder = Meecrowave.Builder.class.cast(context.getAttribute("meecrowave.configuration"));
         if (builder != null && builder.isJaxrsProviderSetup()) {
@@ -102,6 +105,35 @@ public class MeecrowaveBus implements Bu
         }
     }
 
+
+    /**
+     * Unwrap all proxies and get the real underlying class
+     * for detecting annotations, etc.
+     * @param o
+     * @return
+     */
+    protected Class<?> getRealClass(Object o) {
+        final Class<?> aClass = o.getClass();
+        if (aClass.getName().contains("$$")) {
+            Class realClass = aClass.getSuperclass();
+            if (realClass == Object.class || realClass.isInterface()) {
+                // we have to dig deeper as we might have a producer method for an interface
+                Class<?>[] interfaces = aClass.getInterfaces();
+                if (interfaces.length > 0) {
+                    Class<?> rootInterface = interfaces[0];
+                    for (Class<?> anInterface : interfaces) {
+                        if (rootInterface.isAssignableFrom(anInterface)) {
+                            rootInterface = anInterface;
+                        }
+                    }
+                    return rootInterface;
+                }
+            }
+            return realClass;
+        }
+        return aClass;
+    }
+
     @Override
     public <T> T getExtension(final Class<T> extensionType) {
         return delegate.getExtension(extensionType);

Modified: openwebbeans/meecrowave/trunk/meecrowave-core/src/test/java/org/apache/meecrowave/ContextInProxiedInstancesTest.java
URL: http://svn.apache.org/viewvc/openwebbeans/meecrowave/trunk/meecrowave-core/src/test/java/org/apache/meecrowave/ContextInProxiedInstancesTest.java?rev=1812080&r1=1812079&r2=1812080&view=diff
==============================================================================
--- openwebbeans/meecrowave/trunk/meecrowave-core/src/test/java/org/apache/meecrowave/ContextInProxiedInstancesTest.java
(original)
+++ openwebbeans/meecrowave/trunk/meecrowave-core/src/test/java/org/apache/meecrowave/ContextInProxiedInstancesTest.java
Fri Oct 13 08:32:47 2017
@@ -30,10 +30,12 @@ import javax.enterprise.inject.spi.CDI;
 import javax.servlet.ServletContext;
 import javax.ws.rs.GET;
 import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
 import javax.ws.rs.core.Context;
 import javax.ws.rs.core.UriInfo;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.Serializable;
 import java.net.URL;
 
 import static org.junit.Assert.assertEquals;
@@ -97,5 +99,29 @@ public class ContextInProxiedInstancesTe
         public String get() {
             return uri.getPath();
         }
+
+        @Produces
+        @RequestScoped
+        public MyRestApi createMyApi() {
+            return new MyRestApi() {
+                @Override
+                public String get() {
+                    return null;
+                }
+
+                @Override
+                public void close() throws Exception {
+
+                }
+            };
+        }
     }
+
+    @Path("myapi")
+    public  interface MyRestApi extends Serializable, AutoCloseable {
+        @GET
+        String get();
+    }
+
+
 }



Mime
View raw message