Hi

I've patched up the code to add the new flag (topic). Find attached 
JNDISessionFactory.java and a jar containing the updated Messenger config 
files. I'm also testing OpenJMS and SwiftMQ so a config for those two will 
arrive shortly.

Much better now... ;)


Saimon

Attachment: configs.jar
Description: Zip archive

/*
 * Copyright (C) The Apache Software Foundation. All rights reserved.
 *
 * This software is published under the terms of the Apache Software License
 * version 1.1, a copy of which has been included with this distribution in
 * the LICENSE file.
 *
 * $Id: JNDISessionFactory.java,v 1.3 2001/08/29 10:31:55 jstrachan Exp $
 */
package org.apache.commons.messenger;

import java.io.Serializable;
import java.util.Properties;

import javax.jms.ConnectionFactory;
import javax.jms.Connection;
import javax.jms.Session;
import javax.jms.TopicConnection;
import javax.jms.QueueConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.QueueConnectionFactory;
import javax.jms.JMSException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.naming.NamingEnumeration;


/** <p><code>JNDISessionFactory</code> is a Factory of JMS Session objects
  * which looks up the ConnectionFactory object from JNDI.</p>
  *
  * @author <a href="mailto:[EMAIL PROTECTED]";>James Strachan</a>
  * @version $Revision: 1.3 $
  */
public class JNDISessionFactory extends SessionFactory {

    /** the initial JNDI context used to lookup ConnectionFactory objects */
    public Context context;

    /** the name used to lookup the ConnectionFactory */
    private String lookupName = "TopicConnectionFactory";

    /** Wether Topics or Queue Connections should be forced */
    private boolean topic = false;

    // Properties
    //-------------------------------------------------------------------------

    /** The JNDI Name of the ConnectionFactory */
    public String getLookupName() {
        return lookupName;
    }

    /** Sets the JNDI Name of the ConnectionFactory */
    public void setLookupName(String lookupName) {
        this.lookupName = lookupName;
    }

    /** To force Topic or Queue Connections */
    public void setTopic(boolean topic) {
    this.topic = topic;
    }

    /** To force Topic or Queue Connections */
    public boolean isTopic() {
        return topic;
    }

    /** Returns the JNDI Context used to lookup JMS ConnectionFactory objects */
    public Context getContext() throws NamingException {
        if ( context == null ) {
            context = createContext();
        }
        return context;
    }

    public void setContext(Context context) {
        this.context = context;
    }

    // Implementation methods
    //-------------------------------------------------------------------------

    /** Factory method used to create a connection factory.
      * Lookup the ConnectionFactory in JNDI
      */
    protected ConnectionFactory createConnectionFactory() throws JMSException {
        try {
            System.out.println( "Looking up: " + getLookupName() + " in JNDI" );

            if (!topic) {
                return (QueueConnectionFactory) getContext().lookup(getLookupName());
            }

            else return (TopicConnectionFactory) getContext().lookup(getLookupName());

        }
        catch (NamingException e) {
            JMSException jmsException = new JMSException( "Failed to lookup: " + getLookupName() + " using JNDI. " + e );
            jmsException.setLinkedException(e);
            throw jmsException;
        }
    }

    /** Re-implemented from SessionFactory. Method used to create a connection */
    public Connection createConnection() throws JMSException {
        ConnectionFactory factory = getConnectionFactory();

        if ( factory == null ) {
            throw new JMSException( "No ConnectionFactory configured. Cannot create a JMS Session" );
        }

        if (!topic) {
            return createQueueConnection((QueueConnectionFactory) factory);
        }

        else return createTopicConnection((TopicConnectionFactory) factory);
    }

    /** Re-implemented from SessionFactory. Creates a new Session instance */
    public Session createSession(Connection connection) throws JMSException {

         if (!topic) {
                QueueConnection queueConnection = (QueueConnection) connection;
                return queueConnection.createQueueSession( isTransacted(), getAcknowledgeMode() );
            }

           else {
                TopicConnection topicConnection = (TopicConnection) connection;
                return topicConnection.createTopicSession( isTransacted(), getAcknowledgeMode() );
            }
    }

    /** Factory method used to create a connection factory.
      * Derived classes may wish to use JNDI to load the ConnectionFactory
      */
    protected Context createContext() throws NamingException {
        if ( properties != null ) {
            return new InitialContext( properties );
        }
        else {
            return new InitialContext();
        }
    }
}

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to