1 55 56 package org.apache.commons.el; 57 58 import java.beans.BeanInfo ; 59 import java.beans.EventSetDescriptor ; 60 import java.beans.IndexedPropertyDescriptor ; 61 import java.beans.IntrospectionException ; 62 import java.beans.Introspector ; 63 import java.beans.PropertyDescriptor ; 64 import java.lang.reflect.Method ; 65 import java.lang.reflect.Modifier ; 66 import java.text.MessageFormat ; 67 import java.util.HashMap ; 68 import java.util.Map ; 69 import javax.servlet.jsp.el.ELException ; 70 71 83 84 public class BeanInfoManager 85 { 86 91 Class mBeanClass; 92 public Class getBeanClass () 93 { return mBeanClass; } 94 95 99 BeanInfo mBeanInfo; 101 102 Map mPropertyByName; 104 105 Map mIndexedPropertyByName; 107 108 Map mEventSetByName; 110 111 boolean mInitialized; 113 114 static Map mBeanInfoManagerByClass = new HashMap (); 116 117 122 BeanInfoManager (Class pBeanClass) 123 { 124 mBeanClass = pBeanClass; 125 } 126 127 132 public static BeanInfoManager getBeanInfoManager (Class pClass) 133 { 134 BeanInfoManager ret = (BeanInfoManager) 135 mBeanInfoManagerByClass.get (pClass); 136 if (ret == null) { 137 ret = createBeanInfoManager (pClass); 138 } 139 return ret; 140 } 141 142 148 static synchronized BeanInfoManager createBeanInfoManager (Class pClass) 149 { 150 158 BeanInfoManager ret = (BeanInfoManager) 159 mBeanInfoManagerByClass.get (pClass); 160 if (ret == null) { 161 ret = new BeanInfoManager (pClass); 162 mBeanInfoManagerByClass.put (pClass, ret); 163 } 164 return ret; 165 } 166 167 173 public static BeanInfoProperty getBeanInfoProperty 174 (Class pClass, 175 String pPropertyName, 176 Logger pLogger) 177 throws ELException 178 { 179 return getBeanInfoManager (pClass).getProperty (pPropertyName, pLogger); 180 } 181 182 188 public static BeanInfoIndexedProperty getBeanInfoIndexedProperty 189 (Class pClass, 190 String pIndexedPropertyName, 191 Logger pLogger) 192 throws ELException 193 { 194 return getBeanInfoManager 195 (pClass).getIndexedProperty (pIndexedPropertyName, pLogger); 196 } 197 198 204 void checkInitialized (Logger pLogger) 205 throws ELException 206 { 207 if (!mInitialized) { 208 synchronized (this) { 209 if (!mInitialized) { 210 initialize (pLogger); 211 mInitialized = true; 212 } 213 } 214 } 215 } 216 217 222 void initialize (Logger pLogger) 223 throws ELException 224 { 225 try { 226 mBeanInfo = Introspector.getBeanInfo (mBeanClass); 227 228 mPropertyByName = new HashMap (); 229 mIndexedPropertyByName = new HashMap (); 230 PropertyDescriptor [] pds = mBeanInfo.getPropertyDescriptors (); 231 for (int i = 0; pds != null && i < pds.length; i++) { 232 PropertyDescriptor pd = pds [i]; 234 if (pd instanceof IndexedPropertyDescriptor ) { 235 IndexedPropertyDescriptor ipd = (IndexedPropertyDescriptor ) pd; 236 Method readMethod = getPublicMethod (ipd.getIndexedReadMethod ()); 237 Method writeMethod = getPublicMethod (ipd.getIndexedWriteMethod ()); 238 BeanInfoIndexedProperty property = new BeanInfoIndexedProperty 239 (readMethod, 240 writeMethod, 241 ipd); 242 243 mIndexedPropertyByName.put (ipd.getName (), property); 244 } 245 246 Method readMethod = getPublicMethod (pd.getReadMethod ()); 247 Method writeMethod = getPublicMethod (pd.getWriteMethod ()); 248 BeanInfoProperty property = new BeanInfoProperty 249 (readMethod, 250 writeMethod, 251 pd); 252 253 mPropertyByName.put (pd.getName (), property); 254 } 255 256 mEventSetByName = new HashMap (); 257 EventSetDescriptor [] esds = mBeanInfo.getEventSetDescriptors (); 258 for (int i = 0; esds != null && i < esds.length; i++) { 259 EventSetDescriptor esd = esds [i]; 260 mEventSetByName.put (esd.getName (), esd); 261 } 262 } 263 catch (IntrospectionException exc) { 264 if (pLogger.isLoggingWarning ()) { 265 pLogger.logWarning 266 (Constants.EXCEPTION_GETTING_BEANINFO, 267 exc, 268 mBeanClass.getName ()); 269 } 270 } 271 } 272 273 278 BeanInfo getBeanInfo (Logger pLogger) 279 throws ELException 280 { 281 checkInitialized (pLogger); 282 return mBeanInfo; 283 } 284 285 291 public BeanInfoProperty getProperty (String pPropertyName, 292 Logger pLogger) 293 throws ELException 294 { 295 checkInitialized (pLogger); 296 return (BeanInfoProperty) mPropertyByName.get (pPropertyName); 297 } 298 299 305 public BeanInfoIndexedProperty getIndexedProperty 306 (String pIndexedPropertyName, 307 Logger pLogger) 308 throws ELException 309 { 310 checkInitialized (pLogger); 311 return (BeanInfoIndexedProperty) 312 mIndexedPropertyByName.get (pIndexedPropertyName); 313 } 314 315 321 public EventSetDescriptor getEventSet (String pEventSetName, 322 Logger pLogger) 323 throws ELException 324 { 325 checkInitialized (pLogger); 326 return (EventSetDescriptor ) mEventSetByName.get (pEventSetName); 327 } 328 329 341 static Method getPublicMethod (Method pMethod) 342 { 343 if (pMethod == null) { 344 return null; 345 } 346 347 Class cl = pMethod.getDeclaringClass (); 349 if (Modifier.isPublic (cl.getModifiers ())) { 350 return pMethod; 351 } 352 353 Method ret = getPublicMethod (cl, pMethod); 355 if (ret != null) { 356 return ret; 357 } 358 else { 359 return pMethod; 360 } 361 } 362 363 371 static Method getPublicMethod (Class pClass, 372 Method pMethod) 373 { 374 if (Modifier.isPublic (pClass.getModifiers ())) { 376 try { 377 Method m; 378 try { 379 m = pClass.getDeclaredMethod (pMethod.getName (), 380 pMethod.getParameterTypes ()); 381 } catch (java.security.AccessControlException ex) { 382 m = pClass.getMethod(pMethod.getName (), 386 pMethod.getParameterTypes ()); 387 } 388 if (Modifier.isPublic (m.getModifiers ())) { 389 return m; 390 } 391 } 392 catch (NoSuchMethodException exc) {} 393 } 394 395 { 397 Class [] interfaces = pClass.getInterfaces (); 398 if (interfaces != null) { 399 for (int i = 0; i < interfaces.length; i++) { 400 Method m = getPublicMethod (interfaces [i], pMethod); 401 if (m != null) { 402 return m; 403 } 404 } 405 } 406 } 407 408 { 410 Class superclass = pClass.getSuperclass (); 411 if (superclass != null) { 412 Method m = getPublicMethod (superclass, pMethod); 413 if (m != null) { 414 return m; 415 } 416 } 417 } 418 419 return null; 420 } 421 422 } 424 | Popular Tags |