KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > timerservice > quartz > TimerImpl


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.ejb3.timerservice.quartz;
23
24 import java.io.Serializable JavaDoc;
25 import java.util.Date JavaDoc;
26
27 import javax.ejb.EJBException JavaDoc;
28 import javax.ejb.NoSuchObjectLocalException JavaDoc;
29 import javax.ejb.Timer JavaDoc;
30 import javax.ejb.TimerHandle JavaDoc;
31
32 import org.jboss.logging.Logger;
33 import org.quartz.Scheduler;
34 import org.quartz.SchedulerException;
35 import org.quartz.Trigger;
36
37 /**
38  * A view on an actual (persistent) timer.
39  *
40  * This object must never be serializable (EJB3 18.4.1)
41  *
42  * @author <a HREF="mailto:carlo@nerdnet.nl">Carlo de Wolf</a>
43  * @version $Revision: 56169 $
44  */

45 public class TimerImpl implements Timer JavaDoc
46 {
47    private static final Logger log = Logger.getLogger(TimerImpl.class);
48    
49    private Scheduler scheduler;
50    private Trigger trigger;
51    private Serializable JavaDoc info;
52    
53    protected TimerImpl(Scheduler scheduler, Trigger trigger, Serializable JavaDoc info) {
54       assert scheduler != null;
55       assert trigger != null;
56       
57       this.scheduler = scheduler;
58       this.trigger = trigger;
59       this.info = info;
60    }
61    
62    protected void checkState()
63    {
64       // TODO: implement bean state checking to see if a call is allowed
65

66       if(trigger.getNextFireTime() == null)
67          throw new NoSuchObjectLocalException JavaDoc("timer has expired");
68    }
69    
70    /**
71     * Cause the timer and all its associated expiration notifications to be cancelled.
72     *
73     * @throws IllegalStateException If this method is invoked while the instance is in a state that does not allow access to this method.
74     * @throws NoSuchObjectLocalException If invoked on a timer that has expired or has been cancelled.
75     * @throws EJBException If this method could not complete due to a system-level failure.
76     */

77    public void cancel() throws IllegalStateException JavaDoc, NoSuchObjectLocalException JavaDoc, EJBException JavaDoc
78    {
79       checkState();
80       
81       try {
82          // TODO: call TimerServiceImpl.cancelTimer instead
83
scheduler.unscheduleJob(trigger.getName(), trigger.getGroup());
84       }
85       catch(SchedulerException e) {
86          log.error("cancel failed", e);
87          throw new EJBException JavaDoc(e.getMessage());
88       }
89    }
90
91    /**
92     * Get the number of milliseconds that will elapse before the next scheduled timer expiration.
93     *
94     * @return The number of milliseconds that will elapse before the next scheduled timer expiration.
95     * @throws IllegalStateException If this method is invoked while the instance is in a state that does not allow access to this method.
96     * @throws NoSuchObjectLocalException If invoked on a timer that has expired or has been cancelled.
97     * @throws EJBException If this method could not complete due to a system-level failure.
98     */

99    public long getTimeRemaining() throws IllegalStateException JavaDoc, NoSuchObjectLocalException JavaDoc, EJBException JavaDoc
100    {
101       // leave all checks to getNextTimeout
102
return getNextTimeout().getTime() - System.currentTimeMillis();
103    }
104
105    /**
106     * Get the point in time at which the next timer expiration is scheduled to occur.
107     *
108     * @return The point in time at which the next timer expiration is scheduled to occur.
109     * @throws IllegalStateException If this method is invoked while the instance is in a state that does not allow access to this method.
110     * @throws NoSuchObjectLocalException If invoked on a timer that has expired or has been cancelled.
111     * @throws EJBException If this method could not complete due to a system-level failure.
112     */

113    public Date JavaDoc getNextTimeout() throws IllegalStateException JavaDoc, NoSuchObjectLocalException JavaDoc, EJBException JavaDoc
114    {
115       checkState();
116       
117       Date JavaDoc nextTimeout = trigger.getNextFireTime();
118       if(nextTimeout == null)
119          throw new IllegalStateException JavaDoc("trigger does not have a next fire time"); // TODO: proper EJB3 state check & exception
120
return nextTimeout;
121    }
122
123    /**
124     * Get the information associated with the timer at the time of creation.
125     *
126     * @return The Serializable object that was passed in at timer creation, or null if the info argument passed in at timer creation was null.
127     * @throws IllegalStateException If this method is invoked while the instance is in a state that does not allow access to this method.
128     * @throws NoSuchObjectLocalException If invoked on a timer that has expired or has been cancelled.
129     * @throws EJBException If this method could not complete due to a system-level failure.
130     */

131    public Serializable JavaDoc getInfo() throws IllegalStateException JavaDoc, NoSuchObjectLocalException JavaDoc, EJBException JavaDoc
132    {
133       checkState();
134       
135       return info;
136    }
137
138    /**
139     * Get a serializable handle to the timer. This handle can be used at a later time to re-obtain the timer reference.
140     *
141     * @return A serializable handle to the timer.
142     * @throws IllegalStateException If this method is invoked while the instance is in a state that does not allow access to this method.
143     * @throws NoSuchObjectLocalException If invoked on a timer that has expired or has been cancelled.
144     * @throws EJBException If this method could not complete due to a system-level failure.
145     */

146    public TimerHandle JavaDoc getHandle() throws IllegalStateException JavaDoc, NoSuchObjectLocalException JavaDoc, EJBException JavaDoc
147    {
148       checkState();
149       
150       return null; // FIXME: implement getHandle
151
}
152 }
153
Popular Tags