11/*
2- * Copyright 2002-2012 the original author or authors.
2+ * Copyright 2002-2013 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
2525import org .aopalliance .intercept .MethodInvocation ;
2626
2727import org .springframework .aop .framework .ProxyFactory ;
28+ import org .springframework .beans .SimpleTypeConverter ;
29+ import org .springframework .beans .TypeConverter ;
30+ import org .springframework .beans .TypeMismatchException ;
2831import org .springframework .beans .factory .BeanClassLoaderAware ;
32+ import org .springframework .beans .factory .BeanFactory ;
33+ import org .springframework .beans .factory .BeanFactoryAware ;
2934import org .springframework .beans .factory .FactoryBean ;
35+ import org .springframework .beans .factory .config .ConfigurableBeanFactory ;
3036import org .springframework .util .ClassUtils ;
3137
3238/**
6167 * @see #setCache
6268 * @see JndiObjectTargetSource
6369 */
64- public class JndiObjectFactoryBean extends JndiObjectLocator implements FactoryBean <Object >, BeanClassLoaderAware {
70+ public class JndiObjectFactoryBean extends JndiObjectLocator
71+ implements FactoryBean <Object >, BeanFactoryAware , BeanClassLoaderAware {
6572
66- private Class [] proxyInterfaces ;
73+ private Class <?> [] proxyInterfaces ;
6774
6875 private boolean lookupOnStartup = true ;
6976
@@ -73,6 +80,8 @@ public class JndiObjectFactoryBean extends JndiObjectLocator implements FactoryB
7380
7481 private Object defaultObject ;
7582
83+ private ConfigurableBeanFactory beanFactory ;
84+
7685 private ClassLoader beanClassLoader = ClassUtils .getDefaultClassLoader ();
7786
7887 private Object jndiObject ;
@@ -87,8 +96,8 @@ public class JndiObjectFactoryBean extends JndiObjectLocator implements FactoryB
8796 * @see #setLookupOnStartup
8897 * @see #setCache
8998 */
90- public void setProxyInterface (Class proxyInterface ) {
91- this .proxyInterfaces = new Class [] {proxyInterface };
99+ public void setProxyInterface (Class <?> proxyInterface ) {
100+ this .proxyInterfaces = new Class <?> [] {proxyInterface };
92101 }
93102
94103 /**
@@ -100,7 +109,7 @@ public void setProxyInterface(Class proxyInterface) {
100109 * @see #setLookupOnStartup
101110 * @see #setCache
102111 */
103- public void setProxyInterfaces (Class [] proxyInterfaces ) {
112+ public void setProxyInterfaces (Class <?>... proxyInterfaces ) {
104113 this .proxyInterfaces = proxyInterfaces ;
105114 }
106115
@@ -149,12 +158,25 @@ public void setExposeAccessContext(boolean exposeAccessContext) {
149158 * It is typically used for literal values in scenarios where the JNDI environment
150159 * might define specific config settings but those are not required to be present.
151160 * <p>Note: This is only supported for lookup on startup.
161+ * If specified together with {@link #setExpectedType}, the specified value
162+ * needs to be either of that type or convertible to it.
152163 * @see #setLookupOnStartup
164+ * @see ConfigurableBeanFactory#getTypeConverter()
165+ * @see SimpleTypeConverter
153166 */
154167 public void setDefaultObject (Object defaultObject ) {
155168 this .defaultObject = defaultObject ;
156169 }
157170
171+ @ Override
172+ public void setBeanFactory (BeanFactory beanFactory ) {
173+ if (beanFactory instanceof ConfigurableBeanFactory ) {
174+ // Just optional - for getting a specifically configured TypeConverter if needed.
175+ // We'll simply fall back to a SimpleTypeConverter if no specific one available.
176+ this .beanFactory = (ConfigurableBeanFactory ) beanFactory ;
177+ }
178+ }
179+
158180 @ Override
159181 public void setBeanClassLoader (ClassLoader classLoader ) {
160182 this .beanClassLoader = classLoader ;
@@ -180,9 +202,16 @@ public void afterPropertiesSet() throws IllegalArgumentException, NamingExceptio
180202 else {
181203 if (this .defaultObject != null && getExpectedType () != null &&
182204 !getExpectedType ().isInstance (this .defaultObject )) {
183- throw new IllegalArgumentException ("Default object [" + this .defaultObject +
184- "] of type [" + this .defaultObject .getClass ().getName () +
185- "] is not of expected type [" + getExpectedType ().getName () + "]" );
205+ TypeConverter converter = (this .beanFactory != null ?
206+ this .beanFactory .getTypeConverter () : new SimpleTypeConverter ());
207+ try {
208+ this .defaultObject = converter .convertIfNecessary (this .defaultObject , getExpectedType ());
209+ }
210+ catch (TypeMismatchException ex ) {
211+ throw new IllegalArgumentException ("Default object [" + this .defaultObject + "] of type [" +
212+ this .defaultObject .getClass ().getName () + "] is not of expected type [" +
213+ getExpectedType ().getName () + "] and cannot be converted either" , ex );
214+ }
186215 }
187216 // Locate specified JNDI object.
188217 this .jndiObject = lookupWithFallback ();
@@ -267,7 +296,7 @@ public boolean isSingleton() {
267296 * @return the merged interface as Class
268297 * @see java.lang.reflect.Proxy#getProxyClass
269298 */
270- protected Class createCompositeInterface (Class [] interfaces ) {
299+ protected Class <?> createCompositeInterface (Class <?> [] interfaces ) {
271300 return ClassUtils .createCompositeInterface (interfaces , this .beanClassLoader );
272301 }
273302
@@ -294,13 +323,13 @@ private static Object createJndiObjectProxy(JndiObjectFactoryBean jof) throws Na
294323 proxyFactory .setInterfaces (jof .proxyInterfaces );
295324 }
296325 else {
297- Class targetClass = targetSource .getTargetClass ();
326+ Class <?> targetClass = targetSource .getTargetClass ();
298327 if (targetClass == null ) {
299328 throw new IllegalStateException (
300329 "Cannot deactivate 'lookupOnStartup' without specifying a 'proxyInterface' or 'expectedType'" );
301330 }
302- Class [] ifcs = ClassUtils .getAllInterfacesForClass (targetClass , jof .beanClassLoader );
303- for (Class ifc : ifcs ) {
331+ Class <?> [] ifcs = ClassUtils .getAllInterfacesForClass (targetClass , jof .beanClassLoader );
332+ for (Class <?> ifc : ifcs ) {
304333 if (Modifier .isPublic (ifc .getModifiers ())) {
305334 proxyFactory .addInterface (ifc );
306335 }
0 commit comments