Author: rmannibucau
Date: Thu Nov 21 15:45:27 2013
New Revision: 1544222

URL: http://svn.apache.org/r1544222
Log:
OPENEJB-2046 taking into account local identity + entrering ThreadContext when 
appropriate and not too early in async methods

Modified:
    
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/core/ivm/EjbHomeProxyHandler.java
    
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/core/ivm/EjbObjectProxyHandler.java
    
tomee/tomee/trunk/container/openejb-core/src/test/java/org/apache/openejb/core/asynch/AsynchInRoleTest.java

Modified: 
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/core/ivm/EjbHomeProxyHandler.java
URL: 
http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/core/ivm/EjbHomeProxyHandler.java?rev=1544222&r1=1544221&r2=1544222&view=diff
==============================================================================
--- 
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/core/ivm/EjbHomeProxyHandler.java
 (original)
+++ 
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/core/ivm/EjbHomeProxyHandler.java
 Thu Nov 21 15:45:27 2013
@@ -318,23 +318,37 @@ public abstract class EjbHomeProxyHandle
 
         if (beanContext.isAsynchronous(method)) {
 
-            final SecurityService<?> securityService = 
SystemInstance.get().getComponent(SecurityService.class);
-            final Object state = securityService.currentState();
+            final SecurityService securityService = 
SystemInstance.get().getComponent(SecurityService.class);
+            Object stateTmp = securityService.currentState();
+            final boolean associate;
+            if (stateTmp == null) {
+                stateTmp = ClientSecurity.getIdentity();
+                associate = stateTmp != null;
+            } else {
+                associate = false;
+            }
+            final Object securityState = stateTmp;
             final ThreadContext currentCtx = ThreadContext.getThreadContext();
             final AsynchronousPool asynchronousPool = 
beanContext.getModuleContext().getAppContext().getAsynchronousPool();
 
             return asynchronousPool.invoke(new Callable<Object>() {
                 @Override
                 public Object call() throws Exception {
+                    final Object threadState;
+                    if (associate) {
+                        securityService.associate(securityState);
+                        threadState = null;
+                    } else {
+                        threadState = securityService.currentState();
+                        securityService.setState(securityState);
+                    }
+
                     final ThreadContext oldCtx; // ensure context is the same 
as for the caller
                     if (currentCtx != null) {
                         oldCtx = ThreadContext.enter(new 
ThreadContext(currentCtx));
                     } else {
                         oldCtx = null;
                     }
-
-                    final Object threadState = securityService.currentState();
-                    securityService.setState(state);
                     try {
                         return homeMethodInvoke(interfce, method, args);
                     } catch (ApplicationException ae) {
@@ -343,10 +357,14 @@ public abstract class EjbHomeProxyHandle
 
                         throw ae;
                     } finally {
-                        securityService.setState(threadState);
                         if (oldCtx != null) {
                             ThreadContext.exit(oldCtx);
                         }
+                        if (!associate) {
+                            securityService.setState(threadState);
+                        } else {
+                            securityService.disassociate();
+                        }
                     }
                 }
             }, method.getReturnType() == Void.TYPE);

Modified: 
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/core/ivm/EjbObjectProxyHandler.java
URL: 
http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/core/ivm/EjbObjectProxyHandler.java?rev=1544222&r1=1544221&r2=1544222&view=diff
==============================================================================
--- 
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/core/ivm/EjbObjectProxyHandler.java
 (original)
+++ 
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/core/ivm/EjbObjectProxyHandler.java
 Thu Nov 21 15:45:27 2013
@@ -241,14 +241,32 @@ public abstract class EjbObjectProxyHand
 
         if (beanContext.isAsynchronous(method)) {
 
-            final SecurityService<?> securityService = 
SystemInstance.get().getComponent(SecurityService.class);
-            final Object state = securityService.currentState();
+            final SecurityService securityService = 
SystemInstance.get().getComponent(SecurityService.class);
+            Object stateTmp = securityService.currentState();
+            final boolean associate;
+            if (stateTmp == null) {
+                stateTmp = ClientSecurity.getIdentity();
+                associate = stateTmp != null;
+            } else {
+                associate = false;
+            }
+            final Object securityState = stateTmp;
+
             final ThreadContext threadContext = 
ThreadContext.getThreadContext();
             final AsynchronousPool asynchronousPool = 
beanContext.getModuleContext().getAppContext().getAsynchronousPool();
 
             return asynchronousPool.invoke(new Callable<Object>() {
                 @Override
                 public Object call() throws Exception {
+                    final Object threadState;
+                    if (associate) {
+                        securityService.associate(securityState);
+                        threadState = null;
+                    } else {
+                        threadState = securityService.currentState();
+                        securityService.setState(securityState);
+                    }
+
                     final ThreadContext oldCtx; // ensure context is the same 
as for the caller
                     if (threadContext != null) {
                         oldCtx = ThreadContext.enter(new 
ThreadContext(threadContext));
@@ -256,8 +274,6 @@ public abstract class EjbObjectProxyHand
                         oldCtx = null;
                     }
 
-                    final Object threadState = securityService.currentState();
-                    securityService.setState(state);
                     try {
                         return synchronizedBusinessMethod(interfce, method, 
args);
                     } catch (ApplicationException ae) {
@@ -266,10 +282,14 @@ public abstract class EjbObjectProxyHand
 
                         throw ae;
                     } finally {
-                        securityService.setState(threadState);
                         if (oldCtx != null) {
                             ThreadContext.exit(oldCtx);
                         }
+                        if (!associate) {
+                            securityService.setState(threadState);
+                        } else {
+                            securityService.disassociate();
+                        }
                     }
                 }
             }, method.getReturnType() == Void.TYPE);

Modified: 
tomee/tomee/trunk/container/openejb-core/src/test/java/org/apache/openejb/core/asynch/AsynchInRoleTest.java
URL: 
http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/test/java/org/apache/openejb/core/asynch/AsynchInRoleTest.java?rev=1544222&r1=1544221&r2=1544222&view=diff
==============================================================================
--- 
tomee/tomee/trunk/container/openejb-core/src/test/java/org/apache/openejb/core/asynch/AsynchInRoleTest.java
 (original)
+++ 
tomee/tomee/trunk/container/openejb-core/src/test/java/org/apache/openejb/core/asynch/AsynchInRoleTest.java
 Thu Nov 21 15:45:27 2013
@@ -67,11 +67,8 @@ public class AsynchInRoleTest {
         assembler.destroy();
     }
 
-    @SuppressWarnings("UseOfSystemOutOrSystemErr")
     @Test
     public void testMethodScopeAsynch() throws Exception {
-        System.out.println(long.class.getName());
-        System.out.println(String[].class.getCanonicalName());
         //Build the application
         final AppModule app = new AppModule(this.getClass().getClassLoader(), 
"testasynch");
         final EjbJar ejbJar = new EjbJar();
@@ -82,7 +79,10 @@ public class AsynchInRoleTest {
         final AppInfo appInfo = config.configureApplication(app);
         assembler.createApplication(appInfo);
 
-        final InitialContext context = new InitialContext();
+        final Properties env = new Properties();
+        env.put(javax.naming.Context.SECURITY_PRINCIPAL, "jonathan");
+        env.put(javax.naming.Context.SECURITY_CREDENTIALS, "secret");
+        final InitialContext context = new InitialContext(env);
 
         final String[] beans = new String[]{"TestBeanCLocal", 
"TestBeanDLocal"};
         for (final String beanName : beans) {
@@ -100,6 +100,8 @@ public class AsynchInRoleTest {
             testBean.testD(Thread.currentThread().getId());
             Assert.assertEquals("testD was never executed", "testD", 
testBean.getLastInvokeMethod());
         }
+
+        context.close();
     }
 
     @Test
@@ -134,6 +136,8 @@ public class AsynchInRoleTest {
 
         test.testD(Thread.currentThread().getId());
         Assert.assertEquals("testD was never executed", "testD", 
test.getLastInvokeMethod());
+
+        context.close();
     }
 
     @Test
@@ -173,6 +177,8 @@ public class AsynchInRoleTest {
         test.testD(Thread.currentThread().getId());
         Thread.sleep(3000L);
         Assert.assertEquals("testD was never executed", "testD", 
test.getLastInvokeMethod());
+
+        context.close();
     }
 
     public interface TestBean {


Reply via email to