001 /* 002 * Copyright (C) The Apache Software Foundation. All rights reserved. 003 * 004 * This software is published under the terms of the Apache Software License 005 * version 1.1, a copy of which has been included with this distribution in 006 * the LICENSE file. 007 * 008 * $Id: JNDISessionFactory.java 155459 2005-02-26 13:24:44Z dirkv $ 009 */ 010 package org.apache.commons.messenger; 011 012 import javax.jms.Connection; 013 import javax.jms.ConnectionFactory; 014 import javax.jms.JMSException; 015 import javax.jms.QueueConnection; 016 import javax.jms.QueueConnectionFactory; 017 import javax.jms.Session; 018 import javax.jms.TopicConnection; 019 import javax.jms.TopicConnectionFactory; 020 import javax.naming.Context; 021 import javax.naming.InitialContext; 022 import javax.naming.NamingException; 023 024 import org.apache.commons.logging.Log; 025 import org.apache.commons.logging.LogFactory; 026 027 028 /** <p><code>JNDISessionFactory</code> is a Factory of JMS Session objects 029 * which looks up the ConnectionFactory object from JNDI.</p> 030 * 031 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a> 032 * @version $Revision: 155459 $ 033 */ 034 public class JNDISessionFactory extends SessionFactory { 035 036 /** Logger */ 037 private static final Log log = LogFactory.getLog( JNDISessionFactory.class ); 038 039 /** the initial JNDI context used to lookup ConnectionFactory objects */ 040 public Context context; 041 042 /** the name used to lookup the ConnectionFactory */ 043 private String lookupName = "TopicConnectionFactory"; 044 045 046 // Properties 047 //------------------------------------------------------------------------- 048 049 /** The JNDI Name of the ConnectionFactory */ 050 public String getLookupName() { 051 return lookupName; 052 } 053 054 /** Sets the JNDI Name of the ConnectionFactory */ 055 public void setLookupName(String lookupName) { 056 this.lookupName = lookupName; 057 } 058 059 /** Returns the JNDI Context used to lookup JMS ConnectionFactory objects */ 060 public Context getContext() throws NamingException { 061 if ( context == null ) { 062 context = createContext(); 063 } 064 return context; 065 } 066 067 public void setContext(Context context) { 068 this.context = context; 069 } 070 071 // Implementation methods 072 //------------------------------------------------------------------------- 073 074 /** Factory method used to create a connection factory. 075 * Lookup the ConnectionFactory in JNDI 076 */ 077 protected ConnectionFactory createConnectionFactory() throws JMSException { 078 try { 079 if ( log.isInfoEnabled() ) { 080 log.info( "Looking up: " + getLookupName() + " in JNDI" ); 081 } 082 return (ConnectionFactory) getContext().lookup(getLookupName()); 083 } 084 catch (NamingException e) { 085 JMSException jmsException = new JMSException( "Failed to lookup: " + getLookupName() + " using JNDI. " + e ); 086 jmsException.setLinkedException(e); 087 throw jmsException; 088 } 089 } 090 091 /** Re-implemented from SessionFactory. Method used to create a connection */ 092 public Connection createConnection() throws JMSException { 093 ConnectionFactory factory = getConnectionFactory(); 094 095 if ( factory == null ) { 096 throw new JMSException( "No ConnectionFactory configured. Cannot create a JMS Session" ); 097 } 098 099 if ( isTopic() ) { 100 return createTopicConnection((TopicConnectionFactory) factory); 101 } 102 else { 103 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 if ( isTopic() ) { 111 TopicConnection topicConnection = (TopicConnection) connection; 112 return topicConnection.createTopicSession( isTransacted(), getAcknowledgeMode() ); 113 } 114 else { 115 QueueConnection queueConnection = (QueueConnection) connection; 116 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 if ( properties != null ) { 125 return new InitialContext( properties ); 126 } 127 else { 128 return new InitialContext(); 129 } 130 } 131 } 132