001: /*
002: * <copyright>
003: *
004: * Copyright 2002-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.servlet.BaseServletComponent;
032: import org.cougaar.core.servlet.ComponentServlet;
033: import org.cougaar.logistics.plugin.utils.ALStatusChangeMessage;
034: import org.cougaar.util.UnaryPredicate;
035:
036: import org.cougaar.core.agent.service.alarm.ExecutionTimer;
037:
038: import javax.servlet.Servlet;
039: import javax.servlet.http.HttpServlet;
040: import javax.servlet.http.HttpServletRequest;
041: import javax.servlet.http.HttpServletResponse;
042: import java.io.IOException;
043: import java.io.PrintWriter;
044: import java.util.Collection;
045: import java.util.Date;
046: import java.util.Iterator;
047: import java.text.SimpleDateFormat;
048: import java.text.ParsePosition;
049:
050: public class SDUseCaseServlet extends ComponentServlet implements
051: BlackboardClient {
052:
053: private BlackboardService blackboard;
054: private static String ROLE = "PackagedPOLSupplyProvider";
055: private static String[] ROLE_NAMES = { "AmmunitionProvider",
056: "FuelSupplyProvider", "PackagedPOLSupplyProvider",
057: "SparePartsProvider", "StrategicTransportationProvider",
058: "SubsistenceSupplyProvider" };
059: private static String PATH = "/SD_Use_Case";
060: private static String ROLE_PARAMETER = "role";
061: private static String END_DATE_PARAMETER = "enddate";
062:
063: public void init() {
064: // get the blackboard service
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: }
072:
073: public void unload() {
074: super .unload();
075: // release the blackboard service
076: if (blackboard != null) {
077: serviceBroker.releaseService(this , BlackboardService.class,
078: blackboard);
079: blackboard = null;
080: }
081: }
082:
083: protected String getPath() {
084: return PATH;
085: }
086:
087: // public void setBlackboardService(BlackboardService blackboard) {
088: // this.blackboard = blackboard;
089: // }
090:
091: // BlackboardClient method:
092: public String getBlackboardClientName() {
093: return toString();
094: }
095:
096: // BlackboardClient method:
097: public long currentTimeMillis() {
098: return new ExecutionTimer().currentTimeMillis();
099: }
100:
101: // // unused BlackboardClient method:
102: // public boolean triggerEvent(Object event) {
103: // return false;
104: // }
105:
106: UnaryPredicate pred = new UnaryPredicate() {
107: public boolean execute(Object o) {
108: return o instanceof ALStatusChangeMessage;
109: }
110: };
111:
112: public void doGet(HttpServletRequest req, HttpServletResponse res)
113: throws IOException {
114: PrintWriter out = res.getWriter();
115: Collection col;
116: String end_date_string = req.getParameter(END_DATE_PARAMETER);
117: String role = req.getParameter(ROLE_PARAMETER);
118: SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
119: Date end_date = null;
120: if (end_date_string != null) {
121: end_date = dateFormat.parse(end_date_string,
122: new ParsePosition(0));
123: }
124: long now = currentTimeMillis();
125: printTitles(out);
126: printInfo(now, out);
127: try {
128: blackboard.openTransaction();
129: col = blackboard.query(pred);
130:
131: if (!col.isEmpty()) {
132: Iterator it = col.iterator();
133: ALStatusChangeMessage message = (ALStatusChangeMessage) it
134: .next();
135: printChangeInfo(message, false, out);
136:
137: } else {
138: if (end_date_string == null) {
139: printForm(req, out);
140: } else if (end_date == null) {
141: printError("Could not read date; try again.", out);
142: printForm(req, out);
143: } else if (end_date.getTime() < now) {
144: printError(
145: "End date must be in the future; try again.",
146: out);
147: printForm(req, out);
148: } else {
149: ALStatusChangeMessage message = new ALStatusChangeMessage(
150: role, false, end_date);
151: blackboard.publishAdd(message);
152: printChangeInfo(message, true, out);
153: }
154: }
155: } catch (Exception e) {
156: printError("Servlet error: " + e, out);
157: } finally {
158: blackboard.closeTransactionDontReset();
159: out.println("</body>");
160: out.flush();
161: }
162: }
163:
164: private void printTitles(PrintWriter out) {
165: String title = "Service Discovery Use Case Servlet";
166: out.println("<HEAD><TITLE>" + title + "</TITLE></HEAD>"
167: + "<BODY><H1>" + title + "</H1>");
168: }
169:
170: private void printInfo(long now, PrintWriter out) {
171: String localAgent = getEncodedAgentName();
172: out.println("<p>\n" + "Agent: " + localAgent + "<p>\n"
173: + "Current Society Time: " + new Date(now));
174: }
175:
176: private void printForm(HttpServletRequest req, PrintWriter out) {
177: String optionString = "";
178: for (int i = 0; i < ROLE_NAMES.length; i++) {
179: optionString = optionString.concat("<option value=\""
180: + ROLE_NAMES[i] + "\">" + ROLE_NAMES[i]
181: + "</option>" + "\n");
182: }
183:
184: out.print("<form method=\"GET\" action=\""
185: + req.getRequestURI() + "\">\n" + "Enter role: "
186: + "<select size=1 name=\"" + ROLE_PARAMETER + "\">\n"
187: + optionString + "</select>"
188: + "<p>Enter new end date (MM/DD/YYYY): "
189: + "<input type=\"text\" name=\"" + END_DATE_PARAMETER
190: + "\" size=40>\n"
191: + "<p><input type=\"submit\" value=\"Submit\">\n"
192: + "</form>\n");
193: }
194:
195: private void printChangeInfo(ALStatusChangeMessage message,
196: boolean newChange, PrintWriter out) {
197: out.println("<p>\n");
198: if (newChange) {
199: out.println("Successfully posted provider change:");
200: } else {
201: out.println("Provider already changed:");
202: }
203: out.println("<ul><li>Role: " + message.getRole()
204: + "<li>End Date: " + message.getEndDate()
205: + "<li>Registry Updated: " + message.registryUpdated()
206: + "</ul>");
207: }
208:
209: private void printError(String text, PrintWriter out) {
210: out.println("<p>\n" + "<b>" + text + "</b>" + "<p>");
211: }
212:
213: private String headWithTitle(String title) {
214: return "<HEAD><TITLE>" + title + "</TITLE></HEAD>";
215: }
216: }
|