tomee-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rmannibu...@apache.org
Subject svn commit: r1607943 - in /tomee/tomee/trunk/container: openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/task/ openejb-concurrency-utilities-ee/src/test/java/org/apache/openejb/concurrencyutilities/test/ openejb...
Date Fri, 04 Jul 2014 21:39:31 GMT
Author: rmannibucau
Date: Fri Jul  4 21:39:31 2014
New Revision: 1607943

URL: http://svn.apache.org/r1607943
Log:
some cleanup in concurrency utilities + skipping ejbcontext checks when in a task - this should
be enhanced assing something in ThreadContext to validate we can skip this check more accurately

Added:
    tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/test/java/org/apache/openejb/concurrencyutilities/test/SecurityPropagationTest.java
Modified:
    tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/task/CUTask.java
    tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/core/BaseSessionContext.java
    tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/core/managed/ManagedContext.java
    tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/core/security/AbstractSecurityService.java
    tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/core/singleton/SingletonContext.java
    tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/core/stateful/StatefulContext.java
    tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/core/stateless/StatelessContext.java
    tomee/tomee/trunk/container/openejb-core/src/main/resources/META-INF/org.apache.openejb/service-jar.xml

Modified: tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/task/CUTask.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/task/CUTask.java?rev=1607943&r1=1607942&r2=1607943&view=diff
==============================================================================
--- tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/task/CUTask.java
(original)
+++ tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/task/CUTask.java
Fri Jul  4 21:39:31 2014
@@ -18,20 +18,34 @@ package org.apache.openejb.concurrencyut
 
 import org.apache.openejb.OpenEJBRuntimeException;
 import org.apache.openejb.core.ThreadContext;
+import org.apache.openejb.core.ivm.ClientSecurity;
+import org.apache.openejb.core.security.AbstractSecurityService;
 import org.apache.openejb.loader.SystemInstance;
 import org.apache.openejb.spi.SecurityService;
 
