View Javadoc

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  public class JNDISessionFactory extends SessionFactory {
35  
36      /** Logger */
37      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      private String lookupName = "TopicConnectionFactory";
44  
45  
46      // Properties
47      //-------------------------------------------------------------------------
48  
49      /** The JNDI Name of the ConnectionFactory */
50      public String getLookupName() {
51          return lookupName;
52      }
53  
54      /** Sets the JNDI Name of the ConnectionFactory */
55      public void setLookupName(String lookupName) {
56          this.lookupName = lookupName;
57      }
58  
59      /** Returns the JNDI Context used to lookup JMS ConnectionFactory objects */
60      public Context getContext() throws NamingException {
61          if ( context == null ) {
62              context = createContext();
63          }
64          return context;
65      }
66  
67      public void setContext(Context context) {
68          this.context = context;
69      }
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              if ( log.isInfoEnabled() ) {
80                  log.info( "Looking up: " + getLookupName() + " in JNDI" );
81              }
82              return (ConnectionFactory) getContext().lookup(getLookupName());
83          }
84          catch (NamingException e) {
85              JMSException jmsException = new JMSException( "Failed to lookup: " + getLookupName() + " using JNDI. " + e );
86              jmsException.setLinkedException(e);
87              throw jmsException;
88          }
89      }
90  
91      /** Re-implemented from SessionFactory. Method used to create a connection */
92      public Connection createConnection() throws JMSException {
93          ConnectionFactory factory = getConnectionFactory();
94  
95          if ( factory == null ) {
96              throw new JMSException( "No ConnectionFactory configured. Cannot create a JMS Session" );
97          }
98  
99          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