KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_jms > JConnectionFactory


1 /*
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: JConnectionFactory.java,v 1.8 2003/06/10 08:55:20 danesa Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas_jms;
27
28 import java.io.Serializable JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.LinkedList JavaDoc;
31 import javax.naming.NamingException JavaDoc;
32 import javax.naming.Reference JavaDoc;
33 import javax.naming.Referenceable JavaDoc;
34
35 import javax.jms.Connection JavaDoc;
36 import javax.jms.ConnectionFactory JavaDoc;
37 import javax.jms.JMSException JavaDoc;
38 import javax.jms.XAConnectionFactory JavaDoc;
39
40 import org.objectweb.jonas_jms.api.JmsManager;
41
42 import org.objectweb.util.monolog.api.BasicLevel;
43
44 /**
45  * @author Laurent Chauvirey, Frederic Maistre, Nicolas Tachker
46  * Contributor(s):
47  * Philippe Durieux
48  * Jeff Mesnil connection anonymous
49  * Philippe Coq
50  */

51
52 public class JConnectionFactory implements ConnectionFactory JavaDoc, Referenceable JavaDoc, Serializable JavaDoc {
53
54     protected JmsManager jms;
55     protected String JavaDoc name;
56     protected XAConnectionFactory JavaDoc xacf;
57
58     private LinkedList JavaDoc connectionpool = new LinkedList JavaDoc();
59
60     /**
61      * Constructor.
62      * @param name - ConnectionFactory name
63      */

64     public JConnectionFactory(String JavaDoc name) {
65         this.name = name;
66         jms = JmsManagerImpl.getJmsManager();
67         xacf = jms.getXAConnectionFactory();
68     }
69
70     /**
71      * Empty Constructor called by subclasses.
72      */

73     protected JConnectionFactory() {
74     }
75
76
77     // -----------------------------------------------------------------------
78
// ConnectionFactory implementation
79
// -----------------------------------------------------------------------
80

81     /**
82      * Create a connection for an anonymous user.
83      *
84      * @return a newly created connection.
85      * @throws JMSException - if JMS Provider fails to create Connection
86      * due to some internal error. required resources for a Connection.
87      * @throws JMSSecurityException - if client authentication fails due to
88      * invalid user name or password.
89      */

90     public Connection JavaDoc createConnection() throws JMSException JavaDoc {
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 JavaDoc) c;
97     }
98
99     /**
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 JavaDoc createConnection(String JavaDoc userName, String JavaDoc password) throws JMSException JavaDoc {
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 JavaDoc) c;
119     }
120
121     // -----------------------------------------------------------------------
122
// Internal Methods
123
// -----------------------------------------------------------------------
124

125     /**
126      * Free a Connection and return it to the pool
127      * @param con - Connection to be freed
128      */

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 JavaDoc i = connectionpool.iterator();
141             while (i.hasNext()) {
142                 con = (JConnection) i.next();
143                 try {
144                     con.finalClose();
145                 } catch (JMSException JavaDoc e) {
146                 }
147             }
148         }
149     }
150
151     /**
152      * Get a Connection from the pool for an anonymous user
153      *
154      * @return a Connection for an anonymous user
155      */

156     public JConnection getJConnection() {
157         return getJConnection(JConnection.INTERNAL_USER_NAME);
158     }
159
160     /**
161      * Get a Connection from the pool for the specified user
162      *
163      * @param user User wanting a connection
164      * @return Connection from the pool for the specified user
165      */

166     public JConnection getJConnection(String JavaDoc user) {
167         TraceJms.logger.log(BasicLevel.DEBUG, "");
168         JConnection con = null;
169         synchronized (connectionpool) {
170             Iterator JavaDoc 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     // -----------------------------------------------------------------------
185
// Referenceable implementation
186
// -----------------------------------------------------------------------
187

188     public Reference JavaDoc getReference() throws NamingException JavaDoc {
189         TraceJms.logger.log(BasicLevel.DEBUG, "");
190         return new Reference JavaDoc(getClass().getName(), "org.objectweb.jonas_jms.JObjectFactory", null);
191     }
192
193
194 }
195
Popular Tags