1 17 package org.quartz.simpl; 18 19 import java.beans.BeanInfo ; 20 import java.beans.IntrospectionException ; 21 import java.beans.Introspector ; 22 import java.beans.PropertyDescriptor ; 23 import java.lang.reflect.InvocationTargetException ; 24 import java.util.Iterator ; 25 import java.util.Locale ; 26 import java.util.Map ; 27 28 import org.quartz.Job; 29 import org.quartz.JobDataMap; 30 import org.quartz.SchedulerException; 31 import org.quartz.spi.TriggerFiredBundle; 32 33 34 35 49 public class PropertySettingJobFactory extends SimpleJobFactory { 50 private boolean warnIfNotFound = true; 51 private boolean throwIfNotFound = false; 52 53 public Job newJob(TriggerFiredBundle bundle) throws SchedulerException { 54 55 Job job = super.newJob(bundle); 56 57 JobDataMap jobDataMap = new JobDataMap(); 58 jobDataMap.putAll(bundle.getJobDetail().getJobDataMap()); 59 jobDataMap.putAll(bundle.getTrigger().getJobDataMap()); 60 61 setBeanProps(job, jobDataMap); 62 63 return job; 64 } 65 66 protected void setBeanProps(Object obj, JobDataMap data) throws SchedulerException { 67 68 BeanInfo bi = null; 69 try { 70 bi = Introspector.getBeanInfo(obj.getClass()); 71 } catch (IntrospectionException e) { 72 handleError("Unable to introspect Job class.", e); 73 } 74 75 PropertyDescriptor [] propDescs = bi.getPropertyDescriptors(); 76 77 for (Iterator entryIter = data.getWrappedMap().entrySet().iterator(); entryIter.hasNext();) { 80 Map.Entry entry = (Map.Entry )entryIter.next(); 81 82 String name = (String )entry.getKey(); 83 String c = name.substring(0, 1).toUpperCase(Locale.US); 84 String methName = "set" + c + name.substring(1); 85 86 java.lang.reflect.Method setMeth = getSetMethod(methName, propDescs); 87 88 Class paramType = null; 89 Object o = null; 90 91 try { 92 if (setMeth == null) { 93 handleError( 94 "No setter on Job class " + obj.getClass().getName() + 95 " for property '" + name + "'"); 96 continue; 97 } 98 99 paramType = setMeth.getParameterTypes()[0]; 100 o = entry.getValue(); 101 102 Object parm = null; 103 if (paramType.isPrimitive()) { 104 if (o == null) { 105 handleError( 106 "Cannot set primitive property '" + name + 107 "' on Job class " + obj.getClass().getName() + 108 " to null."); 109 continue; 110 } 111 112 if (paramType.equals(int.class)) { 113 if (o instanceof String ) { 114 parm = new Integer ((String )o); 115 } else if (o instanceof Integer ) { 116 parm = o; 117 } 118 } else if (paramType.equals(long.class)) { 119 if (o instanceof String ) { 120 parm = new Long ((String )o); 121 } else if (o instanceof Long ) { 122 parm = o; 123 } 124 } else if (paramType.equals(float.class)) { 125 if (o instanceof String ) { 126 parm = new Float ((String )o); 127 } else if (o instanceof Float ) { 128 parm = o; 129 } 130 } else if (paramType.equals(double.class)) { 131 if (o instanceof String ) { 132 parm = new Double ((String )o); 133 } else if (o instanceof Double ) { 134 parm = o; 135 } 136 } else if (paramType.equals(boolean.class)) { 137 if (o instanceof String ) { 138 parm = new Boolean ((String )o); 139 } else if (o instanceof Boolean ) { 140 parm = o; 141 } 142 } else if (paramType.equals(byte.class)) { 143 if (o instanceof String ) { 144 parm = new Byte ((String )o); 145 } else if (o instanceof Byte ) { 146 parm = o; 147 } 148 } else if (paramType.equals(short.class)) { 149 if (o instanceof String ) { 150 parm = new Short ((String )o); 151 } else if (o instanceof Short ) { 152 parm = o; 153 } 154 } else if (paramType.equals(char.class)) { 155 if (o instanceof String ) { 156 String str = (String )o; 157 if (str.length() == 1) { 158 parm = new Character (str.charAt(0)); 159 } 160 } else if (o instanceof Character ) { 161 parm = o; 162 } 163 } 164 } else if ((o != null) && (paramType.isAssignableFrom(o.getClass()))) { 165 parm = o; 166 } 167 168 if ((o != null) && (parm == null)) { 171 handleError( 172 "The setter on Job class " + obj.getClass().getName() + 173 " for property '" + name + 174 "' expects a " + paramType + 175 " but was given " + o.getClass().getName()); 176 continue; 177 } 178 179 setMeth.invoke(obj, new Object []{ parm }); 180 } catch (NumberFormatException nfe) { 181 handleError( 182 "The setter on Job class " + obj.getClass().getName() + 183 " for property '" + name + 184 "' expects a " + paramType + 185 " but was given " + o.getClass().getName(), nfe); 186 } catch (IllegalArgumentException e) { 187 handleError( 188 "The setter on Job class " + obj.getClass().getName() + 189 " for property '" + name + 190 "' expects a " + paramType + 191 " but was given " + o.getClass().getName(), e); 192 } catch (IllegalAccessException e) { 193 handleError( 194 "The setter on Job class " + obj.getClass().getName() + 195 " for property '" + name + 196 "' could not be accessed.", e); 197 } catch (InvocationTargetException e) { 198 handleError( 199 "The setter on Job class " + obj.getClass().getName() + 200 " for property '" + name + 201 "' could not be invoked.", e); 202 } 203 } 204 } 205 206 private void handleError(String message) throws SchedulerException { 207 handleError(message, null); 208 } 209 210 private void handleError(String message, Exception e) throws SchedulerException { 211 if (isThrowIfPropertyNotFound()) { 212 throw new SchedulerException(message, e); 213 } 214 215 if (isWarnIfPropertyNotFound()) { 216 if (e == null) { 217 getLog().warn(message); 218 } else { 219 getLog().warn(message, e); 220 } 221 } 222 } 223 224 private java.lang.reflect.Method getSetMethod(String name, 225 PropertyDescriptor [] props) { 226 for (int i = 0; i < props.length; i++) { 227 java.lang.reflect.Method wMeth = props[i].getWriteMethod(); 228 229 if(wMeth == null) { 230 continue; 231 } 232 233 if(wMeth.getParameterTypes().length != 1) { 234 continue; 235 } 236 237 if (wMeth.getName().equals(name)) { 238 return wMeth; 239 } 240 } 241 242 return null; 243 } 244 245 252 public boolean isThrowIfPropertyNotFound() { 253 return throwIfNotFound; 254 } 255 256 263 public void setThrowIfPropertyNotFound(boolean throwIfNotFound) { 264 this.throwIfNotFound = throwIfNotFound; 265 } 266 267 274 public boolean isWarnIfPropertyNotFound() { 275 return warnIfNotFound; 276 } 277 278 285 public void setWarnIfPropertyNotFound(boolean warnIfNotFound) { 286 this.warnIfNotFound = warnIfNotFound; 287 } 288 } | Popular Tags |