View Javadoc
1   /*
2    * Copyright 2001-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   */
17  package org.apache.juddi.v3.client.cli;
18  
19  import javax.xml.datatype.DatatypeFactory;
20  import org.apache.juddi.jaxb.PrintUDDI;
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.subscription.ISubscriptionCallback;
25  import org.apache.juddi.v3.client.subscription.SubscriptionCallbackListener;
26  import org.apache.juddi.v3.client.transport.Transport;
27  import org.uddi.api_v3.*;
28  import org.uddi.sub_v3.Subscription;
29  import org.uddi.sub_v3.SubscriptionFilter;
30  import org.uddi.sub_v3.SubscriptionResultsList;
31  
32  /**
33   * Thie class shows you how to create a business and a subscription using UDDI
34   * Subscription asynchronous callbacks
35   *
36   * @author <a href="mailto:alexoree@apache.org">Alex O'Ree</a>
37   */
38  public class UddiSubscribe implements ISubscriptionCallback {
39  
40          public static void main(String args[]) throws Exception {
41                  UddiSubscribe sp = new UddiSubscribe();
42                  sp.fire();
43          }
44  
45          
46          private UDDIClerk clerk = null;
47          private UDDIClient client = null;
48  
49          private String nodename = "default";
50          private PrintUDDI<SubscriptionResultsList> printer = new PrintUDDI<SubscriptionResultsList>();
51  
52          public UddiSubscribe() {
53                  try {
54                          // create a manager and read the config in the archive;
55                          // you can use your config file name
56                          client = new UDDIClient("META-INF/simple-publish-uddi.xml");
57                          clerk = client.getClerk("default");
58                  } catch (Exception e) {
59                          e.printStackTrace();
60                  }
61          }
62  
63          public UddiSubscribe(UDDIClient client, String nodename, Transport transport) {
64                  try {
65                          // create a manager and read the config in the archive; 
66                          // you can use your config file name
67                          //client = new UDDIClient("META-INF/simple-publish-uddi.xml");
68                          clerk = client.getClerk(nodename);
69                          this.nodename = nodename;
70                          // Now you create a reference to the UDDI API
71  
72                  } catch (Exception e) {
73                          e.printStackTrace();
74                  }
75          }
76  
77          public void fire() throws Exception {
78  
79                  TModel createKeyGenator = UDDIClerk.createKeyGenator("somebusiness", "A test key domain SubscriptionCallbackTest1", "SubscriptionCallbackTest1");
80  
81                  clerk.register(createKeyGenator);
82                  System.out.println("Registered tModel keygen: " + createKeyGenator.getTModelKey());
83  
84                  //setup the business to attach to
85                  BusinessEntity be = new BusinessEntity();
86                  be.setBusinessKey("uddi:somebusiness:somebusinesskey");
87                  be.getName().add(new Name("somebusiness SubscriptionCallbackTest1", null));
88                  be.setBusinessServices(new BusinessServices());
89                  BusinessService bs = new BusinessService();
90                  bs.setBusinessKey("uddi:somebusiness:somebusinesskey");
91                  bs.setServiceKey("uddi:somebusiness:someservicekey");
92                  bs.getName().add(new Name("service SubscriptionCallbackTest1", null));
93                  be.getBusinessServices().getBusinessService().add(bs);
94                  BusinessEntity register = clerk.register(be);
95                  System.out.println("Registered business keygen: " + register.getBusinessKey());
96  
97                  //start up our listener
98                  BindingTemplate start = SubscriptionCallbackListener.start(client, nodename);
99  
100                 //register for callbacks
101                 SubscriptionCallbackListener.registerCallback(this);
102 
103                 Subscription sub = new Subscription();
104                 sub.setNotificationInterval(DatatypeFactory.newInstance().newDuration(1000));
105                 sub.setBindingKey(start.getBindingKey());
106                 sub.setSubscriptionFilter(new SubscriptionFilter());
107                 sub.getSubscriptionFilter().setFindBusiness(new FindBusiness());
108                 sub.getSubscriptionFilter().getFindBusiness().setFindQualifiers(new FindQualifiers());
109                 sub.getSubscriptionFilter().getFindBusiness().getFindQualifiers().getFindQualifier().add(UDDIConstants.APPROXIMATE_MATCH);
110                 sub.getSubscriptionFilter().getFindBusiness().getName().add(new Name(UDDIConstants.WILDCARD, null));
111 
112                 Subscription subscriptionBiz = clerk.register(sub, clerk.getUDDINode().getApiNode());
113 
114                 System.out.println("Registered FindBusiness subscription key: " + (subscriptionBiz.getSubscriptionKey()) + " bindingkey: " + subscriptionBiz.getBindingKey());
115 
116                 sub = new Subscription();
117                 sub.setNotificationInterval(DatatypeFactory.newInstance().newDuration(1000));
118                 sub.setBindingKey(start.getBindingKey());
119                 sub.setSubscriptionFilter(new SubscriptionFilter());
120                 sub.getSubscriptionFilter().setFindService(new FindService());
121                 sub.getSubscriptionFilter().getFindService().setFindQualifiers(new FindQualifiers());
122                 sub.getSubscriptionFilter().getFindService().getFindQualifiers().getFindQualifier().add(UDDIConstants.APPROXIMATE_MATCH);
123                 sub.getSubscriptionFilter().getFindService().getName().add(new Name(UDDIConstants.WILDCARD, null));
124 
125                 Subscription subscriptionSvc = clerk.register(sub, clerk.getUDDINode().getApiNode());
126 
127                 System.out.println("Registered FindService subscription key: " + (subscriptionSvc.getSubscriptionKey()) + " bindingkey: " + subscriptionSvc.getBindingKey());
128 
129                 sub = new Subscription();
130                 sub.setNotificationInterval(DatatypeFactory.newInstance().newDuration(1000));
131                 sub.setBindingKey(start.getBindingKey());
132                 sub.setSubscriptionFilter(new SubscriptionFilter());
133                 sub.getSubscriptionFilter().setFindTModel(new FindTModel());
134                 sub.getSubscriptionFilter().getFindTModel().setFindQualifiers(new FindQualifiers());
135                 sub.getSubscriptionFilter().getFindTModel().getFindQualifiers().getFindQualifier().add(UDDIConstants.APPROXIMATE_MATCH);
136                 sub.getSubscriptionFilter().getFindTModel().setName(new Name(UDDIConstants.WILDCARD, null));
137 
138                 Subscription subscriptionTM = clerk.register(sub, clerk.getUDDINode().getApiNode());
139 
140                 System.out.println("Registered FindTModel subscription key: " + (subscriptionTM.getSubscriptionKey()) + " bindingkey: " + subscriptionTM.getBindingKey());
141 
142                 sub = new Subscription();
143                 sub.setNotificationInterval(DatatypeFactory.newInstance().newDuration(1000));
144                 sub.setBindingKey(start.getBindingKey());
145                 sub.setSubscriptionFilter(new SubscriptionFilter());
146                 sub.getSubscriptionFilter().setGetAssertionStatusReport(new GetAssertionStatusReport());
147                 sub.getSubscriptionFilter().getGetAssertionStatusReport().setCompletionStatus(CompletionStatus.STATUS_COMPLETE);
148 
149                 Subscription subscriptionPA = clerk.register(sub, clerk.getUDDINode().getApiNode());
150 
151                 System.out.println("Registered Completed PublisherAssertion subscription key: " + (subscriptionPA.getSubscriptionKey()) + " bindingkey: " + subscriptionTM.getBindingKey());
152 
153                 sub = new Subscription();
154                 sub.setNotificationInterval(DatatypeFactory.newInstance().newDuration(1000));
155                 sub.setBindingKey(start.getBindingKey());
156                 sub.setSubscriptionFilter(new SubscriptionFilter());
157                 sub.getSubscriptionFilter().setGetAssertionStatusReport(new GetAssertionStatusReport());
158                 sub.getSubscriptionFilter().getGetAssertionStatusReport().setCompletionStatus(CompletionStatus.STATUS_FROM_KEY_INCOMPLETE);
159 
160                 Subscription subscriptionPA2 = clerk.register(sub, clerk.getUDDINode().getApiNode());
161 
162                 System.out.println("Registered FROM incomplete PublisherAssertion subscription key: " + (subscriptionPA2.getSubscriptionKey()) + " bindingkey: " + subscriptionTM.getBindingKey());
163 
164                 sub = new Subscription();
165                 sub.setNotificationInterval(DatatypeFactory.newInstance().newDuration(1000));
166                 sub.setBindingKey(start.getBindingKey());
167                 sub.setSubscriptionFilter(new SubscriptionFilter());
168                 sub.getSubscriptionFilter().setGetAssertionStatusReport(new GetAssertionStatusReport());
169                 sub.getSubscriptionFilter().getGetAssertionStatusReport().setCompletionStatus(CompletionStatus.STATUS_TO_KEY_INCOMPLETE);
170 
171                 Subscription subscriptionPA3 = clerk.register(sub, clerk.getUDDINode().getApiNode());
172 
173                 System.out.println("Registered TO incomplete PublisherAssertion subscription key: " + (subscriptionPA3.getSubscriptionKey()) + " bindingkey: " + subscriptionTM.getBindingKey());
174 
175                 sub = new Subscription();
176                 sub.setNotificationInterval(DatatypeFactory.newInstance().newDuration(1000));
177                 sub.setBindingKey(start.getBindingKey());
178                 sub.setSubscriptionFilter(new SubscriptionFilter());
179                 sub.getSubscriptionFilter().setGetAssertionStatusReport(new GetAssertionStatusReport());
180                 sub.getSubscriptionFilter().getGetAssertionStatusReport().setCompletionStatus(CompletionStatus.STATUS_BOTH_INCOMPLETE);
181 
182                 Subscription subscriptionPA4 = clerk.register(sub, clerk.getUDDINode().getApiNode());
183 
184                 System.out.println("Registered recently deleted PublisherAssertion subscription key: " + (subscriptionPA4.getSubscriptionKey()) + " bindingkey: " + subscriptionTM.getBindingKey());
185 
186                 System.out.println("Waiting for callbacks. Now would be a good time to launch either another program or juddi-gui to make some changes. Press any key to stop!");
187                 //Thread hook = new Thread(this);
188                 //  Runtime.getRuntime().addShutdownHook(hook);
189 
190                 System.in.read();
191 
192                 SubscriptionCallbackListener.stop(client, nodename, start.getBindingKey());
193                 clerk.unRegisterSubscription(subscriptionBiz.getSubscriptionKey());
194                 clerk.unRegisterSubscription(subscriptionSvc.getSubscriptionKey());
195                 clerk.unRegisterSubscription(subscriptionTM.getSubscriptionKey());
196                 clerk.unRegisterSubscription(subscriptionPA.getSubscriptionKey());
197                 clerk.unRegisterSubscription(subscriptionPA2.getSubscriptionKey());
198                 clerk.unRegisterSubscription(subscriptionPA3.getSubscriptionKey());
199                 clerk.unRegisterSubscription(subscriptionPA4.getSubscriptionKey());
200 
201                 clerk.unRegisterTModel(createKeyGenator.getTModelKey());
202 
203                 clerk.unRegisterBusiness("uddi:somebusiness:somebusinesskey");
204 
205                 //Runtime.getRuntime().removeShutdownHook(hook);
206         }
207 
208         @Override
209         public void handleCallback(SubscriptionResultsList body) {
210                 System.out.println("Callback received!");
211                 System.out.println(printer.print(body));
212         }
213 
214         @Override
215         public void notifyEndpointStopped() {
216                 System.out.println("The endpoint was stopped!");
217         }
218 
219 }