KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > PublisherAssertionExample


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, Hewlett-Packard Company
5  * All Rights Reserved.
6  *
7  */

8
9 import java.util.Properties JavaDoc;
10 import java.util.Vector JavaDoc;
11
12 import org.uddi4j.UDDIException;
13 import org.uddi4j.client.UDDIProxy;
14 import org.uddi4j.datatype.Name;
15 import org.uddi4j.datatype.assertion.PublisherAssertion;
16 import org.uddi4j.datatype.business.BusinessEntity;
17 import org.uddi4j.datatype.tmodel.TModel;
18 import org.uddi4j.response.AuthToken;
19 import org.uddi4j.response.BusinessDetail;
20 import org.uddi4j.response.BusinessInfo;
21 import org.uddi4j.response.BusinessList;
22 import org.uddi4j.response.DispositionReport;
23 import org.uddi4j.response.RelatedBusinessInfo;
24 import org.uddi4j.response.RelatedBusinessesList;
25 import org.uddi4j.response.Result;
26 import org.uddi4j.util.KeyedReference;
27
28
29 /**
30  * Sample code that exercises the publish API.The Publisher Assertion
31  * is used to express the relationship exist between BusinessEntities.This
32  * sample attempts to add a publisherAssertion , finds the related Businesses
33  * and then delete the publisherAssertion .
34  *
35  * <OL>
36  * <LI>Sets up a UDDIProxy object
37  * <LI>Requests an authorization token
38  * <LI>Save one business & find one business for asserting a relationship
39  * with each other.
40  * <LI>Add a PublisherAssertion between two Business Entity
41  * <LI>Lists businesses Asserted by BusinessKey holder.
42  * <LI>Deletes the PublisherAssertion added.
43  * <LI>Cleans up the Data Structures created.
44  * </OL>
45  *
46  * @author Rajesh Sumra (rajesh_sumra@hp.com)
47  */

