1 16 package org.apache.myfaces.el; 17 18 import org.apache.commons.beanutils.MethodUtils; 19 import org.apache.commons.logging.Log; 20 import org.apache.commons.logging.LogFactory; 21 22 import javax.faces.el.EvaluationException; 23 import javax.faces.el.PropertyNotFoundException; 24 import javax.faces.el.PropertyResolver; 25 import javax.faces.el.ReferenceSyntaxException; 26 import java.beans.BeanInfo ; 27 import java.beans.IntrospectionException ; 28 import java.beans.Introspector ; 29 import java.beans.PropertyDescriptor ; 30 import java.lang.reflect.Array ; 31 import java.lang.reflect.Method ; 32 import java.util.List ; 33 import java.util.Map ; 34 35 36 77 public class PropertyResolverImpl extends PropertyResolver 78 { 79 private static final Log log = 80 LogFactory.getLog(PropertyResolverImpl.class); 81 82 84 private static final Object [] NO_ARGS = {}; 85 86 88 public Object getValue(Object base, Object property) 89 throws EvaluationException, PropertyNotFoundException 90 { 91 try 92 { 93 if (base == null || property == null || 94 property instanceof String && ((String )property).length() == 0) 95 { 96 return null; 97 } 98 if (base instanceof Map ) 99 { 100 return ((Map ) base).get(property); 101 } 102 103 return getProperty(base, property.toString()); 105 } 106 catch (RuntimeException e) 107 { 108 log.error("Exception getting value of property " + property 109 + " of bean " 110 + base != null ? base.getClass().getName() : "NULL", e); 111 throw e; 112 } 113 } 114 115 public Object getValue(Object base, int index) 116 throws EvaluationException, PropertyNotFoundException 117 { 118 try 119 { 120 if (base == null) 121 { 122 return null; 123 } 124 125 try 126 { 127 if (base.getClass().isArray()) 128 { 129 return Array.get(base, index); 130 } 131 if (base instanceof List ) 132 { 133 return ((List ) base).get(index); 134 } 135 } 136 catch (IndexOutOfBoundsException e) 137 { 138 return null; 140 } 141 142 throw new ReferenceSyntaxException("Must be array or List. Bean: " 143 + base.getClass().getName() + ", index " + index); 144 } 145 catch (RuntimeException e) 146 { 147 log.error("Exception getting value for index " + index 148 + " of bean " 149 + base != null ? base.getClass().getName() : "NULL", e); 150 throw e; 151 } 152 } 153 154 public void setValue(Object base, Object property, Object newValue) 155 throws EvaluationException, PropertyNotFoundException 156 { 157 try 158 { 159 if (base == null) 160 { 161 throw new PropertyNotFoundException( 162 "Null bean, property: " + property); 163 } 164 if (property == null || 165 property instanceof String && ((String )property).length() == 0) 166 { 167 throw new PropertyNotFoundException("Bean: " 168 + base.getClass().getName() 169 + ", null or empty property name"); 170 } 171 172 if (base instanceof Map ) 173 { 174 ((Map ) base).put(property, newValue); 175 176 return; 177 } 178 179 setProperty(base, property.toString(), newValue); 181 } 182 catch (RuntimeException e) 183 { 184 log.error("Exception setting property " + property 185 + " of bean " 186 + base != null ? base.getClass().getName() : "NULL", e); 187 throw e; 188 } 189 } 190 191 public void setValue(Object base, int index, Object newValue) 192 throws EvaluationException, PropertyNotFoundException 193 { 194 try 195 { 196 if (base == null) 197 { 198 throw new PropertyNotFoundException( 199 "Null bean, index: " + index); 200 } 201 202 try 203 { 204 if (base.getClass().isArray()) 205 { 206 Array.set(base, index, newValue); 207 208 return; 209 } 210 if (base instanceof List ) 211 { 212 ((List ) base).set(index, newValue); 216 217 return; 218 } 219 } 220 catch (IndexOutOfBoundsException e) 221 { 222 throw new PropertyNotFoundException("Bean: " 223 + base.getClass().getName() + ", index " + index, e); 224 } 225 226 throw new EvaluationException( 227 "Bean must be array or List. Bean: " 228 + base.getClass().getName() + ", index " + index); 229 } 230 catch (RuntimeException e) 231 { 232 log.error("Exception setting value of index " + index + " of bean " 233 + base != null ? base.getClass().getName() : "NULL", e); 234 throw e; 235 } 236 } 237 238 public boolean isReadOnly(Object base, Object property) 239 { 240 try 241 { 242 if (base == null || property == null || 243 property instanceof String && ((String )property).length() == 0) 244 { 245 return false; 247 } 248 249 if (base instanceof Map ) 251 { 252 return false; 253 } 254 255 PropertyDescriptor propertyDescriptor = 257 getPropertyDescriptor(base, property.toString()); 258 259 return propertyDescriptor.getWriteMethod() == null; 260 } 261 catch (Exception e) 262 { 263 return false; 265 } 266 } 267 268 public boolean isReadOnly(Object base, int index) 269 { 270 try 271 { 272 if (base == null) 273 { 274 return false; 276 } 277 if (base instanceof List || base.getClass().isArray()) 278 { 279 return false; 281 } 282 283 return false; 285 } 286 catch (Exception e) 287 { 288 return false; 290 } 291 } 292 293 public Class getType(Object base, Object property) 294 { 295 try 296 { 297 if (base == null || property == null || 298 property instanceof String && ((String )property).length() == 0) 299 { 300 return null; 302 } 303 304 if (base instanceof Map ) 305 { 306 Object value = ((Map ) base).get(property); 307 308 return (value == null) ? Object .class : value.getClass(); 310 } 311 312 PropertyDescriptor propertyDescriptor = 314 getPropertyDescriptor(base, property.toString()); 315 316 return propertyDescriptor.getPropertyType(); 317 } 318 catch (Exception e) 319 { 320 return null; 322 } 323 } 324 325 public Class getType(Object base, int index) 326 { 327 try 328 { 329 if (base == null) 330 { 331 return null; 333 } 334 335 if (base.getClass().isArray()) 336 { 337 return base.getClass().getComponentType(); 338 } 339 340 if (base instanceof List ) 341 { 342 Object value = ((List ) base).get(index); 346 347 return (value != null) ? value.getClass() : Object .class; 349 } 350 351 return null; 353 } 354 catch (Exception e) 355 { 356 return null; 358 } 359 } 360 361 362 364 public static void setProperty(Object base, String name, Object newValue) 365 { 366 PropertyDescriptor propertyDescriptor = 367 getPropertyDescriptor(base, name); 368 369 Method m = propertyDescriptor.getWriteMethod(); 370 if (m == null) 371 { 372 throw new PropertyNotFoundException( 373 "Bean: " + base.getClass().getName() + ", property: " + name); 374 } 375 376 m = MethodUtils.getAccessibleMethod(m); 379 if (m == null) 380 { 381 throw new PropertyNotFoundException( 382 "Bean: " + base.getClass().getName() + ", property: " + name + " (not accessible!)"); 383 } 384 385 try 386 { 387 m.invoke(base, new Object [] {newValue}); 388 } 389 catch (Throwable t) 390 { 391 throw new EvaluationException("Bean: " 392 + base.getClass().getName() + ", property: " + name, t); 393 } 394 } 395 396 public static Object getProperty(Object base, String name) 397 { 398 PropertyDescriptor propertyDescriptor = 399 getPropertyDescriptor(base, name); 400 401 Method m = propertyDescriptor.getReadMethod(); 402 if (m == null) 403 { 404 throw new PropertyNotFoundException( 405 "Bean: " + base.getClass().getName() + ", property: " + name); 406 } 407 408 m = MethodUtils.getAccessibleMethod(m); 411 if (m == null) 412 { 413 throw new PropertyNotFoundException( 414 "Bean: " + base.getClass().getName() + ", property: " + name + " (not accessible!)"); 415 } 416 417 try 418 { 419 return m.invoke(base, NO_ARGS); 420 } 421 catch (Throwable t) 422 { 423 throw new EvaluationException("Bean: " 424 + base.getClass().getName() + ", property: " + name, t); 425 } 426 } 427 428 public static PropertyDescriptor getPropertyDescriptor( 429 Object base, String name) 430 { 431 PropertyDescriptor propertyDescriptor; 432 433 try 434 { 435 propertyDescriptor = 436 getPropertyDescriptor( 437 Introspector.getBeanInfo(base.getClass()), name); 438 } 439 catch (IntrospectionException e) 440 { 441 throw new PropertyNotFoundException("Bean: " 442 + base.getClass().getName() + ", property: " + name, e); 443 } 444 445 return propertyDescriptor; 446 } 447 448 public static PropertyDescriptor getPropertyDescriptor( 449 BeanInfo beanInfo, String propertyName) 450 { 451 PropertyDescriptor [] propDescriptors = 452 beanInfo.getPropertyDescriptors(); 453 454 if (propDescriptors != null) 455 { 456 for (int i = 0, len = propDescriptors.length; i < len; i++) 458 { 459 if (propDescriptors[i].getName().equals(propertyName)) 460 return propDescriptors[i]; 461 } 462 } 463 464 throw new PropertyNotFoundException("Bean: " 465 + beanInfo.getBeanDescriptor().getBeanClass().getName() 466 + ", property: " + propertyName); 467 } 468 469 } 470 | Popular Tags |