Coverage Report - org.apache.commons.messenger.JNDISessionFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
JNDISessionFactory
0%
0/32
0%
0/12
2.625
 
 1  
 /*
 2  
  * Copyright (C) The Apache Software Foundation. All rights reserved.
 3  
  *
 4  
  * This software is published under the terms of the Apache Software License
 5  
  * version 1.1, a copy of which has been included with this distribution in
 6  
  * the LICENSE file.
 7  
  *
 8  
  * $Id: JNDISessionFactory.java 155459 2005-02-26 13:24:44Z dirkv $
 9  
  */
 10  
 package org.apache.commons.messenger;
 11  
 
 12  
 import javax.jms.Connection;
 13  
 import javax.jms.ConnectionFactory;
 14  
 import javax.jms.JMSException;
 15  
 import javax.jms.QueueConnection;
 16  
 import javax.jms.QueueConnectionFactory;
 17  
 import javax.jms.Session;
 18  
 import javax.jms.TopicConnection;
 19  
 import javax.jms.TopicConnectionFactory;
 20  
 import javax.naming.Context;
 21  
 import javax.naming.InitialContext;
 22  
 import javax.naming.NamingException;
 23  
 
 24  
 import org.apache.commons.logging.Log;
 25  
 import org.apache.commons.logging.LogFactory;
 26  
 
 27  
 
 28  
 /** <p><code>JNDISessionFactory</code> is a Factory of JMS Session objects
 29  
   * which looks up the ConnectionFactory object from JNDI.</p>
 30  
   *
 31  
   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
 32  
   * @version $Revision: 155459 $
 33  
   */
 34  0
 public class JNDISessionFactory extends SessionFactory {
 35  
 
 36  
     /** Logger */
 37  0
     private static final Log log = LogFactory.getLog( JNDISessionFactory.class );
 38  
     
 39  
     /** the initial JNDI context used to lookup ConnectionFactory objects */
 40  
     public Context context;
 41  
 
 42  
     /** the name used to lookup the ConnectionFactory */
 43  0
     private String lookupName = "TopicConnectionFactory";
 44  
 
 45  
 
 46  
     // Properties
 47  
     //-------------------------------------------------------------------------
 48  
 
 49  
     /** The JNDI Name of the ConnectionFactory */
 50  
     public String getLookupName() {
 51  0
         return lookupName;
 52  
     }
 53  
 
 54  
     /** Sets the JNDI Name of the ConnectionFactory */
 55  
     public void setLookupName(String lookupName) {
 56  0
         this.lookupName = lookupName;
 57  0
     }
 58  
 
 59  
     /** Returns the JNDI Context used to lookup JMS ConnectionFactory objects */
 60  
     public Context getContext() throws NamingException {
 61  0
         if ( context == null ) {
 62  0
             context = createContext();
 63  
         }
 64  0
         return context;
 65  
     }
 66  
 
 67  
     public void setContext(Context context) {
 68  0
         this.context = context;
 69  0
     }
 70  
 
 71  
     // Implementation methods
 72  
     //-------------------------------------------------------------------------
 73  
 
 74  
     /** Factory method used to create a connection factory.
 75  
       * Lookup the ConnectionFactory in JNDI
 76  
       */
 77  
     protected ConnectionFactory createConnectionFactory() throws JMSException {
 78  
         try {
 79  0
             if ( log.isInfoEnabled() ) {
 80  0
                 log.info( "Looking up: " + getLookupName() + " in JNDI" );
 81  
             }
 82  0
             return (ConnectionFactory) getContext().lookup(getLookupName());
 83  
         }
 84  0
         catch (NamingException e) {
 85  0
             JMSException jmsException = new JMSException( "Failed to lookup: " + getLookupName() + " using JNDI. " + e );
 86  0
             jmsException.setLinkedException(e);
 87  0
             throw jmsException;
 88  
         }
 89  
     }
 90  
 
 91  
     /** Re-implemented from SessionFactory. Method used to create a connection */
 92  
     public Connection createConnection() throws JMSException {
 93  0
         ConnectionFactory factory = getConnectionFactory();
 94  
 
 95  0
         if ( factory == null ) {
 96  0
             throw new JMSException( "No ConnectionFactory configured. Cannot create a JMS Session" );
 97  
         }
 98  
 
 99  0
         if ( isTopic() ) {
 100  0
             return createTopicConnection((TopicConnectionFactory) factory);
 101  
         }
 102  
         else {
 103  0
             return createQueueConnection((QueueConnectionFactory) factory);
 104  
         }
 105  
     }
 106  
 
 107  
     /** Re-implemented from SessionFactory. Creates a new Session instance */
 108  
     public Session createSession(Connection connection) throws JMSException {
 109  
 
 110  0
         if ( isTopic() ) {
 111  0
             TopicConnection topicConnection = (TopicConnection) connection;
 112  0
             return topicConnection.createTopicSession( isTransacted(), getAcknowledgeMode() );
 113  
         }
 114  
         else {
 115  0
             QueueConnection queueConnection = (QueueConnection) connection;
 116  0
             return queueConnection.createQueueSession( isTransacted(), getAcknowledgeMode() );
 117  
         }
 118  
     }
 119  
 
 120  
     /** Factory method used to create a connection factory.
 121  
       * Derived classes may wish to use JNDI to load the ConnectionFactory
 122  
       */
 123  
     protected Context createContext() throws NamingException {
 124  0
         if ( properties != null ) {
 125  0
             return new InitialContext( properties );
 126  
         }
 127  
         else {
 128  0
             return new InitialContext();
 129  
         }
 130  
     }
 131  
 }
 132