001: /*
002: * <copyright>
003: *
004: * Copyright 2003-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: package org.cougaar.lib.aggagent.session;
027:
028: import java.io.PrintWriter;
029:
030: import org.cougaar.core.blackboard.Subscription;
031: import org.cougaar.core.service.BlackboardService;
032: import org.cougaar.lib.aggagent.servlet.SubscriptionListener;
033: import org.cougaar.lib.aggagent.servlet.SubscriptionMonitorSupport;
034: import org.cougaar.util.UnaryPredicate;
035:
036: /**
037: * A Session is a handler for instances of the RemoteBlackboardSubscription
038: * class. In the context of a Servlet, this class can be used to manage
039: * remote access to the local Blackboard through the
040: * RemoteBlackboardSubscription API.
041: */
042: public class ServletSession extends RemoteSession implements
043: SubscriptionListener {
044: protected Object lock = new Object();
045: protected RemoteBlackboardSubscription data = null;
046:
047: /**
048: * Create a new Session with the specified session ID to search the
049: * blackboard for Objects matching the predicate given.
050: */
051: public ServletSession(String k, String queryId, IncrementFormat f) {
052: super (k, queryId, f);
053: }
054:
055: /**
056: * Create the RemoteSubscription instance to be managed by this Session.
057: * Once this method is called, subscriptionChanged() events may start
058: * arriving.
059: */
060: public void start(String agentId, BlackboardService b,
061: SubscriptionMonitorSupport sms, UnaryPredicate p) {
062: synchronized (lock) {
063: setAgentId(agentId);
064: try {
065: b.openTransaction();
066: data = new RemoteBlackboardSubscription(b, p);
067: sms.setSubscriptionListener(data.getSubscription(),
068: this );
069: } finally {
070: b.closeTransactionDontReset();
071: }
072: }
073: }
074:
075: /**
076: * Get the SubscriptionAccess implementation containing the data to be
077: * encoded and sent to a client. In the case of a PSPSession, we have a
078: * RemotePSPSubscription.
079: */
080: protected SubscriptionAccess getData() {
081: return data;
082: }
083:
084: /**
085: * End this session and let it halt all active and passive functions.
086: */
087: public void endSession() {
088: data.shutDown();
089: }
090:
091: /**
092: * Check to see whether new information has been gathered since the last
093: * report.
094: */
095: public boolean hasChanged() {
096: return data.hasChanged();
097: }
098:
099: /**
100: * Send an update of recent changes to the resident RemoteSubscription
101: * through the provided OutputStream. An IncrementFormat instance is used
102: * to encode the data being sent.
103: */
104: public void sendUpdate(PrintWriter out) {
105: UpdateDelta del = null;
106: synchronized (lock) {
107: data.open();
108: del = createUpdateDelta();
109: data.close();
110: }
111: out.println(del.toXml());
112: out.flush();
113: }
114:
115: /**
116: * Implementation of the UISubscriber interface. When the COUGAAR agent has
117: * changes to report, it will call this method so that the Session can
118: * respond accordingly. The only action included by default is to call the
119: * subscriptionChanged() method of the underlying RemotePSPSubscription.
120: * Subclasses may wish to take additional steps, such as sending notices to
121: * other agents, etc.
122: * <br><br>
123: * The Subscription argument is ignored, as the relevant Subscription is
124: * presumed to be the one encapsulated within the RemotePSPSubscription.
125: */
126: public void subscriptionChanged(Subscription sub) {
127: synchronized (lock) {
128: data.subscriptionChanged();
129: }
130: }
131: }
|