1 25 26 package org.objectweb.jonas_jms; 27 28 import java.io.Serializable ; 29 import java.util.Iterator ; 30 import java.util.LinkedList ; 31 import javax.naming.NamingException ; 32 import javax.naming.Reference ; 33 import javax.naming.Referenceable ; 34 35 import javax.jms.Connection ; 36 import javax.jms.ConnectionFactory ; 37 import javax.jms.JMSException ; 38 import javax.jms.XAConnectionFactory ; 39 40 import org.objectweb.jonas_jms.api.JmsManager; 41 42 import org.objectweb.util.monolog.api.BasicLevel; 43 44 51 52 public class JConnectionFactory implements ConnectionFactory , Referenceable , Serializable { 53 54 protected JmsManager jms; 55 protected String name; 56 protected XAConnectionFactory xacf; 57 58 private LinkedList connectionpool = new LinkedList (); 59 60 64 public JConnectionFactory(String name) { 65 this.name = name; 66 jms = JmsManagerImpl.getJmsManager(); 67 xacf = jms.getXAConnectionFactory(); 68 } 69 70 73 protected JConnectionFactory() { 74 } 75 76 77 81 90 public Connection createConnection() throws JMSException { 91 TraceJms.logger.log(BasicLevel.DEBUG,""); 92 JConnection c = (JConnection) getJConnection(); 93 if (c == null) { 94 c = new JConnection(this, xacf); 95 } 96 return (Connection ) c; 97 } 98 99 112 public Connection createConnection(String userName, String password) throws JMSException { 113 TraceJms.logger.log(BasicLevel.DEBUG,""); 114 JConnection c = (JConnection) getJConnection(userName); 115 if (c == null) { 116 c = new JConnection(this, xacf, userName, password); 117 } 118 return (Connection ) c; 119 } 120 121 125 129 public void freeJConnection(JConnection con) { 130 TraceJms.logger.log(BasicLevel.DEBUG, ""); 131 synchronized (connectionpool) { 132 connectionpool.addLast(con); 133 } 134 } 135 136 void cleanPool() { 137 TraceJms.logger.log(BasicLevel.DEBUG, ""); 138 JConnection con = null; 139 synchronized (connectionpool) { 140 Iterator i = connectionpool.iterator(); 141 while (i.hasNext()) { 142 con = (JConnection) i.next(); 143 try { 144 con.finalClose(); 145 } catch (JMSException e) { 146 } 147 } 148 } 149 } 150 151 156 public JConnection getJConnection() { 157 return getJConnection(JConnection.INTERNAL_USER_NAME); 158 } 159 160 166 public JConnection getJConnection(String user) { 167 TraceJms.logger.log(BasicLevel.DEBUG, ""); 168 JConnection con = null; 169 synchronized (connectionpool) { 170 Iterator i = connectionpool.iterator(); 171 while (i.hasNext()) { 172 con = (JConnection) i.next(); 173 if (con.getUser().equals(user)) { 174 i.remove(); 175 break; 176 } 177 } 178 } 179 return con; 180 } 181 182 183 184 188 public Reference getReference() throws NamingException { 189 TraceJms.logger.log(BasicLevel.DEBUG, ""); 190 return new Reference (getClass().getName(), "org.objectweb.jonas_jms.JObjectFactory", null); 191 } 192 193 194 } 195 | Popular Tags |