001: /*
002: * $Id: Jms102bSupport.java 10489 2008-01-23 17:53:38Z dfeist $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010:
011: package org.mule.transport.jms;
012:
013: import javax.jms.Connection;
014: import javax.jms.ConnectionFactory;
015: import javax.jms.DeliveryMode;
016: import javax.jms.Destination;
017: import javax.jms.JMSException;
018: import javax.jms.Message;
019: import javax.jms.MessageConsumer;
020: import javax.jms.MessageProducer;
021: import javax.jms.Queue;
022: import javax.jms.QueueConnection;
023: import javax.jms.QueueConnectionFactory;
024: import javax.jms.QueueSender;
025: import javax.jms.QueueSession;
026: import javax.jms.Session;
027: import javax.jms.Topic;
028: import javax.jms.TopicConnection;
029: import javax.jms.TopicConnectionFactory;
030: import javax.jms.TopicPublisher;
031: import javax.jms.TopicSession;
032:
033: /**
034: * <code>Jms102bSupport</code> is a template class to provide an absstraction
035: * to to the Jms 1.0.2b api specification.
036: *
037: */
038:
039: public class Jms102bSupport extends Jms11Support {
040: public Jms102bSupport(JmsConnector connector) {
041: super (connector);
042: }
043:
044: public Connection createConnection(
045: ConnectionFactory connectionFactory, String username,
046: String password) throws JMSException {
047: if (connectionFactory == null) {
048: throw new IllegalArgumentException(
049: "connectionFactory cannot be null");
050: }
051: if (connectionFactory instanceof QueueConnectionFactory) {
052: return ((QueueConnectionFactory) connectionFactory)
053: .createQueueConnection(username, password);
054: } else if (connectionFactory instanceof TopicConnectionFactory) {
055: return ((TopicConnectionFactory) connectionFactory)
056: .createTopicConnection(username, password);
057: } else {
058: throw new IllegalArgumentException(
059: "Unsupported ConnectionFactory type: "
060: + connectionFactory.getClass().getName());
061: }
062: }
063:
064: public Connection createConnection(
065: ConnectionFactory connectionFactory) throws JMSException {
066: if (connectionFactory == null) {
067: throw new IllegalArgumentException(
068: "connectionFactory cannot be null");
069: }
070: if (connectionFactory instanceof QueueConnectionFactory) {
071: return ((QueueConnectionFactory) connectionFactory)
072: .createQueueConnection();
073: } else if (connectionFactory instanceof TopicConnectionFactory) {
074: return ((TopicConnectionFactory) connectionFactory)
075: .createTopicConnection();
076: } else {
077: throw new IllegalArgumentException(
078: "Unsupported ConnectionFactory type: "
079: + connectionFactory.getClass().getName());
080: }
081: }
082:
083: public Session createSession(Connection connection, boolean topic,
084: boolean transacted, int ackMode, boolean noLocal)
085: throws JMSException {
086: if (topic && connection instanceof TopicConnection) {
087: return ((TopicConnection) connection).createTopicSession(
088: noLocal, ackMode);
089: } else if (connection instanceof QueueConnection) {
090: // for transacted sessions the ackMode is always ignored, but
091: // set it for readability (SESSION_TRANSACTION is recommented
092: // for this case).
093: return ((QueueConnection) connection)
094: .createQueueSession(transacted,
095: (transacted ? Session.SESSION_TRANSACTED
096: : ackMode));
097: } else {
098: throw new IllegalArgumentException(
099: "Connection and domain type do not match");
100: }
101: }
102:
103: public MessageConsumer createConsumer(Session session,
104: Destination destination, String messageSelector,
105: boolean noLocal, String durableName, boolean topic)
106: throws JMSException {
107: if (topic && session instanceof TopicSession) {
108: if (durableName == null) {
109: return ((TopicSession) session).createSubscriber(
110: (Topic) destination, messageSelector, noLocal);
111: } else {
112: // DO NOT REMOVE THE CAST, breaks Weblogic
113: return ((TopicSession) session)
114: .createDurableSubscriber((Topic) destination,
115: durableName, messageSelector, noLocal);
116: }
117: } else if (session instanceof QueueSession) {
118: if (messageSelector != null) {
119: return ((QueueSession) session).createReceiver(
120: (Queue) destination, messageSelector);
121: } else {
122: return ((QueueSession) session)
123: .createReceiver((Queue) destination);
124: }
125: } else {
126: throw new IllegalArgumentException(
127: "MuleSession and domain type do not match");
128: }
129: }
130:
131: public MessageProducer createProducer(Session session,
132: Destination dest, boolean topic) throws JMSException {
133: if (topic && session instanceof TopicSession) {
134: return ((TopicSession) session)
135: .createPublisher((Topic) dest);
136: } else if (session instanceof QueueSession) {
137: return ((QueueSession) session).createSender((Queue) dest);
138: } else {
139: throw new IllegalArgumentException(
140: "MuleSession and domain type do not match");
141: }
142: }
143:
144: public Destination createDestination(Session session, String name,
145: boolean topic) throws JMSException {
146: if (session == null) {
147: throw new IllegalArgumentException(
148: "MuleSession cannot be null when creating a destination");
149: }
150: if (name == null) {
151: throw new IllegalArgumentException(
152: "Destination name cannot be null when creating a destination");
153: }
154:
155: if (topic) {
156: // DO NOT REMOVE THE CAST, BREAKS WEBLOGIC 8.X
157: return ((TopicSession) session).createTopic(name);
158: } else {
159: // DO NOT REMOVE THE CAST, BREAKS WEBLOGIC 8.X
160: return ((QueueSession) session).createQueue(name);
161: }
162: }
163:
164: public Destination createTemporaryDestination(Session session,
165: boolean topic) throws JMSException {
166: if (session == null) {
167: throw new IllegalArgumentException(
168: "MuleSession cannot be null when creating a destination");
169: }
170:
171: if (topic) {
172: // DO NOT REMOVE THE CAST, BREAKS WEBLOGIC 8.X
173: return ((TopicSession) session).createTemporaryTopic();
174: } else {
175: // DO NOT REMOVE THE CAST, BREAKS WEBLOGIC 8.X
176: return ((QueueSession) session).createTemporaryQueue();
177: }
178: }
179:
180: public void send(MessageProducer producer, Message message,
181: boolean persistent, int priority, long ttl, boolean topic)
182: throws JMSException {
183: if (topic && producer instanceof TopicPublisher) {
184: ((TopicPublisher) producer).publish(message,
185: (persistent ? DeliveryMode.PERSISTENT
186: : DeliveryMode.NON_PERSISTENT), priority,
187: ttl);
188: } else if (producer instanceof QueueSender) {
189: // DO NOT REMOVE THIS CAST, it breaks Weblogic
190: ((QueueSender) producer).send(message,
191: (persistent ? DeliveryMode.PERSISTENT
192: : DeliveryMode.NON_PERSISTENT), priority,
193: ttl);
194: } else {
195: throw new IllegalArgumentException(
196: "Producer and domain type do not match");
197: }
198: }
199:
200: public void send(MessageProducer producer, Message message,
201: Destination dest, boolean persistent, int priority,
202: long ttl, boolean topic) throws JMSException {
203: if (topic && producer instanceof TopicPublisher) {
204: ((TopicPublisher) producer).publish((Topic) dest, message,
205: (persistent ? DeliveryMode.PERSISTENT
206: : DeliveryMode.NON_PERSISTENT), priority,
207: ttl);
208: } else if (producer instanceof QueueSender) {
209: ((QueueSender) producer).send((Queue) dest, message,
210: (persistent ? DeliveryMode.PERSISTENT
211: : DeliveryMode.NON_PERSISTENT), priority,
212: ttl);
213: } else {
214: throw new IllegalArgumentException(
215: "Producer and domain type do not match");
216: }
217: }
218: }
|