00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 package org.objectweb.jonas_jms;
00027
00028 import java.io.Serializable;
00029 import java.util.Iterator;
00030 import java.util.LinkedList;
00031 import javax.naming.NamingException;
00032 import javax.naming.Reference;
00033 import javax.naming.Referenceable;
00034
00035 import javax.jms.Connection;
00036 import javax.jms.ConnectionFactory;
00037 import javax.jms.JMSException;
00038 import javax.jms.XAConnectionFactory;
00039
00040 import org.objectweb.jonas_jms.api.JmsManager;
00041
00042 import org.objectweb.util.monolog.api.BasicLevel;
00043
00052 public class JConnectionFactory implements ConnectionFactory, Referenceable, Serializable {
00053
00054 protected JmsManager jms;
00055 protected String name;
00056 protected XAConnectionFactory xacf;
00057
00058 private LinkedList connectionpool = new LinkedList();
00059
00064 public JConnectionFactory(String name) {
00065 this.name = name;
00066 jms = JmsManagerImpl.getJmsManager();
00067 xacf = jms.getXAConnectionFactory();
00068 }
00069
00073 protected JConnectionFactory() {
00074 }
00075
00076
00077
00078
00079
00080
00090 public Connection createConnection() throws JMSException {
00091 TraceJms.logger.log(BasicLevel.DEBUG,"");
00092 JConnection c = (JConnection) getJConnection();
00093 if (c == null) {
00094 c = new JConnection(this, xacf);
00095 }
00096 return (Connection) c;
00097 }
00098
00112 public Connection createConnection(String userName, String password) throws JMSException {
00113 TraceJms.logger.log(BasicLevel.DEBUG,"");
00114 JConnection c = (JConnection) getJConnection(userName);
00115 if (c == null) {
00116 c = new JConnection(this, xacf, userName, password);
00117 }
00118 return (Connection) c;
00119 }
00120
00121
00122
00123
00124
00129 public void freeJConnection(JConnection con) {
00130 TraceJms.logger.log(BasicLevel.DEBUG, "");
00131 synchronized (connectionpool) {
00132 connectionpool.addLast(con);
00133 }
00134 }
00135
00136 void cleanPool() {
00137 TraceJms.logger.log(BasicLevel.DEBUG, "");
00138 JConnection con = null;
00139 synchronized (connectionpool) {
00140 Iterator i = connectionpool.iterator();
00141 while (i.hasNext()) {
00142 con = (JConnection) i.next();
00143 try {
00144 con.finalClose();
00145 } catch (JMSException e) {
00146 }
00147 }
00148 }
00149 }
00150
00156 public JConnection getJConnection() {
00157 return getJConnection(JConnection.INTERNAL_USER_NAME);
00158 }
00159
00166 public JConnection getJConnection(String user) {
00167 TraceJms.logger.log(BasicLevel.DEBUG, "");
00168 JConnection con = null;
00169 synchronized (connectionpool) {
00170 Iterator i = connectionpool.iterator();
00171 while (i.hasNext()) {
00172 con = (JConnection) i.next();
00173 if (con.getUser().equals(user)) {
00174 i.remove();
00175 break;
00176 }
00177 }
00178 }
00179 return con;
00180 }
00181
00182
00183
00184
00185
00186
00187
00188 public Reference getReference() throws NamingException {
00189 TraceJms.logger.log(BasicLevel.DEBUG, "");
00190 return new Reference(getClass().getName(), "org.objectweb.jonas_jms.JObjectFactory", null);
00191 }
00192
00193
00194 }