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: package org.cougaar.lib.aggagent.session;
27:
28: import java.io.PrintWriter;
29: import java.util.HashMap;
30: import java.util.Set;
31:
32: import org.cougaar.core.service.BlackboardService;
33: import org.cougaar.lib.aggagent.servlet.SubscriptionMonitorSupport;
34: import org.cougaar.util.UnaryPredicate;
35:
36: /**
37: * A SessionManager is a container for Sessions (q.v.), which may be created,
38: * retrieved, or destroyed using the methods of this class. Currently, only
39: * one type of Session is supported, that being a passive Session that reports
40: * results only when prompted. Support for other reporting strategies may be
41: * provided elsewhere.
42: */
43: public class SessionManager {
44: public static boolean debug = false;
45:
46: private String agentId = null;
47: private BlackboardService blackboard = null;
48: private SubscriptionMonitorSupport sms = null;
49: private HashMap sessions = new HashMap();
50: private int id_counter = 0;
51:
52: public SessionManager(String agentId, BlackboardService blackboard,
53: SubscriptionMonitorSupport sms) {
54: this .agentId = agentId;
55: this .blackboard = blackboard;
56: this .sms = sms;
57: }
58:
59: public Set getKeys() {
60: return sessions.keySet();
61: }
62:
63: public String addSession(UnaryPredicate p, IncrementFormat f,
64: String queryId) {
65: String k = String.valueOf(id_counter++);
66: ServletSession s = new ServletSession(k, queryId, f);
67: s.start(agentId, blackboard, sms, p);
68:
69: sessions.put(k, s);
70:
71: return k;
72: }
73:
74: public void cancelSession(String k) {
75: if (debug) {
76: System.out
77: .println("SessionManager::cancelSession: called on \""
78: + k + "\"");
79: }
80: ServletSession sess = (ServletSession) sessions.remove(k);
81: sess.endSession();
82: }
83:
84: private ServletSession getSession(String k) {
85: return (ServletSession) sessions.get(k);
86: }
87:
88: public void sendUpdate(String k, PrintWriter out) {
89: ServletSession s = getSession(k);
90: if (s != null)
91: s.sendUpdate(out);
92: }
93: }
|