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.yp.examples;
028:
029: import org.cougaar.yp.*;
030:
031: import org.uddi4j.client.*;
032: import org.uddi4j.transport.*;
033:
034: import org.w3c.dom.Element;
035:
036: import java.io.*;
037: import java.net.*;
038: import java.util.*;
039:
040: import org.cougaar.core.component.*;
041: import org.cougaar.core.mts.*;
042: import org.cougaar.core.agent.*;
043: import org.cougaar.core.agent.service.MessageSwitchService;
044:
045: import org.cougaar.core.blackboard.*;
046: import org.cougaar.core.service.BlackboardService;
047: import org.cougaar.core.service.ThreadService;
048: import org.cougaar.core.plugin.*;
049: import org.cougaar.util.*;
050:
051: import org.cougaar.util.log.*;
052:
053: import org.uddi4j.*;
054: import org.uddi4j.client.*;
055: import org.uddi4j.datatype.*;
056: import org.uddi4j.datatype.assertion.*;
057: import org.uddi4j.datatype.binding.*;
058: import org.uddi4j.datatype.business.*;
059: import org.uddi4j.datatype.service.*;
060: import org.uddi4j.datatype.tmodel.*;
061: import org.uddi4j.request.*;
062: import org.uddi4j.response.*;
063: import org.uddi4j.util.*;
064:
065: /** SMTest is a reimplementation of YPTest which uses a
066: * variety of StateMachines to avoid blocking YP queries
067: **/
068:
069: public class SMTest extends ComponentPlugin {
070: private static final Logger log = Logging.getLogger(SMTest.class);
071:
072: String user = "cougaar";
073: String pass = "cougaarPass";
074: static String sampleName = "Sample Co";
075: static String sampleName2 = "Second Co";
076:
077: IncrementalSubscription sub;
078: long count = 0L;
079: long total = 0L;
080:
081: private String arg = null;
082:
083: private YPProxy yp;
084: private YPService yps;
085: private ThreadService threads;
086:
087: public void setThreadService(ThreadService threads) {
088: this .threads = threads;
089: }
090:
091: public void setYPService(YPService yps) {
092: this .yps = yps;
093: if (yps != null) {
094: yp = yps.getYP(arg);
095: }
096: }
097:
098: public void setParameter(Object p) {
099: if (p != null) {
100: if (p instanceof Collection) {
101: Iterator it = ((Collection) p).iterator();
102: Object ma = it.next();
103: if (ma instanceof String) {
104: arg = (String) ma;
105: } else {
106: System.err.println("First parameter not a string! "
107: + ma);
108: }
109: } else {
110: System.err.println("Parameter not a Collection! " + p);
111: }
112: } else {
113: System.err
114: .println("YPTest requires a parameter which names a YPServer agent!");
115: }
116: }
117:
118: private Put_SM pmachine = null;
119: private Get_SM gmachine = null;
120:
121: protected void setupSubscriptions() {
122: pmachine = new Put_SM();
123: pmachine.init();
124:
125: gmachine = new Get_SM();
126: gmachine.init();
127:
128: sub = (IncrementalSubscription) blackboard
129: .subscribe(new UnaryPredicate() {
130: public boolean execute(Object o) {
131: return o instanceof YPFuture;
132: }
133: });
134: pmachine.start();
135: }
136:
137: public void execute() {
138: // ok - this should be done with a larger state machine rather than two...
139: if (pmachine.isDone() && !gmachine.isDone()) {
140: gmachine.go();
141: }
142: }
143:
144: /**
145: * Demonstrate use of the YPStateMachine base class illustrating non-BB asynchronous
146: * YP operations.
147: **/
148: private class Put_SM extends YPStateMachine {
149: Put_SM() {
150: super (yps, yp, threads);
151: }
152:
153: public void transit(State s0, State s1) {
154: log.warn(this .toString() + " transiting from " + s0
155: + " to " + s1);
156: super .transit(s0, s1);
157: }
158:
159: protected void init() {
160: super .init();
161: // YPStateMachine starts at the "START" state, by default
162: addLink("START", "A");
163: // get the authtoken
164: add(new SState("A") {
165: public void invoke() {
166: call("getAuthToken", null, "B");
167: }
168: });
169: // publish the business
170: addYPQ("B", "C", new YPQ() {
171: public YPFuture get(Frame f) {
172: System.err.println("Using token " + getAuthToken());
173: // Create minimum required data objects
174: Vector entities = new Vector();
175: BusinessEntity be = new BusinessEntity("",
176: sampleName);
177: entities.addElement(be);
178: return yp.save_business(getAuthToken()
179: .getAuthInfoString(), entities);
180: }
181:
182: public void set(Frame f, Object result) {
183: BusinessDetail bd = (BusinessDetail) result;
184: // Process returned BusinessDetail object
185: Vector businessEntities = bd
186: .getBusinessEntityVector();
187: BusinessEntity returnedBusinessEntity = (BusinessEntity) (businessEntities
188: .elementAt(0));
189: System.out.println("Returned businessKey:"
190: + returnedBusinessEntity.getBusinessKey());
191: }
192:
193: public void handle(Frame f, Exception e) {
194: log.error("Caught exception: " + e, e);
195: transit("A"); // start over;
196: }
197: });
198: // discard the authtoken
199: add(new SState("C") {
200: public void invoke() {
201: call("discardAuthToken", null, "D");
202: }
203: });
204: // kick the blackboard so that the other demo can proceed
205: add(new SState("D") {
206: public void invoke() {
207: // kicks the subscriber so that the YP-based machine can take over
208: blackboard.signalClientActivity();
209: transit("DONE");
210: }
211: });
212: }
213: }
214:
215: /**
216: * Demonstrate use of state machine via blackboard.
217: **/
218: private class Get_SM extends StateMachine {
219: private YPFuture ypq = null;
220:
221: public void transit(State s0, State s1) {
222: log.warn(this .toString() + " transiting from " + s0
223: + " to " + s1);
224: super .transit(s0, s1);
225: }
226:
227: void init() {
228: add(new State("A") {
229: public void invoke() {
230: if (ypq != null)
231: transit("ERROR");
232:
233: //creating vector of Name Object
234: Vector names = new Vector();
235: names.add(new Name("S"));
236:
237: // Setting FindQualifiers to 'caseSensitiveMatch'
238: FindQualifiers findQualifiers = new FindQualifiers();
239: Vector qualifier = new Vector();
240: qualifier.add(new FindQualifier(
241: "caseSensitiveMatch"));
242: findQualifiers.setFindQualifierVector(qualifier);
243:
244: System.out.println("Issuing find_business");
245: ypq = yp.find_business(names, null, null, null,
246: null, findQualifiers, 5);
247:
248: blackboard.publishAdd(ypq);
249: transit("B");
250: }
251: });
252:
253: add(new State("B") {
254: public void invoke() {
255: if (ypq == null)
256: transit("ERROR");
257: System.out
258: .println("Checking for find_business response");
259: if (ypq.isReady()) {
260:
261: BusinessList businessList;
262: try {
263: businessList = (BusinessList) ypq.get();
264: } catch (UDDIException ue) {
265: log.error("exception", ue);
266: transit(ERROR);
267: return;
268: }
269: Vector businessInfoVector = businessList
270: .getBusinessInfos()
271: .getBusinessInfoVector();
272: for (int i = 0; i < businessInfoVector.size(); i++) {
273: BusinessInfo businessInfo = (BusinessInfo) businessInfoVector
274: .elementAt(i);
275: System.out.println("in get: business " + i
276: + " = "
277: + businessInfo.getNameString());
278: }
279: blackboard.publishRemove(ypq);
280: ypq = null;
281: transit("DONE");
282: } else {
283: System.out.println(";");
284: }
285: }
286: });
287: set("A");
288: }
289: }
290: }
|