KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > cron > QuartzJobSchedulerEntry


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
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 package org.apache.cocoon.components.cron;
17
18 import java.text.SimpleDateFormat JavaDoc;
19 import java.util.Date JavaDoc;
20
21 import org.quartz.CronTrigger;
22 import org.quartz.JobDataMap;
23 import org.quartz.JobDetail;
24 import org.quartz.Scheduler;
25 import org.quartz.SchedulerException;
26 import org.quartz.SimpleTrigger;
27 import org.quartz.Trigger;
28
29 /**
30  * Implementation of the JobSchedulerEntry interface for the QuartzJobScheduler
31  *
32  * @author <a HREF="mailto:giacomo@apache.org">Giacomo Pati</a>
33  * @version CVS $Id: QuartzJobSchedulerEntry.java 37181 2004-08-29 17:47:03Z vgritsenko $
34  */

35 public class QuartzJobSchedulerEntry implements JobSchedulerEntry {
36
37     /** The data map */
38     private final JobDataMap m_data;
39
40     /** The detail */
41     private final JobDetail m_detail;
42
43     /** The scheduler reference */
44     private final Scheduler m_scheduler;
45
46     /** The date formatter */
47     private final SimpleDateFormat JavaDoc m_formatter = new SimpleDateFormat JavaDoc("yyyy-MM-dd HH:mm:ss");
48
49     /** The name */
50     private final String JavaDoc m_name;
51
52     /** The trigger */
53     private final Trigger m_trigger;
54
55     /**
56      * Construct an JobSchedulerEntry
57      *
58      * @param name The name of the job
59      * @param scheduler The QuartzJobScheduler
60      *
61      * @throws SchedulerException in case of failures
62      */

63     public QuartzJobSchedulerEntry(final String JavaDoc name, final Scheduler scheduler)
64     throws SchedulerException {
65         m_scheduler = scheduler;
66         m_name = name;
67         m_detail = m_scheduler.getJobDetail(name, QuartzJobScheduler.DEFAULT_QUARTZ_JOB_GROUP);
68         m_data = m_detail.getJobDataMap();
69         m_trigger = m_scheduler.getTrigger(name, QuartzJobScheduler.DEFAULT_QUARTZ_JOB_GROUP);
70     }
71
72     /* (non-Javadoc)
73      * @see org.apache.cocoon.components.cron.JobSchedulerEntry#getJobName()
74      */

75     public String JavaDoc getJobName() {
76         String JavaDoc name = (String JavaDoc)m_data.get(QuartzJobScheduler.DATA_MAP_ROLE);
77
78         if (null == name) {
79             name = m_data.get(QuartzJobScheduler.DATA_MAP_OBJECT).getClass().getName();
80         }
81
82         return name;
83     }
84
85     /* (non-Javadoc)
86      * @see org.apache.cocoon.components.cron.JobSchedulerEntry#getName()
87      */

88     public String JavaDoc getName() {
89         return m_name;
90     }
91
92     /* (non-Javadoc)
93      * @see org.apache.cocoon.components.cron.JobSchedulerEntry#getNextTime()
94      */

95     public Date JavaDoc getNextTime() {
96         return m_trigger.getNextFireTime();
97     }
98
99     /* (non-Javadoc)
100      * @see org.apache.cocoon.components.cron.JobSchedulerEntry#isRunning()
101      */

102     public boolean isRunning() {
103         Boolean JavaDoc runs = (Boolean JavaDoc)m_data.get(QuartzJobScheduler.DATA_MAP_KEY_ISRUNNING);
104         if (null != runs) {
105             return runs.booleanValue();
106         }
107
108         return false;
109     }
110
111     /* (non-Javadoc)
112      * @see org.apache.cocoon.components.cron.JobSchedulerEntry#getSchedule()
113      */

114     public String JavaDoc getSchedule() {
115         if (m_trigger instanceof CronTrigger) {
116             return "cron: " + ((CronTrigger)m_trigger).getCronExpression();
117         } else if (m_trigger instanceof SimpleTrigger) {
118             if (((SimpleTrigger)m_trigger).getRepeatInterval() == 0) {
119                 return "once: at " + m_formatter.format(m_trigger.getFinalFireTime());
120             }
121
122             return "periodic: every " + (((SimpleTrigger)m_trigger).getRepeatInterval() / 1000) + "s";
123         } else {
124             return "next: " + m_formatter.format(m_trigger.getNextFireTime());
125         }
126     }
127 }
128
Popular Tags