This project has retired. For details please refer to its Attic page.
SearchByQos xref
View Javadoc
1   /*
2    * Copyright 2013 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.juddi.samples;
17  
18  import java.util.Properties;
19  import java.util.logging.Level;
20  import java.util.logging.Logger;
21  import org.apache.juddi.api_v3.AccessPointType;
22  import org.apache.juddi.jaxb.PrintUDDI;
23  import org.apache.juddi.v3.client.UDDIConstants;
24  import org.apache.juddi.v3.client.config.UDDIClerk;
25  import org.apache.juddi.v3.client.config.UDDIClient;
26  import org.apache.juddi.v3.client.config.UDDIClientContainer;
27  import org.apache.juddi.v3.client.ext.wsdm.WSDMQosConstants;
28  import org.apache.juddi.v3.client.transport.Transport;
29  import org.apache.juddi.v3_service.JUDDIApiPortType;
30  import org.uddi.api_v3.AccessPoint;
31  import org.uddi.api_v3.BindingDetail;
32  import org.uddi.api_v3.BindingTemplate;
33  import org.uddi.api_v3.BindingTemplates;
34  import org.uddi.api_v3.BusinessDetail;
35  import org.uddi.api_v3.BusinessEntity;
36  import org.uddi.api_v3.BusinessInfos;
37  import org.uddi.api_v3.BusinessList;
38  import org.uddi.api_v3.BusinessService;
39  import org.uddi.api_v3.BusinessServices;
40  import org.uddi.api_v3.CategoryBag;
41  import org.uddi.api_v3.Contact;
42  import org.uddi.api_v3.Contacts;
43  import org.uddi.api_v3.DeleteBusiness;
44  import org.uddi.api_v3.Description;
45  import org.uddi.api_v3.DiscoveryURL;
46  import org.uddi.api_v3.DiscoveryURLs;
47  import org.uddi.api_v3.FindBinding;
48  import org.uddi.api_v3.FindBusiness;
49  import org.uddi.api_v3.FindService;
50  import org.uddi.api_v3.FindTModel;
51  import org.uddi.api_v3.GetAuthToken;
52  import org.uddi.api_v3.GetBusinessDetail;
53  import org.uddi.api_v3.IdentifierBag;
54  import org.uddi.api_v3.InstanceDetails;
55  import org.uddi.api_v3.KeyedReference;
56  import org.uddi.api_v3.Name;
57  import org.uddi.api_v3.PersonName;
58  import org.uddi.api_v3.SaveBusiness;
59  import org.uddi.api_v3.SaveTModel;
60  import org.uddi.api_v3.ServiceInfos;
61  import org.uddi.api_v3.ServiceList;
62  import org.uddi.api_v3.TModel;
63  import org.uddi.api_v3.TModelBag;
64  import org.uddi.api_v3.TModelInstanceDetails;
65  import org.uddi.api_v3.TModelInstanceInfo;
66  import org.uddi.v3_service.UDDIInquiryPortType;
67  import org.uddi.v3_service.UDDIPublicationPortType;
68  import org.uddi.v3_service.UDDISecurityPortType;
69  
70  /**
71   *
72   * @author Alex O'Ree
73   */
74  public class SearchByQos {
75  
76          static PrintUDDI<TModel> pTModel = new PrintUDDI<TModel>();
77          static Properties properties = new Properties();
78          static String wsdlURL = null;
79          private static UDDISecurityPortType security = null;
80          private static JUDDIApiPortType juddiApi = null;
81          private static UDDIPublicationPortType publish = null;
82          private static UDDIInquiryPortType inquiry;
83  
84          public static void doFindService(String token) throws Exception {
85                  // create a manager and read the config in the archive; 
86                  // you can use your config file name
87                  UDDIClient clerkManager = new UDDIClient("META-INF/simple-publish-uddi.xml");
88                  Transport transport = clerkManager.getTransport();
89                  // Now you create a reference to the UDDI API
90                  security = transport.getUDDISecurityService();
91                  publish = transport.getUDDIPublishService();
92                  inquiry = transport.getUDDIInquiryService();
93                  //step one, get a token
94                  //GetAuthToken getAuthTokenRoot = new GetAuthToken();
95                  //getAuthTokenRoot.setUserID("uddi");
96                  //getAuthTokenRoot.setCred("uddi");
97  
98                  // Making API call that retrieves the authentication token for the 'root' user.
99                  //String rootAuthToken = clerk.getAuthToken(clerk.getUDDINode().getSecurityUrl());
100                 String uddi = token;// security.getAuthToken(getAuthTokenRoot).getAuthInfo();
101 
102                 System.out.println("killing mary's business if it exists");
103                 //first check is Mary's business exists and delete
104                 DeleteIfExists("uddi:uddi.marypublisher.com:marybusinessone", uddi);
105 
106                 System.out.println("making mary's tmodel key gen");
107                 //make the key gen since our test case uses some custom keys
108                 TModel createKeyGenator = UDDIClerk.createKeyGenator("uddi.marypublisher.com", "mary key gen", "en");
109                 //clerk.register(createKeyGenator);
110                 System.out.println("saving...");
111                 SaveTM(createKeyGenator, uddi);
112 
113                 System.out.println("fetching business list");
114                 ServiceList before = getServiceList(uddi);
115                 if (before.getServiceInfos() == null) {
116                         System.out.println("before no service returned!");
117                         before.setServiceInfos(new ServiceInfos());
118                 } else {
119                         System.out.println(before.getServiceInfos().getServiceInfo().size() + " service returned before");
120                 }
121 
122                 System.out.println("saving mary");
123                 SaveMary(uddi);
124 
125                 ServiceList after = getServiceList(uddi);
126                 if (after.getServiceInfos().getServiceInfo() == null) {
127                         System.out.println("after no service returned!");
128                         after.setServiceInfos(new ServiceInfos());
129                         return;
130                 } else {
131                         System.out.println(after.getServiceInfos().getServiceInfo().size() + " service returned after");
132                 }
133                 PrintUDDI<ServiceList> p = new PrintUDDI<ServiceList>();
134                 if (before.getServiceInfos().getServiceInfo().size()
135                         < after.getServiceInfos().getServiceInfo().size()) {
136                         System.out.println("hey it worked as advertised. Here's all the services with QOS parameters");
137                         System.out.println(p.print(after));
138                 } else {
139 
140                         System.out.println("something's not right, here's the before service listing");
141                         System.out.println(p.print(before));
142                         System.out.println(p.print(after));
143                 }
144 
145         }
146 
147         public static void doFindBinding(String token) throws Exception {
148                 // create a manager and read the config in the archive; 
149                 // you can use your config file name
150                 UDDIClient clerkManager = new UDDIClient("META-INF/simple-publish-uddi.xml");
151                 Transport transport = clerkManager.getTransport();
152                 // Now you create a reference to the UDDI API
153                 security = transport.getUDDISecurityService();
154                 publish = transport.getUDDIPublishService();
155                 inquiry = transport.getUDDIInquiryService();
156                 //step one, get a token
157                 // GetAuthToken getAuthTokenRoot = new GetAuthToken();
158                 //getAuthTokenRoot.setUserID("uddi");
159                 //getAuthTokenRoot.setCred("uddi");
160 
161                 // Making API call that retrieves the authentication token for the 'root' user.
162                 //String rootAuthToken = clerk.getAuthToken(clerk.getUDDINode().getSecurityUrl());
163                 String uddi = token;//security.getAuthToken(getAuthTokenRoot).getAuthInfo();
164 
165                 System.out.println("killing mary's business if it exists");
166                 //first check is Mary's business exists and delete
167                 DeleteIfExists("uddi:uddi.marypublisher.com:marybusinessone", uddi);
168 
169                 System.out.println("making mary's tmodel key gen");
170                 //make the key gen since our test case uses some custom keys
171                 TModel createKeyGenator = UDDIClerk.createKeyGenator("uddi.marypublisher.com", "mary key gen", "en");
172                 //clerk.register(createKeyGenator);
173                 System.out.println("saving...");
174                 SaveTM(createKeyGenator, uddi);
175 
176                 System.out.println("fetching binding list");
177                 BindingDetail before = getBindingList(uddi);
178                 if (before.getBindingTemplate() == null) {
179                         System.out.println("before no service returned!");
180                         // before.setServiceInfos(new ServiceInfos());
181                 } else {
182                         System.out.println(before.getBindingTemplate().size() + " service returned before");
183                 }
184 
185                 System.out.println("saving mary");
186                 SaveMary(uddi);
187 
188                 BindingDetail after = getBindingList(uddi);
189                 if (after.getBindingTemplate() == null) {
190                         System.out.println("after no service returned!");
191                         //after.setServiceInfos(new ServiceInfos());
192                 } else {
193                         System.out.println(after.getBindingTemplate().size() + " service returned after");
194                 }
195                 PrintUDDI<BindingDetail> p = new PrintUDDI<BindingDetail>();
196                 if (before.getBindingTemplate().size()
197                         < after.getBindingTemplate().size()) {
198                         System.out.println("hey it worked as advertised. Here's all the bindings with QOS parameters");
199                         System.out.println(p.print(after));
200 
201                 } else {
202 
203                         System.out.println("something's not right, here's the before service listing");
204                         System.out.println(p.print(before));
205                         System.out.println(p.print(after));
206                 }
207 
208         }
209 
210         public static void doFindBusiness(String token) throws Exception {
211                 // create a manager and read the config in the archive; 
212                 // you can use your config file name
213                 UDDIClient clerkManager = new UDDIClient("META-INF/simple-publish-uddi.xml");
214                 Transport transport = clerkManager.getTransport();
215                 // Now you create a reference to the UDDI API
216                 security = transport.getUDDISecurityService();
217                 publish = transport.getUDDIPublishService();
218                 inquiry = transport.getUDDIInquiryService();
219                 //step one, get a token
220                 // GetAuthToken getAuthTokenRoot = new GetAuthToken();
221                 //getAuthTokenRoot.setUserID("uddi");
222                 //getAuthTokenRoot.setCred("uddi");
223 
224                 // Making API call that retrieves the authentication token for the 'root' user.
225                 //String rootAuthToken = clerk.getAuthToken(clerk.getUDDINode().getSecurityUrl());
226                 String uddi = token;//security.getAuthToken(getAuthTokenRoot).getAuthInfo();
227 
228                 System.out.println("killing mary's business if it exists");
229                 //first check is Mary's business exists and delete
230                 DeleteIfExists("uddi:uddi.marypublisher.com:marybusinessone", uddi);
231 
232                 System.out.println("making mary's tmodel key gen");
233                 //make the key gen since our test case uses some custom keys
234                 TModel createKeyGenator = UDDIClerk.createKeyGenator("uddi.marypublisher.com", "mary key gen", "en");
235                 //clerk.register(createKeyGenator);
236                 System.out.println("saving...");
237                 SaveTM(createKeyGenator, uddi);
238 
239                 System.out.println("fetching business list");
240                 BusinessList before2 = getBusinessList(uddi);
241                 if (before2.getBusinessInfos() == null) {
242                         System.out.println("before no service returned!");
243                         // before.setServiceInfos(new ServiceInfos());
244                 } else {
245                         System.out.println(before2.getBusinessInfos().getBusinessInfo().size() + " business returned before");
246                 }
247 
248                 System.out.println("saving mary");
249                 SaveMary(uddi);
250 
251                 BusinessList after2 = getBusinessList(uddi);
252 
253                 PrintUDDI<BusinessList> p2 = new PrintUDDI<BusinessList>();
254                 if (before2.getBusinessInfos() == null) {
255                         before2.setBusinessInfos(new BusinessInfos());
256                 }
257                 if ((before2.getBusinessInfos().getBusinessInfo().size()
258                         < after2.getBusinessInfos().getBusinessInfo().size())) {
259                         System.out.println("hey it worked as advertised. Here's all the business with QOS parameters");
260                         System.out.println(p2.print(after2));
261 
262                 } else {
263 
264                         System.out.println("something's not right, here's the before business listing");
265                         System.out.println(p2.print(before2));
266                         System.out.println(p2.print(after2));
267                 }
268 
269         }
270 
271         private static void DeleteIfExists(String key, String authInfo) {
272                 GetBusinessDetail gbd = new GetBusinessDetail();
273                 gbd.setAuthInfo(authInfo);
274                 gbd.getBusinessKey().add(key);
275                 boolean found = false;
276                 try {
277                         BusinessDetail businessDetail = inquiry.getBusinessDetail(gbd);
278                         if (businessDetail != null
279                                 && !businessDetail.getBusinessEntity().isEmpty()
280                                 && businessDetail.getBusinessEntity().get(0).getBusinessKey().equals(key)) {
281                                 found = true;
282                         }
283                 } catch (Exception ex) {
284                 }
285                 if (found) {
286                         DeleteBusiness db = new DeleteBusiness();
287                         db.setAuthInfo(authInfo);
288                         db.getBusinessKey().add(key);
289                         try {
290                                 publish.deleteBusiness(db);
291                         } catch (Exception ex) {
292                                 Logger.getLogger(FindBusinessBugHunt.class.getName()).log(Level.SEVERE, null, ex);
293                         }
294                 }
295         }
296 
297         private static BusinessList getBusinessList(String token) throws Exception {
298                 FindBusiness fb = new FindBusiness();
299                 fb.setAuthInfo(token);
300                 org.uddi.api_v3.FindQualifiers fq = new org.uddi.api_v3.FindQualifiers();
301                 fq.getFindQualifier().add(UDDIConstants.APPROXIMATE_MATCH);
302                 fq.getFindQualifier().add(UDDIConstants.OR_ALL_KEYS);
303                 fb.setFindQualifiers(fq);
304                 fb.getName().add((new Name(UDDIConstants.WILDCARD, null)));
305 
306                 fb.setTModelBag(new TModelBag());
307                 fb.getTModelBag().getTModelKey().addAll(WSDMQosConstants.getAllQOSKeys());
308 
309                 return inquiry.findBusiness(fb);
310         }
311 
312         private static ServiceList getServiceList(String token) throws Exception {
313                 FindService fb = new FindService();
314                 fb.setAuthInfo(token);
315                 org.uddi.api_v3.FindQualifiers fq = new org.uddi.api_v3.FindQualifiers();
316                 fq.getFindQualifier().add(UDDIConstants.APPROXIMATE_MATCH);
317                 fq.getFindQualifier().add(UDDIConstants.OR_ALL_KEYS);
318                 fb.setFindQualifiers(fq);
319                 fb.getName().add((new Name(UDDIConstants.WILDCARD, null)));
320 
321                 fb.setTModelBag(new TModelBag());
322                 fb.getTModelBag().getTModelKey().addAll(WSDMQosConstants.getAllQOSKeys());
323 
324                 return inquiry.findService(fb);
325         }
326 
327         private static BindingDetail getBindingList(String token) throws Exception {
328                 FindBinding fb = new FindBinding();
329                 fb.setAuthInfo(token);
330                 fb.setTModelBag(new TModelBag());
331                 fb.getTModelBag().getTModelKey().add(WSDMQosConstants.METRIC_FAULT_COUNT_KEY);
332                 return inquiry.findBinding(fb);
333         }
334 
335         /**
336          * -
337          * adds a business, service, bt with tmodel instance details with qos
338          * parameters
339          *
340          * @param rootAuthToken
341          * @throws Exception
342          */
343         private static void SaveMary(String rootAuthToken) throws Exception {
344                 BusinessEntity be = new BusinessEntity();
345                 be.setBusinessKey("uddi:uddi.marypublisher.com:marybusinessone");
346                 be.setDiscoveryURLs(new DiscoveryURLs());
347                 be.getDiscoveryURLs().getDiscoveryURL().add(new DiscoveryURL("home", "http://www.marybusinessone.com"));
348                 be.getDiscoveryURLs().getDiscoveryURL().add(new DiscoveryURL("serviceList", "http://www.marybusinessone.com/services"));
349                 be.getName().add(new Name("Mary Doe Enterprises", "en"));
350                 be.getName().add(new Name("Maria Negocio Uno", "es"));
351                 be.getDescription().add(new Description("This is the description for Mary Business One.", "en"));
352                 be.setContacts(new Contacts());
353                 Contact c = new Contact();
354                 c.setUseType("administrator");
355                 c.getPersonName().add(new PersonName("Mary Doe", "en"));
356                 c.getPersonName().add(new PersonName("Juan Doe", "es"));
357                 c.getDescription().add(new Description("This is the administrator of the service offerings.", "en"));
358                 be.getContacts().getContact().add(c);
359                 be.setBusinessServices(new BusinessServices());
360                 BusinessService bs = new BusinessService();
361                 bs.setBusinessKey("uddi:uddi.marypublisher.com:marybusinessone");
362                 bs.setServiceKey("uddi:uddi.marypublisher.com:marybusinessoneservice");
363                 bs.getName().add(new Name("name!", "en"));
364                 bs.setBindingTemplates(new BindingTemplates());
365                 BindingTemplate bt = new BindingTemplate();
366                 bt.setAccessPoint(new AccessPoint("http://localhost", AccessPointType.WSDL_DEPLOYMENT.toString()));
367                 bt.setTModelInstanceDetails(new TModelInstanceDetails());
368                 TModelInstanceInfo tii = new TModelInstanceInfo();
369                 tii.setTModelKey(WSDMQosConstants.METRIC_FAULT_COUNT_KEY);
370                 tii.setInstanceDetails(new InstanceDetails());
371                 tii.getInstanceDetails().setInstanceParms("400");
372                 bt = UDDIClient.addSOAPtModels(bt);
373                 bt.getTModelInstanceDetails().getTModelInstanceInfo().add(tii);
374                 bs.getBindingTemplates().getBindingTemplate().add(bt);
375                 be.getBusinessServices().getBusinessService().add(bs);
376                 SaveBusiness sb = new SaveBusiness();
377                 sb.setAuthInfo(rootAuthToken);
378                 sb.getBusinessEntity().add(be);
379                 publish.saveBusiness(sb);
380         }
381 
382         private static void SaveTM(TModel createKeyGenator, String uddi) throws Exception {
383                 SaveTModel stm = new SaveTModel();
384                 stm.setAuthInfo(uddi);
385                 stm.getTModel().add(createKeyGenator);
386                 publish.saveTModel(stm);
387         }
388 }