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

org.ow2.petals.bc.jms.connection.AbstractJMSConnection Maven / Gradle / Ivy

There is a newer version: 3.3.0
Show newest version
/**
 * Copyright (c) 2005-2012 EBM WebSourcing, 2012-2016 Linagora
 * 
 * This program/library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 2.1 of the License, or (at your
 * option) any later version.
 * 
 * This program/library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program/library; If not, see http://www.gnu.org/licenses/
 * for the GNU Lesser General Public License version 2.1.
 */
package org.ow2.petals.bc.jms.connection;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.JMSSecurityException;
import javax.jms.Session;
import javax.naming.Context;
import javax.naming.NamingException;

import org.ow2.petals.component.framework.api.exception.PEtALSCDKException;

import com.ebmwebsourcing.easycommons.lang.StringHelper;

/**
 * This class is used to create a JMS consumer or producer.
 * @author Olivier Fabre - EBM WebSourcing
 * @author Adrien Louis - EBM WebSourcing
 */
public abstract class AbstractJMSConnection {

    private Context context;

    private String connectionFactoryName;

    private String destinationName;

    private String user;

    private String password;

    private Connection connection;

    protected Session session;

    protected Destination destination;

    private boolean transacted;

    /**
     * Constructor. All required information to create the producer or consumer
     * are set here.
     * 
     * @param jndiContext
     *            the context where the connectionFactory and the destination
     *            can be retrieved.
     * @param connectionFactoryName
     *            name of the jms connectionfactory
     * @param destinationName
     *            name of the destination (queue or topic)
     * @param transacted
     *            created a transacted session or not
     * @param user
     *            user to use to connect to the connection factory if required
     * @param password
     *            password to use to connect to the connection factory if
     *            required
     */
    public AbstractJMSConnection(Context jndiContext, String connectionFactoryName,
            String destinationName, boolean transacted, String user, String password) {
        this.context = jndiContext;
        this.connectionFactoryName = connectionFactoryName;
        this.destinationName = destinationName;
        this.user = user;
        this.password = password;
        this.transacted = transacted;
    }

    /**
     * Retrieve the connection factory and the destination from the JNDI
     * context. Create a connection, using user/pwd if required, but do not
     * start it. Create the jms session using connection. Create a
     * consumer/producer, using the specified transacted mode.
     * 
     * @throws TechnicalException
     */
    protected void init() throws PEtALSCDKException {

        ConnectionFactory connFact = AbstractJMSConnection.lookupConnectionFactory(this.context,
                this.connectionFactoryName);

        this.destination = AbstractJMSConnection.lookupDestination(this.context,
                this.destinationName);

        this.connection = AbstractJMSConnection
                .createConnection(connFact, this.user, this.password);

        try {
            this.session = this.connection.createSession(this.transacted, Session.AUTO_ACKNOWLEDGE);
        } catch (JMSException e) {
            throw new PEtALSCDKException("Can not JMS session", e);
        }

        createJMSConsumerOrProducer();
    }

    /**
     * Start the jms connection.
     * 
     * @throws TechnicalException
     *             jmsException on starting connection
     */
    public void start() throws PEtALSCDKException {
        try {
            this.connection.start();
        } catch (JMSException e) {
            throw new PEtALSCDKException("Can not start the JMS connection.", e);
        }
    }

    public abstract void createJMSConsumerOrProducer() throws PEtALSCDKException;

    /**
     * Close messageConsumer/messageProducer, session and connection.
     * 
     * @throws TechnicalException
     *             JmsException on connection stopping
     */
    public void stop() throws PEtALSCDKException {
        try {
            connection.stop();
        } catch (JMSException e) {
            throw new PEtALSCDKException("Can not stop the JMS connection.", e);
        }
    }

    /**
     * Close messageConsumer/messageProducer, session and connection.
     * 
     * @throws TechnicalException
     *             JmsException on connection closing
     */
    public void close() throws PEtALSCDKException {
        try {
            connection.close();
        } catch (JMSException e) {
            throw new PEtALSCDKException("Can not close the JMS connection.", e);
        }
    }

    public Session getSession() {
        return this.session;
    }

    public String getDestinationName() {
        return this.destinationName;
    }

    public boolean isTransacted() {
        return this.transacted;
    }

    /**
     * Retrieve the connectionFactory in the JNDI context
     * 
     * @param ctx
     *            must be connected, must be non null
     * @param connectionFactoryName
     *            must be non null
     * @return the connectionFactory
     * @throws TechnicalException
     *             the factory is not found
     */
    private static ConnectionFactory lookupConnectionFactory(Context ctx,
            String connectionFactoryName) throws PEtALSCDKException {
        ConnectionFactory connFact = null;
        try {
            connFact = (ConnectionFactory) ctx.lookup(connectionFactoryName);
        } catch (NamingException e) {
            throw new PEtALSCDKException("ConnectionFactory '" + connectionFactoryName
                    + "' not found in the JNDI context.", e);
        }
        return connFact;
    }

    /**
     * Retrieve the destination in the JNDI context
     * 
     * @param ctx
     *            must be connected, must be non null
     * @param destination
     *            must be non null
     * @return the destination
     * @throws TechnicalException
     *             the destination is not found
     */
    private static Destination lookupDestination(Context ctx, String destinationName)
            throws PEtALSCDKException {
        Destination dest = null;

        try {
            dest = (Destination) ctx.lookup(destinationName);
        } catch (NamingException e) {
            throw new PEtALSCDKException("Destination '" + destinationName
                    + "' not found in the JNDI context.", e);
        }
        return dest;
    }

    /**
     * Create a JMS Connection.
* If user is null, call a connectionFactory.createConnection() * .
* If user is not null, call a * connectionFactory.createConnection(user,password). * * @param connectionFactory * must be non null * @param user * can be null * @param password * can be null * @return a JMS connection * @throws TechnicalException * JMS security exception or connectionfactory exception */ private static Connection createConnection(ConnectionFactory connectionFactory, String user, String password) throws PEtALSCDKException { Connection conn = null; try { if (StringHelper.isNullOrEmpty(user)) { conn = connectionFactory.createConnection(); } else { conn = connectionFactory.createConnection(user, password); } } catch (JMSSecurityException e) { throw new PEtALSCDKException( "Invalid user/password to connect to the JMS connectionFactory.", e); } catch (JMSException e) { throw new PEtALSCDKException("JMS connectionFactory error.", e); } return conn; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy