KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jms > ConnectionFactoryImpl


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.jms;
30
31 import com.caucho.config.ConfigException;
32 import com.caucho.jms.jdbc.JdbcManager;
33 import com.caucho.jms.jdbc.JdbcQueue;
34 import com.caucho.jms.jdbc.JdbcTopic;
35 import com.caucho.jms.memory.MemoryQueue;
36 import com.caucho.jms.memory.MemoryTopic;
37 import com.caucho.jms.session.ConnectionImpl;
38 import com.caucho.log.Log;
39 import com.caucho.management.j2ee.J2EEManagedObject;
40 import com.caucho.management.j2ee.JMSResource;
41 import com.caucho.util.L10N;
42
43 import javax.jms.Connection JavaDoc;
44 import javax.jms.ConnectionFactory JavaDoc;
45 import javax.jms.JMSException JavaDoc;
46 import javax.jms.JMSSecurityException JavaDoc;
47 import javax.jms.Queue JavaDoc;
48 import javax.jms.Topic JavaDoc;
49 import javax.sql.DataSource JavaDoc;
50 import java.sql.SQLException JavaDoc;
51 import java.util.ArrayList JavaDoc;
52 import java.util.Collections JavaDoc;
53 import java.util.HashMap JavaDoc;
54 import java.util.List JavaDoc;
55 import java.util.logging.Logger JavaDoc;
56
57 /**
58  * A sample connection factory.
59  */

60 public class ConnectionFactoryImpl implements ConnectionFactory JavaDoc {
61   static final Logger JavaDoc log = Log.open(ConnectionFactoryImpl.class);
62   static final L10N L = new L10N(ConnectionFactoryImpl.class);
63
64   private String JavaDoc _name;
65   private String JavaDoc _clientID;
66   
67   private String JavaDoc _user;
68   private String JavaDoc _password;
69
70   private JdbcManager _jdbcManager;
71
72   private List JavaDoc<ConnectionImpl> _connections =
73     Collections.synchronizedList(new ArrayList JavaDoc<ConnectionImpl>());
74
75   private HashMap JavaDoc<String JavaDoc,Queue> _queues =
76     new HashMap JavaDoc<String JavaDoc,Queue>();
77
78   private HashMap JavaDoc<String JavaDoc,Topic> _topics =
79     new HashMap JavaDoc<String JavaDoc,Topic>();
80
81   public ConnectionFactoryImpl()
82   {
83   }
84
85   /**
86    * Sets the user.
87    */

88   public void setUser(String JavaDoc user)
89   {
90     _user = user;
91   }
92
93   /**
94    * Sets the password.
95    */

96   public void setPassword(String JavaDoc password)
97   {
98     _password = password;
99   }
100
101   /**
102    * Sets the name of the connection factory.
103    */

104   public void setName(String JavaDoc name)
105   {
106     _name = name;
107   }
108
109   /**
110    * Returns the name of the connection factory.
111    */

112   public String JavaDoc getName()
113   {
114     // XXX: should this default to client-id and/or jndi-name? (jndi-name is there
115
// when it is configured as a resource)
116
return _name;
117   }
118
119   /**
120    * Sets the client id.
121    */

122   public void setClientID(String JavaDoc id)
123   {
124     _clientID = id;
125   }
126
127   /**
128    * Sets the JDBC manager.
129    */

130   public void setDataSource(DataSource JavaDoc dataSource)
131   {
132     if (_jdbcManager == null)
133       _jdbcManager = new JdbcManager();
134
135     _jdbcManager.setDataSource(dataSource);
136   }
137
138   /**
139    * Returns the JDBC manager.
140    */

141   public JdbcManager getJdbcManager()
142   {
143     return new JdbcManager();
144   }
145
146   /**
147    * Initialize the connection factory.
148    */

149   public void init()
150     throws ConfigException, SQLException JavaDoc
151   {
152     if (_jdbcManager != null)
153       _jdbcManager.init();
154
155     J2EEManagedObject.register(new JMSResource(this));
156   }
157
158   /**
159    * Creates a new queue connection
160    */

161   public Connection JavaDoc createConnection()
162     throws JMSException JavaDoc
163   {
164     return createConnection(_user, _password);
165   }
166
167   /**
168    * Creates a new connection
169    *
170    * @param username the username to authenticate with the server.
171    * @param password the password to authenticate with the server.
172    *
173    * @return the created connection
174    */

175   public Connection JavaDoc createConnection(String JavaDoc username, String JavaDoc password)
176     throws JMSException JavaDoc
177   {
178     authenticate(username, password);
179
180     ConnectionImpl conn = new ConnectionImpl(this);
181
182     if (_clientID != null) {
183       if (findByClientID(_clientID) != null)
184     throw new JMSException JavaDoc(L.l("ClientID[{0}] is only allowed for a single connection.",
185                    _clientID));
186       conn.setClientID(_clientID);
187     }
188
189     addConnection(conn);
190     
191     return conn;
192   }
193
194   protected void addConnection(ConnectionImpl conn)
195   {
196     _connections.add(conn);
197   }
198
199   /**
200    * Returns the connection named by the specified client id.
201    */

202   public ConnectionImpl findByClientID(String JavaDoc id)
203   {
204     for (int i = 0; i < _connections.size(); i++) {
205       ConnectionImpl conn = _connections.get(i);
206
207       try {
208     if (id.equals(conn.getClientID()))
209       return conn;
210       } catch (Throwable JavaDoc e) {
211       }
212     }
213
214     return null;
215   }
216
217   /**
218    * Removes a connection once closed.
219    */

220   public void removeConnection(ConnectionImpl conn)
221   {
222     _connections.remove(conn);
223   }
224
225   /**
226    * Creates queue.
227    */

228   public Queue createQueue(String JavaDoc name)
229     throws JMSException JavaDoc
230   {
231     try {
232       synchronized (_queues) {
233     Queue queue = _queues.get(name);
234
235     if (queue != null)
236       return queue;
237
238     if (_jdbcManager != null) {
239       JdbcQueue jdbcQueue = new JdbcQueue();
240       jdbcQueue.setJdbcManager(_jdbcManager);
241       jdbcQueue.setQueueName(name);
242       jdbcQueue.init();
243
244       _queues.put(name, jdbcQueue);
245
246       return jdbcQueue;
247     }
248     else {
249       MemoryQueue memoryQueue = new MemoryQueue();
250       memoryQueue.setQueueName(name);
251
252       _queues.put(name, memoryQueue);
253
254       return memoryQueue;
255     }
256       }
257     } catch (RuntimeException JavaDoc e) {
258       throw e;
259     } catch (Exception JavaDoc e) {
260       throw new JMSExceptionWrapper(e);
261     }
262   }
263
264   /**
265    * Creates topics.
266    */

267   public Topic createTopic(String JavaDoc name)
268     throws JMSException JavaDoc
269   {
270     try {
271       synchronized (_topics) {
272     Topic topic = _topics.get(name);
273
274     if (topic != null)
275       return topic;
276
277     if (_jdbcManager != null) {
278       JdbcTopic jdbcTopic = new JdbcTopic();
279       jdbcTopic.setJdbcManager(_jdbcManager);
280       jdbcTopic.setTopicName(name);
281       jdbcTopic.init();
282
283       _topics.put(name, jdbcTopic);
284
285       return jdbcTopic;
286     }
287     else {
288       MemoryTopic memoryTopic = new MemoryTopic();
289       memoryTopic.setTopicName(name);
290
291       _topics.put(name, memoryTopic);
292
293       return memoryTopic;
294     }
295       }
296     } catch (RuntimeException JavaDoc e) {
297       throw e;
298     } catch (Exception JavaDoc e) {
299       throw new JMSExceptionWrapper(e);
300     }
301   }
302
303   protected void authenticate(String JavaDoc username, String JavaDoc password)
304     throws JMSException JavaDoc
305   {
306     if (_user != null && ! _user.equals(username) ||
307     _password != null && ! _password.equals(password)) {
308       throw new JMSSecurityException JavaDoc(L.l("'{0}' is an unknown user",
309                      username));
310     }
311   }
312 }
313
Popular Tags