KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quartz > simpl > PropertySettingJobFactory


1 /*
2  * Copyright 2004-2005 OpenSymphony
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy
6  * of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations
14  * under the License.
15  *
16  */

17 package org.quartz.simpl;
18
19 import java.beans.BeanInfo JavaDoc;
20 import java.beans.IntrospectionException JavaDoc;
21 import java.beans.Introspector JavaDoc;
22 import java.beans.PropertyDescriptor JavaDoc;
23 import java.lang.reflect.InvocationTargetException JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.Locale JavaDoc;
26 import java.util.Map JavaDoc;
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 /**
36  * A JobFactory that instantiates the Job instance (using the default no-arg
37  * constructor, or more specifically: <code>class.newInstance()</code>), and
38  * then attempts to set all values in the <code>JobExecutionContext</code>'s
39  * <code>JobDataMap</code> onto bean properties of the <code>Job</code>.
40  *
41  * @see org.quartz.spi.JobFactory
42  * @see SimpleJobFactory
43  * @see org.quartz.JobExecutionContext#getMergedJobDataMap()
44  * @see #setWarnIfPropertyNotFound(boolean)
45  * @see #setThrowIfPropertyNotFound(boolean)
46  *
47  * @author jhouse
48  */

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 JavaDoc obj, JobDataMap data) throws SchedulerException {
67
68         BeanInfo JavaDoc bi = null;
69         try {
70             bi = Introspector.getBeanInfo(obj.getClass());
71         } catch (IntrospectionException JavaDoc e) {
72             handleError("Unable to introspect Job class.", e);
73         }
74         
75         PropertyDescriptor JavaDoc[] propDescs = bi.getPropertyDescriptors();
76         
77         // Get the wrapped entry set so don't have to incur overhead of wrapping for
78
// dirty flag checking since this is read only access
79
for (Iterator JavaDoc entryIter = data.getWrappedMap().entrySet().iterator(); entryIter.hasNext();) {
80             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)entryIter.next();
81             
82             String JavaDoc name = (String JavaDoc)entry.getKey();
83             String JavaDoc c = name.substring(0, 1).toUpperCase(Locale.US);
84             String JavaDoc methName = "set" + c + name.substring(1);
85         
86             java.lang.reflect.Method JavaDoc setMeth = getSetMethod(methName, propDescs);
87         
88             Class JavaDoc paramType = null;
89             Object JavaDoc 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 JavaDoc 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 JavaDoc) {
114                             parm = new Integer JavaDoc((String JavaDoc)o);
115                         } else if (o instanceof Integer JavaDoc) {
116                             parm = o;
117                         }
118                     } else if (paramType.equals(long.class)) {
119                         if (o instanceof String JavaDoc) {
120                             parm = new Long JavaDoc((String JavaDoc)o);
121                         } else if (o instanceof Long JavaDoc) {
122                             parm = o;
123                         }
124                     } else if (paramType.equals(float.class)) {
125                         if (o instanceof String JavaDoc) {
126                             parm = new Float JavaDoc((String JavaDoc)o);
127                         } else if (o instanceof Float JavaDoc) {
128                             parm = o;
129                         }
130                     } else if (paramType.equals(double.class)) {
131                         if (o instanceof String JavaDoc) {
132                             parm = new Double JavaDoc((String JavaDoc)o);
133                         } else if (o instanceof Double JavaDoc) {
134                             parm = o;
135                         }
136                     } else if (paramType.equals(boolean.class)) {
137                         if (o instanceof String JavaDoc) {
138                             parm = new Boolean JavaDoc((String JavaDoc)o);
139                         } else if (o instanceof Boolean JavaDoc) {
140                             parm = o;
141                         }
142                     } else if (paramType.equals(byte.class)) {
143                         if (o instanceof String JavaDoc) {
144                             parm = new Byte JavaDoc((String JavaDoc)o);
145                         } else if (o instanceof Byte JavaDoc) {
146                             parm = o;
147                         }
148                     } else if (paramType.equals(short.class)) {
149                         if (o instanceof String JavaDoc) {
150                             parm = new Short JavaDoc((String JavaDoc)o);
151                         } else if (o instanceof Short JavaDoc) {
152                             parm = o;
153                         }
154                     } else if (paramType.equals(char.class)) {
155                         if (o instanceof String JavaDoc) {
156                             String JavaDoc str = (String JavaDoc)o;
157                             if (str.length() == 1) {
158                                 parm = new Character JavaDoc(str.charAt(0));
159                             }
160                         } else if (o instanceof Character JavaDoc) {
161                             parm = o;
162                         }
163                     }
164                 } else if ((o != null) && (paramType.isAssignableFrom(o.getClass()))) {
165                     parm = o;
166                 }
167                 
168                 // If the parameter wasn't originally null, but we didn't find a
169
// matching parameter, then we are stuck.
170
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 JavaDoc[]{ parm });
180             } catch (NumberFormatException JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc message) throws SchedulerException {
207         handleError(message, null);
208     }
209     
210     private void handleError(String JavaDoc message, Exception JavaDoc 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 JavaDoc getSetMethod(String JavaDoc name,
225             PropertyDescriptor JavaDoc[] props) {
226         for (int i = 0; i < props.length; i++) {
227             java.lang.reflect.Method JavaDoc 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     /**
246      * Whether the JobInstantiation should fail and throw and exception if
247      * a key (name) and value (type) found in the JobDataMap does not
248      * correspond to a proptery setter on the Job class.
249      *
250      * @return Returns the throwIfNotFound.
251      */

252     public boolean isThrowIfPropertyNotFound() {
253         return throwIfNotFound;
254     }
255
256     /**
257      * Whether the JobInstantiation should fail and throw and exception if
258      * a key (name) and value (type) found in the JobDataMap does not
259      * correspond to a proptery setter on the Job class.
260      *
261      * @param throwIfNotFound defaults to <code>false</code>.
262      */

263     public void setThrowIfPropertyNotFound(boolean throwIfNotFound) {
264         this.throwIfNotFound = throwIfNotFound;
265     }
266
267     /**
268      * Whether a warning should be logged if
269      * a key (name) and value (type) found in the JobDataMap does not
270      * correspond to a proptery setter on the Job class.
271      *
272      * @return Returns the warnIfNotFound.
273      */

274     public boolean isWarnIfPropertyNotFound() {
275         return warnIfNotFound;
276     }
277
278     /**
279      * Whether a warning should be logged if
280      * a key (name) and value (type) found in the JobDataMap does not
281      * correspond to a proptery setter on the Job class.
282      *
283      * @param warnIfNotFound defaults to <code>true</code>.
284      */

285     public void setWarnIfPropertyNotFound(boolean warnIfNotFound) {
286         this.warnIfNotFound = warnIfNotFound;
287     }
288 }
Popular Tags