001 /**
002 * Licensed under the Apache License, Version 2.0 (the "License");
003 * you may not use this file except in compliance with the License.
004 * You may obtain a copy of the License at
005 *
006 * http://www.apache.org/licenses/LICENSE-2.0
007 *
008 * Unless required by applicable law or agreed to in writing, software
009 * distributed under the License is distributed on an "AS IS" BASIS,
010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011 * See the License for the specific language governing permissions and
012 * limitations under the License.
013 *
014 **/
015 package org.jencks.pool;
016
017 import java.util.Map;
018
019 import javax.jms.Connection;
020 import javax.jms.JMSException;
021 import javax.jms.XAConnectionFactory;
022 import javax.jms.ConnectionFactory;
023 import javax.jms.XAConnection;
024 import javax.transaction.TransactionManager;
025
026 import org.activemq.pool.ConnectionKey;
027 import org.springframework.transaction.jta.JtaTransactionManager;
028
029 import EDU.oswego.cs.dl.util.concurrent.ConcurrentHashMap;
030
031 public class PooledSpringXAConnectionFactory implements ConnectionFactory {
032 private XAConnectionFactory connectionFactory;
033
034 private TransactionManager transactionManager;
035
036 private Map cache = new ConcurrentHashMap();
037
038 private Map pooledClientIds = new ConcurrentHashMap();
039
040 public PooledSpringXAConnectionFactory(
041 final XAConnectionFactory connectionFactory,
042 final TransactionManager transactionManager) {
043 this.connectionFactory = connectionFactory;
044 this.transactionManager = transactionManager;
045 }
046
047 public PooledSpringXAConnectionFactory() {
048 }
049
050 public TransactionManager getTransactionManager() {
051 return this.transactionManager;
052 }
053
054 public void setTransactionManager(final TransactionManager transactionManager) {
055 this.transactionManager = transactionManager;
056 }
057
058 public void setJtaTransactionManager(final JtaTransactionManager jtaTransactionManager) {
059 setTransactionManager(jtaTransactionManager.getTransactionManager());
060 }
061
062 public XAConnectionFactory getConnectionFactory() {
063 return this.connectionFactory;
064 }
065
066 public void setConnectionFactory(final XAConnectionFactory connectionFactory) {
067 this.connectionFactory = connectionFactory;
068 }
069
070 public Connection createConnection() throws JMSException {
071 return createConnection(null, null);
072 }
073
074 public synchronized Connection createConnection(final String userName, final String password) throws JMSException {
075 final ConnectionKey key = new ConnectionKey(userName, password);
076 PooledSpringXAConnection connection = (PooledSpringXAConnection) this.cache.get(key);
077 if (connection == null) {
078 final XAConnection delegate = createConnection(key);
079 connection = new PooledSpringXAConnection(this, getTransactionManager(), delegate);
080 this.cache.put(key, connection);
081 }
082 return connection.newInstance();
083 }
084
085 protected XAConnection createConnection(ConnectionKey key) throws JMSException {
086 if (key.getUserName() == null && key.getPassword() == null) {
087 return this.connectionFactory.createXAConnection();
088 } else {
089 return this.connectionFactory.createXAConnection(key.getUserName(), key.getPassword());
090 }
091 }
092
093 public String generateClientID(final String requestedClientID) {
094 if (requestedClientID == null) {
095 return null;
096 }
097
098 final int num;
099
100 synchronized (this) {
101 final Integer lastCount = (Integer) this.pooledClientIds
102 .get(requestedClientID);
103 if (lastCount == null) {
104 num = 1;
105 } else {
106 num = lastCount.intValue() + 1;
107 }
108 this.pooledClientIds.put(requestedClientID, new Integer(num));
109 }
110
111 return requestedClientID + "-" + num;
112 }
113
114 public void start() throws JMSException {
115 }
116
117 public void stop() throws JMSException {
118 }
119 }