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.lib.web.axis.blackboardCount;
028:
029: import java.net.URL;
030: import java.util.Iterator;
031: import java.util.Map;
032:
033: import javax.xml.namespace.QName;
034: import javax.xml.rpc.ParameterMode;
035:
036: import org.apache.axis.AxisFault;
037: import org.apache.axis.client.Call;
038: import org.apache.axis.client.Service;
039: import org.apache.axis.encoding.XMLType;
040: import org.apache.axis.utils.Options;
041: import org.apache.axis.encoding.ser.BeanDeserializerFactory;
042: import org.apache.axis.encoding.ser.BeanSerializerFactory;
043:
044: /**
045: * A command-line client that uses SOAP to ask Cougaar for a count of
046: * all objects on a node-agent's blackboard.
047: * <p>
048: * To run on Windows:<pre>
049: * SET CP=-classpath %CIP%\lib\webaxis.jar;%CIP%\sys\axis_1_2_beta.jar;%CIP%\sys\mail.jar;%CIP%\sys\activation.jar
050: * SET CL=org.cougaar.lib.web.axis.blackboardCount.BlackboardCountClient
051: * SET URL=-lhttp://localhost:8800/axis/services
052: * java -classpath %CP% %CL% %URL% %*
053: * </pre>
054: * or on Linux/Unix Bash:<pre>
055: * CP="$CIP/lib/webaxis.jar:$CIP/sys/axis_1_2_beta.jar:$CIP/sys/activation.jar:$CIP/sys/mail.jar"
056: * CL="org.cougaar.lib.web.axis.blackboardCount.BlackboardCountClient"
057: * URL="-lhttp://localhost:8800/axis/services"
058: * java -classpath $CP $CL $URL $*
059: * </pre>
060: * <p>
061: * The expected output is:<pre>
062: * getBlackboardCount(*)={
063: * org.cougaar.lib.web.axis.blackboardCount.BlackboardCountPlugin$TestOne=1,
064: * org.cougaar.lib.web.axis.blackboardCount.BlackboardCountPlugin$TestTwo=2,
065: * }
066: * </pre>
067: * <p>
068: * Most of this is Axis/SOAP boilerplate, based on the Axis sample code:<pre>
069: * $AXIS_HOME/samples/stock/GetQuote.java
070: * </pre>
071: */
072: public class BlackboardCountClient {
073:
074: public static void main(String args[]) throws Exception {
075: // parse options
076: Options opts = new Options(args);
077: String[] extraArgs = opts.getRemainingArgs();
078: String surl = opts.getURL();
079: String classFilter = (extraArgs == null
080: || extraArgs.length <= 0 ? "*" : extraArgs[0]);
081:
082: // invoke
083: Map m = BlackboardCountClient.getBlackboardCount(surl,
084: classFilter);
085:
086: // print result
087: System.out.print("getBlackboardCount(" + classFilter + ")={");
088: for (Iterator iter = m.entrySet().iterator(); iter.hasNext();) {
089: Map.Entry me = (Map.Entry) iter.next();
090: System.out.println(" " + me.getKey() + "=" + me.getValue()
091: + ",");
092: }
093: System.out.println("}");
094: }
095:
096: // helper function; does all the real work
097: //
098: // see the Axis samples for more examples; this one is based on
099: // $AXIS_HOME/samples/stock/GetQuote.java
100: private static Map getBlackboardCount(String surl,
101: String classFilter) throws Exception {
102:
103: URL url = new URL(surl);
104:
105: Service service = new Service();
106:
107: Call call = (Call) service.createCall();
108:
109: // register types:
110: Class cl = ResultEntry.class;
111: QName qn = new QName("urn:BeanService", "ResultEntry");
112: call.registerTypeMapping(cl, qn, new BeanSerializerFactory(cl,
113: qn), new BeanDeserializerFactory(cl, qn));
114:
115: cl = ResultMap.class;
116: qn = new QName("urn:BeanService", "ResultMap");
117: call.registerTypeMapping(cl, qn, new BeanSerializerFactory(cl,
118: qn), new BeanDeserializerFactory(cl, qn));
119:
120: call.setTargetEndpointAddress(url);
121: call.setOperationName(new QName("urn:Cougaar-blackboard-count",
122: "getBlackboardCount"));
123: call.addParameter("classFilter", XMLType.XSD_STRING,
124: ParameterMode.IN);
125: call.setReturnType(qn);
126:
127: Object ret = call.invoke(new Object[] { classFilter });
128: if (ret instanceof String) {
129: System.out
130: .println("Received problem response from server: "
131: + ret);
132: throw new AxisFault("", (String) ret, null, null);
133: }
134: ResultMap rm = (ResultMap) ret;
135: return rm.toMap();
136: }
137: }
|