1 /***
2 * Licensed under the Apache License, Version 2.0 (the "License");
3 * you may not use this file except in compliance with the License.
4 * You may obtain a copy of the License at
5 *
6 * http://www.apache.org/licenses/LICENSE-2.0
7 *
8 * Unless required by applicable law or agreed to in writing, software
9 * distributed under the License is distributed on an "AS IS" BASIS,
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 * See the License for the specific language governing permissions and
12 * limitations under the License.
13 *
14 **/
15 package org.jencks.pool;
16
17 import java.util.Map;
18
19 import javax.jms.Connection;
20 import javax.jms.JMSException;
21 import javax.jms.XAConnectionFactory;
22 import javax.jms.ConnectionFactory;
23 import javax.jms.XAConnection;
24 import javax.transaction.TransactionManager;
25
26 import org.activemq.pool.ConnectionKey;
27 import org.springframework.transaction.jta.JtaTransactionManager;
28
29 import EDU.oswego.cs.dl.util.concurrent.ConcurrentHashMap;
30
31 public class PooledSpringXAConnectionFactory implements ConnectionFactory {
32 private XAConnectionFactory connectionFactory;
33
34 private TransactionManager transactionManager;
35
36 private Map cache = new ConcurrentHashMap();
37
38 private Map pooledClientIds = new ConcurrentHashMap();
39
40 public PooledSpringXAConnectionFactory(
41 final XAConnectionFactory connectionFactory,
42 final TransactionManager transactionManager) {
43 this.connectionFactory = connectionFactory;
44 this.transactionManager = transactionManager;
45 }
46
47 public PooledSpringXAConnectionFactory() {
48 }
49
50 public TransactionManager getTransactionManager() {
51 return this.transactionManager;
52 }
53
54 public void setTransactionManager(final TransactionManager transactionManager) {
55 this.transactionManager = transactionManager;
56 }
57
58 public void setJtaTransactionManager(final JtaTransactionManager jtaTransactionManager) {
59 setTransactionManager(jtaTransactionManager.getTransactionManager());
60 }
61
62 public XAConnectionFactory getConnectionFactory() {
63 return this.connectionFactory;
64 }
65
66 public void setConnectionFactory(final XAConnectionFactory connectionFactory) {
67 this.connectionFactory = connectionFactory;
68 }
69
70 public Connection createConnection() throws JMSException {
71 return createConnection(null, null);
72 }
73
74 public synchronized Connection createConnection(final String userName, final String password) throws JMSException {
75 final ConnectionKey key = new ConnectionKey(userName, password);
76 PooledSpringXAConnection connection = (PooledSpringXAConnection) this.cache.get(key);
77 if (connection == null) {
78 final XAConnection delegate = createConnection(key);
79 connection = new PooledSpringXAConnection(this, getTransactionManager(), delegate);
80 this.cache.put(key, connection);
81 }
82 return connection.newInstance();
83 }
84
85 protected XAConnection createConnection(ConnectionKey key) throws JMSException {
86 if (key.getUserName() == null && key.getPassword() == null) {
87 return this.connectionFactory.createXAConnection();
88 } else {
89 return this.connectionFactory.createXAConnection(key.getUserName(), key.getPassword());
90 }
91 }
92
93 public String generateClientID(final String requestedClientID) {
94 if (requestedClientID == null) {
95 return null;
96 }
97
98 final int num;
99
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 }