001: /*
002: * <copyright>
003: *
004: * Copyright 2000-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.planning.examples;
028:
029: import java.io.IOException;
030: import java.io.PrintWriter;
031: import javax.servlet.http.HttpServletRequest;
032: import javax.servlet.http.HttpServletResponse;
033: import org.cougaar.core.blackboard.BlackboardClient;
034: import org.cougaar.core.domain.Factory;
035: import org.cougaar.core.mts.MessageAddress;
036: import org.cougaar.core.service.BlackboardService;
037: import org.cougaar.core.service.DomainService;
038: import org.cougaar.core.servlet.ComponentServlet;
039: import org.cougaar.core.util.UID;
040: import org.cougaar.planning.ldm.PlanningFactory;
041: import org.cougaar.planning.ldm.plan.NewTask;
042: import org.cougaar.planning.ldm.plan.Verb;
043:
044: /**
045: * An example servlet that adds a task to the blackboard.
046: * <p>
047: * Load with:<pre>
048: * <component
049: * class='org.cougaar.planning.examples.AddTaskServlet'>
050: * <argument>/addtask</argument>
051: * </component>
052: * </pre>
053: * <p>
054: * Note that the compiled examples jars are not included in the
055: * release; you must compile them from the source code using the
056: * ant script, e.g.:<pre>
057: * cd $CIP/planning
058: * ant compile-examples
059: * cd tmp/examples
060: * jar cvf $CIP/lib/planning_examples.jar *
061: * </pre>
062: */
063: public class AddTaskServlet extends ComponentServlet implements
064: BlackboardClient {
065:
066: private BlackboardService blackboard;
067: private DomainService ds;
068: private PlanningFactory ldmf;
069:
070: //
071: // Get the blackboard and planning factory.
072: //
073: // Instead of doing this in "load()", using the usual
074: // "getServiceBroker().getService(..)", let's use the equivalent
075: // component model's reflection support for
076: // "setXService(XService x) {..}".
077: //
078:
079: public void setBlackboardService(BlackboardService blackboard) {
080: this .blackboard = blackboard;
081: }
082:
083: public void setDomainService(DomainService ds) {
084: this .ds = ds;
085: if (ds != null) {
086: this .ldmf = (PlanningFactory) ds.getFactory("planning");
087: }
088: }
089:
090: public void doGet(HttpServletRequest req, HttpServletResponse res)
091: throws IOException {
092: PrintWriter out = res.getWriter();
093: out.println("<html><body bgcolor=\"white\">");
094:
095: // add a new task to the blackboard:
096: NewTask nt = ldmf.newTask();
097: nt.setSource(agentId);
098: nt.setVerb(Verb.get("fromAddTaskServlet"));
099: try {
100: blackboard.openTransaction();
101: blackboard.publishAdd(nt);
102: } finally {
103: blackboard.closeTransactionDontReset();
104: }
105:
106: out.println("AddTaskServlet " + "publish-Added task (<tt>"
107: + nt.getUID() + "</tt>) with verb \"<tt>"
108: + nt.getVerb() + "</tt>\" to agent <b>" + agentId
109: + "</b>'s Blackboard.");
110: out.println("</body></html>");
111: }
112:
113: //
114: // These are oddities of implementing BlackboardClient:
115: //
116: // Note: A Component must implement BlackboardClient in order
117: // to obtain BlackboardService.
118: //
119:
120: // odd BlackboardClient method:
121: public String getBlackboardClientName() {
122: return toString();
123: }
124:
125: // odd BlackboardClient method, bug 2515:
126: public long currentTimeMillis() {
127: throw new UnsupportedOperationException(this
128: + " asked for the current time???");
129: }
130: }
|