1 16 17 package org.apache.taglibs.standard.lang.jstl; 18 19 import java.beans.BeanInfo ; 20 import java.beans.EventSetDescriptor ; 21 import java.beans.IndexedPropertyDescriptor ; 22 import java.beans.IntrospectionException ; 23 import java.beans.Introspector ; 24 import java.beans.PropertyDescriptor ; 25 import java.lang.reflect.Method ; 26 import java.lang.reflect.Modifier ; 27 import java.util.HashMap ; 28 import java.util.Map ; 29 30 42 43 public class BeanInfoManager 44 { 45 50 Class mBeanClass; 51 public Class getBeanClass () 52 { return mBeanClass; } 53 54 58 BeanInfo mBeanInfo; 60 61 Map mPropertyByName; 63 64 Map mIndexedPropertyByName; 66 67 Map mEventSetByName; 69 70 boolean mInitialized; 72 73 static Map mBeanInfoManagerByClass = new HashMap (); 75 76 81 BeanInfoManager (Class pBeanClass) 82 { 83 mBeanClass = pBeanClass; 84 } 85 86 91 public static BeanInfoManager getBeanInfoManager (Class pClass) 92 { 93 BeanInfoManager ret = (BeanInfoManager) 94 mBeanInfoManagerByClass.get (pClass); 95 if (ret == null) { 96 ret = createBeanInfoManager (pClass); 97 } 98 return ret; 99 } 100 101 107 static synchronized BeanInfoManager createBeanInfoManager (Class pClass) 108 { 109 117 BeanInfoManager ret = (BeanInfoManager) 118 mBeanInfoManagerByClass.get (pClass); 119 if (ret == null) { 120 ret = new BeanInfoManager (pClass); 121 mBeanInfoManagerByClass.put (pClass, ret); 122 } 123 return ret; 124 } 125 126 132 public static BeanInfoProperty getBeanInfoProperty 133 (Class pClass, 134 String pPropertyName, 135 Logger pLogger) 136 throws ELException 137 { 138 return getBeanInfoManager (pClass).getProperty (pPropertyName, pLogger); 139 } 140 141 147 public static BeanInfoIndexedProperty getBeanInfoIndexedProperty 148 (Class pClass, 149 String pIndexedPropertyName, 150 Logger pLogger) 151 throws ELException 152 { 153 return getBeanInfoManager 154 (pClass).getIndexedProperty (pIndexedPropertyName, pLogger); 155 } 156 157 163 void checkInitialized (Logger pLogger) 164 throws ELException 165 { 166 if (!mInitialized) { 167 synchronized (this) { 168 if (!mInitialized) { 169 initialize (pLogger); 170 mInitialized = true; 171 } 172 } 173 } 174 } 175 176 181 void initialize (Logger pLogger) 182 throws ELException 183 { 184 try { 185 mBeanInfo = Introspector.getBeanInfo (mBeanClass); 186 187 mPropertyByName = new HashMap (); 188 mIndexedPropertyByName = new HashMap (); 189 PropertyDescriptor [] pds = mBeanInfo.getPropertyDescriptors (); 190 for (int i = 0; pds != null && i < pds.length; i++) { 191 PropertyDescriptor pd = pds [i]; 193 if (pd instanceof IndexedPropertyDescriptor ) { 194 IndexedPropertyDescriptor ipd = (IndexedPropertyDescriptor ) pd; 195 Method readMethod = getPublicMethod (ipd.getIndexedReadMethod ()); 196 Method writeMethod = getPublicMethod (ipd.getIndexedWriteMethod ()); 197 BeanInfoIndexedProperty property = new BeanInfoIndexedProperty 198 (readMethod, 199 writeMethod, 200 ipd); 201 202 mIndexedPropertyByName.put (ipd.getName (), property); 203 } 204 205 Method readMethod = getPublicMethod (pd.getReadMethod ()); 206 Method writeMethod = getPublicMethod (pd.getWriteMethod ()); 207 BeanInfoProperty property = new BeanInfoProperty 208 (readMethod, 209 writeMethod, 210 pd); 211 212 mPropertyByName.put (pd.getName (), property); 213 } 214 215 mEventSetByName = new HashMap (); 216 EventSetDescriptor [] esds = mBeanInfo.getEventSetDescriptors (); 217 for (int i = 0; esds != null && i < esds.length; i++) { 218 EventSetDescriptor esd = esds [i]; 219 mEventSetByName.put (esd.getName (), esd); 220 } 221 } 222 catch (IntrospectionException exc) { 223 if (pLogger.isLoggingWarning ()) { 224 pLogger.logWarning 225 (Constants.EXCEPTION_GETTING_BEANINFO, 226 exc, 227 mBeanClass.getName ()); 228 } 229 } 230 } 231 232 237 BeanInfo getBeanInfo (Logger pLogger) 238 throws ELException 239 { 240 checkInitialized (pLogger); 241 return mBeanInfo; 242 } 243 244 250 public BeanInfoProperty getProperty (String pPropertyName, 251 Logger pLogger) 252 throws ELException 253 { 254 checkInitialized (pLogger); 255 return (BeanInfoProperty) mPropertyByName.get (pPropertyName); 256 } 257 258 264 public BeanInfoIndexedProperty getIndexedProperty 265 (String pIndexedPropertyName, 266 Logger pLogger) 267 throws ELException 268 { 269 checkInitialized (pLogger); 270 return (BeanInfoIndexedProperty) 271 mIndexedPropertyByName.get (pIndexedPropertyName); 272 } 273 274 280 public EventSetDescriptor getEventSet (String pEventSetName, 281 Logger pLogger) 282 throws ELException 283 { 284 checkInitialized (pLogger); 285 return (EventSetDescriptor ) mEventSetByName.get (pEventSetName); 286 } 287 288 300 static Method getPublicMethod (Method pMethod) 301 { 302 if (pMethod == null) { 303 return null; 304 } 305 306 Class cl = pMethod.getDeclaringClass (); 308 if (Modifier.isPublic (cl.getModifiers ())) { 309 return pMethod; 310 } 311 312 Method ret = getPublicMethod (cl, pMethod); 314 if (ret != null) { 315 return ret; 316 } 317 else { 318 return pMethod; 319 } 320 } 321 322 330 static Method getPublicMethod (Class pClass, 331 Method pMethod) 332 { 333 if (Modifier.isPublic (pClass.getModifiers ())) { 335 try { 336 Method m; 337 try { 338 m = pClass.getDeclaredMethod (pMethod.getName (), 339 pMethod.getParameterTypes ()); 340 } catch (java.security.AccessControlException ex) { 341 m = pClass.getMethod(pMethod.getName (), 345 pMethod.getParameterTypes ()); 346 } 347 if (Modifier.isPublic (m.getModifiers ())) { 348 return m; 349 } 350 } 351 catch (NoSuchMethodException exc) {} 352 } 353 354 { 356 Class [] interfaces = pClass.getInterfaces (); 357 if (interfaces != null) { 358 for (int i = 0; i < interfaces.length; i++) { 359 Method m = getPublicMethod (interfaces [i], pMethod); 360 if (m != null) { 361 return m; 362 } 363 } 364 } 365 } 366 367 { 369 Class superclass = pClass.getSuperclass (); 370 if (superclass != null) { 371 Method m = getPublicMethod (superclass, pMethod); 372 if (m != null) { 373 return m; 374 } 375 } 376 } 377 378 return null; 379 } 380 381 } 383 | Popular Tags |