View Javadoc
1   /*
2    * Copyright 2014 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.subscription.notify;
17  
18  import java.util.Properties;
19  import javax.jms.Connection;
20  import javax.jms.ConnectionFactory;
21  import javax.jms.Destination;
22  import javax.jms.Message;
23  import javax.jms.MessageConsumer;
24  import javax.jms.MessageListener;
25  import javax.jms.Session;
26  import javax.naming.Context;
27  import javax.naming.InitialContext;
28  import javax.xml.datatype.DatatypeFactory;
29  import org.apache.juddi.api_v3.AccessPointType;
30  import org.apache.juddi.v3.client.UDDIConstants;
31  import org.apache.juddi.v3.client.config.UDDIClerk;
32  import org.apache.juddi.v3.client.config.UDDIClient;
33  import org.apache.qpid.client.AMQAnyDestination;
34  import org.apache.qpid.client.AMQConnection;
35  import org.apache.qpid.client.AMQTopic;
36  import org.uddi.api_v3.AccessPoint;
37  import org.uddi.api_v3.BindingTemplate;
38  import org.uddi.api_v3.BindingTemplates;
39  import org.uddi.api_v3.BusinessEntity;
40  import org.uddi.api_v3.BusinessService;
41  import org.uddi.api_v3.BusinessServices;
42  import org.uddi.api_v3.Description;
43  import org.uddi.api_v3.FindBusiness;
44  import org.uddi.api_v3.FindQualifiers;
45  import org.uddi.api_v3.FindService;
46  import org.uddi.api_v3.FindTModel;
47  import org.uddi.api_v3.InstanceDetails;
48  import org.uddi.api_v3.Name;
49  import org.uddi.api_v3.TModel;
50  import org.uddi.api_v3.TModelInstanceDetails;
51  import org.uddi.api_v3.TModelInstanceInfo;
52  import org.uddi.sub_v3.Subscription;
53  import org.uddi.sub_v3.SubscriptionFilter;
54  
55  /**
56   *
57   * @author Alex O'Ree
58   */
59  public class Demo implements MessageListener {
60  
61          public static void main(String[] args) throws Exception {
62                  System.out.println("Hello world!");
63                  new Demo().Fire();
64          }
65  
66          public static final String TOPIC = "UDDI";
67          public static final String TMODEL_KEY_BASE = "uddi:amqptestdomain:";
68          public static final String TMODEL_KEY_TOPIC = TMODEL_KEY_BASE + ":topic";
69          public static final String TMODEL_DESTINATION_TYPE = TMODEL_KEY_BASE + "amqp.destination.type";
70          public static final String TMODEL_DESTINATION_NAME = TMODEL_KEY_BASE + "amqp.destination";
71  
72          private void Fire() throws Exception {
73                  String amqpURL = "amqp://guest:guest@clientid/?brokerlist='tcp://localhost:5672'";
74                  UDDIClient client = new UDDIClient("META-INF/subscription-notification-client-uddi.xml");
75                  UDDIClerk clerk = client.getClerk("default");
76                  String base = "uddi:amqptestdomain:";
77                  String lang = "en";
78  
79                  TModel destinationType = new TModel();
80                  destinationType.setName(new Name("AMQP Destination Type", lang));
81                  destinationType.getDescription().add(new Description("topicExchange, queue, etc", lang));
82                  destinationType.setTModelKey(TMODEL_DESTINATION_TYPE);
83  
84                  TModel destinationName = new TModel();
85                  destinationName.setName(new Name("AMQP Destination Name", lang));
86                  destinationName.getDescription().add(new Description("The name of the topic or queue", lang));
87                  destinationName.setTModelKey(TMODEL_DESTINATION_NAME);
88  
89                  TModel createKeyGenator = UDDIClerk.createKeyGenator(base + "keygenerator", "AMQP Test Key domain", lang);
90                  BusinessEntity be = new BusinessEntity();
91                  be.setBusinessKey(base + "business");
92                  be.getName().add(new Name("AMQP Test callbacks", lang));
93                  be.setBusinessServices(new BusinessServices());
94                  BusinessService bs = new BusinessService();
95                  bs.getName().add(new Name("AMQP Test service", lang));
96                  bs.setBindingTemplates(new BindingTemplates());
97                  bs.setBusinessKey(base + "business");
98                  bs.setServiceKey(base + "service");
99  
100                 BindingTemplate bt = new BindingTemplate();
101                 bt.setBindingKey(base + "binding");
102                 bt.setServiceKey(base + "service");
103                 bt.setAccessPoint(new AccessPoint(amqpURL, AccessPointType.END_POINT.toString()));
104                 bt.setTModelInstanceDetails(new TModelInstanceDetails());
105 
106                 TModelInstanceInfo version = UDDIClerk.createServiceInterfaceVersion("1.0", lang);
107                 bt.getTModelInstanceDetails().getTModelInstanceInfo().add(version);
108 
109                 TModelInstanceInfo transport = new TModelInstanceInfo();
110                 transport.setTModelKey(UDDIConstants.TRANSPORT_AMQP);
111                 bt.getTModelInstanceDetails().getTModelInstanceInfo().add(transport);
112 
113                 TModelInstanceInfo topic = new TModelInstanceInfo();
114                 topic.setTModelKey(TMODEL_DESTINATION_TYPE);
115                 topic.setInstanceDetails(new InstanceDetails());
116                 topic.getInstanceDetails().setInstanceParms("amq.topic");
117                 bt.getTModelInstanceDetails().getTModelInstanceInfo().add(topic);
118 
119                 TModelInstanceInfo name = new TModelInstanceInfo();
120                 name.setTModelKey(TMODEL_DESTINATION_NAME);
121                 name.setInstanceDetails(new InstanceDetails());
122                 name.getInstanceDetails().setInstanceParms(TOPIC);
123                 bt.getTModelInstanceDetails().getTModelInstanceInfo().add(name);
124 
125                 bs.getBindingTemplates().getBindingTemplate().add(bt);
126 
127                 be.getBusinessServices().getBusinessService().add(bs);
128                 try {
129                         System.out.println("Registering tModel");
130                         clerk.register(createKeyGenator);
131                         System.out.println("Registering destination type tmodel");
132                         clerk.register(destinationType);
133                         System.out.println("Registering destination name tmodel");
134                         clerk.register(destinationName);
135 
136                         System.out.println("Registering business with callback definition");
137                         clerk.register(be);
138 
139                         Properties p = new Properties();
140                         p.setProperty("java.naming.factory.initial", "org.apache.qpid.jndi.PropertiesFileInitialContextFactory");
141                         p.setProperty("connectionfactory.qpidConnectionfactory", amqpURL);
142                         p.setProperty("destination." + TOPIC, "amq.topic");
143 
144                         System.out.println("Connecting to AMQP at " + amqpURL);
145 
146                         Context context = new InitialContext(p);
147 
148                         ConnectionFactory connectionFactory = (ConnectionFactory) context.lookup("qpidConnectionfactory");
149                         Connection connection = connectionFactory.createConnection();
150                         connection.start();
151 
152                         Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
153                         Destination destination = (Destination) context.lookup(TOPIC);
154 
155                         MessageConsumer consumer = session.createConsumer(destination);
156 
157                         consumer.setMessageListener(this);
158                         System.out.println("Connected and listening...");
159 
160                         Subscription sub = new Subscription();
161                         sub.setNotificationInterval(DatatypeFactory.newInstance().newDuration(1000));
162                         sub.setBindingKey(base + "binding");
163                         sub.setSubscriptionKey(base + "sub-fb");
164                         sub.setSubscriptionFilter(new SubscriptionFilter());
165                         sub.getSubscriptionFilter().setFindBusiness(new FindBusiness());
166                         sub.getSubscriptionFilter().getFindBusiness().setFindQualifiers(new FindQualifiers());
167                         sub.getSubscriptionFilter().getFindBusiness().getFindQualifiers().getFindQualifier().add(UDDIConstants.APPROXIMATE_MATCH);
168                         sub.getSubscriptionFilter().getFindBusiness().getName().add(new Name(UDDIConstants.WILDCARD, null));
169 
170                         Subscription subscriptionBiz = clerk.register(sub, clerk.getUDDINode().getApiNode());
171 
172                         System.out.println("Registered FindBusiness subscription key: " + (subscriptionBiz.getSubscriptionKey()) + " bindingkey: " + subscriptionBiz.getBindingKey());
173 
174                         sub = new Subscription();
175                         sub.setSubscriptionKey(base + "sub-fs");
176                         sub.setNotificationInterval(DatatypeFactory.newInstance().newDuration(1000));
177                         sub.setBindingKey(base + "binding");
178                         sub.setSubscriptionFilter(new SubscriptionFilter());
179                         sub.getSubscriptionFilter().setFindService(new FindService());
180                         sub.getSubscriptionFilter().getFindService().setFindQualifiers(new FindQualifiers());
181                         sub.getSubscriptionFilter().getFindService().getFindQualifiers().getFindQualifier().add(UDDIConstants.APPROXIMATE_MATCH);
182                         sub.getSubscriptionFilter().getFindService().getName().add(new Name(UDDIConstants.WILDCARD, null));
183 
184                         Subscription subscriptionSvc = clerk.register(sub, clerk.getUDDINode().getApiNode());
185 
186                         System.out.println("Registered FindService subscription key: " + (subscriptionSvc.getSubscriptionKey()) + " bindingkey: " + subscriptionSvc.getBindingKey());
187 
188                         sub = new Subscription();
189                         sub.setNotificationInterval(DatatypeFactory.newInstance().newDuration(1000));
190                         sub.setBindingKey(base + "binding");
191                         sub.setSubscriptionKey(base + "sub-ft");
192                         sub.setSubscriptionFilter(new SubscriptionFilter());
193                         sub.getSubscriptionFilter().setFindTModel(new FindTModel());
194                         sub.getSubscriptionFilter().getFindTModel().setFindQualifiers(new FindQualifiers());
195                         sub.getSubscriptionFilter().getFindTModel().getFindQualifiers().getFindQualifier().add(UDDIConstants.APPROXIMATE_MATCH);
196                         sub.getSubscriptionFilter().getFindTModel().setName(new Name(UDDIConstants.WILDCARD, null));
197 
198                         Subscription subscriptionTM = clerk.register(sub, clerk.getUDDINode().getApiNode());
199 
200                         System.out.println("Registered FindTModel subscription key: " + (subscriptionTM.getSubscriptionKey()) + " bindingkey: " + subscriptionTM.getBindingKey());
201 
202                         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!");
203 
204                         System.out.println("Listening, press any key to quit");
205                         System.in.read();
206                         connection.close();
207 
208                 } catch (Exception ex) {
209                         ex.printStackTrace();
210                 } finally {
211                         //clerk.unRegisterBusiness(be.getBusinessKey());
212                         //clerk.unRegisterTModel(createKeyGenator.getTModelKey());
213                 }
214         }
215 
216         @Override
217         public void onMessage(Message msg) {
218 
219                 System.out.println("Message received: " + msg);
220         }
221 }