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.pool;
018
019import java.io.IOException;
020import java.util.HashMap;
021import java.util.Properties;
022
023import javax.jms.Connection;
024import javax.naming.NamingException;
025import javax.naming.Reference;
026
027import org.apache.activemq.ActiveMQConnection;
028import org.apache.activemq.ActiveMQConnectionFactory;
029import org.apache.activemq.Service;
030import org.apache.activemq.jms.pool.ConnectionPool;
031import org.apache.activemq.jndi.JNDIReferenceFactory;
032import org.apache.activemq.jndi.JNDIStorableInterface;
033import org.apache.activemq.transport.TransportListener;
034import org.apache.activemq.util.IntrospectionSupport;
035import org.slf4j.Logger;
036import org.slf4j.LoggerFactory;
037
038/**
039 * Add Service and Referenceable and TransportListener to @link{org.apache.activemq.jms.pool.PooledConnectionFactory}
040 *
041 * @org.apache.xbean.XBean element="pooledConnectionFactory"
042 */
043public class PooledConnectionFactory extends org.apache.activemq.jms.pool.PooledConnectionFactory implements JNDIStorableInterface, Service {
044    public static final String POOL_PROPS_PREFIX = "pool";
045
046    private static final transient Logger LOG = LoggerFactory.getLogger(org.apache.activemq.jms.pool.PooledConnectionFactory.class);
047
048    public PooledConnectionFactory() {
049        super();
050    }
051
052    public PooledConnectionFactory(ActiveMQConnectionFactory activeMQConnectionFactory) {
053        setConnectionFactory(activeMQConnectionFactory);
054    }
055
056    public PooledConnectionFactory(String brokerURL) {
057        setConnectionFactory(new ActiveMQConnectionFactory(brokerURL));
058    }
059
060    @SuppressWarnings({ "unchecked", "rawtypes" })
061    protected void buildFromProperties(Properties props) {
062        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory();
063        activeMQConnectionFactory.buildFromProperties(props);
064        setConnectionFactory(activeMQConnectionFactory);
065        IntrospectionSupport.setProperties(this, new HashMap(props), POOL_PROPS_PREFIX);
066    }
067
068    @Override
069    protected void populateProperties(Properties props) {
070        ((ActiveMQConnectionFactory)getConnectionFactory()).populateProperties(props);
071        super.populateProperties(props);
072    }
073
074    @Override
075    public void setProperties(Properties properties) {
076        buildFromProperties(properties);
077    }
078
079    @Override
080    public Properties getProperties() {
081        Properties properties = new Properties();
082        populateProperties(properties);
083        return properties;
084    }
085
086    @Override
087    public Reference getReference() throws NamingException {
088        return JNDIReferenceFactory.createReference(this.getClass().getName(), this);
089    }
090
091    @Override
092    protected Connection newPooledConnection(ConnectionPool connection) {
093        return new PooledConnection(connection);
094    }
095
096    @Override
097    protected org.apache.activemq.jms.pool.ConnectionPool createConnectionPool(Connection connection) {
098        return new ConnectionPool(connection) {
099
100            @Override
101            protected Connection wrap(final Connection connection) {
102                // Add a transport Listener so that we can notice if this connection
103                // should be expired due to a connection failure.
104                ((ActiveMQConnection)connection).addTransportListener(new TransportListener() {
105                    @Override
106                    public void onCommand(Object command) {
107                    }
108
109                    @Override
110                    public void onException(IOException error) {
111                        synchronized (this) {
112                            setHasExpired(true);
113                            // only log if not stopped
114                            if (!stopped.get()) {
115                                LOG.info("Expiring connection " + connection + " on IOException: " + error.getMessage());
116                                // log stacktrace at debug level
117                                LOG.debug("Expiring connection " + connection + " on IOException: ", error);
118                            }
119                        }
120                    }
121
122                    @Override
123                    public void transportInterupted() {
124                    }
125
126                    @Override
127                    public void transportResumed() {
128                    }
129                });
130
131                // make sure that we set the hasFailed flag, in case the transport already failed
132                // prior to the addition of our new TransportListener
133                setHasExpired(((ActiveMQConnection)connection).isTransportFailed());
134
135                // may want to return an amq EnhancedConnection
136                return connection;
137            }
138
139            @Override
140            protected void unWrap(Connection connection) {
141                if (connection != null) {
142                    ((ActiveMQConnection)connection).cleanUpTempDestinations();
143                }
144            }
145        };
146    }
147}