001: package org.objectweb.celtix.bus.transports.jms;
002:
003: import javax.jms.Connection;
004: import javax.jms.Destination;
005: import javax.jms.JMSException;
006: import javax.jms.QueueConnectionFactory;
007: import javax.jms.TopicConnectionFactory;
008: import javax.naming.Context;
009: import javax.naming.NamingException;
010:
011: import org.objectweb.celtix.transports.jms.JMSAddressPolicyType;
012: import org.objectweb.celtix.transports.jms.JMSServerBehaviorPolicyType;
013:
014: /**
015: * This class acts as the hub of JMS provider usage, creating shared
016: * JMS Connections and providing access to a pool of JMS Sessions.
017: * <p>
018: * A new JMS connection is created for each each port based
019: * <jms:address> - however its likely that in practice the same JMS
020: * provider will be specified for each port, and hence the connection
021: * resources could be shared accross ports.
022: * <p>
023: * For the moment this class is realized as just a container for
024: * static methods, but the intention is to support in future sharing
025: * of JMS resources accross compatible ports.
026: *
027: * @author Eoghan Glynn
028: */
029: public final class JMSProviderHub {
030:
031: /**
032: * Constructor.
033: */
034: private JMSProviderHub() {
035: }
036:
037: //--java.lang.Object Overrides----------------------------------------------
038: public String toString() {
039: return "JMSProviderHub";
040: }
041:
042: //--Methods-----------------------------------------------------------------
043:
044: protected static void connect(JMSTransportBase transport)
045: throws JMSException, NamingException {
046: JMSAddressPolicyType addrDetails = transport
047: .getJmsAddressDetails();
048: JMSServerBehaviorPolicyType serverPolicy = null;
049: if (transport instanceof JMSServerTransport) {
050: serverPolicy = ((JMSServerTransport) transport)
051: .getJMSServerBehaviourPolicy();
052: }
053:
054: // get JMS connection resources and destination
055: //
056: Context context = JMSUtils.getInitialContext(addrDetails);
057: Connection connection = null;
058:
059: if (JMSConstants.JMS_QUEUE.equals(addrDetails
060: .getDestinationStyle().value())) {
061: QueueConnectionFactory qcf = (QueueConnectionFactory) context
062: .lookup(addrDetails.getJndiConnectionFactoryName());
063: if (addrDetails.isSetConnectionUserName()) {
064: connection = qcf.createQueueConnection(addrDetails
065: .getConnectionUserName(), addrDetails
066: .getConnectionPassword());
067: } else {
068: connection = qcf.createQueueConnection();
069: }
070: } else {
071: TopicConnectionFactory tcf = (TopicConnectionFactory) context
072: .lookup(addrDetails.getJndiConnectionFactoryName());
073: if (addrDetails.isSetConnectionUserName()) {
074: connection = tcf.createTopicConnection(addrDetails
075: .getConnectionUserName(), addrDetails
076: .getConnectionPassword());
077: } else {
078: connection = tcf.createTopicConnection();
079: }
080: }
081:
082: connection.start();
083:
084: Destination requestDestination = (Destination) context
085: .lookup(addrDetails.getJndiDestinationName());
086:
087: Destination replyDestination = (null != addrDetails
088: .getJndiReplyDestinationName()) ? (Destination) context
089: .lookup(addrDetails.getJndiReplyDestinationName())
090: : null;
091:
092: // create session factory to manage session, reply destination,
093: // producer and consumer pooling
094: //
095:
096: JMSSessionFactory sf = new JMSSessionFactory(connection,
097: replyDestination, addrDetails, serverPolicy, context);
098:
099: // notify transport that connection is complete
100: //
101: transport.connected(requestDestination, replyDestination, sf);
102: }
103: }
|