001 /** 002 * 003 * Copyright 2004 Hiram Chirino 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 * 017 **/ 018 package org.activemq.ra; 019 020 import org.activemq.ra.jms.ConnectionProxy; 021 import org.apache.commons.logging.Log; 022 import org.apache.commons.logging.LogFactory; 023 024 import javax.jms.Connection; 025 import javax.jms.ConnectionFactory; 026 import javax.jms.JMSException; 027 import javax.jms.QueueConnectionFactory; 028 import javax.jms.QueueConnection; 029 import javax.jms.TopicConnectionFactory; 030 import javax.jms.TopicConnection; 031 import javax.naming.Reference; 032 import javax.resource.Referenceable; 033 import javax.resource.ResourceException; 034 import javax.resource.spi.ConnectionManager; 035 import java.io.Serializable; 036 037 038 /** 039 * @version $Revision: 1.1.1.1 $ 040 */ 041 public class ActiveMQConnectionFactory implements ConnectionFactory, QueueConnectionFactory, TopicConnectionFactory, Referenceable, Serializable { 042 043 private static final long serialVersionUID = -5754338187296859149L; 044 045 private static final Log log = LogFactory.getLog(ActiveMQConnectionFactory.class); 046 transient private ConnectionManager manager; 047 transient private ActiveMQManagedConnectionFactory factory; 048 private Reference reference; 049 private final ActiveMQConnectionRequestInfo info; 050 051 052 /** 053 * @param factory 054 * @param manager 055 * @param info 056 */ 057 public ActiveMQConnectionFactory(ActiveMQManagedConnectionFactory factory, ConnectionManager manager, ActiveMQConnectionRequestInfo info) { 058 this.factory = factory; 059 this.manager = manager; 060 this.info = info; 061 } 062 063 /** 064 * @see javax.jms.ConnectionFactory#createConnection() 065 */ 066 public Connection createConnection() throws JMSException { 067 return createConnection(info.copy()); 068 } 069 070 /** 071 * @see javax.jms.ConnectionFactory#createConnection(java.lang.String, java.lang.String) 072 */ 073 public Connection createConnection(String userName, String password) throws JMSException { 074 ActiveMQConnectionRequestInfo i = info.copy(); 075 i.setUserName(userName); 076 i.setPassword(password); 077 return createConnection(i); 078 } 079 080 /** 081 * @param info 082 * @return 083 * @throws JMSException 084 */ 085 private Connection createConnection(ActiveMQConnectionRequestInfo info) throws JMSException { 086 try { 087 if( info.isUseInboundSessionEnabled() ) { 088 return new ConnectionProxy(); 089 } 090 return (Connection) manager.allocateConnection(factory, info); 091 } 092 catch (ResourceException e) { 093 // Throw the root cause if it was a JMSException.. 094 if (e.getCause() instanceof JMSException) { 095 throw (JMSException) e.getCause(); 096 } 097 log.debug("Connection could not be created:", e); 098 throw new JMSException(e.getMessage()); 099 } 100 } 101 102 /** 103 * @see javax.naming.Referenceable#getReference() 104 */ 105 public Reference getReference() { 106 return reference; 107 } 108 109 /** 110 * @see javax.resource.Referenceable#setReference(javax.naming.Reference) 111 */ 112 public void setReference(Reference reference) { 113 this.reference = reference; 114 } 115 116 public QueueConnection createQueueConnection() throws JMSException { 117 return (QueueConnection) createConnection(); 118 } 119 120 public QueueConnection createQueueConnection(String userName, String password) throws JMSException { 121 return (QueueConnection) createConnection(userName, password); 122 } 123 124 public TopicConnection createTopicConnection() throws JMSException { 125 return (TopicConnection) createConnection(); 126 } 127 128 public TopicConnection createTopicConnection(String userName, String password) throws JMSException { 129 return (TopicConnection) createConnection(userName, password); 130 } 131 }