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 */
017package org.apache.activemq.ra;
018
019import java.io.Serializable;
020
021import javax.jms.Connection;
022import javax.jms.ConnectionFactory;
023import javax.jms.JMSException;
024import javax.jms.QueueConnection;
025import javax.jms.QueueConnectionFactory;
026import javax.jms.TopicConnection;
027import javax.jms.TopicConnectionFactory;
028import javax.naming.Reference;
029import javax.resource.Referenceable;
030import javax.resource.ResourceException;
031import javax.resource.spi.ConnectionManager;
032
033import org.slf4j.Logger;
034import org.slf4j.LoggerFactory;
035
036/**
037 * 
038 */
039public class ActiveMQConnectionFactory implements ConnectionFactory, QueueConnectionFactory, TopicConnectionFactory, Referenceable, Serializable {
040
041    private static final long serialVersionUID = -5754338187296859149L;
042
043    private static final Logger LOG = LoggerFactory.getLogger(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
095            return (Connection)manager.allocateConnection(factory, connectionRequestInfo);
096        } catch (ResourceException e) {
097            // Throw the root cause if it was a JMSException..
098            if (e.getCause() instanceof JMSException) {
099                throw (JMSException)e.getCause();
100            }
101            LOG.debug("Connection could not be created:", e);
102            JMSException jmsException = new JMSException(e.getMessage());
103            jmsException.setLinkedException(e);
104            throw jmsException;
105        }
106    }
107
108    /**
109     * @see javax.naming.Referenceable#getReference()
110     */
111    public Reference getReference() {
112        return reference;
113    }
114
115    /**
116     * @see javax.resource.Referenceable#setReference(javax.naming.Reference)
117     */
118    public void setReference(Reference reference) {
119        this.reference = reference;
120    }
121
122    public QueueConnection createQueueConnection() throws JMSException {
123        return (QueueConnection)createConnection();
124    }
125
126    public QueueConnection createQueueConnection(String userName, String password) throws JMSException {
127        return (QueueConnection)createConnection(userName, password);
128    }
129
130    public TopicConnection createTopicConnection() throws JMSException {
131        return (TopicConnection)createConnection();
132    }
133
134    public TopicConnection createTopicConnection(String userName, String password) throws JMSException {
135        return (TopicConnection)createConnection(userName, password);
136    }
137}