From 92bffc2fb56a7939ce591b17183eba9098177696 Mon Sep 17 00:00:00 2001 From: Costin Leau Date: Thu, 6 Aug 2009 19:08:14 +0000 Subject: [PATCH] + fixed exception unwrapping + optimized path for getBean (and thus fixed another test) git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@1707 50f2f4bb-b051-0410-bef5-90022cba6387 --- build.properties | 5 +- .../AbstractAutowireCapableBeanFactory.java | 9 +- .../factory/support/AbstractBeanFactory.java | 16 +++- .../factory/support/ConstructorResolver.java | 2 +- .../support/DefaultListableBeanFactory.java | 1 + .../support/DisposableBeanAdapter.java | 1 + .../DefaultListableBeanFactoryTests.java | 14 ++- ...yTest.java => CallbacksSecurityTests.java} | 92 ++++++++++++++++++- 8 files changed, 126 insertions(+), 14 deletions(-) rename org.springframework.beans/src/test/java/org/springframework/beans/factory/support/security/{CallbacksSecurityTest.java => CallbacksSecurityTests.java} (71%) diff --git a/build.properties b/build.properties index e3be014def4..ffe56da39a2 100644 --- a/build.properties +++ b/build.properties @@ -8,6 +8,7 @@ integration.repo.dir=${basedir}/../integration-repo javadoc.exclude.package.names=org/springframework/samples/** javadoc.max.memory=256M test.vm.args=-XX:MaxPermSize=128M +compiler.args=-enableJavadoc -warn:none # For when releasing #release.type=release @@ -19,5 +20,7 @@ test.vm.args=-XX:MaxPermSize=128M # For development in trunk #release.type=integration - +#build.stamp=BUILD-SNAPSHOT +overwrite=true +ci.build=true diff --git a/org.springframework.beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java b/org.springframework.beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java index ff499b13952..f5489814b1c 100644 --- a/org.springframework.beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java +++ b/org.springframework.beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java @@ -21,7 +21,6 @@ import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Modifier; -import java.security.AccessControlContext; import java.security.AccessController; import java.security.PrivilegedAction; import java.security.PrivilegedActionException; @@ -103,6 +102,7 @@ import org.springframework.util.StringUtils; * @author Juergen Hoeller * @author Rob Harrop * @author Mark Fisher + * @author Costin Leau * @since 13.02.2004 * @see RootBeanDefinition * @see DefaultListableBeanFactory @@ -1513,7 +1513,12 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac } } else { - initMethod.invoke(bean, (Object[]) null); + try { + initMethod.invoke(bean, (Object[]) null); + } + catch (InvocationTargetException ex) { + throw ex.getTargetException(); + } } } diff --git a/org.springframework.beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java b/org.springframework.beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java index 70c3d4f2922..3ea75663537 100644 --- a/org.springframework.beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java +++ b/org.springframework.beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java @@ -96,6 +96,7 @@ import org.springframework.util.StringValueResolver; * * @author Rod Johnson * @author Juergen Hoeller + * @author Costin Leau * @since 15 April 2001 * @see #getBeanDefinition * @see #createBean @@ -207,12 +208,17 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp protected T doGetBean( final String name, final Class requiredType, final Object[] args, final boolean typeCheckOnly) throws BeansException { - return AccessController.doPrivileged(new PrivilegedAction() { - public T run() { - return doGetBeanRaw(name, requiredType, args, typeCheckOnly); - } - }); + if (System.getSecurityManager() != null) { + return AccessController.doPrivileged(new PrivilegedAction() { + public T run() { + return doGetBeanRaw(name, requiredType, args, typeCheckOnly); + } + }); + } + else { + return doGetBeanRaw(name, requiredType, args, typeCheckOnly); + } } /** * Return an instance, which may be shared or independent, of the specified bean. diff --git a/org.springframework.beans/src/main/java/org/springframework/beans/factory/support/ConstructorResolver.java b/org.springframework.beans/src/main/java/org/springframework/beans/factory/support/ConstructorResolver.java index 57e36026350..66858699e7a 100644 --- a/org.springframework.beans/src/main/java/org/springframework/beans/factory/support/ConstructorResolver.java +++ b/org.springframework.beans/src/main/java/org/springframework/beans/factory/support/ConstructorResolver.java @@ -21,7 +21,6 @@ import java.lang.reflect.Constructor; import java.lang.reflect.Member; import java.lang.reflect.Method; import java.lang.reflect.Modifier; -import java.security.AccessControlContext; import java.security.AccessController; import java.security.PrivilegedAction; import java.util.ArrayList; @@ -63,6 +62,7 @@ import org.springframework.util.ReflectionUtils; * @author Juergen Hoeller * @author Rob Harrop * @author Mark Fisher + * @author Costin Leau * @since 2.0 * @see #autowireConstructor * @see #instantiateUsingFactoryMethod diff --git a/org.springframework.beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java b/org.springframework.beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java index 3fac0d03242..94b4680ed1d 100644 --- a/org.springframework.beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java +++ b/org.springframework.beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java @@ -81,6 +81,7 @@ import org.springframework.util.StringUtils; * @author Rod Johnson * @author Juergen Hoeller * @author Sam Brannen + * @author Costin Leau * @since 16 April 2001 * @see StaticListableBeanFactory * @see PropertiesBeanDefinitionReader diff --git a/org.springframework.beans/src/main/java/org/springframework/beans/factory/support/DisposableBeanAdapter.java b/org.springframework.beans/src/main/java/org/springframework/beans/factory/support/DisposableBeanAdapter.java index 5f3df5050b6..b176b5026f8 100644 --- a/org.springframework.beans/src/main/java/org/springframework/beans/factory/support/DisposableBeanAdapter.java +++ b/org.springframework.beans/src/main/java/org/springframework/beans/factory/support/DisposableBeanAdapter.java @@ -46,6 +46,7 @@ import org.springframework.util.ReflectionUtils; * * * @author Juergen Hoeller + * @author Costin Leau * @since 2.0 * @see AbstractBeanFactory * @see org.springframework.beans.factory.DisposableBean diff --git a/org.springframework.beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java b/org.springframework.beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java index 9ba6eaa8000..12bc63ba8de 100644 --- a/org.springframework.beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java +++ b/org.springframework.beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java @@ -16,10 +16,18 @@ package org.springframework.beans.factory; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNotSame; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import java.lang.reflect.Field; import java.net.MalformedURLException; +import java.security.AccessControlContext; import java.security.AccessController; import java.security.Principal; import java.security.PrivilegedAction; @@ -2018,6 +2026,7 @@ public final class DefaultListableBeanFactoryTests { lbf.registerBeanDefinition("test", bd); final Subject subject = new Subject(); subject.getPrincipals().add(new TestPrincipal("user1")); + TestSecuredBean bean = (TestSecuredBean) Subject.doAsPrivileged(subject, new PrivilegedAction() { public Object run() { @@ -2326,7 +2335,8 @@ public final class DefaultListableBeanFactoryTests { private String userName; public void init() { - Subject subject = Subject.getSubject(AccessController.getContext()); + AccessControlContext acc = AccessController.getContext(); + Subject subject = Subject.getSubject(acc); if (subject == null) { return; } diff --git a/org.springframework.beans/src/test/java/org/springframework/beans/factory/support/security/CallbacksSecurityTest.java b/org.springframework.beans/src/test/java/org/springframework/beans/factory/support/security/CallbacksSecurityTests.java similarity index 71% rename from org.springframework.beans/src/test/java/org/springframework/beans/factory/support/security/CallbacksSecurityTest.java rename to org.springframework.beans/src/test/java/org/springframework/beans/factory/support/security/CallbacksSecurityTests.java index 1f3ffe5cf2e..1392395a918 100644 --- a/org.springframework.beans/src/test/java/org/springframework/beans/factory/support/security/CallbacksSecurityTest.java +++ b/org.springframework.beans/src/test/java/org/springframework/beans/factory/support/security/CallbacksSecurityTests.java @@ -21,14 +21,22 @@ import java.security.AccessControlContext; import java.security.AccessController; import java.security.Permissions; import java.security.Policy; +import java.security.Principal; +import java.security.PrivilegedAction; import java.security.PrivilegedExceptionAction; import java.security.ProtectionDomain; +import java.util.Iterator; import java.util.PropertyPermission; +import java.util.Set; + +import javax.security.auth.Subject; import junit.framework.TestCase; import org.springframework.beans.factory.BeanCreationException; -import org.springframework.beans.factory.support.AbstractBeanFactory; +import org.springframework.beans.factory.config.ConfigurableBeanFactory; +import org.springframework.beans.factory.support.DefaultListableBeanFactory; +import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.beans.factory.support.SecurityContextProvider; import org.springframework.beans.factory.support.security.support.ConstructorBean; import org.springframework.beans.factory.support.security.support.CustomCallbackBean; @@ -39,12 +47,71 @@ import org.springframework.core.io.Resource; /** * @author Costin Leau */ -public class CallbacksSecurityTest extends TestCase { +public class CallbacksSecurityTests extends TestCase { private XmlBeanFactory beanFactory; private SecurityContextProvider provider; - public CallbacksSecurityTest() { + + private static class TestSecuredBean { + + private String userName; + + public void init() { + AccessControlContext acc = AccessController.getContext(); + Subject subject = Subject.getSubject(acc); + System.out.println("Current acc is " +acc +" subject = " + subject); + if (subject == null) { + return; + } + setNameFromPrincipal(subject.getPrincipals()); + } + + private void setNameFromPrincipal(Set principals) { + if (principals == null) { + return; + } + for (Iterator it = principals.iterator(); it.hasNext();) { + Principal p = it.next(); + this.userName = p.getName(); + return; + } + } + + public String getUserName() { + return this.userName; + } + } + + private static class TestPrincipal implements Principal { + + private String name; + + public TestPrincipal(String name) { + this.name = name; + } + + public String getName() { + return this.name; + } + + public boolean equals(Object obj) { + if (obj == this) { + return true; + } + if (!(obj instanceof TestPrincipal)) { + return false; + } + TestPrincipal p = (TestPrincipal) obj; + return this.name.equals(p.name); + } + + public int hashCode() { + return this.name.hashCode(); + } + } + + public CallbacksSecurityTests() { // setup security if (System.getSecurityManager() == null) { Policy policy = Policy.getPolicy(); @@ -220,4 +287,23 @@ public class CallbacksSecurityTest extends TestCase { beanFactory.getBean("working-property-injection"); } + + public void testInitSecurityAwarePrototypeBean() { + final DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); + RootBeanDefinition bd = new RootBeanDefinition(TestSecuredBean.class); + bd.setScope(ConfigurableBeanFactory.SCOPE_PROTOTYPE); + bd.setInitMethodName("init"); + lbf.registerBeanDefinition("test", bd); + final Subject subject = new Subject(); + subject.getPrincipals().add(new TestPrincipal("user1")); + + TestSecuredBean bean = (TestSecuredBean) Subject.doAsPrivileged(subject, + new PrivilegedAction() { + public Object run() { + return lbf.getBean("test"); + } + }, null); + assertNotNull(bean); + assertEquals(null, bean.getUserName()); + } } \ No newline at end of file