Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/activemq/pool/PooledConnectionFactory.java


1   /** 
2    * 
3    * Copyright 2005 Protique Ltd
4    * 
5    * Licensed under the Apache License, Version 2.0 (the "License"); 
6    * you may not use this file except in compliance with the License. 
7    * You may obtain a copy of the License at 
8    * 
9    * http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, 
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
14   * See the License for the specific language governing permissions and 
15   * limitations under the License. 
16   * 
17   **/
18  package org.activemq.pool;
19  
20  import org.activemq.ActiveMQConnection;
21  import org.activemq.ActiveMQConnectionFactory;
22  import org.activemq.service.Service;
23  
24  import javax.jms.Connection;
25  import javax.jms.ConnectionFactory;
26  import javax.jms.JMSException;
27  import java.util.HashMap;
28  import java.util.Iterator;
29  import java.util.Map;
30  
31  /**
32   * @version $Revision: 1.1 $
33   */
34  public class PooledConnectionFactory implements ConnectionFactory, Service {
35      private ActiveMQConnectionFactory connectionFactory;
36      private Map cache = new HashMap();
37  
38      public PooledConnectionFactory() {
39          this(new ActiveMQConnectionFactory());
40      }
41  
42      public PooledConnectionFactory(String brokerURL) {
43          this(new ActiveMQConnectionFactory(brokerURL));
44      }
45  
46      public PooledConnectionFactory(ActiveMQConnectionFactory connectionFactory) {
47          this.connectionFactory = connectionFactory;
48      }
49  
50      public ActiveMQConnectionFactory getConnectionFactory() {
51          return connectionFactory;
52      }
53  
54      public void setConnectionFactory(ActiveMQConnectionFactory connectionFactory) {
55          this.connectionFactory = connectionFactory;
56      }
57  
58      public Connection createConnection() throws JMSException {
59          return createConnection(null, null);
60      }
61  
62      public synchronized Connection createConnection(String userName, String password) throws JMSException {
63          ConnectionKey key = new ConnectionKey(userName, password);
64          PooledConnection connection = (PooledConnection) cache.get(key);
65          if (connection == null) {
66              ActiveMQConnection delegate = createConnection(key);
67              connection = new PooledConnection(delegate);
68              cache.put(key, connection);
69          }
70          return connection.newInstance();
71      }
72  
73      protected ActiveMQConnection createConnection(ConnectionKey key) throws JMSException {
74          if (key.getUserName() == null && key.getPassword() == null) {
75              return (ActiveMQConnection) connectionFactory.createConnection();
76          }
77          else {
78              return (ActiveMQConnection) connectionFactory.createConnection(key.getUserName(), key.getPassword());
79          }
80      }
81  
82      /**
83       * @see org.activemq.service.Service#start()
84       */
85      public void start() throws JMSException {
86          createConnection();
87      }
88  
89      /**
90       * @see org.activemq.service.Service#stop()
91       */
92      public void stop() throws JMSException {
93          for (Iterator iter = cache.values().iterator(); iter.hasNext();) {
94              PooledConnection connection = (PooledConnection) iter.next();
95              connection.getConnection().close();
96              connection.close();
97          }
98      }
99  
100 
101 }