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:
042: import org.cougaar.core.mts.*;
043: import org.cougaar.core.agent.*;
044: import org.cougaar.core.agent.service.MessageSwitchService;
045:
046: import org.cougaar.core.blackboard.*;
047: import org.cougaar.core.service.BlackboardService;
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: public class YPSamplePlugin extends ComponentPlugin {
066:
067: String user = "cougaar";
068: String pass = "cougaarPass";
069: static String sampleName = "Sample Co";
070: static String sampleName2 = "Second Co";
071:
072: IncrementalSubscription futures;
073: YPService yps;
074: YPProxy yp;
075: int count = 0;
076:
077: long startTime = 0L;
078:
079: private Object flock = new Object();
080: YPFuture fut = null;
081:
082: void setFut(YPFuture f) {
083: synchronized (flock) {
084: fut = f;
085: }
086: }
087:
088: YPFuture getFut() {
089: synchronized (flock) {
090: return fut;
091: }
092: }
093:
094: public void setYPService(YPService yps) {
095: yps = (YPService) getServiceBroker().getService(this ,
096: YPService.class, null);
097: this .yp = yps.getYP("A");
098: }
099:
100: protected void setupSubscriptions() {
101: futures = (IncrementalSubscription) blackboard
102: .subscribe(new UnaryPredicate() {
103: public boolean execute(Object o) {
104: return (o instanceof YPFuture);
105: }
106: });
107: issue();
108: }
109:
110: YPFuture issue() {
111: Vector names = new Vector();
112: names.add(new Name("S"));
113:
114: // Setting FindQualifiers to 'caseSensitiveMatch'
115: FindQualifiers findQualifiers = new FindQualifiers();
116: Vector qualifier = new Vector();
117: qualifier.add(new FindQualifier("caseSensitiveMatch"));
118: findQualifiers.setFindQualifierVector(qualifier);
119: setFut(yp.find_business(names, null, null, null, null,
120: findQualifiers, 5));
121: count++;
122: if (count == 5) {
123: startTime = System.currentTimeMillis();
124: }
125: //System.err.println("Issued query "+count); //+": "+fut
126: //System.err.println("ISSUE "+getFut());
127: blackboard.publishAdd(getFut());
128: return getFut();
129: }
130:
131: void report(YPFuture f) {
132: //System.err.println("Closing query "+count); //+": "+fut
133: try {
134: Object o = f.get();
135: if (o instanceof BusinessList) {
136: BusinessList businessList = (BusinessList) o;
137:
138: Vector businessInfoVector = businessList
139: .getBusinessInfos().getBusinessInfoVector();
140: for (int i = 0; i < businessInfoVector.size(); i++) {
141: BusinessInfo businessInfo = (BusinessInfo) businessInfoVector
142: .elementAt(i);
143:
144: // Print name for each business
145: //System.err.println("report "+count+": business " + i + " = " + businessInfo.getNameString());
146: }
147: } else {
148: System.err
149: .println("Didn't get a BusinessList response: "
150: + o);
151: }
152: } catch (Exception e) {
153: System.err.println("Error reporting on " + f);
154: e.printStackTrace();
155: }
156: //System.err.println("CLOSE "+f);
157: blackboard.publishRemove(f);
158: long et = System.currentTimeMillis();
159: double rate = (((double) ((count - 5) + 1)) / (et - startTime)) * 1000.0;
160: System.err.println("Sample rate=" + rate + "t/s");
161: setFut(null);
162: }
163:
164: protected void execute() {
165: Collection changed = futures.getChangedCollection();
166: for (Iterator it = changed.iterator(); it.hasNext();) {
167: YPFuture f = (YPFuture) it.next();
168: if (f.isReady()) {
169: report(f); // sets fut=null
170: issue(); // sets fut=new fut
171: } else {
172: System.err.println("Query changed but not ready: " + f);
173: }
174: }
175: }
176: }
|