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 java.io.*;
030: import java.net.*;
031: import java.util.*;
032:
033: import javax.servlet.http.HttpServlet;
034: import javax.servlet.http.HttpServletRequest;
035: import javax.servlet.http.HttpServletResponse;
036:
037: import org.cougaar.core.adaptivity.*;
038:
039: import org.cougaar.core.servlet.SimpleServletSupport;
040:
041: import org.cougaar.planning.servlet.ServletBase;
042: import org.cougaar.planning.servlet.ServletWorker;
043: import org.cougaar.core.persist.NotPersistable;
044:
045: /**
046: * <pre>
047: * Sets a double condition constrained to be in the range 0.0-1.0, in every agent with this servlet.
048: *
049: *
050: * The name of the condition can be specified as a parameter to the component, e.g.
051: *
052: * plugin = org.cougaar.core.servlet.BlackboardServletComponent(org.cougaar.logistics.servlet.ConditionServlet, /conditionsetter, conditionName=ferris)
053: *
054: * would create a condition named "ferris".
055: *
056: * This condition can then be set in every other agent with a servlet that specifies "ferris" as the
057: * the name of it's condition.
058: *
059: * To concurrently specify a different condition, specify a different servlet name-condition name pair, e.g.
060: *
061: * plugin = org.cougaar.core.servlet.BlackboardServletComponent(org.cougaar.logistics.servlet.ConditionServlet, /buellersetter, conditionName=bueller)
062: *
063: * An example URL to access the servlet is :
064: *
065: * http://localhost:8800/$TRANSCOM/conditionsetter
066: *
067: * NOTES :
068: * - the default name of the condition is "DoubleCondition".
069: * - the ConditionServiceProvider should also be loaded in any agent with this servlet
070: * - the servlet does *not* depend on load order
071: *
072: * </pre>
073: */
074: public class ConditionServlet extends ServletBase {
075:
076: public static String CONDITION_PARAM = "conditionValue";
077: public static String SET_ALL_AGENTS = "setAllAgents";
078:
079: /**
080: * This is the path for my Servlet, relative to the
081: * Agent's URLEncoded name.
082: * <p>
083: * For example, on Agent "X" the URI request path
084: * will be "/$X/hello".
085: */
086: private final String myPath = "/conditionsetter";
087:
088: public static final String CONDITION_IDENTIFIER = "ConditionServlet.DoubleCondition";
089:
090: private static final OMCRange[] Condition_RANGES = { new ConditionRange(
091: 0.0, 10.0) };
092:
093: protected static class ConditionRange extends OMCRange {
094: public ConditionRange(double a, double b) {
095: super (a, b);
096: }
097: }
098:
099: private static final OMCRangeList Condition_VALUES = new OMCRangeList(
100: Condition_RANGES);
101:
102: /**
103: * Inner class precludes use by others to set our
104: * measurement. Others can only reference the base Condition
105: * class which has no setter method.
106: *
107: * Package scope
108: **/
109: static class DoubleCondition extends SensorCondition implements
110: NotPersistable {
111: public DoubleCondition(String name) {
112: this (name, ConditionServlet.Condition_VALUES,
113: ConditionServlet.Condition_RANGES[0].getMin());
114: }
115:
116: public DoubleCondition(String name, OMCRangeList allowedValues,
117: Comparable value) {
118: super (name, allowedValues, value);
119: }
120:
121: public void setValue(Comparable newValue) {
122: super .setValue(newValue);
123: }
124: }
125:
126: /**
127: * Pretty to-String for debugging.
128: */
129: public String toString() {
130: return getClass().getName() + "(" + myPath + ")";
131: }
132:
133: protected ServletWorker createWorker() {
134: return new ConditionWorker();
135: }
136:
137: /** <pre>
138: *
139: * USAGE
140: *
141: * Only called if no arguments are given.
142: * </pre>
143: */
144: public void getUsage(PrintWriter out, SimpleServletSupport support) {
145: out
146: .print("<HTML><HEAD><TITLE>Condition Setting Usage</TITLE></HEAD><BODY>\n"
147: + "<H2><CENTER>Condition Setting Usage</CENTER></H2><P>\n"
148: + "<FORM METHOD=\"GET\" ACTION=\"/$");
149: out.print(support.getEncodedAgentName());
150: out.print(support.getPath());
151: // choose between shallow and recursive displays
152: out
153: .print("\">\n"
154: + "Set generic condition to this value in all agents:<p>\n"
155: + " <INPUT TYPE=\"text\" NAME=\""
156: + CONDITION_PARAM
157: + "\" "
158: + "VALUE=\"1.0\"> Please type in a double value, e.g. in the range 0.0-1.0."
159: + " <p>\n");
160: out.print("<INPUT TYPE=\"hidden\" NAME=\"" + SET_ALL_AGENTS
161: + "\" " + "VALUE=\"true\">" + " \n");
162: out.print("<P>\n"
163: + "<INPUT TYPE=\"submit\" NAME=\"Display\">\n"
164: + "</FORM></BODY></HTML>");
165: }
166: }
|