48 public class PublisherAssertionExample
49 {
50
51     Properties JavaDoc config = null;
52
53     public static void main (String JavaDoc args[])
54     {
55         PublisherAssertionExample app = new PublisherAssertionExample();
56         System.out.println("\n*********** Running PublisherAssertionExample ***********");
57         app.run();
58         System.exit(0);
59     }
60
61     public void run()
62     {
63         // Load samples configuration
64
config = Configurator.load();
65
66         // Construct a UDDIProxy object
67
UDDIProxy proxy = new UDDIProxy();
68
69         try
70         {
71             // Select the desired UDDI server node
72
proxy.setInquiryURL(config.getProperty("inquiryURL"));
73             proxy.setPublishURL(config.getProperty("publishURL"));
74
75             // Get an authorization token
76
System.out.println("\n Get authtoken");
77
78             // Pass in userid and password registered at the UDDI site
79
AuthToken token = proxy.get_authToken(config.getProperty("userid"),
80                                                                                         config.getProperty("password"));
81
82             System.out.println(" Returned authToken:" + token.getAuthInfoString());
83
84             // PublisherAssertion is created between two Business Entities.
85
// Search for a business whose name is in the configuration file
86
// and assert a relationship with the newly created business .
87
// The relationship is picked up from the configuration file.
88

89             System.out.println("\n Saving one Business Entity for PublisherAssertion");
90
91             // Create minimum required data objects
92

93             System.out.println(" Finding One Business Entity for PublisherAssertion");
94             // Find one more Business Entity from registry, to be used for
95
// asserting a relationship with former saved business entity.
96

97             //creating vector of Name Object
98
Vector JavaDoc names = new Vector JavaDoc();
99             names.add(new Name(config.getProperty("sampleEntityName")));
100
101             // Finds one more business .
102
// And setting the maximum rows to be returned as 5.
103
String JavaDoc toKey = "";
104             BusinessList businessList = proxy.find_business(names, null, null, null,null,null,5);
105             Vector JavaDoc businessInfoVector = businessList.getBusinessInfos().getBusinessInfoVector();
106             if( businessInfoVector.size() > 0 )
107             {
108                 BusinessInfo bi = (BusinessInfo)businessInfoVector.elementAt(0);
109                 toKey = bi.getBusinessKey();
110             }
111             else
112             {
113                 System.out.println(" No businesses found....");
114                 System.out.println(" Please change the Business Entity name to be searched !!");
115                 System.exit(0);
116             }
117
118             Vector JavaDoc entities = new Vector JavaDoc();
119
120             // Create one new business entity using required elements constructor
121
// These will be used for PublisherAssertion.Name is the business name.
122
// BusinessKey must be "" to save a new business
123
BusinessEntity be = new BusinessEntity("", config.getProperty("businessName"));
124             entities.addElement(be);
125
126             // Save business
127
BusinessDetail bd = proxy.save_business(token.getAuthInfoString(),entities);
128
129             // Process returned BusinessDetail object
130
Vector JavaDoc businessEntities = bd.getBusinessEntityVector();
131             BusinessEntity returnedBusinessEntity = (BusinessEntity)(businessEntities.elementAt(0));
132
133
134             // Get FromKey And ToKey from Business Entities returned and found
135
String JavaDoc fromKey = returnedBusinessEntity.getBusinessKey();
136
137             // Create KeyedReference with relationShip between
138
// FromKey and Tokey . And set the TModelKey
139
// so that it refers to uddi-org:relationships
140
KeyedReference keyedReference = new KeyedReference("Holding Company",
141                                                                                                                   config.getProperty("assertionRelationship")
142                                                                                                                 );
143
144             keyedReference.setTModelKey(TModel.RELATIONSHIPS_TMODEL_KEY);
145
146             // Create PublisherAssertion using KeyedReference, Fromkey
147
// and Tokey. The Publisher Assertion is used to express the
148
// relationship exist between BusinessEntities.
149
PublisherAssertion publisherAssertion =
150             new PublisherAssertion(fromKey,toKey,keyedReference);
151
152             System.out.println("\n Adding PublisherAssertions");
153             System.out.println(" FromKey : " + fromKey);
154             System.out.println(" ToKey : " + toKey);
155
156             // **** Add the PublisherAssertion
157
DispositionReport dispositionReport=proxy.add_publisherAssertions(token.getAuthInfoString(),
158                                                                                                                                               publisherAssertion);
159
160
161             System.out.println("\n Finding out the businesses which are related");
162
163             // Find related businesses using business key and keyedReference
164
// maxRows set at 0 means to return as many results as possible
165
RelatedBusinessesList relatedBusinessesList =
166             proxy.find_relatedBusinesses(
167                                                                     fromKey, keyedReference, null, 0);
168
169             System.out.println(" The businesses which are related to '"
170                                                  + config.getProperty("businessName") + "': ");
171
172             Vector JavaDoc relatedBusinessInfoVector =
173             relatedBusinessesList.getRelatedBusinessInfos().
174             getRelatedBusinessInfoVector();
175             for( int i = 0; i < relatedBusinessInfoVector.size(); i++ )
176             {
177                 RelatedBusinessInfo relatedBusinessInfo =
178                 (RelatedBusinessInfo)relatedBusinessInfoVector.elementAt(i);
179
180                 // Print name for each businesses
181
System.out.println(" " + relatedBusinessInfo.getDefaultNameString());
182             }
183
184             // **** Deletes the PublisherAssertion
185
System.out.println("\n Deleting the PublisherAssertion added in First step");
186             dispositionReport = proxy.delete_publisherAssertions (token.getAuthInfoString(),publisherAssertion);
187             if( dispositionReport.success() )
188             {
189                 System.out.println(" PublisherAssertion successfully deleted");
190             }
191             else
192             {
193                 System.out.println(" Error during deletion of PublisherAssertion\n"+
194                                                      "\n operator:" + dispositionReport.getOperator() +
195                                                      "\n generic:" + dispositionReport.getGeneric() );
196
197                 Vector JavaDoc results = dispositionReport.getResultVector();
198                 for( int i=0; i<results.size(); i++ )
199                 {
200                     Result r = (Result)results.elementAt(i);
201                     System.out.println("\n errno:" + r.getErrno() );
202                     if( r.getErrInfo()!=null )
203                     {
204                         System.out.println("\n errCode:" + r.getErrInfo().getErrCode() +
205                                                              "\n errInfoText:" + r.getErrInfo().getText());
206                     }
207                 }
208             }
209
210
211             // Tries to clean up the registry, for the structures we just created
212
//(by deleting the BusinessEntity)
213
System.out.println("\n Cleaning the Data Structures added/saved from registry");
214             {
215                 // delete using the authToken and businessKey
216
DispositionReport dr = proxy.delete_business(token.getAuthInfoString(),
217                                                                                                          fromKey);
218
219                 if( dr.success() )
220                 {
221                     System.out.println(" Registry successfully cleaned");
222                 }
223                 else
224                 {
225                     System.out.println(" Error during deletion of Business\n"+
226                                                          "\n operator:" + dr.getOperator() +
227                                                          "\n generic:" + dr.getGeneric() );
228
229                     Vector JavaDoc results = dr.getResultVector();
230                     for( int i=0; i<results.size(); i++ )
231                     {
232                         Result r = (Result)results.elementAt(i);
233                         System.out.println("\n errno:" + r.getErrno() );
234                         if( r.getErrInfo()!=null )
235                         {
236                             System.out.println("\n errCode:" + r.getErrInfo().getErrCode() +
237                                                                  "\n errInfoText:" + r.getErrInfo().getText());
238                         }
239                     }
240
241                 }
242             }
243         }
244         // Handle possible errors
245
catch( UDDIException e )
246         {
247             DispositionReport dr = e.getDispositionReport();
248             if( dr!=null )
249             {
250                 System.out.println("UDDIException faultCode:" + e.getFaultCode() +
251                                                      "\n operator:" + dr.getOperator() +
252                                                      "\n generic:" + dr.getGeneric() );
253
254                 Vector JavaDoc results = dr.getResultVector();
255                 for( int i=0; i<results.size(); i++ )
256                 {
257                     Result r = (Result)results.elementAt(i);
258                     System.out.println("\n errno:" + r.getErrno() );
259                     if( r.getErrInfo()!=null )
260                     {
261                         System.out.println("\n errCode:" + r.getErrInfo().getErrCode() +
262                                                              "\n errInfoText:" + r.getErrInfo().getText());
263                     }
264                 }
265             }
266             e.printStackTrace();
267         }
268         // Catch any other exception that may occur
269
catch( Exception JavaDoc e )
270         {
271             e.printStackTrace();
272         }
273     }
274 }
275
Popular Tags