1 16 17 package org.apache.commons.beanutils; 18 19 import org.apache.commons.collections.Closure; 20 import org.apache.commons.logging.Log; 21 import org.apache.commons.logging.LogFactory; 22 23 import java.lang.reflect.InvocationTargetException ; 24 25 26 79 public class BeanPropertyValueChangeClosure implements Closure { 80 81 82 private final Log log = LogFactory.getLog(this.getClass()); 83 84 87 private String propertyName; 88 89 93 private Object propertyValue; 94 95 105 private boolean ignoreNull; 106 107 117 public BeanPropertyValueChangeClosure(String propertyName, Object propertyValue) { 118 this(propertyName, propertyValue, false); 119 } 120 121 134 public BeanPropertyValueChangeClosure(String propertyName, Object propertyValue, boolean ignoreNull) { 135 super(); 136 137 if ((propertyName != null) && (propertyName.length() > 0)) { 138 this.propertyName = propertyName; 139 this.propertyValue = propertyValue; 140 this.ignoreNull = ignoreNull; 141 } else { 142 throw new IllegalArgumentException ("propertyName cannot be null or empty"); 143 } 144 } 145 146 160 public void execute(Object object) { 161 162 try { 163 PropertyUtils.setProperty(object, propertyName, propertyValue); 164 } catch (IllegalArgumentException e) { 165 final String errorMsg = "Unable to execute Closure. Null value encountered in property path..."; 166 167 if (ignoreNull) { 168 log.warn("WARNING: " + errorMsg, e); 169 } else { 170 log.error("ERROR: " + errorMsg, e); 171 throw e; 172 } 173 } catch (IllegalAccessException e) { 174 final String errorMsg = "Unable to access the property provided."; 175 log.error(errorMsg, e); 176 throw new IllegalArgumentException (errorMsg); 177 } catch (InvocationTargetException e) { 178 final String errorMsg = "Exception occurred in property's getter"; 179 log.error(errorMsg, e); 180 throw new IllegalArgumentException (errorMsg); 181 } catch (NoSuchMethodException e) { 182 final String errorMsg = "Property not found"; 183 log.error(errorMsg, e); 184 throw new IllegalArgumentException (errorMsg); 185 } 186 } 187 188 193 public String getPropertyName() { 194 return propertyName; 195 } 196 197 204 public Object getPropertyValue() { 205 return propertyValue; 206 } 207 208 221 public boolean isIgnoreNull() { 222 return ignoreNull; 223 } 224 } 225 | Popular Tags |