001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. 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 package org.apache.activemq.ra;
018
019 import java.io.Serializable;
020
021 import javax.jms.Connection;
022 import javax.jms.ConnectionFactory;
023 import javax.jms.JMSException;
024 import javax.jms.QueueConnection;
025 import javax.jms.QueueConnectionFactory;
026 import javax.jms.TopicConnection;
027 import javax.jms.TopicConnectionFactory;
028 import javax.naming.Reference;
029 import javax.resource.Referenceable;
030 import javax.resource.ResourceException;
031 import javax.resource.spi.ConnectionManager;
032
033 import org.apache.commons.logging.Log;
034 import org.apache.commons.logging.LogFactory;
035
036 /**
037 * @version $Revision$
038 */
039 public class ActiveMQConnectionFactory implements ConnectionFactory, QueueConnectionFactory, TopicConnectionFactory, Referenceable, Serializable {
040
041 private static final long serialVersionUID = -5754338187296859149L;
042
043 private static final Log LOG = LogFactory.getLog(ActiveMQConnectionFactory.class);
044 private ConnectionManager manager;
045 private ActiveMQManagedConnectionFactory factory;
046 private Reference reference;
047 private final ActiveMQConnectionRequestInfo info;
048
049 /**
050 * @param factory
051 * @param manager
052 * @param connectionRequestInfo
053 */
054 public ActiveMQConnectionFactory(
055 ActiveMQManagedConnectionFactory factory,
056 ConnectionManager manager,
057 ActiveMQConnectionRequestInfo connectionRequestInfo) {
058 this.factory = factory;
059 this.manager = manager;
060 this.info = connectionRequestInfo;
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,
072 * java.lang.String)
073 */
074 public Connection createConnection(String userName, String password) throws JMSException {
075 ActiveMQConnectionRequestInfo i = info.copy();
076 i.setUserName(userName);
077 i.setPassword(password);
078 return createConnection(i);
079 }
080
081 /**
082 * @param connectionRequestInfo
083 * @return
084 * @throws JMSException
085 */
086 private Connection createConnection(ActiveMQConnectionRequestInfo connectionRequestInfo) throws JMSException {
087 try {
088 if (connectionRequestInfo.isUseInboundSessionEnabled()) {
089 return new InboundConnectionProxy();
090 }
091 if (manager == null) {
092 throw new JMSException("No JCA ConnectionManager configured! Either enable UseInboundSessionEnabled or get your JCA container to configure one.");
093 }
094 return (Connection)manager.allocateConnection(factory, connectionRequestInfo);
095 } catch (ResourceException e) {
096 // Throw the root cause if it was a JMSException..
097 if (e.getCause() instanceof JMSException) {
098 throw (JMSException)e.getCause();
099 }
100 LOG.debug("Connection could not be created:", e);
101 throw new JMSException(e.getMessage());
102 }
103 }
104
105 /**
106 * @see javax.naming.Referenceable#getReference()
107 */
108 public Reference getReference() {
109 return reference;
110 }
111
112 /**
113 * @see javax.resource.Referenceable#setReference(javax.naming.Reference)
114 */
115 public void setReference(Reference reference) {
116 this.reference = reference;
117 }
118
119 public QueueConnection createQueueConnection() throws JMSException {
120 return (QueueConnection)createConnection();
121 }
122
123 public QueueConnection createQueueConnection(String userName, String password) throws JMSException {
124 return (QueueConnection)createConnection(userName, password);
125 }
126
127 public TopicConnection createTopicConnection() throws JMSException {
128 return (TopicConnection)createConnection();
129 }
130
131 public TopicConnection createTopicConnection(String userName, String password) throws JMSException {
132 return (TopicConnection)createConnection(userName, password);
133 }
134 }