1 16 17 18 package org.oddjob.framework; 19 20 import org.apache.commons.beanutils.ConversionException; 21 import org.apache.commons.beanutils.DynaBean; 22 import org.apache.commons.beanutils.DynaClass; 23 import org.apache.commons.beanutils.DynaProperty; 24 import org.apache.commons.beanutils.PropertyUtils; 25 26 50 51 public class WrapDynaBean implements DynaBean { 52 53 54 56 57 63 public WrapDynaBean(Object instance) { 64 65 super(); 66 this.instance = instance; 67 this.dynaClass = WrapDynaClass.createDynaClass(instance.getClass()); 68 69 } 70 71 72 74 75 79 protected WrapDynaClass dynaClass = null; 80 81 82 85 protected Object instance = null; 86 87 88 90 91 101 public boolean contains(String name, String key) { 102 103 throw new UnsupportedOperationException 104 ("WrapDynaBean does not support contains()"); 105 106 } 107 108 109 117 public Object get(String name) { 118 119 Object value = null; 120 try { 121 value = PropertyUtils.getSimpleProperty(instance, name); 122 } catch (Throwable t) { 123 throw new IllegalArgumentException 124 ("Property '" + name + "' has no read method"); 125 } 126 return (value); 127 128 } 129 130 131 146 public Object get(String name, int index) { 147 148 Object value = null; 149 try { 150 value = PropertyUtils.getIndexedProperty(instance, name, index); 151 } catch (IndexOutOfBoundsException e) { 152 throw e; 153 } catch (Throwable t) { 154 throw new IllegalArgumentException 155 ("Property '" + name + "' has no indexed read method"); 156 } 157 return (value); 158 159 } 160 161 162 174 public Object get(String name, String key) { 175 176 Object value = null; 177 try { 178 value = PropertyUtils.getMappedProperty(instance, name, key); 179 } catch (Throwable t) { 180 throw new IllegalArgumentException 181 ("Property '" + name + "' has no mapped read method"); 182 } 183 return (value); 184 185 } 186 187 188 192 public DynaClass getDynaClass() { 193 194 return (this.dynaClass); 195 196 } 197 198 199 210 public void remove(String name, String key) { 211 212 213 throw new UnsupportedOperationException 214 ("WrapDynaBean does not support remove()"); 215 216 } 217 218 219 232 public void set(String name, Object value) { 233 234 try { 235 PropertyUtils.setSimpleProperty(instance, name, value); 236 } catch (Throwable t) { 237 throw new IllegalArgumentException 238 ("Property '" + name + "' has no write method"); 239 } 240 241 } 242 243 244 260 public void set(String name, int index, Object value) { 261 262 try { 263 PropertyUtils.setIndexedProperty(instance, name, index, value); 264 } catch (IndexOutOfBoundsException e) { 265 throw e; 266 } catch (Throwable t) { 267 throw new IllegalArgumentException 268 ("Property '" + name + "' has no indexed write method"); 269 } 270 271 } 272 273 274 288 public void set(String name, String key, Object value) { 289 290 try { 291 PropertyUtils.setMappedProperty(instance, name, key, value); 292 } catch (Throwable t) { 293 throw new IllegalArgumentException 294 ("Property '" + name + "' has no mapped write method"); 295 } 296 297 } 298 299 309 public Object getInstance() { 310 return instance; 311 } 312 313 314 316 317 325 protected DynaProperty getDynaProperty(String name) { 326 327 DynaProperty descriptor = getDynaClass().getDynaProperty(name); 328 if (descriptor == null) { 329 throw new IllegalArgumentException 330 ("Invalid property name '" + name + "'"); 331 } 332 return (descriptor); 333 334 } 335 336 337 } 338 | Popular Tags |