1 7 package com.inversoft.beans; 8 9 10 import java.util.ArrayList ; 11 import java.util.Iterator ; 12 import java.util.List ; 13 14 import com.inversoft.util.ReflectionException; 15 import com.inversoft.util.ReflectionTools; 16 import com.inversoft.util.typeconverter.TypeConversionException; 17 import com.inversoft.util.typeconverter.TypeConverter; 18 import com.inversoft.util.typeconverter.TypeConverterRegistry; 19 20 21 28 public abstract class BaseBeanProperty { 29 30 34 35 38 protected static final int GET = 0; 39 40 43 protected static final int SET = 1; 44 45 49 protected static final int PRE_CONVERT = 2; 50 51 55 protected static final int POST_CONVERT = 3; 56 57 61 protected static final int BAD_CONVERT = 4; 62 63 64 67 protected String propertyName; 68 69 72 protected Class beanClass; 73 74 77 protected Class propertyType; 78 79 82 protected List propertyListeners; 83 84 87 protected List conversionListeners; 88 89 90 99 protected BaseBeanProperty() { 100 propertyListeners = new ArrayList (); 101 conversionListeners = new ArrayList (); 102 } 103 104 113 protected BaseBeanProperty(String propertyName, Class beanClass) throws BeanException { 114 this(); 115 this.propertyName = propertyName; 116 this.beanClass = beanClass; 117 118 initialize(); 120 } 121 122 132 protected BaseBeanProperty(String propertyName, String beanClass) throws BeanException { 133 this(); 134 this.propertyName = propertyName; 135 136 try { 137 this.beanClass = Class.forName(beanClass); 138 } catch (ClassNotFoundException cnfe) { 139 throw new BeanException(cnfe.getMessage(), cnfe); 140 } 141 142 initialize(); 144 } 145 146 160 abstract protected void initialize() throws BeanException; 161 162 180 public String getPropertyName() { 181 return propertyName; 182 } 183 184 228 public Class getPropertyType() { 229 return propertyType; 230 } 231 232 242 public String getFullName() { 243 return propertyName; 244 } 245 246 249 public Class getBeanClass() { 250 return beanClass; 251 } 252 253 260 public Object instantiate() throws BeanException { 261 try { 262 return ReflectionTools.instantiate(beanClass); 263 } catch (ReflectionException re) { 264 throw new BeanException(re); 265 } 266 } 267 268 275 public void addPropertyListener(PropertyListener listener) { 276 propertyListeners.add(listener); 277 } 278 279 286 public void removePropertyListener(PropertyListener listener) { 287 propertyListeners.remove(listener); 288 } 289 290 297 public Iterator getPropertyListeners() { 298 return propertyListeners.iterator(); 299 } 300 301 308 public boolean hasPropertyListeners() { 309 return !propertyListeners.isEmpty(); 310 } 311 312 319 public void addConversionListener(ConversionListener listener) { 320 conversionListeners.add(listener); 321 } 322 323 330 public void removeConversionListener(ConversionListener listener) { 331 conversionListeners.remove(listener); 332 } 333 334 339 public Iterator getConversionListeners() { 340 return conversionListeners.iterator(); 341 } 342 343 348 public boolean hasConversionListeners() { 349 return !conversionListeners.isEmpty(); 350 } 351 352 358 abstract public Object getPropertyValue(final Object bean) throws BeanException; 359 360 372 abstract public void setPropertyValue(final Object bean, Object value, 373 final boolean convert) 374 throws BeanException, TypeConversionException; 375 376 386 public void setPropertyValue(final Object bean, Object value) throws BeanException { 387 try { 388 setPropertyValue(bean, value, false); 389 } catch (TypeConversionException tce) { 390 assert false : "FATAL: should never throw TypeConversionException" + 391 " because this method does no conversion"; 392 } 393 } 394 395 408 protected Object convertParameter(final Object value, final Object bean, 409 Class type) 410 throws TypeConversionException { 411 412 Object newValue = value; 413 414 if (!type.isInstance(value)) { 417 418 if (hasConversionListeners()) { 419 fireConversionEvent(PRE_CONVERT, value, newValue, bean); 420 } 421 422 TypeConverter converter = TypeConverterRegistry.lookup(type); 423 if (converter == null) { 424 throw new TypeConversionException("No type converter found for the type: " 425 + type.getName()); 426 } 427 428 try { 429 newValue = converter.convert(value, type); 430 } catch (TypeConversionException tce) { 431 432 if (hasConversionListeners()) { 433 fireConversionEvent(BAD_CONVERT, value, newValue, bean); 434 } 435 436 throw tce; 437 } 438 } 439 440 if (hasConversionListeners()) { 441 fireConversionEvent(POST_CONVERT, value, newValue, bean); 442 } 443 444 return newValue; 445 } 446 447 464 protected void firePropertyEvent(final int type, final Object oldValue, 465 final Object newValue, final Object bean, final Object index) 466 throws BeanException { 467 468 Iterator iter = propertyListeners.iterator(); 470 PropertyEvent event = new PropertyEvent(this, oldValue, newValue, bean, index); 471 PropertyListener listener; 472 473 while (iter.hasNext()) { 474 listener = (PropertyListener) iter.next(); 475 476 switch (type) { 477 case GET: 478 listener.handleGet(event); 479 break; 480 case SET: 481 listener.handleSet(event); 482 break; 483 default: 484 throw new IllegalArgumentException ("Invalid event type"); 485 } 486 } 487 } 488 489 504 protected void fireConversionEvent(final int type, final Object oldValue, final Object newValue, 505 final Object bean) { 506 507 Iterator iter = conversionListeners.iterator(); 509 ConversionEvent event = new ConversionEvent(this, oldValue, newValue, bean); 510 ConversionListener listener; 511 512 while (iter.hasNext()) { 513 listener = (ConversionListener) iter.next(); 514 515 switch (type) { 516 case PRE_CONVERT: 517 listener.handlePreConversion(event); 518 break; 519 case POST_CONVERT: 520 listener.handlePostConversion(event); 521 break; 522 case BAD_CONVERT: 523 listener.handleFailedConversion(event); 524 break; 525 default: 526 throw new IllegalArgumentException ("Invalid event type"); 527 } 528 } 529 } 530 } 531 | Popular Tags |