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.naming.Reference;
27  import javax.resource.Referenceable;
28  import javax.resource.ResourceException;
29  import javax.resource.spi.ConnectionManager;
30  import java.io.Serializable;
31  
32  
33  /***
34   * @version $Revision: 1.6 $
35   */
36  public class ActiveMQConnectionFactory implements ConnectionFactory, Referenceable, Serializable {
37  
38      private static final Log log = LogFactory.getLog(ActiveMQConnectionFactory.class);
39      transient private ConnectionManager manager;
40      transient private ActiveMQManagedConnectionFactory factory;
41      private Reference reference;
42  	private final ActiveMQConnectionRequestInfo info;
43  
44  
45      /***
46       * @param factory
47       * @param manager
48       * @param info
49       */
50      public ActiveMQConnectionFactory(ActiveMQManagedConnectionFactory factory, ConnectionManager manager, ActiveMQConnectionRequestInfo info) {
51          this.factory = factory;
52          this.manager = manager;
53  		this.info = info;
54      }
55  
56      /***
57       * @see javax.jms.ConnectionFactory#createConnection()
58       */
59      public Connection createConnection() throws JMSException {
60          return createConnection(info.copy());
61      }
62  
63      /***
64       * @see javax.jms.ConnectionFactory#createConnection(java.lang.String, java.lang.String)
65       */
66      public Connection createConnection(String userName, String password) throws JMSException {
67          ActiveMQConnectionRequestInfo i = info.copy();
68          i.setUserName(userName);
69          i.setPassword(password);
70          return createConnection(i);
71      }
72  
73      /***
74       * @param info
75       * @return
76       * @throws JMSException
77       */
78      private Connection createConnection(ActiveMQConnectionRequestInfo info) throws JMSException {
79          try {
80              return (Connection) manager.allocateConnection(factory, info);
81          }
82          catch (ResourceException e) {
83              // Throw the root cause if it was a JMSException..
84              if (e.getCause() instanceof JMSException) {
85                  throw (JMSException) e.getCause();
86              }
87              log.debug("Connection could not be created:", e);
88              throw new JMSException(e.getMessage());
89          }
90      }
91  
92      /***
93       * @see javax.naming.Referenceable#getReference()
94       */
95      public Reference getReference() {
96          return reference;
97      }
98  
99      /***
100      * @see javax.resource.Referenceable#setReference(javax.naming.Reference)
101      */
102     public void setReference(Reference reference) {
103         this.reference = reference;
104     }
105 
106 }