KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > bus > transports > jms > JMSProviderHub


1 package org.objectweb.celtix.bus.transports.jms;
2
3 import javax.jms.Connection JavaDoc;
4 import javax.jms.Destination JavaDoc;
5 import javax.jms.JMSException JavaDoc;
6 import javax.jms.QueueConnectionFactory JavaDoc;
7 import javax.jms.TopicConnectionFactory JavaDoc;
8 import javax.naming.Context JavaDoc;
9 import javax.naming.NamingException JavaDoc;
10
11 import org.objectweb.celtix.transports.jms.JMSAddressPolicyType;
12 import org.objectweb.celtix.transports.jms.JMSServerBehaviorPolicyType;
13
14
15 /**
16  * This class acts as the hub of JMS provider usage, creating shared
17  * JMS Connections and providing access to a pool of JMS Sessions.
18  * <p>
19  * A new JMS connection is created for each each port based
20  * <jms:address> - however its likely that in practice the same JMS
21  * provider will be specified for each port, and hence the connection
22  * resources could be shared accross ports.
23  * <p>
24  * For the moment this class is realized as just a container for
25  * static methods, but the intention is to support in future sharing
26  * of JMS resources accross compatible ports.
27  *
28  * @author Eoghan Glynn
29  */

30 public final class JMSProviderHub {
31
32     /**
33      * Constructor.
34      */

35     private JMSProviderHub() {
36     }
37
38     //--java.lang.Object Overrides----------------------------------------------
39
public String JavaDoc toString() {
40         return "JMSProviderHub";
41     }
42
43
44     //--Methods-----------------------------------------------------------------
45

46     protected static void connect(JMSTransportBase transport) throws JMSException JavaDoc, NamingException JavaDoc {
47         JMSAddressPolicyType addrDetails = transport.getJmsAddressDetails();
48         JMSServerBehaviorPolicyType serverPolicy = null;
49         if (transport instanceof JMSServerTransport) {
50             serverPolicy = ((JMSServerTransport)transport).getJMSServerBehaviourPolicy();
51         }
52
53         // get JMS connection resources and destination
54
//
55
Context JavaDoc context = JMSUtils.getInitialContext(addrDetails);
56         Connection JavaDoc connection = null;
57         
58         if (JMSConstants.JMS_QUEUE.equals(addrDetails.getDestinationStyle().value())) {
59             QueueConnectionFactory JavaDoc qcf =
60                 (QueueConnectionFactory JavaDoc)context.lookup(addrDetails.getJndiConnectionFactoryName());
61             if (addrDetails.isSetConnectionUserName()) {
62                 connection = qcf.createQueueConnection(addrDetails.getConnectionUserName(),
63                                                        addrDetails.getConnectionPassword());
64             } else {
65                 connection = qcf.createQueueConnection();
66             }
67         } else {
68             TopicConnectionFactory JavaDoc tcf =
69                 (TopicConnectionFactory JavaDoc)context.lookup(addrDetails.getJndiConnectionFactoryName());
70             if (addrDetails.isSetConnectionUserName()) {
71                 connection = tcf.createTopicConnection(addrDetails.getConnectionUserName(),
72                                                        addrDetails.getConnectionPassword());
73             } else {
74                 connection = tcf.createTopicConnection();
75             }
76         }
77
78         connection.start();
79
80         Destination JavaDoc requestDestination =
81                 (Destination JavaDoc)context.lookup(
82                                            addrDetails.getJndiDestinationName());
83
84         Destination JavaDoc replyDestination = (null != addrDetails.getJndiReplyDestinationName())
85             ? (Destination JavaDoc)context.lookup(addrDetails.getJndiReplyDestinationName()) : null;
86
87         // create session factory to manage session, reply destination,
88
// producer and consumer pooling
89
//
90

91         JMSSessionFactory sf =
92             new JMSSessionFactory(connection,
93                                   replyDestination,
94                                   addrDetails,
95                                   serverPolicy,
96                                   context);
97
98         // notify transport that connection is complete
99
//
100
transport.connected(requestDestination, replyDestination, sf);
101     }
102 }
103
Popular Tags