1919import java .beans .BeanInfo ;
2020import java .beans .Introspector ;
2121import java .beans .PropertyDescriptor ;
22- import java .lang .reflect .InvocationTargetException ;
2322import java .lang .reflect .Method ;
2423import java .util .Enumeration ;
25- import java .util .HashMap ;
2624import java .util .Hashtable ;
27- import java .util .Locale ;
28- import java .util .Map ;
2925
3026import javax .naming .Context ;
3127import javax .naming .Name ;
3430import javax .naming .Reference ;
3531import javax .naming .spi .ObjectFactory ;
3632
33+ import org .apache .juli .logging .Log ;
34+ import org .apache .juli .logging .LogFactory ;
3735import org .apache .naming .ResourceRef ;
3836import org .apache .naming .StringManager ;
3937
@@ -92,6 +90,8 @@ public class BeanFactory implements ObjectFactory {
9290
9391 private static final StringManager sm = StringManager .getManager (BeanFactory .class );
9492
93+ private final Log log = LogFactory .getLog (BeanFactory .class ); // Not static
94+
9595 /**
9696 * Create a new Bean instance.
9797 *
@@ -125,44 +125,14 @@ public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtabl
125125
126126 Object bean = beanClass .getConstructor ().newInstance ();
127127
128- /* Look for properties with explicitly configured setter */
128+ // Look for the removed forceString option
129129 RefAddr ra = ref .get ("forceString" );
130- Map <String , Method > forced = new HashMap <>();
131- String value ;
132-
133130 if (ra != null ) {
134- value = (String )ra .getContent ();
135- Class <?> paramTypes [] = new Class [1 ];
136- paramTypes [0 ] = String .class ;
137- String setterName ;
138- int index ;
139-
140- /* Items are given as comma separated list */
141- for (String param : value .split ("," )) {
142- param = param .trim ();
143- /* A single item can either be of the form name=method
144- * or just a property name (and we will use a standard
145- * setter) */
146- index = param .indexOf ('=' );
147- if (index >= 0 ) {
148- setterName = param .substring (index + 1 ).trim ();
149- param = param .substring (0 , index ).trim ();
150- } else {
151- setterName = "set" +
152- param .substring (0 , 1 ).toUpperCase (Locale .ENGLISH ) +
153- param .substring (1 );
154- }
155- try {
156- forced .put (param , beanClass .getMethod (setterName , paramTypes ));
157- } catch (NoSuchMethodException |SecurityException ex ) {
158- throw new NamingException
159- ("Forced String setter " + setterName +
160- " not found for property " + param );
161- }
162- }
131+ log .warn (sm .getString ("beanFactory.noForceString" ));
163132 }
164133
165134 Enumeration <RefAddr > e = ref .getAll ();
135+ String value ;
166136
167137 while (e .hasMoreElements ()) {
168138
@@ -180,28 +150,13 @@ public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtabl
180150
181151 Object [] valueArray = new Object [1 ];
182152
183- /* Shortcut for properties with explicitly configured setter */
184- Method method = forced .get (propName );
185- if (method != null ) {
186- valueArray [0 ] = value ;
187- try {
188- method .invoke (bean , valueArray );
189- } catch (IllegalAccessException |
190- IllegalArgumentException |
191- InvocationTargetException ex ) {
192- throw new NamingException
193- ("Forced String setter " + method .getName () +
194- " threw exception for property " + propName );
195- }
196- continue ;
197- }
198-
199153 int i = 0 ;
200154 for (i = 0 ; i < pda .length ; i ++) {
201155
202156 if (pda [i ].getName ().equals (propName )) {
203157
204158 Class <?> propType = pda [i ].getPropertyType ();
159+ Method setProp = pda [i ].getWriteMethod ();
205160
206161 if (propType .equals (String .class )) {
207162 valueArray [0 ] = value ;
@@ -221,12 +176,22 @@ public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtabl
221176 valueArray [0 ] = Double .valueOf (value );
222177 } else if (propType .equals (Boolean .class ) || propType .equals (boolean .class )) {
223178 valueArray [0 ] = Boolean .valueOf (value );
179+ } else if (setProp != null ) {
180+ // This is a Tomcat specific extension and is not part of the
181+ // Java Bean specification.
182+ String setterName = setProp .getName ();
183+ try {
184+ setProp = bean .getClass ().getMethod (setterName , String .class );
185+ valueArray [0 ] = value ;
186+ } catch (NoSuchMethodException nsme ) {
187+ throw new NamingException (sm .getString (
188+ "beanFactory.noStringConversion" , propName , propType .getName ()));
189+ }
224190 } else {
225- throw new NamingException (
226- sm . getString ( "beanFactory.noStringConversion" , propName , propType .getName ()));
191+ throw new NamingException (sm . getString (
192+ "beanFactory.noStringConversion" , propName , propType .getName ()));
227193 }
228194
229- Method setProp = pda [i ].getWriteMethod ();
230195 if (setProp != null ) {
231196 setProp .invoke (bean , valueArray );
232197 } else {
0 commit comments