KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_ejb > container > JTimerService


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: JTimerService.java,v 1.16 2005/07/12 08:48:04 durieuxp Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas_ejb.container;
27
28 import java.io.Serializable JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Collection JavaDoc;
31 import java.util.Date JavaDoc;
32 import java.util.Iterator JavaDoc;
33
34 import javax.ejb.EJBException JavaDoc;
35 import javax.ejb.Timer JavaDoc;
36 import javax.ejb.TimerService JavaDoc;
37 import javax.ejb.TransactionRolledbackLocalException JavaDoc;
38 import javax.transaction.TransactionManager JavaDoc;
39
40 import org.objectweb.jonas.jtm.TransactionService;
41 import org.objectweb.jonas.service.ServiceException;
42 import org.objectweb.jonas.service.ServiceManager;
43 import org.objectweb.jonas_timer.TraceTimer;
44 import org.objectweb.util.monolog.api.BasicLevel;
45
46 /**
47  * JOnAS Implementation of the TimerService interface (from EJB 2.1) One such
48  * object is created the first time a bean calls getTimerService. Basically
49  * manages the list of the Timers for that bean.
50  * @author Philippe Durieux
51  */

52 public class JTimerService implements TimerService JavaDoc {
53
54     private ArrayList JavaDoc mytimers = new ArrayList JavaDoc();
55
56     private JFactory bf = null;
57
58     private JEntitySwitch es = null;
59
60     private TransactionManager JavaDoc tm;
61
62     /**
63      * constructor used for MDB or Session beans
64      */

65     public JTimerService(JFactory bf) {
66         this.bf = bf;
67         // Get the transaction service
68
try {
69             TransactionService ts = (TransactionService) ServiceManager.getInstance().getTransactionService();
70             tm = ts.getTransactionManager();
71         } catch (Exception JavaDoc e) {
72             throw new EJBException JavaDoc("Error when starting the Timer service ", e);
73         }
74
75     }
76
77     /**
78      * constructor used for Entity beans
79      */

80     public JTimerService(JEntitySwitch es) {
81         this.es = es;
82         // Get the transaction service
83
try {
84             TransactionService ts = (TransactionService) ServiceManager.getInstance().getTransactionService();
85             tm = ts.getTransactionManager();
86         } catch (Exception JavaDoc e) {
87             throw new EJBException JavaDoc("Error when starting the Timer service ", e);
88         }
89
90     }
91
92     /**
93      * @return the Transaction Manager
94      */

95     public TransactionManager JavaDoc getTransactionManager() {
96         return tm;
97     }
98
99     /**
100      * Notify the timer to the listener
101      * @param timer The Timer object that will be notified
102      */

103     public void notify(Timer JavaDoc timer) {
104         if (es != null) {
105             // Entity Bean
106
es.notifyTimeout(timer);
107         } else {
108             if (bf instanceof JMdbFactory) {
109                 ((JMdbFactory) bf).notifyTimeout(timer);
110             } else if (bf instanceof JMdbEndpointFactory) {
111                 ((JMdbEndpointFactory) bf).notifyTimeout(timer);
112             } else if (bf instanceof JStatelessFactory) {
113                 ((JStatelessFactory) bf).notifyTimeout(timer);
114             } else {
115                 TraceEjb.logger.log(BasicLevel.ERROR, "Cannot notify this type of bean");
116             }
117         }
118     }
119
120     /**
121      * Remove the Timer
122      * @param timer The Timer object that will be removed
123      */

124     public void remove(Timer JavaDoc timer) {
125         synchronized (this) {
126             int index = mytimers.lastIndexOf(timer);
127             if (index == -1) {
128                 TraceTimer.logger.log(BasicLevel.WARN, "try to remove unexisting timer");
129             } else {
130                 mytimers.remove(index);
131             }
132         }
133     }
134
135     /**
136      * cancel all timers (when entity bean is removed)
137      */

138     public void cancelAllTimers() {
139         synchronized (this) {
140             es = null;
141             for (Iterator JavaDoc i = mytimers.iterator(); i.hasNext(); ) {
142                 JTimer timer = (JTimer) i.next();
143                 timer.stopTimer();
144             }
145             mytimers.clear();
146         }
147     }
148
149     /**
150      * get a Timer from the list
151      */

152     public Timer JavaDoc getTimer(long initialDuration, long intervalDuration, Serializable JavaDoc info) {
153         Timer JavaDoc dummy = new JTimer(this, initialDuration, intervalDuration, info);
154         synchronized (this) {
155             for (Iterator JavaDoc i = mytimers.iterator(); i.hasNext(); ) {
156                 JTimer timer = (JTimer) i.next();
157                 if (timer.equals(dummy)) {
158                     return timer;
159                 }
160             }
161         }
162         return null;
163     }
164
165     // ------------------------------------------------------------------------------------
166
// TimerService implemetation
167
// ------------------------------------------------------------------------------------
168

169     /**
170      * Create an interval timer whose first expiration occurs at a given point
171      * in time and whose subsequent expirations occur after a specified
172      * interval.
173      * @param initialExpiration The point in time at which the first timer
174      * expiration must occur.
175      * @param intervalDuration The number of milliseconds that must elapse
176      * between timer expiration notifications.
177      * @param info Application information to be delivered along with the timer
178      * expiration. This can be null.
179      * @return the newly created Timer.
180      * @throws IllegalArgumentException initialExpiration = 0, or
181      * intervalDuration < 0 or initialExpiration.getTime() < 0.
182      * @throws IllegalStateException the instance is in a state that does not
183      * allow access to this method.
184      * @throws EJBException If this method could not complete due to a
185      * system-level failure.
186      */

187     public Timer JavaDoc createTimer(Date JavaDoc initialExpiration, long intervalDuration, Serializable JavaDoc info)
188             throws IllegalArgumentException JavaDoc, IllegalStateException JavaDoc, EJBException JavaDoc {
189         if (initialExpiration == null) {
190             throw new IllegalArgumentException JavaDoc("expiration date is null");
191         }
192         // avoids negative argument here (add a few milliseconds)
193
long initialDuration = initialExpiration.getTime() - System.currentTimeMillis() + 20;
194         return createTimer(initialDuration, intervalDuration, info);
195     }
196
197     /**
198      * Create a single-action timer that expires at a given point in time.
199      * @param expiration The point in time at which the timer expiration must
200      * occur.
201      * @param info Application information to be delivered along with the timer
202      * expiration. This can be null.
203      * @return the newly created Timer.
204      * @throws IllegalArgumentException expiration = 0, or expiration.getTime() <
205      * 0.
206      * @throws IllegalStateException the instance is in a state that does not
207      * allow access to this method.
208      * @throws EJBException If this method could not complete due to a
209      * system-level failure.
210      */

211     public Timer JavaDoc createTimer(Date JavaDoc expiration, Serializable JavaDoc info) throws IllegalArgumentException JavaDoc,
212             IllegalStateException JavaDoc, EJBException JavaDoc {
213         return createTimer(expiration, 0, info);
214     }
215
216     /**
217      * Create an interval timer whose first expiration occurs after a specified
218      * duration, and whose subsequent expirations occur after a specified
219      * interval.
220      * @param initialDuration The number of milliseconds that must elapse before
221      * the first timer expiration notification.
222      * @param intervalDuration The number of milliseconds that must elapse
223      * between timer expiration notifications.
224      * @param info Application information to be delivered along with the timer
225      * expiration. This can be null.
226      * @return the newly created Timer.
227      * @throws IllegalArgumentException initialExpiration = 0, or
228      * intervalDuration < 0.
229      * @throws IllegalStateException the instance is in a state that does not
230      * allow access to this method.
231      * @throws EJBException If this method could not complete due to a
232      * system-level failure.
233      */

234     public Timer JavaDoc createTimer(long initialDuration, long intervalDuration, Serializable JavaDoc info)
235         throws IllegalArgumentException JavaDoc, IllegalStateException JavaDoc, EJBException JavaDoc {
236
237         // Check that we are not called from entity ejbCreate
238
if (es != null) {
239             es.getPrimaryKey(); // can throw IllegalStateException
240
}
241
242         // Check duration positive
243
if (initialDuration < 0 || intervalDuration < 0) {
244             throw new IllegalArgumentException JavaDoc("duration is negative");
245         }
246
247         JTimer timer = new JTimer(this, initialDuration, intervalDuration, info);
248         synchronized (this) {
249             if (mytimers.add(timer)) {
250                 timer.startTimer();
251             } else {
252                 TraceTimer.logger.log(BasicLevel.WARN, "create a timer already known");
253             }
254         }
255         return timer;
256     }
257
258
259     /**
260      * Create a single-action timer that expires after a specified duration.
261      * @param duration The number of milliseconds that must elapse before the
262      * timer expires.
263      * @param info Application information to be delivered along with the timer
264      * expiration. This can be null.
265      * @return the newly created Timer.
266      * @throws IllegalArgumentException initialExpiration = 0, or
267      * intervalDuration < 0.
268      * @throws IllegalStateException the instance is in a state that does not
269      * allow access to this method.
270      * @throws EJBException If this method could not complete due to a
271      * system-level failure.
272      */

273     public Timer JavaDoc createTimer(long duration, Serializable JavaDoc info) throws IllegalArgumentException JavaDoc, IllegalStateException JavaDoc,
274             EJBException JavaDoc {
275         return createTimer(duration, 0, info);
276     }
277
278     /**
279      * Get all the active timers associated with this bean.
280      * @return A collection of javax.ejb.Timer objects.
281      * @throws IllegalStateException the instance is in a state that does not
282      * allow access to this method.
283      * @throws EJBException If this method could not complete due to a
284      * system-level failure.
285      */

286     public Collection JavaDoc getTimers() throws IllegalStateException JavaDoc, EJBException JavaDoc {
287         // Check that we are not called from entity ejbCreate
288
if (es != null) {
289             es.getPrimaryKey(); // can throw IllegalStateException
290
}
291         ArrayList JavaDoc ret = new ArrayList JavaDoc();
292         for (Iterator JavaDoc i = mytimers.iterator(); i.hasNext(); ) {
293             JTimer t = (JTimer) i.next();
294             if (! t.isCancelled()) {
295                 ret.add(t);
296             }
297         }
298         return ret;
299     }
300
301     /**
302      * @return the EjbName used to retrieve the bean factory
303      */

304     public String JavaDoc getEjbName() {
305         if (bf != null) {
306             return bf.getEJBName();
307         }
308         if (es != null) {
309             return es.getBeanFactory().getEJBName();
310         }
311         return null;
312     }
313
314     /**
315      * @return the encoded PK for entity bean timers
316      */

317     public Serializable JavaDoc getPK() {
318         if (es != null) {
319             JEntityFactory ef = (JEntityFactory) es.getBeanFactory();
320             Serializable JavaDoc pks = (Serializable JavaDoc) es.getPrimaryKey();
321             Serializable JavaDoc pk = (Serializable JavaDoc) ef.encodePK(pks);
322             if (TraceEjb.isDebugIc()) {
323                 TraceEjb.interp.log(BasicLevel.DEBUG, "pk = " + pks);
324                 TraceEjb.interp.log(BasicLevel.DEBUG, "encoded pk = " + pk);
325             }
326             return pk;
327         } else {
328             return null;
329         }
330     }
331
332     /**
333      * @return the Container File Name
334      */

335     public String JavaDoc getContainer() {
336         JFactory f = bf;
337         if (es != null) {
338             f = es.getBeanFactory();
339         }
340         JContainer cont = f.getContainer();
341         return cont.getExternalFileName();
342     }
343 }
344
Popular Tags