KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.Map JavaDoc;
20
21 import org.quartz.Job;
22 import org.quartz.JobDetail;
23 import org.quartz.Scheduler;
24
25 import org.springframework.beans.factory.BeanNameAware;
26 import org.springframework.beans.factory.InitializingBean;
27 import org.springframework.context.ApplicationContext;
28 import org.springframework.context.ApplicationContextAware;
29
30 /**
31  * Convenience subclass of Quartz' JobDetail class that eases bean-style
32  * usage.
33  *
34  * <p><code>JobDetail</code> itself is already a JavaBean but lacks
35  * sensible defaults. This class uses the Spring bean name as job name,
36  * and the Quartz default group ("DEFAULT") as job group if not specified.
37  *
38  * @author Juergen Hoeller
39  * @since 18.02.2004
40  * @see #setName
41  * @see #setGroup
42  * @see org.springframework.beans.factory.BeanNameAware
43  * @see org.quartz.Scheduler#DEFAULT_GROUP
44  */

45 public class JobDetailBean extends JobDetail
46     implements BeanNameAware, ApplicationContextAware, InitializingBean {
47
48     private Class JavaDoc actualJobClass;
49
50     private String JavaDoc beanName;
51
52     private ApplicationContext applicationContext;
53
54     private String JavaDoc applicationContextJobDataKey;
55
56
57     /**
58      * Overridden to support any job class, to allow a custom JobFactory
59      * to adapt the given job class to the Quartz Job interface.
60      * @see SchedulerFactoryBean#setJobFactory
61      */

62     public void setJobClass(Class JavaDoc jobClass) {
63         if (jobClass != null && !Job.class.isAssignableFrom(jobClass)) {
64             super.setJobClass(DelegatingJob.class);
65             this.actualJobClass = jobClass;
66         }
67         else {
68             super.setJobClass(jobClass);
69         }
70     }
71
72     /**
73      * Overridden to support any job class, to allow a custom JobFactory
74      * to adapt the given job class to the Quartz Job interface.
75      */

76     public Class JavaDoc getJobClass() {
77         return (this.actualJobClass != null ? this.actualJobClass : super.getJobClass());
78     }
79
80     /**
81      * Register objects in the JobDataMap via a given Map.
82      * <p>These objects will be available to this Job only,
83      * in contrast to objects in the SchedulerContext.
84      * <p>Note: When using persistent Jobs whose JobDetail will be kept in the
85      * database, do not put Spring-managed beans or an ApplicationContext
86      * reference into the JobDataMap but rather into the SchedulerContext.
87      * @param jobDataAsMap Map with String keys and any objects as values
88      * (for example Spring-managed beans)
89      * @see SchedulerFactoryBean#setSchedulerContextAsMap
90      */

91     public void setJobDataAsMap(Map JavaDoc jobDataAsMap) {
92         getJobDataMap().putAll(jobDataAsMap);
93     }
94
95     /**
96      * Set a list of JobListener names for this job, referring to
97      * non-global JobListeners registered with the Scheduler.
98      * <p>A JobListener name always refers to the name returned
99      * by the JobListener implementation.
100      * @see SchedulerFactoryBean#setJobListeners
101      * @see org.quartz.JobListener#getName
102      */

103     public void setJobListenerNames(String JavaDoc[] names) {
104         for (int i = 0; i < names.length; i++) {
105             addJobListener(names[i]);
106         }
107     }
108
109     public void setBeanName(String JavaDoc beanName) {
110         this.beanName = beanName;
111     }
112
113     public void setApplicationContext(ApplicationContext applicationContext) {
114         this.applicationContext = applicationContext;
115     }
116
117     /**
118      * Set the key of an ApplicationContext reference to expose in the JobDataMap,
119      * for example "applicationContext". Default is none.
120      * Only applicable when running in a Spring ApplicationContext.
121      * <p>In case of a QuartzJobBean, the reference will be applied to the Job
122      * instance as bean property. An "applicationContext" attribute will correspond
123      * to a "setApplicationContext" method in that scenario.
124      * <p>Note that BeanFactory callback interfaces like ApplicationContextAware
125      * are not automatically applied to Quartz Job instances, because Quartz
126      * itself is responsible for the lifecycle of its Jobs.
127      * <p><b>Note: When using persistent job stores where JobDetail contents will
128      * be kept in the database, do not put an ApplicationContext reference into
129      * the JobDataMap but rather into the SchedulerContext.</b>
130      * @see SchedulerFactoryBean#setApplicationContextSchedulerContextKey
131      * @see org.springframework.context.ApplicationContext
132      */

133     public void setApplicationContextJobDataKey(String JavaDoc applicationContextJobDataKey) {
134         this.applicationContextJobDataKey = applicationContextJobDataKey;
135     }
136
137
138     public void afterPropertiesSet() {
139         if (getName() == null) {
140             setName(this.beanName);
141         }
142         if (getGroup() == null) {
143             setGroup(Scheduler.DEFAULT_GROUP);
144         }
145         if (this.applicationContextJobDataKey != null) {
146             if (this.applicationContext == null) {
147                 throw new IllegalStateException JavaDoc(
148                     "JobDetailBean needs to be set up in an ApplicationContext " +
149                     "to be able to handle an 'applicationContextJobDataKey'");
150             }
151             getJobDataMap().put(this.applicationContextJobDataKey, this.applicationContext);
152         }
153     }
154
155 }
156
Popular Tags