01: /*
02: * <copyright>
03: *
04: * Copyright 2003-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.lib.aggagent.plugin;
28:
29: import java.util.Enumeration;
30: import java.util.Iterator;
31:
32: import org.cougaar.core.blackboard.IncrementalSubscription;
33: import org.cougaar.core.plugin.ComponentPlugin;
34: import org.cougaar.core.service.BlackboardService;
35: import org.cougaar.lib.aggagent.query.Alert;
36: import org.cougaar.lib.aggagent.query.QueryResultAdapter;
37: import org.cougaar.util.UnaryPredicate;
38:
39: /**
40: * This Plugin serves a dual purpose. It notices when queries have changed
41: * and updates the associated Alert instances, and also notices when an Alert
42: * has changed its state so that notices may be sent.
43: * <br><br>
44: * Currently, notices are not implemented, so none are actually sent.
45: */
46: public class AlertPlugin extends ComponentPlugin {
47: private static class ClassInstanceSeeker implements UnaryPredicate {
48: private Class type = null;
49:
50: public ClassInstanceSeeker(Class c) {
51: type = c;
52: }
53:
54: public boolean execute(Object o) {
55: return type.isInstance(o);
56: }
57: }
58:
59: private static UnaryPredicate resultSetSeeker = new ClassInstanceSeeker(
60: QueryResultAdapter.class);
61: private static UnaryPredicate alertSeeker = new ClassInstanceSeeker(
62: Alert.class);
63:
64: private IncrementalSubscription resultSets = null;
65: private IncrementalSubscription alerts = null;
66:
67: public void setupSubscriptions() {
68: BlackboardService blackboard = getBlackboardService();
69: resultSets = (IncrementalSubscription) blackboard
70: .subscribe(resultSetSeeker);
71: alerts = (IncrementalSubscription) blackboard
72: .subscribe(alertSeeker);
73: }
74:
75: public void execute() {
76: if (resultSets.hasChanged()) {
77: // update alerts associated with changed result set(s)
78: for (Enumeration e = resultSets.getChangedList(); e
79: .hasMoreElements();) {
80: updateAlerts(((QueryResultAdapter) e.nextElement())
81: .getAlerts());
82: }
83: }
84: if (alerts.hasChanged()) {
85: // set initial state of new alert(s)
86: updateAlerts(alerts.getAddedCollection().iterator());
87: System.out
88: .println("AlertPlugin::execute: Alerts have changed");
89: }
90: }
91:
92: private void updateAlerts(Iterator alerts) {
93: while (alerts.hasNext()) {
94: Alert a = (Alert) alerts.next();
95: if (a.update())
96: getBlackboardService().publishChange(a);
97: }
98: }
99: }
|