001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.components.cron;
018:
019: import java.text.SimpleDateFormat;
020: import java.util.Date;
021:
022: import org.quartz.CronTrigger;
023: import org.quartz.JobDataMap;
024: import org.quartz.JobDetail;
025: import org.quartz.Scheduler;
026: import org.quartz.SchedulerException;
027: import org.quartz.SimpleTrigger;
028: import org.quartz.Trigger;
029:
030: /**
031: * Implementation of the JobSchedulerEntry interface for the QuartzJobScheduler
032: *
033: * @author <a href="mailto:giacomo@apache.org">Giacomo Pati</a>
034: * @version CVS $Id: QuartzJobSchedulerEntry.java 433543 2006-08-22 06:22:54Z crossley $
035: */
036: public class QuartzJobSchedulerEntry implements JobSchedulerEntry {
037:
038: /** The data map */
039: private final JobDataMap m_data;
040:
041: /** The detail */
042: private final JobDetail m_detail;
043:
044: /** The scheduler reference */
045: private final Scheduler m_scheduler;
046:
047: /** The date formatter */
048: private final SimpleDateFormat m_formatter = new SimpleDateFormat(
049: "yyyy-MM-dd HH:mm:ss");
050:
051: /** The name */
052: private final String m_name;
053:
054: /** The trigger */
055: private final Trigger m_trigger;
056:
057: /**
058: * Construct an JobSchedulerEntry
059: *
060: * @param name The name of the job
061: * @param scheduler The QuartzJobScheduler
062: *
063: * @throws SchedulerException in case of failures
064: */
065: public QuartzJobSchedulerEntry(final String name,
066: final Scheduler scheduler) throws SchedulerException {
067: m_scheduler = scheduler;
068: m_name = name;
069: m_detail = m_scheduler.getJobDetail(name,
070: QuartzJobScheduler.DEFAULT_QUARTZ_JOB_GROUP);
071: m_data = m_detail.getJobDataMap();
072: m_trigger = m_scheduler.getTrigger(name,
073: QuartzJobScheduler.DEFAULT_QUARTZ_JOB_GROUP);
074: }
075:
076: /* (non-Javadoc)
077: * @see org.apache.cocoon.components.cron.JobSchedulerEntry#getJobName()
078: */
079: public String getJobName() {
080: String name = (String) m_data
081: .get(QuartzJobScheduler.DATA_MAP_ROLE);
082:
083: if (null == name) {
084: name = m_data.get(QuartzJobScheduler.DATA_MAP_OBJECT)
085: .getClass().getName();
086: }
087:
088: return name;
089: }
090:
091: /* (non-Javadoc)
092: * @see org.apache.cocoon.components.cron.JobSchedulerEntry#getName()
093: */
094: public String getName() {
095: return m_name;
096: }
097:
098: /* (non-Javadoc)
099: * @see org.apache.cocoon.components.cron.JobSchedulerEntry#getNextTime()
100: */
101: public Date getNextTime() {
102: return m_trigger.getNextFireTime();
103: }
104:
105: /* (non-Javadoc)
106: * @see org.apache.cocoon.components.cron.JobSchedulerEntry#isRunning()
107: */
108: public boolean isRunning() {
109: Boolean runs = (Boolean) m_data
110: .get(QuartzJobScheduler.DATA_MAP_KEY_ISRUNNING);
111: if (null != runs) {
112: return runs.booleanValue();
113: }
114:
115: return false;
116: }
117:
118: /* (non-Javadoc)
119: * @see org.apache.cocoon.components.cron.JobSchedulerEntry#getSchedule()
120: */
121: public String getSchedule() {
122: if (m_trigger instanceof CronTrigger) {
123: return "cron: "
124: + ((CronTrigger) m_trigger).getCronExpression();
125: } else if (m_trigger instanceof SimpleTrigger) {
126: if (((SimpleTrigger) m_trigger).getRepeatInterval() == 0) {
127: return "once: at "
128: + m_formatter.format(m_trigger
129: .getFinalFireTime());
130: }
131:
132: return "periodic: every "
133: + (((SimpleTrigger) m_trigger).getRepeatInterval() / 1000)
134: + "s";
135: } else {
136: return "next: "
137: + m_formatter.format(m_trigger.getNextFireTime());
138: }
139: }
140: }
|