001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.transport.jms;
019:
020: import javax.jms.Connection;
021: import javax.jms.Destination;
022: import javax.jms.JMSException;
023: import javax.jms.QueueConnectionFactory;
024: import javax.jms.TopicConnectionFactory;
025: import javax.naming.Context;
026: import javax.naming.NamingException;
027:
028: /**
029: * This class acts as the hub of JMS provider usage, creating shared
030: * JMS Connections and providing access to a pool of JMS Sessions.
031: * <p>
032: * A new JMS connection is created for each each port based
033: * <jms:address> - however its likely that in practice the same JMS
034: * provider will be specified for each port, and hence the connection
035: * resources could be shared accross ports.
036: * <p>
037: * For the moment this class is realized as just a container for
038: * static methods, but the intention is to support in future sharing
039: * of JMS resources accross compatible ports.
040: *
041: * @author Eoghan Glynn
042: */
043: public final class JMSProviderHub {
044:
045: /**
046: * Constructor.
047: */
048: private JMSProviderHub() {
049: }
050:
051: public String toString() {
052: return "JMSProviderHub";
053: }
054:
055: protected static void connect(JMSTransport jmsTransport)
056: throws JMSException, NamingException {
057: connect(jmsTransport, null, null);
058: }
059:
060: protected static void connect(JMSTransport jmsTransport,
061: ServerConfig jmsDestConfigBean,
062: ServerBehaviorPolicyType runtimePolicy)
063: throws JMSException, NamingException {
064:
065: AddressType addrDetails = jmsTransport.getJMSAddress();
066:
067: // get JMS connection resources and destination
068: //
069: Context context = JMSUtils.getInitialContext(addrDetails);
070: Connection connection = null;
071:
072: if (JMSConstants.JMS_QUEUE.equals(addrDetails
073: .getDestinationStyle().value())) {
074: QueueConnectionFactory qcf = (QueueConnectionFactory) context
075: .lookup(addrDetails.getJndiConnectionFactoryName());
076: if (addrDetails.isSetConnectionUserName()) {
077: connection = qcf.createQueueConnection(addrDetails
078: .getConnectionUserName(), addrDetails
079: .getConnectionPassword());
080: } else {
081: connection = qcf.createQueueConnection();
082: }
083: } else {
084: TopicConnectionFactory tcf = (TopicConnectionFactory) context
085: .lookup(addrDetails.getJndiConnectionFactoryName());
086: if (addrDetails.isSetConnectionUserName()) {
087: connection = tcf.createTopicConnection(addrDetails
088: .getConnectionUserName(), addrDetails
089: .getConnectionPassword());
090: } else {
091: connection = tcf.createTopicConnection();
092: }
093: }
094:
095: if (null != jmsDestConfigBean) {
096: String clientID = jmsDestConfigBean
097: .getDurableSubscriptionClientId();
098:
099: if (clientID != null) {
100: connection.setClientID(clientID);
101: }
102: }
103: connection.start();
104:
105: Destination requestDestination = (Destination) context
106: .lookup(addrDetails.getJndiDestinationName());
107:
108: Destination replyDestination = (null != addrDetails
109: .getJndiReplyDestinationName()) ? (Destination) context
110: .lookup(addrDetails.getJndiReplyDestinationName())
111: : null;
112:
113: // create session factory to manage session, reply destination,
114: // producer and consumer pooling
115: //
116:
117: JMSSessionFactory sf = new JMSSessionFactory(connection,
118: replyDestination, context, jmsTransport, runtimePolicy);
119:
120: // notify transport that connection is complete
121: jmsTransport
122: .connected(requestDestination, replyDestination, sf);
123: }
124: }
|