001: /**
002: * EasyBeans
003: * Copyright (C) 2007 Bull S.A.S.
004: * Contact: easybeans@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: EasyBeansTimer.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.component.quartz;
025:
026: import java.io.Serializable;
027: import java.util.Date;
028:
029: import javax.ejb.EJBException;
030: import javax.ejb.NoSuchObjectLocalException;
031: import javax.ejb.Timer;
032: import javax.ejb.TimerHandle;
033:
034: import org.quartz.Scheduler;
035: import org.quartz.SchedulerException;
036: import org.quartz.Trigger;
037:
038: /**
039: * Implementation of the Timer interface of EJB specification.
040: * @author Florent Benoit
041: */
042: public class EasyBeansTimer implements Timer {
043:
044: /**
045: * Job Detail for this timer.
046: */
047: private EasyBeansJobDetail jobDetail = null;
048:
049: /**
050: * Trigger used by this timer.
051: */
052: private Trigger trigger = null;
053:
054: /**
055: * Scheduler used for this timer.
056: */
057: private Scheduler scheduler = null;
058:
059: /**
060: * Create a new Timer object with the given objects (job, trigger and scheduler).
061: * @param jobDetail the given job used to cancel the timer or in order to get Serializable info
062: * @param trigger the trigger used to get the next fire
063: * @param scheduler for canceling jobs
064: */
065: public EasyBeansTimer(final EasyBeansJobDetail jobDetail,
066: final Trigger trigger, final Scheduler scheduler) {
067: this .jobDetail = jobDetail;
068: this .trigger = trigger;
069: this .scheduler = scheduler;
070: }
071:
072: /**
073: * Cause the timer and all its associated expiration notifications to be
074: * cancelled.
075: * @throws IllegalStateException If this method is invoked while the
076: * instance is in a state that does not allow access to this method.
077: * @throws NoSuchObjectLocalException If invoked on a timer that has expired
078: * or has been cancelled.
079: * @throws EJBException If this method could not complete due to a
080: * system-level failure.
081: */
082: public void cancel() throws IllegalStateException,
083: NoSuchObjectLocalException, EJBException {
084: // Delete job that has been registered by timer service
085: try {
086: scheduler.deleteJob(jobDetail.getName(), jobDetail
087: .getGroup());
088: } catch (SchedulerException e) {
089: throw new EJBException("Cannot cancel job with name '"
090: + jobDetail.getName() + "'.", e);
091: }
092: }
093:
094: /**
095: * Get the number of milliseconds that will elapse before the next scheduled
096: * timer expiration.
097: * @return the number of milliseconds that will elapse before the next
098: * scheduled timer expiration.
099: * @throws IllegalStateException If this method is invoked while the
100: * instance is in a state that does not allow access to this method.
101: * @throws NoSuchObjectLocalException If invoked on a timer that has expired
102: * or has been cancelled.
103: * @throws EJBException If this method could not complete due to a
104: * system-level failure.
105: */
106: public long getTimeRemaining() throws IllegalStateException,
107: NoSuchObjectLocalException, EJBException {
108: // If there is no more next timeout, getNextTimeout() method will throw IllegalStateException
109: return getNextTimeout().getTime() - System.currentTimeMillis();
110: }
111:
112: /**
113: * Get the point in time at which the next timer expiration is scheduled to
114: * occur.
115: * @return the point in time at which the next timer expiration is scheduled
116: * to occur.
117: * @throws IllegalStateException If this method is invoked while the
118: * instance is in a state that does not allow access to this method.
119: * @throws NoSuchObjectLocalException If invoked on a timer that has expired
120: * or has been cancelled.
121: * @throws EJBException If this method could not complete due to a
122: * system-level failure.
123: */
124: public Date getNextTimeout() throws IllegalStateException,
125: NoSuchObjectLocalException, EJBException {
126:
127: // Get next timeout
128: Date date = trigger.getNextFireTime();
129:
130: // May be null (trigger will not fire again)
131: if (date == null) {
132: throw new IllegalStateException(
133: "No next timeout for this timer");
134: }
135:
136: // return the date
137: return date;
138: }
139:
140: /**
141: * Get the information associated with the timer at the time of creation.
142: * @return The Serializable object that was passed in at timer creation, or
143: * null if the info argument passed in at timer creation was null.
144: * @throws IllegalStateException If this method is invoked while the
145: * instance is in a state that does not allow access to this method.
146: * @throws NoSuchObjectLocalException If invoked on a timer that has expired
147: * or has been cancelled.
148: * @throws EJBException If this method could not complete due to a
149: * system-level failure.
150: */
151: public Serializable getInfo() throws IllegalStateException,
152: NoSuchObjectLocalException, EJBException {
153: // Get info from the data of the job detail
154: return jobDetail.getJobDetailData().getInfo();
155: }
156:
157: /**
158: * Get a serializable handle to the timer. This handle can be used at a
159: * later time to re-obtain the timer reference.
160: * @return a serializable handle to the timer.
161: * @throws IllegalStateException If this method is invoked while the
162: * instance is in a state that does not allow access to this method.
163: * @throws NoSuchObjectLocalException If invoked on a timer that has expired
164: * or has been cancelled.
165: * @throws EJBException If this method could not complete due to a
166: * system-level failure.
167: */
168: public TimerHandle getHandle() throws IllegalStateException,
169: NoSuchObjectLocalException, EJBException {
170: return new EasyBeansTimerHandle(jobDetail);
171: }
172:
173: }
|