001: /*
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: JConnectionFactory.java 2563 2003-06-10 08:55:20Z danesa $
023: * --------------------------------------------------------------------------
024: */
025:
026: package org.objectweb.jonas_jms;
027:
028: import java.io.Serializable;
029: import java.util.Iterator;
030: import java.util.LinkedList;
031: import javax.naming.NamingException;
032: import javax.naming.Reference;
033: import javax.naming.Referenceable;
034:
035: import javax.jms.Connection;
036: import javax.jms.ConnectionFactory;
037: import javax.jms.JMSException;
038: import javax.jms.XAConnectionFactory;
039:
040: import org.objectweb.jonas_jms.api.JmsManager;
041:
042: import org.objectweb.util.monolog.api.BasicLevel;
043:
044: /**
045: * @author Laurent Chauvirey, Frederic Maistre, Nicolas Tachker
046: * Contributor(s):
047: * Philippe Durieux
048: * Jeff Mesnil connection anonymous
049: * Philippe Coq
050: */
051:
052: public class JConnectionFactory implements ConnectionFactory,
053: Referenceable, Serializable {
054:
055: protected JmsManager jms;
056: protected String name;
057: protected XAConnectionFactory xacf;
058:
059: private LinkedList connectionpool = new LinkedList();
060:
061: /**
062: * Constructor.
063: * @param name - ConnectionFactory name
064: */
065: public JConnectionFactory(String name) {
066: this .name = name;
067: jms = JmsManagerImpl.getJmsManager();
068: xacf = jms.getXAConnectionFactory();
069: }
070:
071: /**
072: * Empty Constructor called by subclasses.
073: */
074: protected JConnectionFactory() {
075: }
076:
077: // -----------------------------------------------------------------------
078: // ConnectionFactory implementation
079: // -----------------------------------------------------------------------
080:
081: /**
082: * Create a connection for an anonymous user.
083: *
084: * @return a newly created connection.
085: * @throws JMSException - if JMS Provider fails to create Connection
086: * due to some internal error. required resources for a Connection.
087: * @throws JMSSecurityException - if client authentication fails due to
088: * invalid user name or password.
089: */
090: public Connection createConnection() throws JMSException {
091: TraceJms.logger.log(BasicLevel.DEBUG, "");
092: JConnection c = (JConnection) getJConnection();
093: if (c == null) {
094: c = new JConnection(this , xacf);
095: }
096: return (Connection) c;
097: }
098:
099: /**
100: * Create a connection with specified user identity.
101: * The connection is created in stopped mode. No messages will
102: * be delivered until Connection.start method is explicitly called.
103: *
104: * @param userName - the caller's user name
105: * @param password - the caller's password
106: *
107: * @throws JMSException - if JMS Provider fails to create Connection
108: * due to some internal error. required resources for a Connection.
109: * @throws JMSSecurityException - if client authentication fails due to
110: * invalid user name or password.
111: */
112: public Connection createConnection(String userName, String password)
113: throws JMSException {
114: TraceJms.logger.log(BasicLevel.DEBUG, "");
115: JConnection c = (JConnection) getJConnection(userName);
116: if (c == null) {
117: c = new JConnection(this , xacf, userName, password);
118: }
119: return (Connection) c;
120: }
121:
122: // -----------------------------------------------------------------------
123: // Internal Methods
124: // -----------------------------------------------------------------------
125:
126: /**
127: * Free a Connection and return it to the pool
128: * @param con - Connection to be freed
129: */
130: public void freeJConnection(JConnection con) {
131: TraceJms.logger.log(BasicLevel.DEBUG, "");
132: synchronized (connectionpool) {
133: connectionpool.addLast(con);
134: }
135: }
136:
137: void cleanPool() {
138: TraceJms.logger.log(BasicLevel.DEBUG, "");
139: JConnection con = null;
140: synchronized (connectionpool) {
141: Iterator i = connectionpool.iterator();
142: while (i.hasNext()) {
143: con = (JConnection) i.next();
144: try {
145: con.finalClose();
146: } catch (JMSException e) {
147: }
148: }
149: }
150: }
151:
152: /**
153: * Get a Connection from the pool for an anonymous user
154: *
155: * @return a Connection for an anonymous user
156: */
157: public JConnection getJConnection() {
158: return getJConnection(JConnection.INTERNAL_USER_NAME);
159: }
160:
161: /**
162: * Get a Connection from the pool for the specified user
163: *
164: * @param user User wanting a connection
165: * @return Connection from the pool for the specified user
166: */
167: public JConnection getJConnection(String user) {
168: TraceJms.logger.log(BasicLevel.DEBUG, "");
169: JConnection con = null;
170: synchronized (connectionpool) {
171: Iterator i = connectionpool.iterator();
172: while (i.hasNext()) {
173: con = (JConnection) i.next();
174: if (con.getUser().equals(user)) {
175: i.remove();
176: break;
177: }
178: }
179: }
180: return con;
181: }
182:
183: // -----------------------------------------------------------------------
184: // Referenceable implementation
185: // -----------------------------------------------------------------------
186:
187: public Reference getReference() throws NamingException {
188: TraceJms.logger.log(BasicLevel.DEBUG, "");
189: return new Reference(getClass().getName(),
190: "org.objectweb.jonas_jms.JObjectFactory", null);
191: }
192:
193: }
|