3535import  javax .naming .spi .ObjectFactory ;
3636
3737import  org .apache .naming .ResourceRef ;
38+ import  org .apache .naming .StringManager ;
3839
3940/** 
4041 * Object factory for any Resource conforming to the JavaBean spec. 
8788 * 
8889 * @author Aner Perez [aner at ncstech.com] 
8990 */ 
90- public  class  BeanFactory 
91-     implements  ObjectFactory  {
92- 
93-     // ----------------------------------------------------------- Constructors 
94- 
95- 
96-     // -------------------------------------------------------------- Constants 
97- 
98- 
99-     // ----------------------------------------------------- Instance Variables 
100- 
101- 
102-     // --------------------------------------------------------- Public Methods 
103- 
104- 
105-     // -------------------------------------------------- ObjectFactory Methods 
91+ public  class  BeanFactory  implements  ObjectFactory  {
10692
93+     private  static  final  StringManager  sm  = StringManager .getManager (BeanFactory .class );
10794
10895    /** 
10996     * Create a new Bean instance. 
11097     * 
11198     * @param obj The reference object describing the Bean 
11299     */ 
113100    @ Override 
114-     public  Object  getObjectInstance (Object  obj , Name  name , Context  nameCtx ,
115-                                     Hashtable <?,?> environment )
116-         throws  NamingException  {
101+     public  Object  getObjectInstance (Object  obj , Name  name , Context  nameCtx , Hashtable <?,?> environment )
102+             throws  NamingException  {
117103
118104        if  (obj  instanceof  ResourceRef ) {
119105
120106            try  {
121- 
122107                Reference  ref  = (Reference ) obj ;
123108                String  beanClassName  = ref .getClassName ();
124109                Class <?> beanClass  = null ;
125-                 ClassLoader  tcl  =
126-                     Thread .currentThread ().getContextClassLoader ();
127-                 if  (tcl  != null ) {
128-                     try  {
110+                 ClassLoader  tcl  = Thread .currentThread ().getContextClassLoader ();
111+                 try  {
112+                     if  (tcl  != null ) {
129113                        beanClass  = tcl .loadClass (beanClassName );
130-                     } catch (ClassNotFoundException  e ) {
131-                     }
132-                 } else  {
133-                     try  {
114+                     } else  {
134115                        beanClass  = Class .forName (beanClassName );
135-                     } catch (ClassNotFoundException  e ) {
136-                         e .printStackTrace ();
137116                    }
138-                 }
139-                 if  ( beanClass  ==  null ) { 
140-                     throw   new   NamingException 
141-                         ( "Class not found: "  +  beanClassName ) ;
117+                 }  catch ( ClassNotFoundException   cnfe ) { 
118+                      NamingException   ne  =  new   NamingException ( sm . getString ( "beanFactory.classNotFound" ,  beanClassName )); 
119+                     ne . initCause ( cnfe ); 
120+                     throw   ne ;
142121                }
143122
144123                BeanInfo  bi  = Introspector .getBeanInfo (beanClass );
@@ -174,8 +153,7 @@ public Object getObjectInstance(Object obj, Name name, Context nameCtx,
174153                                         param .substring (1 );
175154                        }
176155                        try  {
177-                             forced .put (param ,
178-                                        beanClass .getMethod (setterName , paramTypes ));
156+                             forced .put (param , beanClass .getMethod (setterName , paramTypes ));
179157                        } catch  (NoSuchMethodException |SecurityException  ex ) {
180158                            throw  new  NamingException 
181159                                ("Forced String setter "  + setterName  +
@@ -219,66 +197,49 @@ public Object getObjectInstance(Object obj, Name name, Context nameCtx,
219197                    }
220198
221199                    int  i  = 0 ;
222-                     for  (i  = 0 ; i < pda .length ; i ++) {
200+                     for  (i  = 0 ; i  <  pda .length ; i ++) {
223201
224202                        if  (pda [i ].getName ().equals (propName )) {
225203
226204                            Class <?> propType  = pda [i ].getPropertyType ();
227205
228206                            if  (propType .equals (String .class )) {
229207                                valueArray [0 ] = value ;
230-                             } else  if  (propType .equals (Character .class )
231-                                        || propType .equals (char .class )) {
232-                                 valueArray [0 ] =
233-                                     Character .valueOf (value .charAt (0 ));
234-                             } else  if  (propType .equals (Byte .class )
235-                                        || propType .equals (byte .class )) {
208+                             } else  if  (propType .equals (Character .class ) || propType .equals (char .class )) {
209+                                 valueArray [0 ] = Character .valueOf (value .charAt (0 ));
210+                             } else  if  (propType .equals (Byte .class ) || propType .equals (byte .class )) {
236211                                valueArray [0 ] = Byte .valueOf (value );
237-                             } else  if  (propType .equals (Short .class )
238-                                        || propType .equals (short .class )) {
212+                             } else  if  (propType .equals (Short .class ) || propType .equals (short .class )) {
239213                                valueArray [0 ] = Short .valueOf (value );
240-                             } else  if  (propType .equals (Integer .class )
241-                                        || propType .equals (int .class )) {
214+                             } else  if  (propType .equals (Integer .class ) || propType .equals (int .class )) {
242215                                valueArray [0 ] = Integer .valueOf (value );
243-                             } else  if  (propType .equals (Long .class )
244-                                        || propType .equals (long .class )) {
216+                             } else  if  (propType .equals (Long .class ) || propType .equals (long .class )) {
245217                                valueArray [0 ] = Long .valueOf (value );
246-                             } else  if  (propType .equals (Float .class )
247-                                        || propType .equals (float .class )) {
218+                             } else  if  (propType .equals (Float .class ) || propType .equals (float .class )) {
248219                                valueArray [0 ] = Float .valueOf (value );
249-                             } else  if  (propType .equals (Double .class )
250-                                        || propType .equals (double .class )) {
220+                             } else  if  (propType .equals (Double .class ) || propType .equals (double .class )) {
251221                                valueArray [0 ] = Double .valueOf (value );
252-                             } else  if  (propType .equals (Boolean .class )
253-                                        || propType .equals (boolean .class )) {
222+                             } else  if  (propType .equals (Boolean .class ) || propType .equals (boolean .class )) {
254223                                valueArray [0 ] = Boolean .valueOf (value );
255224                            } else  {
256-                                 throw  new  NamingException 
257-                                     ("String conversion for property "  + propName  +
258-                                      " of type '"  + propType .getName () +
259-                                      "' not available" );
225+                                 throw  new  NamingException (
226+                                         sm .getString ("beanFactory.noStringConversion" , propName , propType .getName ()));
260227                            }
261228
262229                            Method  setProp  = pda [i ].getWriteMethod ();
263230                            if  (setProp  != null ) {
264231                                setProp .invoke (bean , valueArray );
265232                            } else  {
266-                                 throw  new  NamingException 
267-                                     ("Write not allowed for property: " 
268-                                      + propName );
233+                                 throw  new  NamingException (sm .getString ("beanFactory.readOnlyProperty" , propName ));
269234                            }
270235
271236                            break ;
272- 
273237                        }
274- 
275238                    }
276239
277240                    if  (i  == pda .length ) {
278-                         throw  new  NamingException 
279-                             ("No set method found for property: "  + propName );
241+                         throw  new  NamingException (sm .getString ("beanFactory.noSetMethod" , propName ));
280242                    }
281- 
282243                }
283244
284245                return  bean ;
@@ -303,6 +264,5 @@ public Object getObjectInstance(Object obj, Name name, Context nameCtx,
303264        } else  {
304265            return  null ;
305266        }
306- 
307267    }
308268}
0 commit comments