KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > BusinessServiceExample


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.datatype.service.BusinessService;
18 import org.uddi4j.response.AuthToken;
19 import org.uddi4j.response.BusinessDetail;
20 import org.uddi4j.response.DispositionReport;
21 import org.uddi4j.response.Result;
22 import org.uddi4j.response.ServiceDetail;
23 import org.uddi4j.response.ServiceInfo;
24 import org.uddi4j.response.ServiceList;
25 import org.uddi4j.util.FindQualifier;
26 import org.uddi4j.util.FindQualifiers;
27
28 /**
29  * Sample code that exercises the publish API. Attempts
30  * to save a businessService, finds the saved businessService and then
31  * deletes the saved businessService.
32  *
33  * <OL>
34  * <LI>Sets up an UDDIProxy object
35  * <LI>Requests an authorization token
36  * <LI>Saves a businessEntity.
37  * <LI>Saves a businessService.
38  * <LI>Finds a saved businessService.
39  * <LI>Deletes the saved businessService.
40  * <LI>Cleans up the data structures created (businessEntity)
41  * </OL>
42  *
43  * @author Rajesh Sumra (rajesh_sumra@hp.com)
44  */

45 public class BusinessServiceExample
46 {
47
48     Properties JavaDoc config = null;
49
50     public static void main (String JavaDoc args[])
51     {
52         BusinessServiceExample app = new BusinessServiceExample();
53         System.out.println("\n*********** Running BusinessServiceExample ***********");
54         app.run();
55         System.exit(0);
56     }
57
58     public void run()
59     {
60         // Load samples configuration
61
config = Configurator.load();
62
63         // Construct a UDDIProxy object
64
UDDIProxy proxy = new UDDIProxy();
65
66         try
67         {
68             // Select the desired UDDI server node
69
proxy.setInquiryURL(config.getProperty("inquiryURL"));
70             proxy.setPublishURL(config.getProperty("publishURL"));
71
72             // Get an authorization token
73
System.out.println("\nGet authtoken");
74
75             // Pass in userid and password registered at the UDDI site
76
AuthToken token = proxy.get_authToken(config.getProperty("userid"),
77                                                                                         config.getProperty("password"));
78
79             System.out.println("Returned authToken:" + token.getAuthInfoString());
80
81             System.out.println("\nSave '" + config.getProperty("serviceName") + "'");
82
83             // Create minimum required data objects
84

85             Vector JavaDoc entities = new Vector JavaDoc();
86
87             // For saving a Business Service we need a Business Entity .
88
// Hence create a new business entity . Name is the business name.
89
// BusinessKey must be "" and not null to save a new business.
90
BusinessEntity be = new BusinessEntity("", config.getProperty("businessName"));
91             entities.addElement(be);
92
93             // Save business
94
BusinessDetail bd = proxy.save_business(token.getAuthInfoString(),entities);
95
96             // Process returned BusinessDetail object to get the
97
// busines key.
98
Vector JavaDoc businessEntities = bd.getBusinessEntityVector();
99             BusinessEntity returnedBusinessEntity = (BusinessEntity)(businessEntities.elementAt(0));
100             String JavaDoc businessKey = returnedBusinessEntity.getBusinessKey();
101
102             // Create a new business service using BindingTemplates default
103
// constructor.
104
// DefaultName is the service name. ServiceKey must be "" to save a new service
105
BusinessService businessService = new BusinessService("");
106             businessService.setDefaultNameString(config.getProperty("serviceName"),null);
107             businessService.setBusinessKey(businessKey);
108             Vector JavaDoc services = new Vector JavaDoc();
109             services.addElement(businessService);
110
111             // **** First save a Business Service
112
ServiceDetail serviceDetail = proxy.save_service(token.getAuthInfoString(),services);
113
114             // Process returned ServiceDetail object to list the
115
// saved services.
116
Vector JavaDoc businessServices = serviceDetail.getBusinessServiceVector();
117             BusinessService businessServiceReturned = (BusinessService)(businessServices.elementAt(0));
118             String JavaDoc serviceKey = businessServiceReturned.getServiceKey();
119             System.out.println("The saved Service : "+ businessServiceReturned.getDefaultNameString());
120             System.out.println("The ServiceKey : "+ serviceKey);
121
122             System.out.println("\nFinding Service saved in first step");
123
124             //creating vector of Name Object
125
Vector JavaDoc names = new Vector JavaDoc();
126             names.add(new Name(config.getProperty("serviceName")));
127
128             // Setting FindQualifiers to 'exactNameMatch'
129
FindQualifiers findQualifiers = new FindQualifiers();
130             Vector JavaDoc qualifier = new Vector JavaDoc();
131             qualifier.add(new FindQualifier("exactNameMatch"));
132             findQualifiers.setFindQualifierVector(qualifier);
133
134             // **** Find the Business Service saved.
135
// And setting the maximum rows to be returned as 5.
136
ServiceList serviceList = proxy.find_service(businessKey, names, null,null,findQualifiers,5);
137
138             // Process the returned ServiceList object
139
Vector JavaDoc serviceInfoVector = serviceList.getServiceInfos().getServiceInfoVector();
140             for( int i = 0; i < serviceInfoVector.size(); i++ )
141             {
142                 ServiceInfo serviceInfo = (ServiceInfo)serviceInfoVector.elementAt(i);
143                 // Print name for each service
144
System.out.println("Name of Service : " + serviceInfo.getDefaultNameString());
145                 System.out.println("Service key : " + serviceInfo.getServiceKey());
146             }
147
148             System.out.println("\nDeleting the saved service");
149             // Try to delete already saved Business Service. Delete will fail for services
150
// not created by this id
151
// Print name for each service
152
System.out.println("Name of Service : " +
153                                                  businessServiceReturned.getDefaultNameString());
154             System.out.println("Service key : " +
155                                                  businessServiceReturned.getServiceKey());
156
157             // **** Having service key, delete using the authToken
158
DispositionReport dr = proxy.delete_service (
159                                                                                                     token.getAuthInfoString(),
160                                                                                                     businessServiceReturned.getServiceKey());
161
162             if( dr.success() )
163             {
164                 System.out.println("Service successfully deleted");
165             }
166             else
167             {
168                 System.out.println(" Error during deletion of Service\n"+
169                                                      "\n operator:" + dr.getOperator() +
170                                                      "\n generic:" + dr.getGeneric() );
171
172                 Vector JavaDoc results = dr.getResultVector();
173                 for( int i=0; i<results.size(); i++ )
174                 {
175                     Result r = (Result)results.elementAt(i);
176                     System.out.println("\n errno:" + r.getErrno() );
177                     if( r.getErrInfo()!=null )
178                     {
179                         System.out.println("\n errCode:" + r.getErrInfo().getErrCode() +
180                                                              "\n errInfoText:" + r.getErrInfo().getText());
181                     }
182                 }
183             }
184
185
186             // Tries to clean up the registry, for the structures we just created
187
//(by deleting the BusinessEntity)
188
System.out.println("\nCleaning the Data Structures added/saved from registry");
189             {
190                 // delete using the authToken and businessKey
191
DispositionReport dr1 = proxy.delete_business(token.getAuthInfoString(),
192                                                                                                             businessKey);
193
194                 if( dr1.success() )
195                 {
196                     System.out.println("Registry successfully cleaned");
197                 }
198                 else
199                 {
200                     System.out.println(" Error during deletion of Business\n"+
201                                                          "\n operator:" + dr1.getOperator() +
202                                                          "\n generic:" + dr1.getGeneric() );
203
204                     Vector JavaDoc results = dr1.getResultVector();
205                     for( int i=0; i<results.size(); i++ )
206                     {
207                         Result r = (Result)results.elementAt(i);
208                         System.out.println("\n errno:" + r.getErrno() );
209                         if( r.getErrInfo()!=null )
210                         {
211                             System.out.println("\n errCode:" + r.getErrInfo().getErrCode() +
212                                                                  "\n errInfoText:" + r.getErrInfo().getText());
213                         }
214                     }
215                 }
216             }
217         }
218         // Handle possible errors
219
catch( UDDIException e )
220         {
221             DispositionReport dr = e.getDispositionReport();
222             if( dr!=null )
223             {
224                 System.out.println("UDDIException faultCode:" + e.getFaultCode() +
225                                                      "\n operator:" + dr.getOperator() +
226                                                      "\n generic:" + dr.getGeneric() );
227
228                 Vector JavaDoc results = dr.getResultVector();
229                 for( int i=0; i<results.size(); i++ )
230                 {
231                     Result r = (Result)results.elementAt(i);
232                     System.out.println("\n errno:" + r.getErrno() );
233                     if( r.getErrInfo()!=null )
234                     {
235                         System.out.println("\n errCode:" + r.getErrInfo().getErrCode() +
236                                                              "\n errInfoText:" + r.getErrInfo().getText());
237                     }
238                 }
239             }
240             e.printStackTrace();
241         }
242         // Catch any other exception that may occur
243
catch( Exception JavaDoc e )
244         {
245             e.printStackTrace();
246         }
247     }
248 }
249
Popular Tags