All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.mule.providers.jms.Jms11Support Maven / Gradle / Ivy

The newest version!
/*
 * $Id: Jms11Support.java 7963 2007-08-21 08:53:15Z dirk.olmes $
 * --------------------------------------------------------------------------------------
 * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
 *
 * The software in this package is published under the terms of the CPAL v1.0
 * license, a copy of which has been included with this distribution in the
 * LICENSE.txt file.
 */
/**
 * Copyright 2012 Bull S.A.S.
 *
 * Licensed  under the  Apache License,  Version 2.0  (the "License");
 * you may not use  this file  except in  compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
// Jasmine: Add traces

package org.mule.providers.jms;

import java.text.MessageFormat;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.Topic;
import javax.naming.Context;
import javax.naming.NamingException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Jms11Support is a template class to provide an abstraction to to
 * the JMS 1.1 API specification.
 */

public class Jms11Support implements JmsSupport {
    protected Context context;
    protected boolean jndiDestinations = false;
    protected boolean forceJndiDestinations = false;
    protected JmsConnector connector;
    protected final static Log logger = LogFactory.getLog(Jms11Support.class);

    public Jms11Support(JmsConnector connector,
                        Context context,
                        boolean jndiDestinations,
                        boolean forceJndiDestinations) {
        this.connector = connector;
        this.context = context;
        this.jndiDestinations = jndiDestinations;
        this.forceJndiDestinations = forceJndiDestinations;
    }

    /**
     * Create a Connection for a specified user
     * @param connectionFactory
     * @param username
     * @param password
     * @return
     * @throws JMSException
     */
    public Connection createConnection(ConnectionFactory connectionFactory, String username, String password)
            throws JMSException {
        logger.debug("");
        if (connectionFactory == null) {
            throw new IllegalArgumentException("connectionFactory cannot be null");
        }
        return connectionFactory.createConnection(username, password);
    }

    /**
     * Create a Connection
     * @param connectionFactory
     * @return
     * @throws JMSException
     */
    public Connection createConnection(ConnectionFactory connectionFactory) throws JMSException {
        logger.debug("");
        if (connectionFactory == null) {
            throw new IllegalArgumentException("connectionFactory cannot be null");
        }
        return connectionFactory.createConnection();
    }

    /**
     * Create a Session on this Connection
     * @param connection
     * @param topic  NOT USED
     * @param transacted
     * @param ackMode
     * @param noLocal NOT USED
     * @return
     * @throws JMSException
     */
    public Session createSession(Connection connection,
                                 boolean topic,
                                 boolean transacted,
                                 int ackMode,
                                 boolean noLocal) throws JMSException {
        logger.debug("");
        return connection.createSession(transacted, (transacted ? Session.SESSION_TRANSACTED : ackMode));
    }

    /**
     *
     * @param session
     * @param destination
     * @param topic NOT USED
     * @return
     * @throws JMSException
     */
    public MessageProducer createProducer(Session session, Destination destination, boolean topic)
            throws JMSException {
        logger.debug("");
        return session.createProducer(destination);
    }

    /**
     *
     * @param session
     * @param destination
     * @param topic
     * @return
     * @throws JMSException
     */
    public MessageConsumer createConsumer(Session session, Destination destination, boolean topic)
            throws JMSException {
        logger.debug("");
        return createConsumer(session, destination, null, false, null, topic);
    }

    /**
     *
     * @param session
     * @param destination
     * @param messageSelector
     * @param noLocal
     * @param durableName
     * @param topic
     * @return
     * @throws JMSException
     */
    public MessageConsumer createConsumer(Session session,
                                          Destination destination,
                                          String messageSelector,
                                          boolean noLocal,
                                          String durableName,
                                          boolean topic) throws JMSException {
        logger.debug("");
        if (durableName == null) {
            if (topic) {
                return session.createConsumer(destination, messageSelector, noLocal);
            } else {
                return session.createConsumer(destination, messageSelector);
            }
        } else {
            if (topic) {
                return session.createDurableSubscriber((Topic) destination, durableName, messageSelector,
                        noLocal);
            } else {
                throw new JMSException(
                        "A durable subscriber name was set but the destination was not a Topic");
            }
        }
    }

