001 /**
002 *
003 * Copyright 2005 Protique Ltd
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.pool;
019
020 import org.activemq.ActiveMQConnection;
021 import org.activemq.ActiveMQConnectionFactory;
022 import org.activemq.service.Service;
023
024 import javax.jms.Connection;
025 import javax.jms.ConnectionFactory;
026 import javax.jms.JMSException;
027 import java.util.HashMap;
028 import java.util.Iterator;
029 import java.util.Map;
030
031 /**
032 * @version $Revision: 1.1 $
033 */
034 public class PooledConnectionFactory implements ConnectionFactory, Service {
035 private ActiveMQConnectionFactory connectionFactory;
036 private Map cache = new HashMap();
037
038 public PooledConnectionFactory() {
039 this(new ActiveMQConnectionFactory());
040 }
041
042 public PooledConnectionFactory(String brokerURL) {
043 this(new ActiveMQConnectionFactory(brokerURL));
044 }
045
046 public PooledConnectionFactory(ActiveMQConnectionFactory connectionFactory) {
047 this.connectionFactory = connectionFactory;
048 }
049
050 public ActiveMQConnectionFactory getConnectionFactory() {
051 return connectionFactory;
052 }
053
054 public void setConnectionFactory(ActiveMQConnectionFactory connectionFactory) {
055 this.connectionFactory = connectionFactory;
056 }
057
058 public Connection createConnection() throws JMSException {
059 return createConnection(null, null);
060 }
061
062 public synchronized Connection createConnection(String userName, String password) throws JMSException {
063 ConnectionKey key = new ConnectionKey(userName, password);
064 PooledConnection connection = (PooledConnection) cache.get(key);
065 if (connection == null) {
066 ActiveMQConnection delegate = createConnection(key);
067 connection = new PooledConnection(delegate);
068 cache.put(key, connection);
069 }
070 return connection.newInstance();
071 }
072
073 protected ActiveMQConnection createConnection(ConnectionKey key) throws JMSException {
074 if (key.getUserName() == null && key.getPassword() == null) {
075 return (ActiveMQConnection) connectionFactory.createConnection();
076 }
077 else {
078 return (ActiveMQConnection) connectionFactory.createConnection(key.getUserName(), key.getPassword());
079 }
080 }
081
082 /**
083 * @see org.activemq.service.Service#start()
084 */
085 public void start() throws JMSException {
086 createConnection();
087 }
088
089 /**
090 * @see org.activemq.service.Service#stop()
091 */
092 public void stop() throws JMSException {
093 for (Iterator iter = cache.values().iterator(); iter.hasNext();) {
094 PooledConnection connection = (PooledConnection) iter.next();
095 connection.close();
096 }
097 }
098
099
100 }