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 all of uddi4j
032: import org.uddi4j.*;
033: import org.uddi4j.client.*;
034: import org.uddi4j.datatype.*;
035: import org.uddi4j.datatype.assertion.*;
036: import org.uddi4j.datatype.binding.*;
037: import org.uddi4j.datatype.business.*;
038: import org.uddi4j.datatype.service.*;
039: import org.uddi4j.datatype.tmodel.*;
040: import org.uddi4j.request.*;
041: import org.uddi4j.response.*;
042: import org.uddi4j.util.*;
043:
044: import java.io.*;
045: import java.net.*;
046: import java.util.*;
047:
048: import org.cougaar.core.component.*;
049: import org.cougaar.core.mts.*;
050: import org.cougaar.util.log.*;
051:
052: // the next three are solely for the standalone test
053: import org.uddi4j.transport.*;
054: import org.w3c.dom.Element;
055: import org.w3c.dom.Node;
056:
057: /** This is a trivial YP service tester.
058: * Use this as a plugin/component by adding the appropriate line in
059: * a society config file, e.g. "plugin = org.cougaar.yp.YPTest(A)" where "A" is the
060: * name of an agent which has a YPServer loaded and the current agent has
061: * YPClientComponent loaded).
062: * @note Also see yp/configs/miniyp for a small society with the right parts to run this sample code.
063: */
064:
065: public class YPTest extends ComponentSupport {
066: private static final Logger logger = Logging
067: .getLogger(YPServer.class);
068:
069: private String arg = "Unknown";
070:
071: public void setParameter(Object p) {
072: if (p != null) {
073: if (p instanceof Collection) {
074: Iterator it = ((Collection) p).iterator();
075: Object ma = it.next();
076: if (ma instanceof String) {
077: arg = (String) ma;
078: } else {
079: System.err.println("First parameter not a string! "
080: + ma);
081: }
082: } else {
083: System.err.println("Parameter not a Collection! " + p);
084: }
085: } else {
086: System.err
087: .println("YPTest requires a parameter which names a YPServer agent!");
088: }
089: }
090:
091: YPService yps = null;
092:
093: public void load() {
094: super .load();
095: System.err.println("YPTest running.");
096:
097: yps = (YPService) getServiceBroker().getService(this ,
098: YPService.class, null);
099:
100: String context = arg;
101:
102: YPProxy proxy = yps.getAutoYP(context);
103:
104: test(proxy);
105: System.err.println("YPTest done.");
106: }
107:
108: String user = "cougaar";
109: String pass = "cougaarPass";
110: static String sampleName = "Sample Co";
111: static String sampleName2 = "Second Co";
112:
113: public void test(YPProxy proxy) {
114: test(proxy, sampleName);
115: }
116:
117: public void test(YPProxy proxy, String bName) {
118: testPutBusiness(proxy, bName);
119: testGetBusiness(proxy);
120: try {
121: Thread.sleep(10000);
122: } catch (Exception e) {
123: System.out.println("Interrupted sleep");
124: }
125: testGetBusiness(proxy);
126: }
127:
128: public void testPutBusiness(YPProxy proxy, String businessName) {
129: try {
130: // Get an authorization token
131: System.out.println("\nGet authtoken");
132:
133: // Pass in userid and password registered at the UDDI site
134: AuthToken token = (AuthToken) proxy.get_authToken(user,
135: pass).get();
136:
137: System.out.println("Returned authToken:"
138: + token.getAuthInfoString());
139:
140: System.out.println("\nSave '" + businessName + "'");
141:
142: // Create minimum required data objects
143: Vector entities = new Vector();
144:
145: // Create a new business entity using required elements constructor
146: // Name is the business name. BusinessKey must be "" to save a new
147: // business
148: BusinessEntity be = new BusinessEntity("", businessName);
149: entities.addElement(be);
150:
151: // Save business
152: BusinessDetail bd = (BusinessDetail) proxy.save_business(
153: token.getAuthInfoString(), entities).get();
154:
155: // Process returned BusinessDetail object
156: Vector businessEntities = bd.getBusinessEntityVector();
157: BusinessEntity returnedBusinessEntity = (BusinessEntity) (businessEntities
158: .elementAt(0));
159: System.out.println("Returned businessKey:"
160: + returnedBusinessEntity.getBusinessKey());
161:
162: // Find all businesses that start with that particular letter e.g. 'S' for 'Sample Business'.
163: String businessNameLeadingSubstring = businessName
164: .substring(0, 1);
165: System.out.println("\nListing businesses starting with "
166: + businessNameLeadingSubstring
167: + " after we publish");
168:
169: //creating vector of Name Object
170: Vector names = new Vector();
171: names.add(new Name(businessNameLeadingSubstring));
172:
173: // Setting FindQualifiers to 'caseSensitiveMatch'
174: FindQualifiers findQualifiers = new FindQualifiers();
175: Vector qualifier = new Vector();
176: qualifier.add(new FindQualifier("caseSensitiveMatch"));
177: findQualifiers.setFindQualifierVector(qualifier);
178:
179: // Find businesses by name
180: // And setting the maximum rows to be returned as 5.
181: BusinessList businessList = (BusinessList) proxy
182: .find_business(names, null, null, null, null,
183: findQualifiers, 5).get();
184:
185: Vector businessInfoVector = businessList.getBusinessInfos()
186: .getBusinessInfoVector();
187: for (int i = 0; i < businessInfoVector.size(); i++) {
188: BusinessInfo businessInfo = (BusinessInfo) businessInfoVector
189: .elementAt(i);
190: System.out.println("in put: business " + i + " = "
191: + businessInfo.getNameString());
192: }
193:
194: // Handle possible errors
195: } catch (Exception e) {
196: e.printStackTrace();
197: }
198: }
199:
200: public void testGetBusiness(YPProxy proxy) {
201: try {
202: //creating vector of Name Object
203: Vector names = new Vector();
204: names.add(new Name("S"));
205:
206: // Setting FindQualifiers to 'caseSensitiveMatch'
207: FindQualifiers findQualifiers = new FindQualifiers();
208: Vector qualifier = new Vector();
209: qualifier.add(new FindQualifier("caseSensitiveMatch"));
210: findQualifiers.setFindQualifierVector(qualifier);
211:
212: // Find businesses by name
213: // And setting the maximum rows to be returned as 5.
214: BusinessList businessList = (BusinessList) proxy
215: .find_business(names, null, null, null, null,
216: findQualifiers, 5).get();
217:
218: Vector businessInfoVector = businessList.getBusinessInfos()
219: .getBusinessInfoVector();
220: for (int i = 0; i < businessInfoVector.size(); i++) {
221: BusinessInfo businessInfo = (BusinessInfo) businessInfoVector
222: .elementAt(i);
223:
224: // Print name for each business
225: System.out.println("in get: business " + i + " = "
226: + businessInfo.getNameString());
227: }
228:
229: // Handle possible errors
230: } catch (Exception ex) {
231: ex.printStackTrace();
232: }
233: }
234:
235: // hack XML printer for debugging
236: public static void describeElement(Node el) {
237: describeElement(el, "");
238: }
239:
240: public static void describeElement(Node el, String prefix) {
241: System.out.println(prefix + el);
242: String pn = prefix + " ";
243: if (el.hasChildNodes()) {
244: for (Node c = el.getFirstChild(); c != null; c = c
245: .getNextSibling()) {
246: describeElement(c, pn);
247: }
248: }
249: }
250: }
|