001: /*
002: * $Id: Jms11Support.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.Session;
022: import javax.jms.Topic;
023:
024: /**
025: * <code>Jms11Support</code> is a template class to provide an abstraction to to
026: * the JMS 1.1 API specification.
027: */
028:
029: public class Jms11Support implements JmsSupport {
030: protected JmsConnector connector;
031:
032: public Jms11Support(JmsConnector connector) {
033: this .connector = connector;
034: }
035:
036: public Connection createConnection(
037: ConnectionFactory connectionFactory, String username,
038: String password) throws JMSException {
039: if (connectionFactory == null) {
040: throw new IllegalArgumentException(
041: "connectionFactory cannot be null");
042: }
043: return connectionFactory.createConnection(username, password);
044: }
045:
046: public Connection createConnection(
047: ConnectionFactory connectionFactory) throws JMSException {
048: if (connectionFactory == null) {
049: throw new IllegalArgumentException(
050: "connectionFactory cannot be null");
051: }
052: return connectionFactory.createConnection();
053: }
054:
055: public Session createSession(Connection connection, boolean topic,
056: boolean transacted, int ackMode, boolean noLocal)
057: throws JMSException {
058: return connection.createSession(transacted,
059: (transacted ? Session.SESSION_TRANSACTED : ackMode));
060: }
061:
062: public MessageProducer createProducer(Session session,
063: Destination destination, boolean topic) throws JMSException {
064: return session.createProducer(destination);
065: }
066:
067: public MessageConsumer createConsumer(Session session,
068: Destination destination, boolean topic) throws JMSException {
069: return createConsumer(session, destination, null, false, null,
070: topic);
071: }
072:
073: public MessageConsumer createConsumer(Session session,
074: Destination destination, String messageSelector,
075: boolean noLocal, String durableName, boolean topic)
076: throws JMSException {
077: if (durableName == null) {
078: if (topic) {
079: return session.createConsumer(destination,
080: messageSelector, noLocal);
081: } else {
082: return session.createConsumer(destination,
083: messageSelector);
084: }
085: } else {
086: if (topic) {
087: return session.createDurableSubscriber(
088: (Topic) destination, durableName,
089: messageSelector, noLocal);
090: } else {
091: throw new JMSException(
092: "A durable subscriber name was set but the destination was not a Topic");
093: }
094: }
095: }
096:
097: public Destination createDestination(Session session, String name,
098: boolean topic) throws JMSException {
099: if (session == null) {
100: throw new IllegalArgumentException(
101: "MuleSession cannot be null when creating a destination");
102: }
103: if (name == null) {
104: throw new IllegalArgumentException(
105: "Destination name cannot be null when creating a destination");
106: }
107:
108: if (topic) {
109: return session.createTopic(name);
110: } else {
111: return session.createQueue(name);
112: }
113: }
114:
115: public Destination createTemporaryDestination(Session session,
116: boolean topic) throws JMSException {
117: if (session == null) {
118: throw new IllegalArgumentException(
119: "MuleSession cannot be null when creating a destination");
120: }
121:
122: if (topic) {
123: return session.createTemporaryTopic();
124: } else {
125: return session.createTemporaryQueue();
126: }
127: }
128:
129: public void send(MessageProducer producer, Message message,
130: boolean topic) throws JMSException {
131: send(producer, message, connector.isPersistentDelivery(),
132: Message.DEFAULT_PRIORITY, Message.DEFAULT_TIME_TO_LIVE,
133: topic);
134: }
135:
136: public void send(MessageProducer producer, Message message,
137: Destination dest, boolean topic) throws JMSException {
138: send(producer, message, dest, connector.isPersistentDelivery(),
139: Message.DEFAULT_PRIORITY, Message.DEFAULT_TIME_TO_LIVE,
140: topic);
141: }
142:
143: public void send(MessageProducer producer, Message message,
144: boolean persistent, int priority, long ttl, boolean topic)
145: throws JMSException {
146: producer.send(message, (persistent ? DeliveryMode.PERSISTENT
147: : DeliveryMode.NON_PERSISTENT), priority, ttl);
148: }
149:
150: public void send(MessageProducer producer, Message message,
151: Destination dest, boolean persistent, int priority,
152: long ttl, boolean topic) throws JMSException {
153: producer.send(dest, message,
154: (persistent ? DeliveryMode.PERSISTENT
155: : DeliveryMode.NON_PERSISTENT), priority, ttl);
156: }
157:
158: }
|