+import javax.security.auth.login.LoginException;
 import java.util.concurrent.Callable;
 
 public abstract class CUTask<T> extends ManagedTaskListenerTask {
-    private static final SecurityService<?> SECURITY_SERVICE = SystemInstance.get().getComponent(SecurityService.class);
+    private static final SecurityService SECURITY_SERVICE = SystemInstance.get().getComponent(SecurityService.class);
 
     private final Context initialContext;
 
     public CUTask(final Object task) {
         super(task);
 
-        initialContext = new Context(SECURITY_SERVICE.currentState(), ThreadContext.getThreadContext(),
Thread.currentThread().getContextClassLoader());
+        Object stateTmp = SECURITY_SERVICE.currentState();
+        final boolean associate;
+        if (stateTmp == null) {
+            stateTmp = ClientSecurity.getIdentity();
+            associate = stateTmp != null;
+        } else {
+            associate = false;
+        }
+        final ThreadContext threadContext = ThreadContext.getThreadContext();
+        initialContext = new Context(
+                associate, stateTmp, threadContext == null ? null : threadContext.get(AbstractSecurityService.SecurityContext.class),
+                threadContext, Thread.currentThread().getContextClassLoader());
     }
 
     protected T invoke(final Callable<T> call) throws Exception{
@@ -71,6 +85,8 @@ public abstract class CUTask<T> extends 
         private final Object securityServiceState;
         private final ThreadContext threadContext;
         private final ClassLoader loader;
+        private final boolean associate;
+        private final AbstractSecurityService.SecurityContext securityContext;
 
         /* propagation of CDI context seems wrong
         private final CdiAppContextsService contextService;
@@ -79,8 +95,12 @@ public abstract class CUTask<T> extends 
 
         private Context currentContext = null;
 
-        private Context(final Object initialSecurityServiceState, final ThreadContext initialThreadContext,
final ClassLoader initialLoader) {
+        private Context(boolean associate, final Object initialSecurityServiceState,
+                        final AbstractSecurityService.SecurityContext securityContext, final
ThreadContext initialThreadContext,
+                        final ClassLoader initialLoader) {
+            this.associate = associate;
             this.securityServiceState = initialSecurityServiceState;
+            this.securityContext = securityContext;
             this.threadContext = initialThreadContext;
             this.loader = initialLoader;
 
@@ -102,17 +122,30 @@ public abstract class CUTask<T> extends 
             final ClassLoader oldCl = thread.getContextClassLoader();
             thread.setContextClassLoader(loader);
 
+            final Object threadState;
+            if (associate) {
+                //noinspection unchecked
+                try {
+                    SECURITY_SERVICE.associate(securityServiceState);
+                } catch (final LoginException e) {
+                    throw new IllegalStateException(e);
+                }
+                threadState = null;
+            } else {
+                threadState = SECURITY_SERVICE.currentState();
+                SECURITY_SERVICE.setState(securityServiceState);
+            }
+
             final ThreadContext oldCtx;
             if (threadContext != null) {
-                oldCtx = ThreadContext.enter(new ThreadContext(threadContext));
+                final ThreadContext newContext = new ThreadContext(threadContext);
+                newContext.set(AbstractSecurityService.ProvidedSecurityContext.class, new
AbstractSecurityService.ProvidedSecurityContext(securityContext));
+                oldCtx = ThreadContext.enter(newContext);
             } else {
                 oldCtx = null;
             }
 
-            final Object threadState = SECURITY_SERVICE.currentState();
-            SECURITY_SERVICE.setState(securityServiceState);
-
-            currentContext = new Context(threadState, oldCtx, oldCl);
+            currentContext = new Context(associate, threadState, securityContext, oldCtx,
oldCl);
 
             /* propagation of CDI context seems wrong
             if (cdiState != null) {
@@ -122,12 +155,16 @@ public abstract class CUTask<T> extends 
         }
 
         public void exit() {
-            SECURITY_SERVICE.setState(currentContext.securityServiceState);
-
             if (currentContext.threadContext != null) {
                 ThreadContext.exit(currentContext.threadContext);
             }
 
+            if (!associate) {
+                SECURITY_SERVICE.setState(currentContext.securityServiceState);
+            } else {
+                SECURITY_SERVICE.disassociate();
+            }
+
             /* propagation of CDI context seems wrong
             if (currentContext.cdiState != null) {
                 contextService.restoreState(currentContext.cdiState);

Added: tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/test/java/org/apache/openejb/concurrencyutilities/test/SecurityPropagationTest.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/test/java/org/apache/openejb/concurrencyutilities/test/SecurityPropagationTest.java?rev=1607943&view=auto
==============================================================================
--- tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/test/java/org/apache/openejb/concurrencyutilities/test/SecurityPropagationTest.java
(added)
+++ tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/test/java/org/apache/openejb/concurrencyutilities/test/SecurityPropagationTest.java
Fri Jul  4 21:39:31 2014
@@ -0,0 +1,112 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.openejb.concurrencyutilities.test;
+
+import org.apache.openejb.jee.EnterpriseBean;
+import org.apache.openejb.jee.StatelessBean;
+import org.apache.openejb.junit.ApplicationComposer;
+import org.apache.openejb.testing.Module;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import javax.annotation.Resource;
+import javax.annotation.security.RunAs;
+import javax.ejb.EJB;
+import javax.ejb.EJBContext;
+import javax.ejb.SessionContext;
+import javax.ejb.Stateless;
+import javax.enterprise.concurrent.ManagedExecutorService;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+import java.security.Principal;
+import java.util.concurrent.Future;
+
+import static org.junit.Assert.assertTrue;
+
+@RunWith(ApplicationComposer.class)
+public class SecurityPropagationTest {
+    @Module
+    public EnterpriseBean bean() {
+        return new StatelessBean(ExecutorBean.class).localBean();
+    }
+
+    @EJB
+    private ExecutorBean bean;
+
+    @Test
+    public void checkItIsTrue() throws Exception {
+        bean.submit(new RunnableTest()).get();
+    }
+
+    @Stateless
+    @RunAs("tomee")
+    public static class ExecutorBean {
+        @Resource
+        private ManagedExecutorService executorService;
+
+        @Resource
+        private EJBContext ejbContext;
+
+        public Future<?> submit(final RunnableTest task) throws NamingException {
+            final Principal principal = ejbContext.getCallerPrincipal();
+            task.setExpectedPrincipal(principal);
+            return executorService.submit(task);
+        }
+    }
+
+    public static class RunnableTest implements Runnable {
+        private Principal expectedPrincipal;
+
+        public void setExpectedPrincipal(Principal expectedPrincipal) {
+            this.expectedPrincipal = expectedPrincipal;
+        }
+
+        @Override
+        public void run() {
+            try {
+                Thread.sleep(200);
+            } catch (InterruptedException e) {
+                Thread.interrupted();
+            }
+
+            final InitialContext initialContext;
+            try {
+                initialContext = new InitialContext();
+            } catch (NamingException e) {
+                throw new RuntimeException(e);
+            }
+            final EJBContext ejbContext;
+            try {
+                ejbContext = (SessionContext) initialContext.lookup("java:comp/EJBContext");
+            } catch (NamingException e) {
+                throw new RuntimeException(e);
+            }
+
+            final Principal callerPrincipal = ejbContext.getCallerPrincipal();
+            if (expectedPrincipal != null) {
+                if (!expectedPrincipal.equals(callerPrincipal)) {
+                    throw new IllegalStateException("the caller principal " + callerPrincipal
+ " is not the expected " + expectedPrincipal);
+                }
+            } else {
+                if (callerPrincipal != null) {
+                    throw new IllegalStateException("the caller principal " + callerPrincipal
+ " is not the expected " + expectedPrincipal);
+                }
+            }
+        }
+
+    }
+}

Modified: tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/core/BaseSessionContext.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/core/BaseSessionContext.java?rev=1607943&r1=1607942&r2=1607943&view=diff
==============================================================================
--- tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/core/BaseSessionContext.java
(original)
+++ tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/core/BaseSessionContext.java
Fri Jul  4 21:39:31 2014
@@ -57,6 +57,9 @@ public abstract class BaseSessionContext
         final ThreadContext threadContext = ThreadContext.getThreadContext();
         final BeanContext di = threadContext.getBeanContext();
         final Method runningMethod = threadContext.get(Method.class);
+        if (runningMethod == null) {
+            throw new IllegalStateException("No running method");
+        }
         if (di.isAsynchronous(runningMethod)) {
             if(runningMethod.getReturnType() == void.class) {
                 throw new IllegalStateException("Current running method " + runningMethod.getName()
+ " is an asynchronous method, but its return type is void :" + di.getDestinationId());

Modified: tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/core/managed/ManagedContext.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/core/managed/ManagedContext.java?rev=1607943&r1=1607942&r2=1607943&view=diff
==============================================================================
--- tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/core/managed/ManagedContext.java
(original)
+++ tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/core/managed/ManagedContext.java
Fri Jul  4 21:39:31 2014
@@ -37,7 +37,12 @@ public class ManagedContext extends Base
 
     @Override
     public void check(final Call call) {
-        final Operation operation = ThreadContext.getThreadContext().getCurrentOperation();
+        final ThreadContext threadContext = ThreadContext.getThreadContext();
+        final Operation operation = threadContext.getCurrentOperation();
+        if (operation == null) {
+            return; // concurrency utilities or similar
+        }
+
         switch (call) {
             case getCallerPrincipal:
             case isCallerInRole:

Modified: tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/core/security/AbstractSecurityService.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/core/security/AbstractSecurityService.java?rev=1607943&r1=1607942&r2=1607943&view=diff
==============================================================================
--- tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/core/security/AbstractSecurityService.java
(original)
+++ tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/core/security/AbstractSecurityService.java
Fri Jul  4 21:39:31 2014
@@ -146,18 +146,22 @@ public abstract class AbstractSecuritySe
             runAsSubject = getRunAsSubject(oldContext.getBeanContext());
         }
 
-        SecurityContext securityContext = oldContext != null ? oldContext.get(SecurityContext.class)
: null;
-        if (runAsSubject != null) {
-
-            securityContext = new SecurityContext(runAsSubject);
-
-        } else if (securityContext == null) {
-
-            final Identity identity = clientIdentity.get();
-            if (identity != null) {
-                securityContext = new SecurityContext(identity.subject);
-            } else {
-                securityContext = defaultContext;
+        final ProvidedSecurityContext providedSecurityContext = newContext.get(ProvidedSecurityContext.class);
+        SecurityContext securityContext = oldContext != null ? oldContext.get(SecurityContext.class)
:
+                (providedSecurityContext != null ? providedSecurityContext.context : null);
+        if (providedSecurityContext == null) {
+            if (runAsSubject != null) {
+
+                securityContext = new SecurityContext(runAsSubject);
+
+            } else if (securityContext == null) {
+
+                final Identity identity = clientIdentity.get();
+                if (identity != null) {
+                    securityContext = new SecurityContext(identity.subject);
+                } else {
+                    securityContext = defaultContext;
+                }
             }
         }
 
@@ -360,7 +364,15 @@ public abstract class AbstractSecuritySe
         }
     }
 
-    protected static final class SecurityContext {
+    public static final class ProvidedSecurityContext {
+        public final SecurityContext context;
+
+        public ProvidedSecurityContext(SecurityContext context) {
+            this.context = context;
+        }
+    }
+
+    public static final class SecurityContext {
 
         public final Subject subject;
         public final AccessControlContext acc;

Modified: tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/core/singleton/SingletonContext.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/core/singleton/SingletonContext.java?rev=1607943&r1=1607942&r2=1607943&view=diff
==============================================================================
--- tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/core/singleton/SingletonContext.java
(original)
+++ tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/core/singleton/SingletonContext.java
Fri Jul  4 21:39:31 2014
@@ -34,6 +34,9 @@ public class SingletonContext extends Ba
     @Override
     public void check(final Call call) {
         final Operation operation = ThreadContext.getThreadContext().getCurrentOperation();
+        if (operation == null) {
+            return; // concurrency utilities or similar
+        }
 
         switch (call) {
             case getEJBLocalObject:

Modified: tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/core/stateful/StatefulContext.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/core/stateful/StatefulContext.java?rev=1607943&r1=1607942&r2=1607943&view=diff
==============================================================================
--- tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/core/stateful/StatefulContext.java
(original)
+++ tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/core/stateful/StatefulContext.java
Fri Jul  4 21:39:31 2014
@@ -38,6 +38,10 @@ public class StatefulContext extends Bas
     @Override
     public void check(final Call call) {
         final Operation operation = ThreadContext.getThreadContext().getCurrentOperation();
+        if (operation == null) {
+            return; // concurrency utilities or similar
+        }
+
         switch (call) {
             case getCallerPrincipal:
             case isCallerInRole:

Modified: tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/core/stateless/StatelessContext.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/core/stateless/StatelessContext.java?rev=1607943&r1=1607942&r2=1607943&view=diff
==============================================================================
--- tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/core/stateless/StatelessContext.java
(original)
+++ tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/core/stateless/StatelessContext.java
Fri Jul  4 21:39:31 2014
@@ -40,6 +40,9 @@ public class StatelessContext extends Ba
     @Override
     public void check(final Call call) {
         final Operation operation = ThreadContext.getThreadContext().getCurrentOperation();
+        if (operation == null) {
+            return; // concurrency utilities or similar
+        }
 
         switch (call) {
             case getEJBLocalObject:

Modified: tomee/tomee/trunk/container/openejb-core/src/main/resources/META-INF/org.apache.openejb/service-jar.xml
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/main/resources/META-INF/org.apache.openejb/service-jar.xml?rev=1607943&r1=1607942&r2=1607943&view=diff
==============================================================================
--- tomee/tomee/trunk/container/openejb-core/src/main/resources/META-INF/org.apache.openejb/service-jar.xml
(original)
+++ tomee/tomee/trunk/container/openejb-core/src/main/resources/META-INF/org.apache.openejb/service-jar.xml
Fri Jul  4 21:39:31 2014
@@ -1120,7 +1120,6 @@
     Max = 25
     KeepAlive = 5 s
     Queue = 15
-    WaitAtShutdown = 30 seconds
     ThreadFactory = org.apache.openejb.concurrencyutilities.ee.impl.ManagedThreadFactoryImpl
   </ServiceProvider>
   <ServiceProvider id="Default Scheduled Executor Service"
@@ -1129,7 +1128,6 @@
                    factory-name="create"
                    class-name="org.apache.openejb.concurrencyutilities.ee.factory.ManagedScheduledExecutorServiceImplFactory">
     Core = 5
-    WaitAtShutdown = 30 seconds
     ThreadFactory = org.apache.openejb.concurrencyutilities.ee.impl.ManagedThreadFactoryImpl
   </ServiceProvider>
   <ServiceProvider id="Default Managed Thread Factory"



Mime
View raw message