tomee-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rmannibu...@apache.org
Subject svn commit: r1607746 - in /tomee/tomee/trunk: container/openejb-core/src/main/java/org/apache/openejb/bval/ server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/ server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/
Date Thu, 03 Jul 2014 21:01:12 GMT
Author: rmannibucau
Date: Thu Jul  3 21:01:12 2014
New Revision: 1607746

URL: http://svn.apache.org/r1607746
Log:
ensuring our pojo are using correct tccl when invoked from jaxrs endpoints + fixing tests
(was not deterministic)

Added:
    tomee/tomee/trunk/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/PojoInvoker.java
      - copied, changed from r1607736, tomee/tomee/trunk/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/OpenEJBEJBInvoker.java
    tomee/tomee/trunk/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/JndiForPojoEndpointsTest.java
      - copied, changed from r1607736, tomee/tomee/trunk/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/CDIApplicationTest.java
Modified:
    tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/bval/ValidatorUtil.java
    tomee/tomee/trunk/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/AutoJAXRSInvoker.java
    tomee/tomee/trunk/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/OpenEJBEJBInvoker.java
    tomee/tomee/trunk/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/OpenEJBPerRequestPojoResourceProvider.java
    tomee/tomee/trunk/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/EJBAccessExceptionMapperTest.java

