001: /*
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 Bull S.A.
004: * Contact: jonas-team@objectweb.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: * Initial developer(s): ____________________________________.
022: * Contributor(s): ______________________________________.
023: *
024: * --------------------------------------------------------------------------
025: * $Id: TimerEvent.java 4766 2004-05-17 09:57:47Z durieuxp $
026: * --------------------------------------------------------------------------
027: */
028:
029: package org.objectweb.jonas_timer;
030:
031: import org.objectweb.util.monolog.api.BasicLevel;
032:
033: public class TimerEvent {
034:
035: private TimerEventListener listener = null;
036: private Object arg = null;
037: private long remaining; // millisec
038: private long startvalue; // millisec
039: private long createtime;
040: private boolean permanent = false;
041: private boolean stopped = false;
042:
043: /**
044: * Constructor
045: * @param l Object that will be notified when the timer expire.
046: * @param timeout nb of milliseconds before the timer expires.
047: * @param a info passed with the timer
048: * @param p true if the timer is permanent.
049: */
050: public TimerEvent(TimerEventListener l, long timeout, Object a,
051: boolean p) {
052: if (TraceTimer.isDebug())
053: TraceTimer.logger.log(BasicLevel.DEBUG, "listener = " + l
054: + ",timeout = " + timeout + " ,object = " + a
055: + " ,permanent = " + p + ")");
056: listener = l;
057: remaining = timeout;
058: startvalue = timeout;
059: createtime = System.currentTimeMillis();
060: arg = a;
061: permanent = p;
062: }
063:
064: /**
065: * Update timer every second. Used by clock.
066: * - this must be called with the timerList monitor.
067: * @param ms clock period
068: */
069: public long update() {
070: remaining = startvalue + createtime
071: - System.currentTimeMillis();
072: if (TraceTimer.isDebug() && remaining <= 10) {
073: TraceTimer.logger.log(BasicLevel.DEBUG, "listener = "
074: + listener + " remaining = " + remaining);
075: }
076: return remaining;
077: }
078:
079: /**
080: * Restart timer to its initial value
081: */
082: public long restart() {
083: stopped = false;
084: // remaining should be < 0 here.
085: createtime = System.currentTimeMillis() + remaining;
086: remaining += startvalue;
087: if (TraceTimer.isDebug())
088: TraceTimer.logger.log(BasicLevel.DEBUG, "listener = "
089: + listener + " remaining = " + remaining);
090: return remaining;
091: }
092:
093: /**
094: * Process the Timer
095: */
096: public void process() {
097: if (listener != null) {
098: if (TraceTimer.isDebug())
099: TraceTimer.logger.log(BasicLevel.DEBUG, "listener = "
100: + listener + " remaining = " + remaining);
101: listener.timeoutExpired(arg);
102: }
103: }
104:
105: /**
106: * Change the Timer value
107: * @param timeout in milliseconds
108: */
109: public void change(long timeout, Object a) {
110: if (TraceTimer.isDebug())
111: TraceTimer.logger.log(BasicLevel.DEBUG, "listener = "
112: + listener + " timeout = " + timeout + ",object = "
113: + a);
114: stopped = false;
115: startvalue = timeout;
116: remaining = startvalue;
117: arg = a;
118: }
119:
120: /**
121: * Unvalidate the timer. It will be removed by the timer manager.
122: */
123: public void unset() {
124: if (TraceTimer.isDebug())
125: TraceTimer.logger.log(BasicLevel.DEBUG, "listener = "
126: + listener);
127: // the timerlist is not locked.
128: remaining = 100;
129: arg = null;
130: listener = null;
131: permanent = false;
132: stopped = false;
133: }
134:
135: /**
136: * stop the timer, but keep it for further reuse (See change())
137: */
138: public void stop() {
139: if (TraceTimer.isDebug())
140: TraceTimer.logger.log(BasicLevel.DEBUG, "listener = "
141: + listener);
142: // the timerlist is not locked.
143: remaining = 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: * @return remaining time in millisec
170: */
171: public long getRemaining() {
172: return remaining;
173: }
174: }
|