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 2012/03/28 13:41:05 UTC

svn commit: r1306267 - /openejb/trunk/openejb/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/OpenEJBPerRequestPojoResourceProvider.java

Author: rmannibucau
Date: Wed Mar 28 11:41:05 2012
New Revision: 1306267

URL: http://svn.apache.org/viewvc?rev=1306267&view=rev
Log:
better handling of the context to find resources in rest pojos

Modified:
    openejb/trunk/openejb/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/OpenEJBPerRequestPojoResourceProvider.java

Modified: openejb/trunk/openejb/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/OpenEJBPerRequestPojoResourceProvider.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/OpenEJBPerRequestPojoResourceProvider.java?rev=1306267&r1=1306266&r2=1306267&view=diff
==============================================================================
--- openejb/trunk/openejb/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/OpenEJBPerRequestPojoResourceProvider.java (original)
+++ openejb/trunk/openejb/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/OpenEJBPerRequestPojoResourceProvider.java Wed Mar 28 11:41:05 2012
@@ -23,7 +23,6 @@ import java.util.ArrayList;
 import java.util.Collection;
 import javax.naming.Context;
 import javax.naming.InitialContext;
-import javax.naming.NamingException;
 import javax.ws.rs.WebApplicationException;
 import org.apache.cxf.jaxrs.lifecycle.PerRequestResourceProvider;
 import org.apache.cxf.message.Message;
@@ -37,29 +36,11 @@ public class OpenEJBPerRequestPojoResour
     protected Context context;
     protected WebBeansContext webbeansContext;
 
-    public OpenEJBPerRequestPojoResourceProvider(Class<?> clazz, Collection<Injection> injectionCollection, Context ctx, WebBeansContext owbCtx) {
+    public OpenEJBPerRequestPojoResourceProvider(final Class<?> clazz, final Collection<Injection> injectionCollection, final Context initialContext, final WebBeansContext owbCtx) {
         super(clazz);
         injections = injectionCollection;
         webbeansContext = owbCtx;
-        context = ctx;
-        if (ctx == null) {
-            // TODO: context shouldn't be null here so it should be removed
-            context = (Context) Proxy.newProxyInstance(clazz.getClassLoader(), new Class<?>[]{Context.class}, new InvocationHandler() {
-                @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
-                    Context ctx = new InitialContext();
-                    if (method.getName().equals("lookup")) {
-                        if (args[0].getClass().equals(String.class)) {
-                            try {
-                                return ctx.lookup("java:" + String.class.cast(args[0]));
-                            } catch (NamingException ne) {
-                                // let try it in the normal way (without java:)
-                            }
-                        }
-                    }
-                    return method.invoke(ctx, args);
-                }
-            });
-        }
+        context = (Context) Proxy.newProxyInstance(clazz.getClassLoader(), new Class<?>[]{Context.class}, new InitialContextWrapper(initialContext));
     }
 
     protected Object createInstance(Message m) {
@@ -79,4 +60,47 @@ public class OpenEJBPerRequestPojoResour
             throw new WebApplicationException(e);
         }
     }
+
+    private static class InitialContextWrapper implements InvocationHandler {
+        private Context ctx;
+
+        public InitialContextWrapper(Context initialContext) {
+            ctx = initialContext;
+        }
+
+        @Override
+        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
+            if (method.getName().equals("lookup")) {
+                final String name = "java:" + String.class.cast(args[0]);
+                if (args[0].getClass().equals(String.class)) {
+                    // Note: we catch exception instead of namingexception
+                    // because in environment with proxies on Context
+                    // InvocationtargetException can be throuwn instead of NamingException
+                    if (ctx != null) {
+                        try {
+                            return ctx.lookup(name);
+                        } catch (Exception ne) {
+                            try {
+                                return ctx.lookup(String.class.cast(args[0]));
+                            } catch (Exception ignored) {
+                                // no-op
+                            }
+                        }
+                    } else {
+                        final Context initialContext = new InitialContext();
+                        try {
+                            return initialContext.lookup(name);
+                        } catch (Exception swallowed) {
+                            try {
+                                return initialContext.lookup(String.class.cast(args[0]));
+                            } catch (Exception ignored) {
+                                // no-op
+                            }
+                        }
+                    }
+                }
+            }
+            return method.invoke(ctx, args);
+        }
+    }
 }