KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jotm > TimerEvent


1 /*
2  * @(#) TimerEvent.java
3  *
4  * JOTM: Java Open Transaction Manager
5  *
6  *
7  * This module was originally developed by
8  *
9  * - Bull S.A. as part of the JOnAS application server code released in
10  * July 1999 (www.bull.com)
11  *
12  * --------------------------------------------------------------------------
13  * The original code and portions created by Bull SA are
14  * Copyright (c) 1999 BULL SA
15  * All rights reserved.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions are met:
19  *
20  * -Redistributions of source code must retain the above copyright notice, this
21  * list of conditions and the following disclaimer.
22  *
23  * -Redistributions in binary form must reproduce the above copyright notice,
24  * this list of conditions and the following disclaimer in the documentation
25  * and/or other materials provided with the distribution.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
31  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  *
39  * --------------------------------------------------------------------------
40  * $Id: TimerEvent.java,v 1.2 2005/03/15 00:05:38 tonyortiz Exp $
41  * --------------------------------------------------------------------------
42  */

43 package org.objectweb.jotm;
44
45 public class TimerEvent {
46
47     private TimerEventListener listener = null;
48     private Object JavaDoc arg = null;
49     private long reminding = 0;
50     private long startvalue;
51     private boolean permanent = false;
52     private boolean stopped = false;
53
54     /**
55      * Constructor
56      * @param l Object that will be notified when the timer expire.
57      * @param timeout nb of seconds before the timer expires.
58      * @param a info passed with the timer
59      * @param p true if the timer is permanent.
60      */

61     public TimerEvent(TimerEventListener l, long timeout, Object JavaDoc a, boolean p) {
62         if (TraceTm.jta.isDebugEnabled()) {
63             TraceTm.jta.debug("TimerEvent.new("+l+","+timeout+","+a+","+p+")");
64         }
65         
66         listener = l;
67         reminding = timeout;
68         startvalue = timeout;
69         arg = a;
70         permanent = p;
71     }
72
73
74     /**
75      * Update timer every second. Used by clock.
76      * - this must be called with the timerList monitor.
77      */

78     public long update() {
79         return --reminding;
80     }
81
82     /**
83      * Restart timer to its initial value
84      */

85     public void restart() {
86         if (TraceTm.jta.isDebugEnabled()) {
87             TraceTm.jta.debug("TimerEvent.new("+listener+").restart");
88         }
89
90         stopped = false;
91         reminding = startvalue;
92     }
93
94     /**
95      * Process the Timer
96      */

97     public void process() {
98         if (listener != null) {
99             if (TraceTm.jta.isDebugEnabled()) {
100                 TraceTm.jta.debug("TimerEvent.new("+listener+".process");
101             }
102
103             listener.timeoutExpired(arg);
104         }
105     }
106
107     public void change(long timeout, Object JavaDoc a) {
108         if (TraceTm.jta.isDebugEnabled()) {
109             TraceTm.jta.debug("TimerEvent("+listener+").change("+timeout+","+a+")");
110         }
111
112         stopped = false;
113         startvalue = timeout;
114         reminding = startvalue;
115         arg = a;
116     }
117
118     /**
119      * Unvalidate the timer. It will be removed by the timer manager.
120      */

121     public void unset() {
122         if (TraceTm.jta.isDebugEnabled()) {
123             TraceTm.jta.debug("TimerEvent("+listener+").unset");
124         }
125
126         // the timerlist is not locked.
127
reminding = 1;
128         arg = null;
129         listener = null;
130         permanent = false;
131         stopped = false;
132     }
133
134     /**
135      * stop the timer, but keep it for further reuse (See change())
136      */

137     public void stop() {
138         if (TraceTm.jta.isDebugEnabled()) {
139             TraceTm.jta.debug("TimerEvent("+listener+").stop");
140         }
141
142         // the timerlist is not locked.
143
reminding = 1000000;
144         stopped = true;
145     }
146
147     /**
148      * Is this timer valid ?
149      */

150     public boolean valid() {
151         return (listener != null);
152     }
153
154     /**
155      * Is this timer permanent ?
156      */

157     public boolean ispermanent() {
158         return permanent;
159     }
160
161     /**
162      * Is this timer stopped ?
163      */

164     public boolean isStopped() {
165         return stopped;
166     }
167 }
168
169
Popular Tags