01: /*
02: * <copyright>
03: *
04: * Copyright 1997-2004 BBNT Solutions, LLC
05: * under sponsorship of the Defense Advanced Research Projects
06: * Agency (DARPA).
07: *
08: * You can redistribute this software and/or modify it under the
09: * terms of the Cougaar Open Source License as published on the
10: * Cougaar Open Source Website (www.cougaar.org).
11: *
12: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
13: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
14: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
15: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
16: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
17: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
18: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23: *
24: * </copyright>
25: */
26:
27: package org.cougaar.core.qos.ca;
28:
29: import java.util.HashMap;
30:
31: import org.cougaar.core.component.ServiceBroker;
32: import org.cougaar.core.service.ThreadService;
33: import org.cougaar.core.thread.Schedulable;
34:
35: /**
36: * Does the main work of the Alarm Coordinaton Artifact via
37: * ThreadService Schedulables.
38: */
39: abstract public class ThreadServiceSleeperFacet extends SleeperFacet {
40:
41: private ThreadService threadService;
42: private HashMap runners, facts;
43:
44: protected ThreadServiceSleeperFacet(CoordinationArtifact owner,
45: ServiceBroker sb, ConnectionSpec spec, RolePlayer player) {
46: super (owner, sb, spec, player);
47: runners = new HashMap();
48: facts = new HashMap();
49: threadService = (ThreadService) sb.getService(this ,
50: ThreadService.class, null);
51:
52: linkPlayer();
53: }
54:
55: abstract protected String getId(Object fact);
56:
57: abstract protected long getFireTime(Object fact);
58:
59: abstract protected void fireAlarm(Object fact);
60:
61: private class Runner implements Runnable {
62: String key;
63: Schedulable schedulable;
64:
65: Runner(String key) {
66: this .key = key;
67: schedulable = makeSchedulable(key, this );
68: }
69:
70: public void run() {
71: Object fact = null;
72: synchronized (facts) {
73: fact = facts.get(key);
74: }
75: fireAlarm(fact);
76: }
77: }
78:
79: private Schedulable makeSchedulable(String id, Runnable runner) {
80: return threadService.getThread(this , runner, id);
81: }
82:
83: public void processFactAssertion(Object fact) {
84: String key = getId(fact);
85: long time = getFireTime(fact);
86: Runner runner = null;
87: synchronized (runners) {
88: runner = (Runner) runners.get(key);
89: if (runner == null) {
90: runner = new Runner(key);
91: runners.put(key, runner);
92: }
93: facts.put(key, fact);
94: }
95: runner.schedulable.schedule(time);
96: }
97:
98: }
|