KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mypackage > UddiPing


1 /*
2  * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved. U.S.
3  * Government Rights - Commercial software. Government users are subject
4  * to the Sun Microsystems, Inc. standard license agreement and
5  * applicable provisions of the FAR and its supplements. Use is subject
6  * to license terms.
7  *
8  * This distribution may include materials developed by third parties.
9  * Sun, Sun Microsystems, the Sun logo, Java and J2EE are trademarks
10  * or registered trademarks of Sun Microsystems, Inc. in the U.S. and
11  * other countries.
12  *
13  * Copyright (c) 2005 Sun Microsystems, Inc. Tous droits reserves.
14  *
15  * Droits du gouvernement americain, utilisateurs gouvernementaux - logiciel
16  * commercial. Les utilisateurs gouvernementaux sont soumis au contrat de
17  * licence standard de Sun Microsystems, Inc., ainsi qu'aux dispositions
18  * en vigueur de la FAR (Federal Acquisition Regulations) et des
19  * supplements a celles-ci. Distribue par des licences qui en
20  * restreignent l'utilisation.
21  *
22  * Cette distribution peut comprendre des composants developpes par des
23  * tierces parties. Sun, Sun Microsystems, le logo Sun, Java et J2EE
24  * sont des marques de fabrique ou des marques deposees de Sun
25  * Microsystems, Inc. aux Etats-Unis et dans d'autres pays.
26  */

27
28 package mypackage;
29
30 import javax.xml.soap.*;
31 import java.net.*;
32 import java.util.*;
33 import java.io.*;
34
35
36 public class UddiPing {
37     public static void main(String JavaDoc[] args) {
38         try {
39             // two arguments are passed in from build.xml
40
if (args.length != 2) {
41                 System.err.println("Usage: asant run " +
42                     "-Dbusiness-name=<name>");
43                 System.exit(1);
44             }
45
46             // Retrieve settings from uddi.properties file,
47
// add to system properties
48
Properties myprops = new Properties();
49             myprops.load(new FileInputStream(args[0]));
50
51             Properties props = System.getProperties();
52
53             Enumeration propNames = myprops.propertyNames();
54             while (propNames.hasMoreElements()) {
55                 String JavaDoc s = (String JavaDoc) propNames.nextElement();
56                 props.setProperty(s, myprops.getProperty(s));
57             }
58
59             // Create connection, message factory, and
60
// SOAP factory
61
SOAPConnectionFactory soapConnectionFactory =
62                 SOAPConnectionFactory.newInstance();
63             SOAPConnection connection =
64                 soapConnectionFactory.createConnection();
65             MessageFactory messageFactory = MessageFactory.newInstance();
66             SOAPFactory soapFactory = SOAPFactory.newInstance();
67
68             // Create a message
69
SOAPMessage message = messageFactory.createMessage();
70
71             // Get the SOAP header from the message and remove it
72
SOAPHeader header = message.getSOAPHeader();
73             header.detachNode();
74
75             // Get the SOAP body from the message
76
SOAPBody body = message.getSOAPBody();
77
78             // Create a UDDI v2 find_business body element
79
SOAPBodyElement findBusiness =
80                 body.addBodyElement(soapFactory.createName("find_business",
81                         "",
82                         "urn:uddi-org:api_v2"));
83             findBusiness.addAttribute(soapFactory.createName("generic"), "2.0");
84             findBusiness.addAttribute(soapFactory.createName("maxRows"), "100");
85
86             SOAPElement businessName =
87                 findBusiness.addChildElement(soapFactory.createName("name"));
88             businessName.addTextNode(args[1]);
89
90             message.saveChanges();
91
92             // Display the message you are sending
93
System.out.println("\n----- Request Message ----\n");
94             message.writeTo(System.out);
95
96             // Retrieve the endpoint from system properties
97
URL endpoint = new URL(System.getProperties().getProperty("URL"));
98
99             // Send message and get reply
100
SOAPMessage reply = connection.call(message, endpoint);
101
102             System.out.println("\n\nReceived reply from: " + endpoint);
103             System.out.println("\n------ Reply Message -----\n");
104
105             // Display the reply
106
reply.writeTo(System.out);
107
108             // Now parse the reply
109
SOAPBody replyBody = reply.getSOAPBody();
110
111             /*
112              * The response to a find_business query is a
113              * businessList conformant to the UDDI V2 Data
114              * Structure specifications. For further details,
115              * see
116              * http://uddi.org/pubs/DataStructure-V2.03-Published-20020719.htm#_Toc25130802
117              */

118             System.out.println("\n\nContent extracted from " +
119                 "the reply message:\n");
120
121             Iterator businessListIterator =
122                 replyBody.getChildElements(soapFactory.createName(
123                         "businessList",
124                         "",
125                         "urn:uddi-org:api_v2"));
126
127             /*
128              * businessList is a mandatory element in a
129              * successful response and appears only once.
130              */

131             SOAPBodyElement businessList =
132                 (SOAPBodyElement) businessListIterator.next();
133
134             /*
135              * Get the businessInfos element. This contains
136              * the business entries retrieved that match the
137              * criteria specified in the find_business
138              * request.
139              */

140             Iterator businessInfosIterator =
141                 businessList.getChildElements(soapFactory.createName(
142                         "businessInfos",
143                         "",
144                         "urn:uddi-org:api_v2"));
145
146             /*
147              * businessInfos, too is a mandatory element in
148              * a successful response and appears only once.
149              */

150             SOAPElement businessInfos =
151                 (SOAPElement) businessInfosIterator.next();
152
153             /*
154              * The businessInfos contains zero or more
155              * businessInfo structures. Each businessInfo
156              * structure contains the company name and
157              * optional description data.
158              */

159             Iterator businessInfoIterator =
160                 businessInfos.getChildElements(soapFactory.createName(
161                         "businessInfo",
162                         "",
163                         "urn:uddi-org:api_v2"));
164
165             if (!businessInfoIterator.hasNext()) {
166                 System.out.println("No businesses found " +
167                     "matching the name \"" + args[1] + "\".");
168             } else {
169                 while (businessInfoIterator.hasNext()) {
170                     SOAPElement businessInfo =
171                         (SOAPElement) businessInfoIterator.next();
172
173                     // Extract name and description from the
174
// businessInfo
175
Iterator nameIterator =
176                         businessInfo.getChildElements(soapFactory.createName(
177                                 "name",
178                                 "",
179                                 "urn:uddi-org:api_v2"));
180
181                     while (nameIterator.hasNext()) {
182                         businessName = (SOAPElement) nameIterator.next();
183                         System.out.println("Company name: " +
184                             businessName.getValue());
185                     }
186
187                     Iterator descriptionIterator =
188                         businessInfo.getChildElements(soapFactory.createName(
189                                 "description",
190                                 "",
191                                 "urn:uddi-org:api_v2"));
192
193                     while (descriptionIterator.hasNext()) {
194                         SOAPElement businessDescription =
195                             (SOAPElement) descriptionIterator.next();
196                         System.out.println("Description: " +
197                             businessDescription.getValue());
198                     }
199
200                     System.out.println("");
201                 }
202             }
203
204             // Close the connection
205
connection.close();
206         } catch (Exception JavaDoc ex) {
207             ex.printStackTrace();
208         }
209     }
210 }
211
Popular Tags