[jboss-cvs] JBossAS SVN: r84140 - in projects/ejb3/trunk/async: src/main/java/org/jboss/ejb3/async/interceptor and 5 other directories.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Thu Feb 12 15:33:43 EST 2009
Author: ALRubinger
Date: 2009-02-12 15:33:42 -0500 (Thu, 12 Feb 2009)
New Revision: 84140
Added:
projects/ejb3/trunk/async/src/main/java/org/jboss/ejb3/async/interceptor/SecurityActions.java
projects/ejb3/trunk/async/src/test/java/org/jboss/ejb3/async/test/common/
projects/ejb3/trunk/async/src/test/java/org/jboss/ejb3/async/test/common/SecurityActions.java
projects/ejb3/trunk/async/src/test/java/org/jboss/ejb3/async/test/common/TestConstants.java
projects/ejb3/trunk/async/src/test/java/org/jboss/ejb3/async/test/security/
projects/ejb3/trunk/async/src/test/java/org/jboss/ejb3/async/test/security/SecurityAwarePojo.java
projects/ejb3/trunk/async/src/test/java/org/jboss/ejb3/async/test/security/unit/
projects/ejb3/trunk/async/src/test/java/org/jboss/ejb3/async/test/security/unit/SecurityContextPropagationTestCase.java
Modified:
projects/ejb3/trunk/async/pom.xml
projects/ejb3/trunk/async/src/main/java/org/jboss/ejb3/async/interceptor/AsynchronousInterceptor.java
projects/ejb3/trunk/async/src/test/java/org/jboss/ejb3/async/test/simple/unit/SimpleAsyncTestCase.java
Log:
[EJBTHREE-1721] EJB 3.1 @Asynchronous SecurityContext propagation and tests, some other dev fixes
Modified: projects/ejb3/trunk/async/pom.xml
===================================================================
--- projects/ejb3/trunk/async/pom.xml 2009-02-12 19:46:18 UTC (rev 84139)
+++ projects/ejb3/trunk/async/pom.xml 2009-02-12 20:33:42 UTC (rev 84140)
@@ -78,6 +78,8 @@
<!-- Versioning -->
<version.org.jboss.ejb3_jboss.ejb3.api>3.1.0-SNAPSHOT</version.org.jboss.ejb3_jboss.ejb3.api>
<version.org.jboss.ejb3_jboss.ejb3.interceptors>1.0.0</version.org.jboss.ejb3_jboss.ejb3.interceptors>
+ <version.org.jboss.security_jboss.security>2.0.2.SP6</version.org.jboss.security_jboss.security>
+ <version.org.jboss.javaee_jboss.jacc.api>1.1.0.GA</version.org.jboss.javaee_jboss.jacc.api>
</properties>
@@ -126,5 +128,28 @@
<scope>test</scope>
</dependency>
+ <!-- org.jboss.security:jboss-security-spi -->
+ <dependency>
+ <groupId>org.jboss.security</groupId>
+ <artifactId>jboss-security-spi</artifactId>
+ <version>${version.org.jboss.security_jboss.security}</version>
+ </dependency>
+
+ <!-- org.jboss.security:jbosssx-client -->
+ <dependency>
+ <groupId>org.jboss.security</groupId>
+ <artifactId>jbosssx-client</artifactId>
+ <version>${version.org.jboss.security_jboss.security}</version>
+ <scope>test</scope>
+ </dependency>
+
+ <!-- org.jboss.javaee:jboss-jacc-api -->
+ <dependency>
+ <groupId>org.jboss.javaee</groupId>
+ <artifactId>jboss-jacc-api</artifactId>
+ <version>${version.org.jboss.javaee_jboss.jacc.api}</version>
+ <scope>test</scope>
+ </dependency>
+
</dependencies>
</project>
Modified: projects/ejb3/trunk/async/src/main/java/org/jboss/ejb3/async/interceptor/AsynchronousInterceptor.java
===================================================================
--- projects/ejb3/trunk/async/src/main/java/org/jboss/ejb3/async/interceptor/AsynchronousInterceptor.java 2009-02-12 19:46:18 UTC (rev 84139)
+++ projects/ejb3/trunk/async/src/main/java/org/jboss/ejb3/async/interceptor/AsynchronousInterceptor.java 2009-02-12 20:33:42 UTC (rev 84140)
@@ -35,6 +35,7 @@
import org.jboss.ejb3.async.future.AsyncFutureWrapper;
import org.jboss.ejb3.async.hack.DevelopmentHacks;
import org.jboss.logging.Logger;
+import org.jboss.security.SecurityContext;
/**
* AsynchronousInterceptor
@@ -115,8 +116,11 @@
// Get the ExecutorService
ExecutorService executorService = container.getAsynchronousExecutor();
+ // Get the existing SecurityContext
+ SecurityContext sc = SecurityActions.getSecurityContext();
+
// Make the asynchronous task from the invocation
- Callable<Object> asyncTask = new AsyncInvocationTask<Object>(invocation);
+ Callable<Object> asyncTask = new AsyncInvocationTask<Object>(invocation.copy(), sc);
// Short-circuit the invocation into new Thread
Future<Object> task = executorService.submit(asyncTask);
@@ -145,7 +149,7 @@
Method actualMethod = si.getActualMethod();
// Determine if asynchronous (either returns Future or has @Asynchronous)
- if (actualMethod.getAnnotation(Asynchronous.class) != null || actualMethod.getReturnType().equals(Future.class))
+ if (invocation.resolveAnnotation(Asynchronous.class) != null || actualMethod.getReturnType().equals(Future.class))
{
// Log
if (log.isTraceEnabled())
@@ -202,22 +206,38 @@
{
private Invocation invocation;
- public AsyncInvocationTask(Invocation invocation)
+ /**
+ * SecurityContext to use for the invocation
+ */
+ private SecurityContext sc;
+
+ public AsyncInvocationTask(Invocation invocation, SecurityContext sc)
{
this.invocation = invocation;
+ this.sc = sc;
}
@SuppressWarnings("unchecked")
public V call() throws Exception
{
+ // Get existing security context
+ SecurityContext oldSc = SecurityActions.getSecurityContext();
+
try
{
+ // Set new sc
+ SecurityActions.setSecurityContext(this.sc);
return (V) invocation.invokeNext();
}
catch (Throwable t)
{
throw new Exception(t);
}
+ finally
+ {
+ // Replace the old security context
+ SecurityActions.setSecurityContext(oldSc);
+ }
}
}
Added: projects/ejb3/trunk/async/src/main/java/org/jboss/ejb3/async/interceptor/SecurityActions.java
===================================================================
--- projects/ejb3/trunk/async/src/main/java/org/jboss/ejb3/async/interceptor/SecurityActions.java (rev 0)
+++ projects/ejb3/trunk/async/src/main/java/org/jboss/ejb3/async/interceptor/SecurityActions.java 2009-02-12 20:33:42 UTC (rev 84140)
@@ -0,0 +1,62 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.async.interceptor;
+
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+
+import org.jboss.security.SecurityContext;
+import org.jboss.security.SecurityContextAssociation;
+
+/**
+ * SecurityActions
+ *
+ * Self-contained privileged actions
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+class SecurityActions
+{
+ static SecurityContext getSecurityContext()
+ {
+ return AccessController.doPrivileged(new PrivilegedAction<SecurityContext>()
+ {
+ public SecurityContext run()
+ {
+ return SecurityContextAssociation.getSecurityContext();
+ }
+ });
+ }
+
+ static void setSecurityContext(final SecurityContext sc)
+ {
+ AccessController.doPrivileged(new PrivilegedAction<Void>()
+ {
+ public Void run()
+ {
+ SecurityContextAssociation.setSecurityContext(sc);
+ return null;
+ }
+ });
+ }
+}
Added: projects/ejb3/trunk/async/src/test/java/org/jboss/ejb3/async/test/common/SecurityActions.java
===================================================================
--- projects/ejb3/trunk/async/src/test/java/org/jboss/ejb3/async/test/common/SecurityActions.java (rev 0)
+++ projects/ejb3/trunk/async/src/test/java/org/jboss/ejb3/async/test/common/SecurityActions.java 2009-02-12 20:33:42 UTC (rev 84140)
@@ -0,0 +1,66 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.async.test.common;
+
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+
+import org.jboss.security.SecurityContext;
+import org.jboss.security.SecurityContextAssociation;
+
+/**
+ * SecurityActions
+ *
+ * Privileged actions available publically.
+ *
+ * Note that this implementation may have exported visibility
+ * only by nature of tha tit's in a test package, and not
+ * in the main of ejb3-async
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+public class SecurityActions
+{
+ public static SecurityContext getSecurityContext()
+ {
+ return AccessController.doPrivileged(new PrivilegedAction<SecurityContext>()
+ {
+ public SecurityContext run()
+ {
+ return SecurityContextAssociation.getSecurityContext();
+ }
+ });
+ }
+
+ public static void setSecurityContext(final SecurityContext sc)
+ {
+ AccessController.doPrivileged(new PrivilegedAction<Void>()
+ {
+ public Void run()
+ {
+ SecurityContextAssociation.setSecurityContext(sc);
+ return null;
+ }
+ });
+ }
+}
Added: projects/ejb3/trunk/async/src/test/java/org/jboss/ejb3/async/test/common/TestConstants.java
===================================================================
--- projects/ejb3/trunk/async/src/test/java/org/jboss/ejb3/async/test/common/TestConstants.java (rev 0)
+++ projects/ejb3/trunk/async/src/test/java/org/jboss/ejb3/async/test/common/TestConstants.java 2009-02-12 20:33:42 UTC (rev 84140)
@@ -0,0 +1,37 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.async.test.common;
+
+/**
+ * TestConstants
+ *
+ * A series of contracted values used in testing
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+public interface TestConstants
+{
+ String DOMAIN_ASYNC = "Asynchronous Container";
+
+ String AOP_DEPLOYABLE_FILENAME_SIMPLE = "simple/jboss-aop.xml";
+}
Added: projects/ejb3/trunk/async/src/test/java/org/jboss/ejb3/async/test/security/SecurityAwarePojo.java
===================================================================
--- projects/ejb3/trunk/async/src/test/java/org/jboss/ejb3/async/test/security/SecurityAwarePojo.java (rev 0)
+++ projects/ejb3/trunk/async/src/test/java/org/jboss/ejb3/async/test/security/SecurityAwarePojo.java 2009-02-12 20:33:42 UTC (rev 84140)
@@ -0,0 +1,53 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.async.test.security;
+
+import java.util.concurrent.Future;
+
+import javax.ejb.AsyncResult;
+
+import org.jboss.ejb3.async.test.common.SecurityActions;
+import org.jboss.security.SecurityContext;
+
+/**
+ * SecurityAwarPojo
+ *
+ * POJO for testing Security Context propagation in @Asynchronous
+ * invocations
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+public class SecurityAwarePojo
+{
+
+ /**
+ * Returns the security context associated with this Thread
+ *
+ * @return
+ */
+ public Future<SecurityContext> getSecurityContext()
+ {
+ return new AsyncResult<SecurityContext>(SecurityActions.getSecurityContext());
+ }
+
+}
Added: projects/ejb3/trunk/async/src/test/java/org/jboss/ejb3/async/test/security/unit/SecurityContextPropagationTestCase.java
===================================================================
--- projects/ejb3/trunk/async/src/test/java/org/jboss/ejb3/async/test/security/unit/SecurityContextPropagationTestCase.java (rev 0)
+++ projects/ejb3/trunk/async/src/test/java/org/jboss/ejb3/async/test/security/unit/SecurityContextPropagationTestCase.java 2009-02-12 20:33:42 UTC (rev 84140)
@@ -0,0 +1,128 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.async.test.security.unit;
+
+import java.util.concurrent.Future;
+
+import junit.framework.TestCase;
+
+import org.jboss.aspects.common.AOPDeployer;
+import org.jboss.ejb3.async.test.common.SecurityActions;
+import org.jboss.ejb3.async.test.common.TestConstants;
+import org.jboss.ejb3.async.test.container.AsyncContainer;
+import org.jboss.ejb3.async.test.security.SecurityAwarePojo;
+import org.jboss.ejb3.interceptors.container.BeanContext;
+import org.jboss.logging.Logger;
+import org.jboss.security.SecurityContext;
+import org.jboss.security.client.SecurityClient;
+import org.jboss.security.client.SecurityClientFactory;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+/**
+ * SecurityContextPropagationTestCase
+ *
+ * Tests for SecurityContext propagation in
+ * EJB 3.1 @Asynchronous invocations
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+public class SecurityContextPropagationTestCase
+{
+
+ // --------------------------------------------------------------------------------||
+ // Class Members ------------------------------------------------------------------||
+ // --------------------------------------------------------------------------------||
+
+ private static final Logger log = Logger.getLogger(SecurityContextPropagationTestCase.class);
+
+ private static AOPDeployer aopDeployer = new AOPDeployer(TestConstants.AOP_DEPLOYABLE_FILENAME_SIMPLE);
+
+ private static AsyncContainer<SecurityAwarePojo> container;
+
+ /*
+ * Method names in Test POJO
+ */
+
+ private static final String METHOD_NAME_GET_SECURITY_CONTEXT = "getSecurityContext";
+
+ // --------------------------------------------------------------------------------||
+ // Test Lifecycle -----------------------------------------------------------------||
+ // --------------------------------------------------------------------------------||
+
+ @BeforeClass
+ public static void beforeClass() throws Throwable
+ {
+ aopDeployer.deploy();
+ container = new AsyncContainer<SecurityAwarePojo>("Test Security Aware POJO Container",
+ TestConstants.DOMAIN_ASYNC, SecurityAwarePojo.class);
+ }
+
+ @AfterClass
+ public static void afterClass() throws Throwable
+ {
+ aopDeployer.undeploy();
+ }
+
+ // --------------------------------------------------------------------------------||
+ // Tests --------------------------------------------------------------------------||
+ // --------------------------------------------------------------------------------||
+
+ /**
+ * Tests that security context propagates from
+ * the calling Thread to the invocation Thread
+ *
+ * @throws Throwable
+ */
+ @Test
+ @SuppressWarnings("unchecked")
+ public void testSecurityPropagation() throws Throwable
+ {
+ // Initialize
+ String username = "ALR";
+ String password = "Get 'er done";
+
+ // Make a new bean instance upon which we'll invoke
+ BeanContext<SecurityAwarePojo> bean = container.construct();
+
+ // Set a Security Context via the client
+ SecurityClient client = SecurityClientFactory.getSecurityClient();
+ client.setSimple(username, password);
+ client.login();
+
+ // Get the SecurityContext for this Thread
+ SecurityContext scBefore = SecurityActions.getSecurityContext();
+ TestCase.assertNotNull("Test is invalid as security context before invocation is null", scBefore);
+
+ // Use the container to get a contracted value from the bean
+ Future<SecurityContext> futureResult = (Future<SecurityContext>) container.invoke(bean,
+ METHOD_NAME_GET_SECURITY_CONTEXT);
+
+ // Get the Future value
+ SecurityContext scFromInvocation = futureResult.get();
+ TestCase.assertNotNull("SecurtyContext from invocation did not propagate", scFromInvocation);
+ TestCase.assertEquals("Security Context from invocation did not match that of the calling Thread", scBefore,
+ scFromInvocation);
+ }
+}
Modified: projects/ejb3/trunk/async/src/test/java/org/jboss/ejb3/async/test/simple/unit/SimpleAsyncTestCase.java
===================================================================
--- projects/ejb3/trunk/async/src/test/java/org/jboss/ejb3/async/test/simple/unit/SimpleAsyncTestCase.java 2009-02-12 19:46:18 UTC (rev 84139)
+++ projects/ejb3/trunk/async/src/test/java/org/jboss/ejb3/async/test/simple/unit/SimpleAsyncTestCase.java 2009-02-12 20:33:42 UTC (rev 84140)
@@ -28,6 +28,7 @@
import junit.framework.TestCase;
import org.jboss.aspects.common.AOPDeployer;
+import org.jboss.ejb3.async.test.common.TestConstants;
import org.jboss.ejb3.async.test.container.AsyncContainer;
import org.jboss.ejb3.async.test.simple.Pojo;
import org.jboss.ejb3.interceptors.container.BeanContext;
@@ -53,10 +54,8 @@
private static final Logger log = Logger.getLogger(SimpleAsyncTestCase.class);
- private static AOPDeployer aopDeployer = new AOPDeployer("simple/jboss-aop.xml");
+ private static AOPDeployer aopDeployer = new AOPDeployer(TestConstants.AOP_DEPLOYABLE_FILENAME_SIMPLE);
- private static final String DOMAIN_ASYNC = "Asynchronous Container";
-
private static AsyncContainer<Pojo> container;
/*
@@ -81,7 +80,7 @@
public static void beforeClass() throws Throwable
{
aopDeployer.deploy();
- container = new AsyncContainer<Pojo>("Test Async POJO Container", DOMAIN_ASYNC, Pojo.class);
+ container = new AsyncContainer<Pojo>("Test Async POJO Container", TestConstants.DOMAIN_ASYNC, Pojo.class);
}
@AfterClass
@@ -135,7 +134,7 @@
int initialCounter = initialCounterFuture.get();
// Increment the counter
- Future<Integer> incrementCounterFutureResult = (Future<Integer>) container.invoke(bean,
+ Future<Void> incrementCounterFutureResult = (Future<Void>) container.invoke(bean,
METHOD_NAME_INCREMENT_COUNTER_ASYNCHRONOUS);
TestCase.assertNotNull("void return type not intercepted as asynchronous invocation",
incrementCounterFutureResult);
@@ -163,7 +162,7 @@
// Invoke and test
Object shouldBeNullReturnValue = container.invoke(bean, METHOD_NAME_INCREMENT_COUNTER_SYNCHRONOUS);
TestCase.assertNull("methods with void return type not annotated with @" + Asynchronous.class.getSimpleName()
- + " should have null return tyope from container invocation", shouldBeNullReturnValue);
+ + " should have null return type from container invocation", shouldBeNullReturnValue);
}
More information about the jboss-cvs-commits
mailing list