Skip to content

Commit 81eb114

Browse files
author
Costin Leau
committed
+ fixed exception unwrapping
+ optimized path for getBean (and thus fixed another test)
1 parent 65e00f7 commit 81eb114

File tree

8 files changed

+126
-14
lines changed

8 files changed

+126
-14
lines changed

build.properties

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ integration.repo.dir=${basedir}/../integration-repo
88
javadoc.exclude.package.names=org/springframework/samples/**
99
javadoc.max.memory=256M
1010
test.vm.args=-XX:MaxPermSize=128M
11+
compiler.args=-enableJavadoc -warn:none
1112

1213
# For when releasing
1314
#release.type=release
@@ -19,5 +20,7 @@ test.vm.args=-XX:MaxPermSize=128M
1920

2021
# For development in trunk
2122
#release.type=integration
22-
23+
#build.stamp=BUILD-SNAPSHOT
24+
overwrite=true
25+
ci.build=true
2326

org.springframework.beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.lang.reflect.InvocationTargetException;
2222
import java.lang.reflect.Method;
2323
import java.lang.reflect.Modifier;
24-
import java.security.AccessControlContext;
2524
import java.security.AccessController;
2625
import java.security.PrivilegedAction;
2726
import java.security.PrivilegedActionException;
@@ -103,6 +102,7 @@
103102
* @author Juergen Hoeller
104103
* @author Rob Harrop
105104
* @author Mark Fisher
105+
* @author Costin Leau
106106
* @since 13.02.2004
107107
* @see RootBeanDefinition
108108
* @see DefaultListableBeanFactory
@@ -1513,7 +1513,12 @@ public Object run() throws Exception {
15131513
}
15141514
}
15151515
else {
1516-
initMethod.invoke(bean, (Object[]) null);
1516+
try {
1517+
initMethod.invoke(bean, (Object[]) null);
1518+
}
1519+
catch (InvocationTargetException ex) {
1520+
throw ex.getTargetException();
1521+
}
15171522
}
15181523
}
15191524

org.springframework.beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
*
9797
* @author Rod Johnson
9898
* @author Juergen Hoeller
99+
* @author Costin Leau
99100
* @since 15 April 2001
100101
* @see #getBeanDefinition
101102
* @see #createBean
@@ -207,12 +208,17 @@ public <T> T getBean(String name, Class<T> requiredType, Object[] args) throws B
207208
protected <T> T doGetBean(
208209
final String name, final Class<T> requiredType, final Object[] args, final boolean typeCheckOnly)
209210
throws BeansException {
210-
return AccessController.doPrivileged(new PrivilegedAction<T>() {
211211

212-
public T run() {
213-
return doGetBeanRaw(name, requiredType, args, typeCheckOnly);
214-
}
215-
});
212+
if (System.getSecurityManager() != null) {
213+
return AccessController.doPrivileged(new PrivilegedAction<T>() {
214+
public T run() {
215+
return doGetBeanRaw(name, requiredType, args, typeCheckOnly);
216+
}
217+
});
218+
}
219+
else {
220+
return doGetBeanRaw(name, requiredType, args, typeCheckOnly);
221+
}
216222
}
217223
/**
218224
* Return an instance, which may be shared or independent, of the specified bean.

org.springframework.beans/src/main/java/org/springframework/beans/factory/support/ConstructorResolver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.lang.reflect.Member;
2222
import java.lang.reflect.Method;
2323
import java.lang.reflect.Modifier;
24-
import java.security.AccessControlContext;
2524
import java.security.AccessController;
2625
import java.security.PrivilegedAction;
2726
import java.util.ArrayList;
@@ -63,6 +62,7 @@
6362
* @author Juergen Hoeller
6463
* @author Rob Harrop
6564
* @author Mark Fisher
65+
* @author Costin Leau
6666
* @since 2.0
6767
* @see #autowireConstructor
6868
* @see #instantiateUsingFactoryMethod

org.springframework.beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
* @author Rod Johnson
8282
* @author Juergen Hoeller
8383
* @author Sam Brannen
84+
* @author Costin Leau
8485
* @since 16 April 2001
8586
* @see StaticListableBeanFactory
8687
* @see PropertiesBeanDefinitionReader

org.springframework.beans/src/main/java/org/springframework/beans/factory/support/DisposableBeanAdapter.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
* </ul>
4747
*
4848
* @author Juergen Hoeller
49+
* @author Costin Leau
4950
* @since 2.0
5051
* @see AbstractBeanFactory
5152
* @see org.springframework.beans.factory.DisposableBean

org.springframework.beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,18 @@
1616

1717
package org.springframework.beans.factory;
1818

19-
import static org.junit.Assert.*;
19+
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertFalse;
21+
import static org.junit.Assert.assertNotNull;
22+
import static org.junit.Assert.assertNotSame;
23+
import static org.junit.Assert.assertNull;
24+
import static org.junit.Assert.assertSame;
25+
import static org.junit.Assert.assertTrue;
26+
import static org.junit.Assert.fail;
2027

2128
import java.lang.reflect.Field;
2229
import java.net.MalformedURLException;
30+
import java.security.AccessControlContext;
2331
import java.security.AccessController;
2432
import java.security.Principal;
2533
import java.security.PrivilegedAction;
@@ -2018,6 +2026,7 @@ public void testInitSecurityAwarePrototypeBean() {
20182026
lbf.registerBeanDefinition("test", bd);
20192027
final Subject subject = new Subject();
20202028
subject.getPrincipals().add(new TestPrincipal("user1"));
2029+
20212030
TestSecuredBean bean = (TestSecuredBean) Subject.doAsPrivileged(subject,
20222031
new PrivilegedAction() {
20232032
public Object run() {
@@ -2326,7 +2335,8 @@ private static class TestSecuredBean {
23262335
private String userName;
23272336

23282337
public void init() {
2329-
Subject subject = Subject.getSubject(AccessController.getContext());
2338+
AccessControlContext acc = AccessController.getContext();
2339+
Subject subject = Subject.getSubject(acc);
23302340
if (subject == null) {
23312341
return;
23322342
}
Lines changed: 89 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,22 @@
2121
import java.security.AccessController;
2222
import java.security.Permissions;
2323
import java.security.Policy;
24+
import java.security.Principal;
25+
import java.security.PrivilegedAction;
2426
import java.security.PrivilegedExceptionAction;
2527
import java.security.ProtectionDomain;
28+
import java.util.Iterator;
2629
import java.util.PropertyPermission;
30+
import java.util.Set;
31+
32+
import javax.security.auth.Subject;
2733

2834
import junit.framework.TestCase;
2935

3036
import org.springframework.beans.factory.BeanCreationException;
31-
import org.springframework.beans.factory.support.AbstractBeanFactory;
37+
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
38+
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
39+
import org.springframework.beans.factory.support.RootBeanDefinition;
3240
import org.springframework.beans.factory.support.SecurityContextProvider;
3341
import org.springframework.beans.factory.support.security.support.ConstructorBean;
3442
import org.springframework.beans.factory.support.security.support.CustomCallbackBean;
@@ -39,12 +47,71 @@
3947
/**
4048
* @author Costin Leau
4149
*/
42-
public class CallbacksSecurityTest extends TestCase {
50+
public class CallbacksSecurityTests extends TestCase {
4351

4452
private XmlBeanFactory beanFactory;
4553
private SecurityContextProvider provider;
4654

47-
public CallbacksSecurityTest() {
55+
56+
private static class TestSecuredBean {
57+
58+
private String userName;
59+
60+
public void init() {
61+
AccessControlContext acc = AccessController.getContext();
62+
Subject subject = Subject.getSubject(acc);
63+
System.out.println("Current acc is " +acc +" subject = " + subject);
64+
if (subject == null) {
65+
return;
66+
}
67+
setNameFromPrincipal(subject.getPrincipals());
68+
}
69+
70+
private void setNameFromPrincipal(Set<Principal> principals) {
71+
if (principals == null) {
72+
return;
73+
}
74+
for (Iterator<Principal> it = principals.iterator(); it.hasNext();) {
75+
Principal p = it.next();
76+
this.userName = p.getName();
77+
return;
78+
}
79+
}
80+
81+
public String getUserName() {
82+
return this.userName;
83+
}
84+
}
85+
86+
private static class TestPrincipal implements Principal {
87+
88+
private String name;
89+
90+
public TestPrincipal(String name) {
91+
this.name = name;
92+
}
93+
94+
public String getName() {
95+
return this.name;
96+
}
97+
98+
public boolean equals(Object obj) {
99+
if (obj == this) {
100+
return true;
101+
}
102+
if (!(obj instanceof TestPrincipal)) {
103+
return false;
104+
}
105+
TestPrincipal p = (TestPrincipal) obj;
106+
return this.name.equals(p.name);
107+
}
108+
109+
public int hashCode() {
110+
return this.name.hashCode();
111+
}
112+
}
113+
114+
public CallbacksSecurityTests() {
48115
// setup security
49116
if (System.getSecurityManager() == null) {
50117
Policy policy = Policy.getPolicy();
@@ -220,4 +287,23 @@ public void testPropertyInjection() throws Exception {
220287

221288
beanFactory.getBean("working-property-injection");
222289
}
290+
291+
public void testInitSecurityAwarePrototypeBean() {
292+
final DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
293+
RootBeanDefinition bd = new RootBeanDefinition(TestSecuredBean.class);
294+
bd.setScope(ConfigurableBeanFactory.SCOPE_PROTOTYPE);
295+
bd.setInitMethodName("init");
296+
lbf.registerBeanDefinition("test", bd);
297+
final Subject subject = new Subject();
298+
subject.getPrincipals().add(new TestPrincipal("user1"));
299+
300+
TestSecuredBean bean = (TestSecuredBean) Subject.doAsPrivileged(subject,
301+
new PrivilegedAction() {
302+
public Object run() {
303+
return lbf.getBean("test");
304+
}
305+
}, null);
306+
assertNotNull(bean);
307+
assertEquals(null, bean.getUserName());
308+
}
223309
}

0 commit comments

Comments
 (0)