11/*
2- * Copyright 2002-2010 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,26 @@ 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+
180+ @ Override
158181 public void setBeanClassLoader (ClassLoader classLoader ) {
159182 this .beanClassLoader = classLoader ;
160183 }
@@ -179,9 +202,16 @@ public void afterPropertiesSet() throws IllegalArgumentException, NamingExceptio
179202 else {
180203 if (this .defaultObject != null && getExpectedType () != null &&
181204 !getExpectedType ().isInstance (this .defaultObject )) {
182- throw new IllegalArgumentException ("Default object [" + this .defaultObject +
183- "] of type [" + this .defaultObject .getClass ().getName () +
184- "] 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+ }
185215 }
186216 // Locate specified JNDI object.
187217 this .jndiObject = lookupWithFallback ();
@@ -263,7 +293,7 @@ public boolean isSingleton() {
263293 * @return the merged interface as Class
264294 * @see java.lang.reflect.Proxy#getProxyClass
265295 */
266- protected Class createCompositeInterface (Class [] interfaces ) {
296+ protected Class <?> createCompositeInterface (Class <?> [] interfaces ) {
267297 return ClassUtils .createCompositeInterface (interfaces , this .beanClassLoader );
268298 }
269299
@@ -290,13 +320,13 @@ private static Object createJndiObjectProxy(JndiObjectFactoryBean jof) throws Na
290320 proxyFactory .setInterfaces (jof .proxyInterfaces );
291321 }
292322 else {
293- Class targetClass = targetSource .getTargetClass ();
323+ Class <?> targetClass = targetSource .getTargetClass ();
294324 if (targetClass == null ) {
295325 throw new IllegalStateException (
296326 "Cannot deactivate 'lookupOnStartup' without specifying a 'proxyInterface' or 'expectedType'" );
297327 }
298- Class [] ifcs = ClassUtils .getAllInterfacesForClass (targetClass , jof .beanClassLoader );
299- for (Class ifc : ifcs ) {
328+ Class <?> [] ifcs = ClassUtils .getAllInterfacesForClass (targetClass , jof .beanClassLoader );
329+ for (Class <?> ifc : ifcs ) {
300330 if (Modifier .isPublic (ifc .getModifiers ())) {
301331 proxyFactory .addInterface (ifc );
302332 }
0 commit comments