KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SaveBusinessExample


1 /*
2  * The source code contained herein is licensed under the IBM Public License
3  * Version 1.0, which has been approved by the Open Source Initiative.
4  * Copyright (C) 2001, International Business Machines Corporation
5  * Copyright (C) 2001, Hewlett-Packard Company
6  * All Rights Reserved.
7  *
8  */

9
10 import java.util.Properties JavaDoc;
11 import java.util.Vector JavaDoc;
12
13 import org.uddi4j.UDDIException;
14 import org.uddi4j.client.UDDIProxy;
15 import org.uddi4j.datatype.Name;
16 import org.uddi4j.datatype.business.BusinessEntity;
17 import org.uddi4j.response.AuthToken;
18 import org.uddi4j.response.BusinessDetail;
19 import org.uddi4j.response.BusinessInfo;
20 import org.uddi4j.response.BusinessList;
21 import org.uddi4j.response.DispositionReport;
22 import org.uddi4j.response.Result;
23 import org.uddi4j.util.FindQualifier;
24 import org.uddi4j.util.FindQualifiers;
25
26 /**
27  * Sample code that exercises the publish API. Attempts
28  * to save a businessEntity.
29  *
30  * <OL>
31  * <LI>Sets up a UDDIProxy object
32  * <LI>Requests an authorization token
33  * <LI>Saves a businessEntity
34  * <LI>Lists businesses starting with the the first letter of the
35  * business's name. The new business should be in the list.
36  * </OL>
37  *
38  * @author David Melgar (dmelgar@us.ibm.com)
39  * @author Rajesh Sumra (rajesh_sumra@hp.com)
40  */

41 public class SaveBusinessExample
42 {
43
44     Properties JavaDoc config = null;
45
46     public static void main (String JavaDoc args[])
47     {
48         SaveBusinessExample app = new SaveBusinessExample();
49         System.out.println("\n*********** Running SaveBusinessExample ***********");
50         app.run();
51         System.exit(0);
52     }
53
54     public void run()
55     {
56         // Load samples configuration
57
config = Configurator.load();
58
59         // Construct a UDDIProxy object
60
UDDIProxy proxy = new UDDIProxy();
61
62         try
63         {
64             // Select the desired UDDI server node
65
proxy.setInquiryURL(config.getProperty("inquiryURL"));
66             proxy.setPublishURL(config.getProperty("publishURL"));
67
68             // Get an authorization token
69
System.out.println("\nGet authtoken");
70
71             // Pass in userid and password registered at the UDDI site
72
AuthToken token = proxy.get_authToken(config.getProperty("userid"),
73                                                                                         config.getProperty("password"));
74
75             System.out.println("Returned authToken:" + token.getAuthInfoString());
76
77             System.out.println("\nSave '" + config.getProperty("businessName") + "'");
78
79             // Create minimum required data objects
80
Vector JavaDoc entities = new Vector JavaDoc();
81
82             // Create a new business entity using required elements constructor
83
// Name is the business name. BusinessKey must be "" to save a new
84
// business
85
BusinessEntity be = new BusinessEntity("", config.getProperty("businessName"));
86             entities.addElement(be);
87
88             // Save business
89
BusinessDetail bd = proxy.save_business(token.getAuthInfoString(),entities);
90
91             // Process returned BusinessDetail object
92
Vector JavaDoc businessEntities = bd.getBusinessEntityVector();
93             BusinessEntity returnedBusinessEntity = (BusinessEntity)(businessEntities.elementAt(0));
94             System.out.println("Returned businessKey:" + returnedBusinessEntity.getBusinessKey());
95
96             // Find all businesses that start with that particular letter e.g. 'S' for 'Sample Business'.
97
String JavaDoc businessNameLeadingSubstring = (config.getProperty("businessName")).substring (0,1);
98             System.out.println("\nListing businesses starting with " + businessNameLeadingSubstring
99                                                  + " after we publish");
100
101             //creating vector of Name Object
102
Vector JavaDoc names = new Vector JavaDoc();
103             names.add(new Name(businessNameLeadingSubstring));
104
105             // Setting FindQualifiers to 'caseSensitiveMatch'
106
FindQualifiers findQualifiers = new FindQualifiers();
107             Vector JavaDoc qualifier = new Vector JavaDoc();
108             qualifier.add(new FindQualifier("caseSensitiveMatch"));
109             findQualifiers.setFindQualifierVector(qualifier);
110
111             // Find businesses by name
112
// And setting the maximum rows to be returned as 5.
113
BusinessList businessList = proxy.find_business(names, null, null, null,null,findQualifiers,5);
114
115             Vector JavaDoc businessInfoVector = businessList.getBusinessInfos().getBusinessInfoVector();
116             for( int i = 0; i < businessInfoVector.size(); i++ )
117             {
118                 BusinessInfo businessInfo = (BusinessInfo)businessInfoVector.elementAt(i);
119                 System.out.println(businessInfo.getDefaultNameString());
120             }
121         }
122         // Handle possible errors
123
catch( UDDIException e )
124         {
125             DispositionReport dr = e.getDispositionReport();
126             if( dr!=null )
127             {
128                 System.out.println("UDDIException faultCode:" + e.getFaultCode() +
129                                                      "\n operator:" + dr.getOperator() +
130                                                      "\n generic:" + dr.getGeneric() );
131
132                 Vector JavaDoc results = dr.getResultVector();
133                 for( int i=0; i<results.size(); i++ )
134                 {
135                     Result r = (Result)results.elementAt(i);
136                     System.out.println("\n errno:" + r.getErrno() );
137                     if( r.getErrInfo()!=null )
138                     {
139                         System.out.println("\n errCode:" + r.getErrInfo().getErrCode() +
140                                                              "\n errInfoText:" + r.getErrInfo().getText());
141                     }
142                 }
143             }
144
145             e.printStackTrace();
146         }
147         // Catch any other exception that may occur
148
catch( Exception JavaDoc e )
149         {
150             e.printStackTrace();
151         }
152     }
153 }
154
Popular Tags