001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.core.node;
028:
029: import java.util.Iterator;
030: import java.util.HashMap;
031: import java.util.Map;
032: import org.cougaar.core.agent.service.alarm.Alarm;
033: import org.cougaar.core.agent.service.alarm.Timer;
034:
035: /**
036: * A base class for timer service implementations.
037: */
038: public abstract class TimeServiceBase {
039:
040: private final String name;
041:
042: /** map of <Alarm,AlarmWrapper> */
043: private final Map alarms = new HashMap(11);
044:
045: public TimeServiceBase(Object requestor) {
046: name = (this .getClass().getName()) + " for "
047: + (requestor.toString());
048: }
049:
050: public String toString() {
051: return name;
052: }
053:
054: /**
055: * @return a {@link Timer}, not a {@link java.util.Timer}!
056: */
057: protected abstract Timer getTimer();
058:
059: public long currentTimeMillis() {
060: return getTimer().currentTimeMillis();
061: }
062:
063: public void addAlarm(Alarm alarm) {
064: getTimer().addAlarm(wrap(alarm));
065: }
066:
067: public void cancelAlarm(Alarm alarm) {
068: Alarm w = find(alarm);
069: if (w != null) {
070: getTimer().cancelAlarm(w);
071: }
072: }
073:
074: /** clear out any saved state, e.g. remove outstanding alarms */
075: protected void clear() {
076: synchronized (alarms) {
077: for (Iterator it = alarms.values().iterator(); it.hasNext();) {
078: Alarm w = (Alarm) it.next();
079: // should the Alarms themselves be cancelled? I'm guessing not
080: getTimer().cancelAlarm(w);
081: }
082: }
083: }
084:
085: /** create an AlarmWrapper around an Alarm, and remember it */
086: protected Alarm wrap(Alarm a) {
087: Alarm w = new AlarmWrapper(a);
088: synchronized (alarms) {
089: alarms.put(a, w);
090: }
091: return w;
092: }
093:
094: /** drop an Alarm (not an AlarmWrapper) from the remembered alarms */
095: protected void forget(Alarm a) {
096: synchronized (alarms) {
097: alarms.remove(a);
098: }
099: }
100:
101: /** Find the remembered AlarmWrapper matching a given Alarm */
102: protected AlarmWrapper find(Alarm a) {
103: synchronized (alarms) {
104: return (AlarmWrapper) alarms.get(a);
105: }
106: }
107:
108: protected class AlarmWrapper implements Alarm {
109: private Alarm alarm;
110:
111: AlarmWrapper(Alarm alarm) {
112: this .alarm = alarm;
113: }
114:
115: public long getExpirationTime() {
116: return alarm.getExpirationTime();
117: }
118:
119: public boolean hasExpired() {
120: return alarm.hasExpired();
121: }
122:
123: // called by Timer to notify that the alarm has rung
124: public void expire() {
125: forget(alarm);
126: alarm.expire();
127: }
128:
129: // called by client to notify that the alarm should be cancelled
130: // usually just sets hasExpired
131: public boolean cancel() {
132: forget(alarm);
133: return alarm.cancel();
134: }
135:
136: public String toString() {
137: return "AlarmWrapper(" + alarm + ") of "
138: + (TimeServiceBase.this.toString());
139: }
140: }
141: }
|