Skip to content

Commit 7565b24

Browse files
mbensonchkal
authored andcommitted
119: filter proxy bean names from bean names reported by Spring
1 parent 67e14b7 commit 7565b24

2 files changed

Lines changed: 149 additions & 0 deletions

File tree

‎core/src/main/java/com/ocpsoft/pretty/faces/el/resolver/SpringBeanNameResolver.java‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import java.lang.reflect.InvocationTargetException;
1919
import java.lang.reflect.Method;
2020

21+
import java.util.ArrayList;
22+
2123
import javax.servlet.ServletContext;
2224

2325
import org.apache.commons.logging.Log;
@@ -53,11 +55,26 @@ public class SpringBeanNameResolver implements ELBeanNameResolver
5355
*/
5456
private final static String GET_BEAN_NAMES_METHOD = "getBeanNamesForType";
5557

58+
/**
59+
* FQCN of the ScopedProxyUtils class
60+
*/
61+
private final static String SCOPED_PROXY_UTILS_CLASS = "org.springframework.aop.scope.ScopedProxyUtils";
62+
63+
/**
64+
* Name of the static getTargetBeanName() method
65+
*/
66+
private final static String GET_TARGET_BEAN_NAME_METHOD = "getTargetBeanName";
67+
5668
/**
5769
* The getBeanNamesForType() method
5870
*/
5971
private Method getBeanNamesMethod;
6072

73+
/**
74+
* The static getTargetBeanName() method
75+
*/
76+
private Method getTargetBeanNameMethod;
77+
6178
/**
6279
* The Spring WebApplicationContext
6380
*/
@@ -94,6 +111,7 @@ public boolean init(ServletContext servletContext, ClassLoader classLoader)
94111
{
95112
log.debug("Spring detected. Enabling Spring bean name resolving.");
96113
}
114+
getTargetBeanNameMethod = getProxyTargetBeanNameMethod(classLoader);
97115
return true;
98116

99117
}
@@ -146,6 +164,9 @@ public String getBeanName(Class<?> clazz)
146164
return null;
147165
}
148166

167+
// filter out scoped proxies, which are really Spring-internal names for beans we want:
168+
names = filterProxyNames(names);
169+
149170
// more than one name? Warn the user..
150171
if (names.length > 1)
151172
{
@@ -181,4 +202,63 @@ public String getBeanName(Class<?> clazz)
181202
return null;
182203
}
183204

205+
private String[] filterProxyNames(String[] names)
206+
{
207+
if (getTargetBeanNameMethod == null)
208+
{
209+
return names;
210+
}
211+
ArrayList<String> result = new ArrayList<String>(names.length);
212+
for (int i = names.length - 1; i >= 0; i--)
213+
{
214+
String name = names[i];
215+
if (name == null)
216+
{
217+
continue;
218+
}
219+
boolean isProxy = false;
220+
for (int j = 0; !isProxy && j < names.length; j++)
221+
{
222+
if (j == i)
223+
{
224+
continue;
225+
}
226+
try
227+
{
228+
isProxy = name.equals(getTargetBeanNameMethod.invoke(null, names[j]));
229+
}
230+
catch (Exception e)
231+
{
232+
if (log.isDebugEnabled())
233+
{
234+
log.debug(String.format("Error invoking %s(%s)", getTargetBeanNameMethod, name));
235+
}
236+
}
237+
}
238+
if (!isProxy)
239+
{
240+
result.add(name);
241+
}
242+
}
243+
244+
return result.toArray(new String[result.size()]);
245+
}
246+
247+
private static Method getProxyTargetBeanNameMethod(ClassLoader classLoader)
248+
{
249+
try {
250+
Class<?> scopedProxyUtilsClass = Class.forName(SCOPED_PROXY_UTILS_CLASS, true, classLoader);
251+
return scopedProxyUtilsClass.getMethod(GET_TARGET_BEAN_NAME_METHOD, String.class);
252+
}
253+
catch (Exception e)
254+
{
255+
// will happen when Spring AOP is not on the classpath, or similar
256+
if (log.isDebugEnabled())
257+
{
258+
log.debug(String.format("Could not find %s#%s method; filtering of proxy bean names has been disabled.",
259+
SCOPED_PROXY_UTILS_CLASS, GET_TARGET_BEAN_NAME_METHOD));
260+
}
261+
return null;
262+
}
263+
}
184264
}