Modified: tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/bval/ValidatorUtil.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/bval/ValidatorUtil.java?rev=1607746&r1=1607745&r2=1607746&view=diff
==============================================================================
--- tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/bval/ValidatorUtil.java
(original)
+++ tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/bval/ValidatorUtil.java
Thu Jul  3 21:01:12 2014
@@ -81,11 +81,16 @@ public final class ValidatorUtil {
                         // so the behavior will be the same
                         // + this code should rarely be used
                         final ClassLoader tccl = Thread.currentThread().getContextClassLoader();
+                        if (tccl == null) {
+                            return null;
+                        }
+
                         final ContainerSystem containerSystem = SystemInstance.get().getComponent(ContainerSystem.class);
 
                         Object value = null;
                         for (final AppContext appContext : containerSystem.getAppContexts())
{
-                            if (appContext.getClassLoader().equals(tccl)) {
+                            final ClassLoader appContextClassLoader = appContext.getClassLoader();
+                            if (tccl.equals(appContextClassLoader) || appContextClassLoader.equals(tccl))
{
                                 final Collection<String> tested = new ArrayList<String>();
                                 for (final BeanContext bean : appContext.getBeanContexts())
{
                                     if (BeanContext.Comp.class.equals(bean.getBeanClass()))
{
@@ -111,7 +116,8 @@ public final class ValidatorUtil {
                                 break;
                             }
                             for (final WebContext web : appContext.getWebContexts()) {
-                                if (web.getClassLoader().equals(tccl)) {
+                                final ClassLoader webClassLoader = web.getClassLoader();
+                                if (webClassLoader.equals(tccl) || tccl.equals(webClassLoader))
{
                                     value = web.getJndiEnc().lookup(jndi);
                                     break;
                                 }

Modified: tomee/tomee/trunk/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/AutoJAXRSInvoker.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/AutoJAXRSInvoker.java?rev=1607746&r1=1607745&r2=1607746&view=diff
==============================================================================
--- tomee/tomee/trunk/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/AutoJAXRSInvoker.java
(original)
+++ tomee/tomee/trunk/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/AutoJAXRSInvoker.java
Thu Jul  3 21:01:12 2014
@@ -37,7 +37,7 @@ public class AutoJAXRSInvoker implements
         ejbs = restEjbs;
 
         // delegates
-        jaxrsInvoker = new JAXRSInvoker();
+        jaxrsInvoker = new PojoInvoker();
         if (!ejbs.isEmpty()) {
             ejbInvoker = new OpenEJBEJBInvoker(beanContexts(restEjbs));
         } else {
@@ -54,7 +54,7 @@ public class AutoJAXRSInvoker implements
     }
 
     @Override
-    public Object invoke(Exchange exchange, Object o) { // mainly a select the right invoker
impl
+    public Object invoke(final Exchange exchange, final Object o) { // mainly a select the
right invoker impl
         final ClassResourceInfo cri = (ClassResourceInfo) exchange.get("root.resource.class");
 
         if (cri != null) {

Modified: tomee/tomee/trunk/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/OpenEJBEJBInvoker.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/OpenEJBEJBInvoker.java?rev=1607746&r1=1607745&r2=1607746&view=diff
==============================================================================
--- tomee/tomee/trunk/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/OpenEJBEJBInvoker.java
(original)
+++ tomee/tomee/trunk/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/OpenEJBEJBInvoker.java
Thu Jul  3 21:01:12 2014
@@ -85,7 +85,8 @@ public class OpenEJBEJBInvoker extends J
     protected Object performInvocation(final Exchange exchange, final Object serviceObject,
                                        final Method m, final Object[] paramArray) throws
Exception {
         try {
-            return m.invoke(serviceObject, insertExchange(m, paramArray, exchange));
+            final Object[] args = super.insertExchange(m, paramArray, exchange);
+            return m.invoke(serviceObject, args);
         } catch (final InvocationTargetException ite) {
             Throwable cause = ite.getTargetException();
             // unwrap to get ExceptionMapper working

Modified: tomee/tomee/trunk/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/OpenEJBPerRequestPojoResourceProvider.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/OpenEJBPerRequestPojoResourceProvider.java?rev=1607746&r1=1607745&r2=1607746&view=diff
==============================================================================
--- tomee/tomee/trunk/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/OpenEJBPerRequestPojoResourceProvider.java
(original)
+++ tomee/tomee/trunk/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/OpenEJBPerRequestPojoResourceProvider.java
Thu Jul  3 21:01:12 2014
@@ -19,6 +19,7 @@ package org.apache.openejb.server.cxf.rs
 import org.apache.cxf.jaxrs.lifecycle.ResourceProvider;
 import org.apache.cxf.jaxrs.utils.InjectionUtils;
 import org.apache.cxf.jaxrs.utils.ResourceUtils;
+import org.apache.cxf.message.Exchange;
 import org.apache.cxf.message.Message;
 import org.apache.openejb.Injection;
 import org.apache.openejb.InjectionProcessor;
@@ -46,9 +47,11 @@ import java.lang.reflect.InvocationTarge
 import java.lang.reflect.Method;
 import java.lang.reflect.Proxy;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.HashSet;
 import java.util.Set;
+import java.util.logging.Level;
 
 public class OpenEJBPerRequestPojoResourceProvider implements ResourceProvider {
     protected final Collection<Injection> injections;
@@ -148,10 +151,12 @@ public class OpenEJBPerRequestPojoResour
             creator = new DefaultBeanCreator(m);
         }
         m.put(BeanCreator.class, creator);
+        m.put(OpenEJBPerRequestPojoResourceProvider.class, this);
 
         // important to switch of classloader to get the right InitialContext
-        final ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
-        Thread.currentThread().setContextClassLoader(classLoader);
+        final Thread thread = Thread.currentThread();
+        final ClassLoader oldLoader = thread.getContextClassLoader();
+        thread.setContextClassLoader(classLoader);
         try {
             return creator.create();
         } catch (final NoBeanFoundException nbfe) {
@@ -159,7 +164,7 @@ public class OpenEJBPerRequestPojoResour
             m.put(BeanCreator.class, creator);
             return creator.create();
         } finally {
-            Thread.currentThread().setContextClassLoader(oldLoader);
+            thread.setContextClassLoader(oldLoader);
         }
     }
 
@@ -181,6 +186,10 @@ public class OpenEJBPerRequestPojoResour
         return false;
     }
 
+    public ClassLoader getClassLoader() {
+        return classLoader;
+    }
+
     private static class InitialContextWrapper implements InvocationHandler {
         private final Context ctx;
 

Copied: tomee/tomee/trunk/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/PojoInvoker.java
(from r1607736, tomee/tomee/trunk/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/OpenEJBEJBInvoker.java)
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/PojoInvoker.java?p2=tomee/tomee/trunk/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/PojoInvoker.java&p1=tomee/tomee/trunk/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/OpenEJBEJBInvoker.java&r1=1607736&r2=1607746&rev=1607746&view=diff
==============================================================================
--- tomee/tomee/trunk/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/OpenEJBEJBInvoker.java
(original)
+++ tomee/tomee/trunk/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/PojoInvoker.java
Thu Jul  3 21:01:12 2014
@@ -18,92 +18,45 @@ package org.apache.openejb.server.cxf.rs
 
 import org.apache.cxf.jaxrs.JAXRSInvoker;
 import org.apache.cxf.message.Exchange;
+import org.apache.cxf.message.Message;
 import org.apache.openejb.ApplicationException;
-import org.apache.openejb.BeanContext;
 import org.apache.openejb.InvalidateReferenceException;
-import org.apache.openejb.core.interceptor.InterceptorData;
-import org.apache.openejb.monitoring.StatsInterceptor;
-import org.apache.openejb.util.proxy.BeanContextInvocationHandler;
-import org.apache.openejb.util.proxy.LocalBeanProxyFactory;
-import org.apache.openejb.util.proxy.ProxyManager;
 
-import java.lang.reflect.InvocationHandler;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.rmi.RemoteException;
-import java.util.*;
+import java.util.Arrays;
+import java.util.logging.Level;
 
-public class OpenEJBEJBInvoker extends JAXRSInvoker {
-    private final Map<Class<?>, Collection<Class<?>>> contextTypes
= new HashMap<Class<?>, Collection<Class<?>>>();
-
-    public OpenEJBEJBInvoker(final Collection<BeanContext> restEjbs) {
-        for (final BeanContext context : restEjbs) {
-            final Collection<Class<?>> classes = new HashSet<Class<?>>();
-            Contexts.findContextFields(context.getBeanClass(), classes);
-            for (final Collection<InterceptorData> list :
-                    Arrays.asList(
-                            context.getInterceptorData(),
-                            context.getInstanceScopedInterceptors(),
-                            context.getCallbackInterceptors())) {
-                for (final InterceptorData id : list) {
-                    final Class<?> interceptorClass = id.getInterceptorClass();
-                    if (!StatsInterceptor.class.equals(interceptorClass)) {
-                        Contexts.findContextFields(interceptorClass, classes);
-                    }
-                }
-            }
-            contextTypes.put(context.getBeanClass(), classes);
-        }
-    }
-
-    @Override
-    public Object invoke(final Exchange exchange, final Object request, final Object resourceObject)
{
-        Contexts.bind(exchange, getContextTypes(resourceObject));
-        return super.invoke(exchange, request, resourceObject);
-    }
-
-    private Collection<Class<?>> getContextTypes(final Object resourceObject)
{
-        if (!ProxyManager.isProxyClass(resourceObject.getClass())
-                && !LocalBeanProxyFactory.isProxy(resourceObject.getClass())) {
-            return Collections.emptySet();
-        }
-
-        final InvocationHandler handler = ProxyManager.getInvocationHandler(resourceObject);
-        if (!(handler instanceof BeanContextInvocationHandler)) {
-            return Collections.emptySet();
-        }
-
-        final BeanContext beanContext = ((BeanContextInvocationHandler) handler).getBeanContext();
-
-        if (beanContext == null) {
-            return Collections.emptySet();
-        }
-        return contextTypes.get(beanContext.getBeanClass());
-    }
-
-    @Override
+public class PojoInvoker extends JAXRSInvoker {
     protected Object performInvocation(final Exchange exchange, final Object serviceObject,
                                        final Method m, final Object[] paramArray) throws
Exception {
+        final Object[] args = insertExchange(m, paramArray, exchange);
+        final ClassLoader tcclToUse = getClassLoader(exchange);
+        final Thread thread = Thread.currentThread();
+        ClassLoader oldLoader = null;
+        if (tcclToUse != null) {
+            oldLoader = thread.getContextClassLoader();
+            thread.setContextClassLoader(tcclToUse);
+        }
         try {
-            return m.invoke(serviceObject, insertExchange(m, paramArray, exchange));
-        } catch (final InvocationTargetException ite) {
-            Throwable cause = ite.getTargetException();
-            // unwrap to get ExceptionMapper working
-            if (cause instanceof InvalidateReferenceException) {
-                cause = cause.getCause();
-                if (cause instanceof RemoteException) {
-                    cause = cause.getCause();
-                }
-            }
-
-            if (ApplicationException.class.isInstance(cause) && Exception.class.isInstance(cause.getCause()))
{
-                throw Exception.class.cast(ApplicationException.class.cast(cause).getCause());
+            return m.invoke(serviceObject, args);
+        } finally {
+            if (tcclToUse != null) {
+                thread.setContextClassLoader(oldLoader);
             }
+        }
+    }
 
-            if (cause instanceof Exception) {
-                throw (Exception) cause;
-            }
-            throw ite;
+    private ClassLoader getClassLoader(final Exchange exchange) {
+        final Message inMessage = exchange.getInMessage();
+        if (inMessage == null) {
+            return null;
+        }
+        final OpenEJBPerRequestPojoResourceProvider requestPojoResourceProvider = inMessage.get(OpenEJBPerRequestPojoResourceProvider.class);
+        if (requestPojoResourceProvider != null) {
+            return requestPojoResourceProvider.getClassLoader();
         }
+        return null;
     }
 }

Modified: tomee/tomee/trunk/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/EJBAccessExceptionMapperTest.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/EJBAccessExceptionMapperTest.java?rev=1607746&r1=1607745&r2=1607746&view=diff
==============================================================================
--- tomee/tomee/trunk/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/EJBAccessExceptionMapperTest.java
(original)
+++ tomee/tomee/trunk/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/EJBAccessExceptionMapperTest.java
Thu Jul  3 21:01:12 2014
@@ -64,7 +64,7 @@ public class EJBAccessExceptionMapperTes
 
     @Test
     public void rest() {
-        final Response response = WebClient.create("http://localhost:4204/openejb-cxf-rs").path("/ejb/rest").get();
+        final Response response = WebClient.create("http://localhost:4204/openejb-cxf-rs").path("/ejbsecu/rest").get();
         assertEquals(403, response.getStatus());
     }
 
@@ -72,7 +72,7 @@ public class EJBAccessExceptionMapperTes
     @Singleton
     @RolesAllowed("Something that does not exit at all")
     @Lock(LockType.READ)
-    @Path("/ejb")
+    @Path("/ejbsecu")
     public static class RESTIsCoolOne {
         @EJB
         private SimpleEJB simpleEJB;

Copied: tomee/tomee/trunk/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/JndiForPojoEndpointsTest.java
(from r1607736, 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/JndiForPojoEndpointsTest.java?p2=tomee/tomee/trunk/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/JndiForPojoEndpointsTest.java&p1=tomee/tomee/trunk/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/CDIApplicationTest.java&r1=1607736&r2=1607746&rev=1607746&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/JndiForPojoEndpointsTest.java
Thu Jul  3 21:01:12 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.EnableServices;
 import org.apache.openejb.testing.Module;
@@ -27,50 +26,47 @@ import org.junit.Test;
 import org.junit.runner.RunWith;
 
 import javax.inject.Inject;
+import javax.validation.Validator;
+import javax.validation.constraints.NotNull;
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
 import javax.ws.rs.core.Application;
-import java.util.HashSet;
-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 JndiForPojoEndpointsTest {
     @Module
-    @Classes(cdi = true, value = { MyCdiRESTApplication.class, MyFirstRestClass.class,  ACdiBeanInjectedInApp.class
})
+    @Classes(cdi = true, value = { JndiEndpoint.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());
+                .addServlet(Application.class.getName(), null, "/api/*");
     }
 
     @Test
-    public void isCdi() {
-        assertTrue(MyCdiRESTApplication.injection);
-        assertEquals("Hi from REST World!", WebClient.create("http://localhost:4204/foo/").path("/first/hi").get(String.class));
+    public void injectionWorked() {
+        assertEquals("1", WebClient.create("http://localhost:4204/foo/").path("/api/jndi").get(String.class));
     }
 
-    public static class ACdiBeanInjectedInApp {}
-
-    public static class MyCdiRESTApplication extends Application {
-        public static boolean injection = false;
-
+    @Path("jndi")
+    public static class JndiEndpoint {
         @Inject
-        private ACdiBeanInjectedInApp cdi;
+        private Validator val;
+
+        @GET
+        public int doIt() {
+            return val.validate(new ToVal()).size();
+        }
+    }
 
-        public Set<Class<?>> getClasses() {
-            injection = cdi != null;
+    public static class ToVal {
+        @NotNull
+        private String val;
 
-            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 String getVal() {
+            return val;
         }
     }
 }



Mime
View raw message