    /**
     *
     * @param session
     * @param name
     * @param topic
     * @return
     * @throws JMSException
     */
    public Destination createDestination(Session session, String name, boolean topic) throws JMSException {
        logger.debug("");
        if (session == null) {
            throw new IllegalArgumentException("Session cannot be null when creating a destination");
        }
        if (name == null) {
            throw new IllegalArgumentException("Destination name cannot be null when creating a destination");
        }

        if (jndiDestinations) {
            if (context == null) {
                throw new IllegalArgumentException(
                        "Jndi Context name cannot be null when looking up a destination");
            }
            Destination dest = getJndiDestination(name);
            if (dest != null) {
                return dest;
            } else if (forceJndiDestinations) {
                throw new JMSException("JNDI destination not found with name: " + name);
            }
        }

        if (topic) {
            return session.createTopic(name);
        } else {
            return session.createQueue(name);
        }
    }

    /**
     *
     * @param name
     * @return
     * @throws JMSException
     */
    protected Destination getJndiDestination(String name) throws JMSException {
        logger.debug("");
        Object temp;
        try {
            temp = context.lookup(name);
        } catch (NamingException e) {
            throw new JMSException(MessageFormat.format("Failed to look up destination {0}. Reason: {1}",
                    new Object[]{name, e.getMessage()}));
        }
        if (temp != null) {
            if (temp instanceof Destination) {
                return (Destination) temp;
            } else if (forceJndiDestinations) {
                throw new JMSException("JNDI destination not found with name: " + name);
            }
        }
        return null;
    }

    /**
     *
     * @param session
     * @param topic
     * @return
     * @throws JMSException
     */
    public Destination createTemporaryDestination(Session session, boolean topic) throws JMSException {
        logger.debug("");
        if (session == null) {
            throw new IllegalArgumentException("Session cannot be null when creating a destination");
        }

        if (topic) {
            return session.createTemporaryTopic();
        } else {
            return session.createTemporaryQueue();
        }
    }

    /**
     *
     * @param producer
     * @param message
     * @param topic NOT USED
     * @throws JMSException
     */
    public void send(MessageProducer producer, Message message, boolean topic) throws JMSException {
        logger.debug("");
        send(producer, message, connector.isPersistentDelivery(), Message.DEFAULT_PRIORITY,
                Message.DEFAULT_TIME_TO_LIVE, topic);
    }

    /**
     *
     * @param producer
     * @param message
     * @param dest
     * @param topic NOT USED
     * @throws JMSException
     */
    public void send(MessageProducer producer, Message message, Destination dest, boolean topic)
            throws JMSException {
        logger.debug("");
        send(producer, message, dest, connector.isPersistentDelivery(), Message.DEFAULT_PRIORITY,
                Message.DEFAULT_TIME_TO_LIVE, topic);
    }

    /**
     *
     * @param producer
     * @param message
     * @param persistent
     * @param priority
     * @param ttl
     * @param topic NOT USED
     * @throws JMSException
     */
    public void send(MessageProducer producer,
                     Message message,
                     boolean persistent,
                     int priority,
                     long ttl,
                     boolean topic) throws JMSException {
        logger.debug("");
        producer.send(message, (persistent ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT),
                priority, ttl);
    }

    /**
     * Use the producer to send a message on a destination
     * @param producer
     * @param message
     * @param dest
     * @param persistent
     * @param priority
     * @param ttl
     * @param topic NOT USED
     * @throws JMSException
     */
    public void send(MessageProducer producer,
                     Message message,
                     Destination dest,
                     boolean persistent,
                     int priority,
                     long ttl,
                     boolean topic) throws JMSException {
        logger.debug("");
        producer.send(dest, message, (persistent ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT),
                priority, ttl);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy