This project has retired. For details please refer to its Attic page.
ServiceVersioningExample xref
View Javadoc
1   /*
2    * Copyright 2001-2008 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   */
17  package org.apache.juddi.examples.service.version;
18  
19  import java.util.Iterator;
20  import java.util.Set;
21  import org.apache.juddi.v3.client.UDDIConstants;
22  import org.apache.juddi.v3.client.config.UDDIClerk;
23  import org.apache.juddi.v3.client.config.UDDIClient;
24  import org.apache.juddi.v3.client.transport.Transport;
25  import org.uddi.api_v3.AccessPoint;
26  import org.uddi.api_v3.BindingTemplate;
27  import org.uddi.api_v3.BindingTemplates;
28  import org.uddi.api_v3.BusinessEntity;
29  import org.uddi.api_v3.BusinessService;
30  import org.uddi.api_v3.BusinessServices;
31  import org.uddi.api_v3.FindQualifiers;
32  import org.uddi.api_v3.FindService;
33  import org.uddi.api_v3.GetServiceDetail;
34  import org.uddi.api_v3.Name;
35  import org.uddi.api_v3.ServiceDetail;
36  import org.uddi.api_v3.ServiceList;
37  import org.uddi.api_v3.TModel;
38  import org.uddi.api_v3.TModelInstanceDetails;
39  import org.uddi.v3_service.UDDIInquiryPortType;
40  import org.uddi.v3_service.UDDIPublicationPortType;
41  import org.uddi.v3_service.UDDISecurityPortType;
42  
43  /**
44   * Hello world!
45   *
46   * This gives you an example of one way to use service version with UDDI and is
47   * a partial solution to https://issues.apache.org/jira/browse/JUDDI-509 which 
48   * references the following
49   * http://www.ibm.com/developerworks/webservices/library/ws-version/
50   *
51   */
52  public class ServiceVersioningExample {
53  
54      public static void main(String[] args) throws Exception {
55          System.out.println("Hello World!");
56          new ServiceVersioningExample().go();
57      }
58  
59      /**
60       * Main entry point
61       */
62      private void go() throws Exception {
63  
64          Init();
65  
66          Setup();
67  
68          ServiceLookUpByName();
69  
70          ServiceLookUpByVersion();
71  
72          Destroy();
73      }
74  
75      /**
76       * This will setup new a business, service, and binding template that's
77       * versioned per the article linked above
78       */
79      private void Setup() {
80  
81          BusinessEntity be = new BusinessEntity();
82          keygen = clerk.register(UDDIClerk.createKeyGenator(domain_prefix + "keygenerator", "my domain", lang)).getTModel().get(0);
83          be.setBusinessServices(new BusinessServices());
84          be.setBusinessKey(domain_prefix + "zerocoolbiz");
85          be.getName().add(new Name("ZeroCool Business", lang));
86          BusinessService bs = new BusinessService();
87          bs.getName().add(new Name("ZeroCool Service", lang));
88          bs.setBindingTemplates(new BindingTemplates());
89          bs.setBusinessKey(domain_prefix + "zerocoolbiz");
90          bs.setServiceKey(domain_prefix + "zerocoolsvc");
91  
92          //version 1
93          BindingTemplate bt1 = new BindingTemplate();
94          String version = "1.0.0.0";
95          bt1.setBindingKey(domain_prefix + "binding10");
96          bt1.setAccessPoint(new AccessPoint("http://localhost", "wsdl"));
97          bt1.setTModelInstanceDetails(new TModelInstanceDetails());
98          bt1.getTModelInstanceDetails().getTModelInstanceInfo().add(UDDIClerk.createServiceInterfaceVersion(version, lang));
99          bt1 = UDDIClient.addSOAPtModels(bt1);
100         bs.getBindingTemplates().getBindingTemplate().add(bt1);
101         
102 
103 
104         //version 2
105         BindingTemplate bt2 = new BindingTemplate();
106         bt2.setBindingKey(domain_prefix + "binding12");
107         String version2 = "1.2.0.0";
108         bt2.setAccessPoint(new AccessPoint("http://localhost", "wsdl"));
109         bt2.setTModelInstanceDetails(new TModelInstanceDetails());
110         bt2.getTModelInstanceDetails().getTModelInstanceInfo().add(UDDIClerk.createServiceInterfaceVersion(version2, lang));
111         bt2 = UDDIClient.addSOAPtModels(bt2);
112         bs.getBindingTemplates().getBindingTemplate().add(bt2);
113 
114         be.getBusinessServices().getBusinessService().add(bs);
115         clerk.register(be);
116 
117     }
118     private String domain_prefix = "uddi:mydomain.com:";
119     ;
120     private String lang = "en";
121 
122     /**
123      * this will look up our service by name and we'll discover multiple
124      * bindings with different versions
125      */
126     private void ServiceLookUpByName() throws Exception {
127 
128         //here we are assuming we don't know what key is used for the service, so we look it up
129         FindService fs = new FindService();
130         fs.setFindQualifiers(new FindQualifiers());
131         fs.getFindQualifiers().getFindQualifier().add(UDDIConstants.EXACT_MATCH);
132         fs.getName().add(new Name("ZeroCool Service", lang));
133         ServiceList findService = inquiry.findService(fs);
134 
135         //parse the results and get a list of services to get the details on
136         GetServiceDetail gsd = new GetServiceDetail();
137         for (int i = 0; i < findService.getServiceInfos().getServiceInfo().size(); i++) {
138             gsd.getServiceKey().add(findService.getServiceInfos().getServiceInfo().get(i).getServiceKey());
139         }
140         ServiceDetail serviceDetail = inquiry.getServiceDetail(gsd);
141 
142         //parse the service details, looking for our versioned service
143         if (serviceDetail != null) {
144             for (int i = 0; i < serviceDetail.getBusinessService().size(); i++) {
145                 if (serviceDetail.getBusinessService().get(i).getBindingTemplates() != null) {
146                     Set<BindingTemplate> bindingByVersion = UDDIClerk.getBindingByVersion("1.2.0.0", serviceDetail.getBusinessService().get(i).getBindingTemplates().getBindingTemplate());
147                     Iterator<BindingTemplate> iterator = bindingByVersion.iterator();
148                     while (iterator.hasNext()) {
149                         BindingTemplate next = iterator.next();
150                         System.out.println("SUCCESS! Found the right version on key " + next.getBindingKey());
151                         //TODO so now that you've found the right version of the right service
152                         //now you can go execute that the url
153                     }
154 
155                 }
156             }
157         }
158     }
159 
160     /**
161      * this will look up our service by name and version number
162      */
163     private void ServiceLookUpByVersion() throws Exception {
164         //here we are assuming we don't know what key is used for the service, so we look it up
165         //TODO
166     }
167     private TModel keygen = null;
168     private UDDISecurityPortType security = null;
169     private UDDIInquiryPortType inquiry = null;
170     private UDDIPublicationPortType publish = null;
171     private UDDIClient client = null;
172     private UDDIClerk clerk = null;
173 
174     private void Init() {
175         try {
176             // create a client and read the config in the archive; 
177             // you can use your config file name
178             client = new UDDIClient("META-INF/uddi.xml");
179             clerk = client.getClerk("default");
180             // a UddiClient can be a client to multiple UDDI nodes, so 
181             // supply the nodeName (defined in your uddi.xml.
182             // The transport can be WS, inVM etc which is defined in the uddi.xml
183             Transport transport = client.getTransport("default");
184             // Now you create a reference to the UDDI API
185             security = transport.getUDDISecurityService();
186             inquiry = transport.getUDDIInquiryService();
187             publish = transport.getUDDIPublishService();
188         } catch (Exception e) {
189             e.printStackTrace();
190         }
191 
192     }
193 
194     private void Destroy() {
195         clerk.unRegisterBusiness(domain_prefix + "zerocoolbiz");
196         clerk.unRegisterTModel(keygen.getTModelKey());
197         clerk.discardAuthToken();
198     }
199 }