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.logistics.servlet;
028:
029: import org.cougaar.core.blackboard.BlackboardClient;
030: import org.cougaar.core.service.BlackboardService;
031: import org.cougaar.core.service.AlarmService;
032: import org.cougaar.core.servlet.BaseServletComponent;
033: import org.cougaar.util.UnaryPredicate;
034:
035: import javax.servlet.Servlet;
036: import javax.servlet.http.HttpServlet;
037: import javax.servlet.http.HttpServletRequest;
038: import javax.servlet.http.HttpServletResponse;
039: import java.io.IOException;
040: import java.io.PrintWriter;
041: import java.util.Collection;
042: import java.util.Iterator;
043:
044: /**
045: * This servlet publishes a CommStatus object to the agent's blackboard.
046: *
047: * commStatus?commUp=false&connectedAgentName=<AGENT NAME>
048: * <AGENT NAME> refers to the agent that has lost communications
049: */
050:
051: public class CommStatusServlet extends BaseServletComponent implements
052: BlackboardClient {
053: private BlackboardService blackboard;
054: private AlarmService alarmService;
055:
056: protected String getPath() {
057: return "/" + getRelativePath();
058: }
059:
060: protected String getRelativePath() {
061: return "commStatus";
062: }
063:
064: protected Servlet createServlet() {
065: blackboard = (BlackboardService) serviceBroker.getService(this ,
066: BlackboardService.class, null);
067: if (blackboard == null) {
068: throw new RuntimeException(
069: "Unable to obtain blackboard service");
070: }
071: alarmService = (AlarmService) serviceBroker.getService(this ,
072: AlarmService.class, null);
073: if (alarmService == null) {
074: throw new RuntimeException("Unable to set alarm service");
075: }
076: return new MyServlet();
077: }
078:
079: public void setBlackboardService(BlackboardService blackboard) {
080: this .blackboard = blackboard;
081: }
082:
083: // BlackboardClient method:
084: public String getBlackboardClientName() {
085: return toString();
086: }
087:
088: public long currentTimeMillis() {
089: return alarmService.currentTimeMillis();
090: }
091:
092: // unused BlackboardClient method:
093: public boolean triggerEvent(Object event) {
094: return false;
095: }
096:
097: public void unload() {
098: super .unload();
099: // release the blackboard service
100: if (blackboard != null) {
101: serviceBroker.releaseService(this , BlackboardService.class,
102: blackboard);
103: blackboard = null;
104: }
105:
106: // release the alarm service
107: if (alarmService != null) {
108: serviceBroker.releaseService(this , AlarmService.class,
109: alarmService);
110: alarmService = null;
111: }
112: }
113:
114: private class MyServlet extends HttpServlet {
115: UnaryPredicate commStatusObjectsQuery = new UnaryPredicate() {
116: public boolean execute(Object o) {
117: return o instanceof CommStatus;
118: }
119: };
120:
121: public void doGet(HttpServletRequest req,
122: HttpServletResponse res) throws IOException {
123: execute(req, res);
124: }
125:
126: public void doPost(HttpServletRequest req,
127: HttpServletResponse res) throws IOException {
128: execute(req, res);
129: }
130:
131: private void errorMsg(String msg, PrintWriter pw) {
132: pw.println("<BR>Error. " + msg + "</body></html>");
133: pw.flush();
134: }
135:
136: public void execute(HttpServletRequest req,
137: HttpServletResponse res) throws IOException {
138: String connectedAgentName = req
139: .getParameter("connectedAgentName");
140: //parse the commUp
141: String commUpStr = req.getParameter("commUp");
142: boolean commUp;
143: CommStatus cs = null;
144:
145: //initialize the PrintWriter
146: PrintWriter out = res.getWriter();
147: out.println("<html><head></head><body>");
148:
149: if (commUpStr == null && connectedAgentName == null) {
150: blackboard.openTransaction();
151: Collection col = blackboard
152: .query(commStatusObjectsQuery);
153: for (Iterator i = col.iterator(); i.hasNext();) {
154: CommStatus temp = (CommStatus) i.next();
155: out
156: .println("<BR>"
157: + "CSS Found matching CommStatus object for connectedAgentName: "
158: + temp.getConnectedAgentName()
159: + " isCommUp " + temp.isCommUp()
160: + "</body></html>");
161:
162: }
163: blackboard.closeTransaction();
164: return;
165: }
166:
167: if (commUpStr != null) {
168: commUp = commUpStr.trim().equals("true");
169: } else {
170: errorMsg("commUpStr incorrectly specified: "
171: + commUpStr, out);
172: return;
173: }
174:
175: if (connectedAgentName != null) {
176: //iterate over the existing CommStatusObjects to see if one already exists
177: blackboard.openTransaction();
178: Collection col = blackboard
179: .query(commStatusObjectsQuery);
180: for (Iterator i = col.iterator(); i.hasNext();) {
181: CommStatus temp = (CommStatus) i.next();
182: if (temp.connectedAgentName
183: .equals(connectedAgentName)) {
184: cs = temp;
185: System.out
186: .println("\n CSS Found matching CommStatus object for connectedAgentName: "
187: + cs.getConnectedAgentName()
188: + " isCommUp " + cs.isCommUp());
189: break;
190: }
191: }
192: blackboard.closeTransaction();
193: } else {
194: errorMsg("ConnectedAgentName not specified", out);
195: return;
196: }
197:
198: if (cs == null) { //if an existing CommStatus object was not found then create one
199: cs = new CommStatus(connectedAgentName);
200: if (commUp == false) {
201: cs.setCommLoss(currentTimeMillis());
202: System.out
203: .println("\n CSS Setting Comm LOSS for NEW CommStatus object for connectedAgentName: "
204: + connectedAgentName
205: + " isCommUp "
206: + cs.isCommUp());
207: } else {
208: cs.setCommRestore(currentTimeMillis());
209: System.out
210: .println("\n CSS Setting Comm RESTORE for NEW CommStatus object for connectedAgentName: "
211: + connectedAgentName
212: + " isCommUp "
213: + cs.isCommUp());
214: }
215: blackboard.openTransaction();
216: blackboard.publishAdd(cs);
217: blackboard.closeTransaction();
218: } else { //otherwise update the existing CommStatus object
219: if (commUp == false) {
220: cs.setCommLoss(currentTimeMillis());
221: System.out
222: .println("\n CSS Setting Comm LOSS for MATCHING CommStatus object for connectedAgentName: "
223: + connectedAgentName
224: + " isCommUp "
225: + cs.isCommUp());
226: } else {
227: cs.setCommRestore(currentTimeMillis());
228: System.out
229: .println("\n CSS Setting Comm RESTORE for MATCHING CommStatus object for connectedAgentName: "
230: + connectedAgentName
231: + " isCommUp "
232: + cs.isCommUp());
233: }
234: blackboard.openTransaction();
235: blackboard.publishChange(cs);
236: blackboard.closeTransaction();
237: }
238:
239: out.println("<BR>Success. </body></html>");
240: out.flush();
241: }
242: }
243: }
|