‎core/src/test/java/com/ocpsoft/pretty/faces/el/resolver/SpringBeanNameResolverTest.java‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import org.easymock.classextension.EasyMock;
2626
import org.junit.Test;
27+
import org.springframework.aop.scope.ScopedProxyUtils;
2728
import org.springframework.web.context.WebApplicationContext;
2829

2930
@SuppressWarnings("unchecked")
@@ -145,6 +146,74 @@ public void testSpringMultipleNamesForClass() throws Exception
145146

146147
}
147148

149+
@Test
150+
public void testSpringScopedProxyNames() throws Exception
151+
{
152+
153+
// WebApplicationContext that knows multiple names for our test class
154+
WebApplicationContext appContext = EasyMock.createMock(WebApplicationContext.class);
155+
String scopedProxyName = ScopedProxyUtils.getTargetBeanName("springManagedBean");
156+
EasyMock.expect(appContext.getBeanNamesForType((Class) SpringManagedBean.class)).andReturn(
157+
new String[] { "springManagedBean", scopedProxyName }).once();
158+
EasyMock.replay(appContext);
159+
160+
// Simple mock of ServletContext containing the WebApplicationContext
161+
ServletContext servletContext = EasyMock.createNiceMock(ServletContext.class);
162+
EasyMock.expect(servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE))
163+
.andReturn(appContext).once();
164+
EasyMock.replay(servletContext);
165+
166+
// ClassLoader that knows WebApplicationContext
167+
ClassLoader classLoader = EasyMock.createNiceMock(ClassLoader.class);
168+
EasyMock.expect(classLoader.loadClass(WebApplicationContext.class.getName())).andReturn(
169+
(Class) WebApplicationContext.class).once();
170+
EasyMock.expect(classLoader.loadClass(ScopedProxyUtils.class.getName())).andReturn(
171+
(Class) ScopedProxyUtils.class).once();
172+
EasyMock.replay(classLoader);
173+
174+
// initialize resolver
175+
SpringBeanNameResolver resolver = new SpringBeanNameResolver();
176+
boolean initCompleted = resolver.init(servletContext, classLoader);
177+
assertTrue(initCompleted);
178+
179+
// Verify: Successful resolving
180+
assertEquals("springManagedBean", resolver.getBeanName(SpringManagedBean.class));
181+
182+
}
183+
184+
@Test
185+
public void testSpringScopedProxyNamesWithoutAOP() throws Exception
186+
{
187+
188+
// WebApplicationContext that knows multiple names for our test class
189+
WebApplicationContext appContext = EasyMock.createMock(WebApplicationContext.class);
190+
String scopedProxyName = ScopedProxyUtils.getTargetBeanName("springManagedBean");
191+
EasyMock.expect(appContext.getBeanNamesForType((Class) SpringManagedBean.class)).andReturn(
192+
new String[] { "springManagedBean", scopedProxyName }).once();
193+
EasyMock.replay(appContext);
194+
195+
// Simple mock of ServletContext containing the WebApplicationContext
196+
ServletContext servletContext = EasyMock.createNiceMock(ServletContext.class);
197+
EasyMock.expect(servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE))
198+
.andReturn(appContext).once();
199+
EasyMock.replay(servletContext);
200+
201+
// ClassLoader that knows WebApplicationContext
202+
ClassLoader classLoader = EasyMock.createNiceMock(ClassLoader.class);
203+
EasyMock.expect(classLoader.loadClass(WebApplicationContext.class.getName())).andReturn(
204+
(Class) WebApplicationContext.class).once();
205+
EasyMock.replay(classLoader);
206+
207+
// initialize resolver
208+
SpringBeanNameResolver resolver = new SpringBeanNameResolver();
209+
boolean initCompleted = resolver.init(servletContext, classLoader);
210+
assertTrue(initCompleted);
211+
212+
// Verify: unknown because of multiple known names
213+
assertNull(resolver.getBeanName(SpringManagedBean.class));
214+
215+
}
216+
148217
/**
149218
* Test bean
150219
*/

0 commit comments

Comments
 (0)