KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > scheduling > quartz > SpringBeanJobFactory


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy 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,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.scheduling.quartz;
18
19 import org.quartz.SchedulerContext;
20 import org.quartz.spi.TriggerFiredBundle;
21
22 import org.springframework.beans.BeanWrapper;
23 import org.springframework.beans.BeanWrapperImpl;
24 import org.springframework.beans.MutablePropertyValues;
25
26 /**
27  * Subclass of {@link AdaptableJobFactory} that also supports Spring-style
28  * dependency injection on bean properties. This is essentially the direct
29  * equivalent of Spring's {@link QuartzJobBean} in the shape of a
30  * Quartz 1.5 {@link org.quartz.spi.JobFactory}.
31  *
32  * <p>Applies scheduler context, job data map and trigger data map entries
33  * as bean property values. If no matching bean property is found, the entry
34  * is by default simply ignored. This is analogous to QuartzJobBean's behavior.
35  *
36  * @author Juergen Hoeller
37  * @since 2.0
38  * @see SchedulerFactoryBean#setJobFactory
39  * @see QuartzJobBean
40  */

41 public class SpringBeanJobFactory extends AdaptableJobFactory implements SchedulerContextAware {
42
43     private String JavaDoc[] ignoredUnknownProperties;
44
45     private SchedulerContext schedulerContext;
46
47
48     /**
49      * Specify the unknown properties (not found in the bean) that should be ignored.
50      * <p>Default is <code>null</code>, indicating that all unknown properties
51      * should be ignored. Specify an empty array to throw an exception in case
52      * of any unknown properties, or a list of property names that should be
53      * ignored if there is no corresponding property found on the particular
54      * job class (all other unknown properties will still trigger an exception).
55      */

56     public void setIgnoredUnknownProperties(String JavaDoc[] ignoredUnknownProperties) {
57         this.ignoredUnknownProperties = ignoredUnknownProperties;
58     }
59
60     public void setSchedulerContext(SchedulerContext schedulerContext) {
61         this.schedulerContext = schedulerContext;
62     }
63
64
65     /**
66      * Create the job instance, populating it with property values taken
67      * from the scheduler context, job data map and trigger data map.
68      */

69     protected Object JavaDoc createJobInstance(TriggerFiredBundle bundle) {
70         BeanWrapper bw = new BeanWrapperImpl(bundle.getJobDetail().getJobClass());
71         if (isEligibleForPropertyPopulation(bw.getWrappedInstance())) {
72             MutablePropertyValues pvs = new MutablePropertyValues();
73             if (this.schedulerContext != null) {
74                 pvs.addPropertyValues(this.schedulerContext);
75             }
76             pvs.addPropertyValues(bundle.getJobDetail().getJobDataMap());
77             pvs.addPropertyValues(bundle.getTrigger().getJobDataMap());
78             if (this.ignoredUnknownProperties != null) {
79                 for (int i = 0; i < this.ignoredUnknownProperties.length; i++) {
80                     String JavaDoc propName = this.ignoredUnknownProperties[i];
81                     if (pvs.contains(propName) && !bw.isWritableProperty(propName)) {
82                         pvs.removePropertyValue(propName);
83                     }
84                 }
85                 bw.setPropertyValues(pvs);
86             }
87             else {
88                 bw.setPropertyValues(pvs, true);
89             }
90         }
91         return bw.getWrappedInstance();
92     }
93
94     /**
95      * Return whether the given job object is eligible for having
96      * its bean properties populated.
97      * <p>The default implementation ignores {@link QuartzJobBean} instances,
98      * which will inject bean properties themselves.
99      * @param jobObject the job object to introspect
100      * @see QuartzJobBean
101      */

102     protected boolean isEligibleForPropertyPopulation(Object JavaDoc jobObject) {
103         return (!(jobObject instanceof QuartzJobBean));
104     }
105
106 }
107
Popular Tags