JConnectionFactory.java

00001 /*
00002  * JOnAS: Java(TM) Open Application Server
00003  * Copyright (C) 1999 Bull S.A.
00004  * Contact: jonas-team@objectweb.org
00005  * 
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or any later version.
00010  * 
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  * 
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00019  * USA
00020  *
00021  * --------------------------------------------------------------------------
00022  * $Id: JConnectionFactory.java,v 1.8 2003/06/10 08:55:20 danesa Exp $
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     // ConnectionFactory implementation
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     // Internal Methods
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     // Referenceable implementation
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 }

Generated on Tue Feb 15 15:05:49 2005 for JOnAS by  doxygen 1.3.9.1