View Javadoc

    1   /***
    2    *
    3    * Copyright 2004 Hiram Chirino
    4    *
    5    * Licensed under the Apache License, Version 2.0 (the "License");
    6    * you may not use this file except in compliance with the License.
    7    * You may obtain a copy of the License at
    8    *
    9    * http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   **/
    18  package org.codehaus.activemq.ra;
    19
    20  import org.apache.commons.logging.Log;
    21  import org.apache.commons.logging.LogFactory;
    22
    23  import javax.jms.Connection;
    24  import javax.jms.ConnectionFactory;
    25  import javax.jms.JMSException;
    26  import javax.jms.QueueConnectionFactory;
    27  import javax.jms.QueueConnection;
    28  import javax.jms.TopicConnectionFactory;
    29  import javax.jms.TopicConnection;
    30  import javax.naming.Reference;
    31  import javax.resource.Referenceable;
    32  import javax.resource.ResourceException;
    33  import javax.resource.spi.ConnectionManager;
    34  import java.io.Serializable;
    35
    36
    37  /***
    38   * @version $Revision: 1.8 $
    39   */
    40  public class ActiveMQConnectionFactory implements ConnectionFactory, QueueConnectionFactory, TopicConnectionFactory, Referenceable, Serializable {
    41
    42      private static final long serialVersionUID = -5754338187296859149L;
    43
    44      private static final Log log = LogFactory.getLog(ActiveMQConnectionFactory.class);
    45      transient private ConnectionManager manager;
    46      transient private ActiveMQManagedConnectionFactory factory;
    47      private Reference reference;
    48      private final ActiveMQConnectionRequestInfo info;
    49
    50
    51      /***
    52       * @param factory
    53       * @param manager
    54       * @param info
    55       */
    56      public ActiveMQConnectionFactory(ActiveMQManagedConnectionFactory factory, ConnectionManager manager, ActiveMQConnectionRequestInfo info) {
    57          this.factory = factory;
    58          this.manager = manager;
    59          this.info = info;
    60      }
    61
    62      /***
    63       * @see javax.jms.ConnectionFactory#createConnection()
    64       */
    65      public Connection createConnection() throws JMSException {
    66          return createConnection(info.copy());
    67      }
    68
    69      /***
    70       * @see javax.jms.ConnectionFactory#createConnection(java.lang.String, java.lang.String)
    71       */
    72      public Connection createConnection(String userName, String password) throws JMSException {
    73          ActiveMQConnectionRequestInfo i = info.copy();
    74          i.setUserName(userName);
    75          i.setPassword(password);
    76          return createConnection(i);
    77      }
    78
    79      /***
    80       * @param info
    81       * @return
    82       * @throws JMSException
    83       */
    84      private Connection createConnection(ActiveMQConnectionRequestInfo info) throws JMSException {
    85          try {
    86              return (Connection) manager.allocateConnection(factory, info);
    87          }
    88          catch (ResourceException e) {
    89              // Throw the root cause if it was a JMSException..
    90              if (e.getCause() instanceof JMSException) {
    91                  throw (JMSException) e.getCause();
    92              }
    93              log.debug("Connection could not be created:", e);
    94              throw new JMSException(e.getMessage());
    95          }
    96      }
    97
    98      /***
    99       * @see javax.naming.Referenceable#getReference()
    100      */
    101     public Reference getReference() {
    102         return reference;
    103     }
    104
    105     /***
    106      * @see javax.resource.Referenceable#setReference(javax.naming.Reference)
    107      */
    108     public void setReference(Reference reference) {
    109         this.reference = reference;
    110     }
    111
    112     public QueueConnection createQueueConnection() throws JMSException {
    113         return (QueueConnection) createConnection();
    114     }
    115
    116     public QueueConnection createQueueConnection(String userName, String password) throws JMSException {
    117         return (QueueConnection) createConnection(userName, password);
    118     }
    119
    120     public TopicConnection createTopicConnection() throws JMSException {
    121         return (TopicConnection) createConnection();
    122     }
    123
    124     public TopicConnection createTopicConnection(String userName, String password) throws JMSException {
    125         return (TopicConnection) createConnection(userName, password);
    126     }
    127 }