001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.jms.connection;
030:
031: import com.caucho.config.ConfigException;
032: import com.caucho.management.j2ee.J2EEManagedObject;
033: import com.caucho.management.j2ee.JMSResource;
034: import com.caucho.jms.memory.*;
035: import com.caucho.util.L10N;
036: import com.caucho.webbeans.component.HandleAware;
037:
038: import javax.jms.*;
039: import javax.sql.DataSource;
040: import java.sql.SQLException;
041: import java.util.ArrayList;
042: import java.util.Collections;
043: import java.util.HashMap;
044: import java.util.List;
045: import java.util.logging.Logger;
046:
047: /**
048: * A sample connection factory.
049: */
050: public class ConnectionFactoryImpl implements XAQueueConnectionFactory,
051: XATopicConnectionFactory, java.io.Serializable, HandleAware {
052: private static final Logger log = Logger
053: .getLogger(ConnectionFactoryImpl.class.getName());
054: private static final L10N L = new L10N(ConnectionFactoryImpl.class);
055:
056: private String _name;
057: private String _clientID;
058:
059: private String _user;
060: private String _password;
061:
062: // private JdbcManager _jdbcManager;
063:
064: private List<ConnectionImpl> _connections = Collections
065: .synchronizedList(new ArrayList<ConnectionImpl>());
066:
067: private HashMap<String, Queue> _queues = new HashMap<String, Queue>();
068:
069: private HashMap<String, Topic> _topics = new HashMap<String, Topic>();
070:
071: private Object _serializationHandle;
072:
073: public ConnectionFactoryImpl() {
074: }
075:
076: /**
077: * Sets the user.
078: */
079: public void setUser(String user) {
080: _user = user;
081: }
082:
083: /**
084: * Sets the password.
085: */
086: public void setPassword(String password) {
087: _password = password;
088: }
089:
090: /**
091: * Sets the name of the connection factory.
092: */
093: public void setName(String name) {
094: _name = name;
095: }
096:
097: /**
098: * Returns the name of the connection factory.
099: */
100: public String getName() {
101: // XXX: should this default to client-id and/or jndi-name? (jndi-name is there
102: // when it is configured as a resource)
103: return _name;
104: }
105:
106: /**
107: * Sets the client id.
108: */
109: public void setClientID(String id) {
110: _clientID = id;
111: }
112:
113: /**
114: * Sets the JDBC manager.
115: */
116: public void setDataSource(DataSource dataSource) {
117: /*
118: if (_jdbcManager == null)
119: _jdbcManager = new JdbcManager();
120:
121: _jdbcManager.setDataSource(dataSource);
122: */
123: }
124:
125: /**
126: * Returns the JDBC manager.
127: */
128: /*
129: public JdbcManager getJdbcManager()
130: {
131: return new JdbcManager();
132: }
133: */
134:
135: /**
136: * Sets the serialization handle
137: */
138: public void setSerializationHandle(Object handle) {
139: _serializationHandle = handle;
140: }
141:
142: /**
143: * Initialize the connection factory.
144: */
145: public void init() throws ConfigException, SQLException {
146: /*
147: if (_jdbcManager != null)
148: _jdbcManager.init();
149: */
150:
151: //J2EEManagedObject.register(new JMSResource(this));
152: }
153:
154: /**
155: * Creates a new queue connection
156: */
157: public Connection createConnection() throws JMSException {
158: return createConnection(_user, _password);
159: }
160:
161: /**
162: * Creates a new connection
163: *
164: * @param username the username to authenticate with the server.
165: * @param password the password to authenticate with the server.
166: *
167: * @return the created connection
168: */
169: public Connection createConnection(String username, String password)
170: throws JMSException {
171: authenticate(username, password);
172:
173: ConnectionImpl conn = new ConnectionImpl(this );
174:
175: if (_clientID != null) {
176: if (findByClientID(_clientID) != null)
177: throw new JMSException(
178: L
179: .l(
180: "ClientID[{0}] is only allowed for a single connection.",
181: _clientID));
182: conn.setClientID(_clientID);
183: }
184:
185: addConnection(conn);
186:
187: return conn;
188: }
189:
190: protected void addConnection(ConnectionImpl conn) {
191: _connections.add(conn);
192: }
193:
194: /**
195: * Returns the connection named by the specified client id.
196: */
197: public ConnectionImpl findByClientID(String id) {
198: for (int i = 0; i < _connections.size(); i++) {
199: ConnectionImpl conn = _connections.get(i);
200:
201: try {
202: if (id.equals(conn.getClientID()))
203: return conn;
204: } catch (Throwable e) {
205: }
206: }
207:
208: return null;
209: }
210:
211: /**
212: * Removes a connection once closed.
213: */
214: public void removeConnection(ConnectionImpl conn) {
215: _connections.remove(conn);
216: }
217:
218: /**
219: * Creates queue.
220: */
221: public Queue createQueue(String name) throws JMSException {
222: /*
223: try {
224: synchronized (_queues) {
225: Queue queue = _queues.get(name);
226:
227: if (queue != null)
228: return queue;
229:
230: if (_jdbcManager != null) {
231: JdbcQueue jdbcQueue = new JdbcQueue();
232: jdbcQueue.setJdbcManager(_jdbcManager);
233: jdbcQueue.setQueueName(name);
234: jdbcQueue.init();
235:
236: _queues.put(name, jdbcQueue);
237:
238: return jdbcQueue;
239: }
240: else {
241: MemoryQueue memoryQueue = new MemoryQueue();
242: memoryQueue.setQueueName(name);
243:
244: _queues.put(name, memoryQueue);
245:
246: return memoryQueue;
247: }
248: }
249: } catch (RuntimeException e) {
250: throw e;
251: } catch (Exception e) {
252: throw new JMSExceptionWrapper(e);
253: }
254: */
255:
256: throw new UnsupportedOperationException();
257: }
258:
259: /**
260: * Creates topics.
261: */
262: public Topic createTopic(String name) throws JMSException {
263: /*
264: try {
265: synchronized (_topics) {
266: Topic topic = _topics.get(name);
267:
268: if (topic != null)
269: return topic;
270:
271: if (_jdbcManager != null) {
272: JdbcTopic jdbcTopic = new JdbcTopic();
273: jdbcTopic.setJdbcManager(_jdbcManager);
274: jdbcTopic.setTopicName(name);
275: jdbcTopic.init();
276:
277: _topics.put(name, jdbcTopic);
278:
279: return jdbcTopic;
280: }
281: else {
282: MemoryTopic memoryTopic = new MemoryTopic();
283: memoryTopic.setTopicName(name);
284:
285: _topics.put(name, memoryTopic);
286:
287: return memoryTopic;
288: }
289: }
290: } catch (RuntimeException e) {
291: throw e;
292: } catch (Exception e) {
293: throw new JMSExceptionWrapper(e);
294: }
295: */
296:
297: throw new UnsupportedOperationException();
298: }
299:
300: protected void authenticate(String username, String password)
301: throws JMSException {
302: if (_user != null && !_user.equals(username)
303: || _password != null && !_password.equals(password)) {
304: throw new JMSSecurityException(L.l(
305: "'{0}' is an unknown user", username));
306: }
307: }
308:
309: /**
310: * Creates a new queue connection
311: */
312: public QueueConnection createQueueConnection() throws JMSException {
313: return createQueueConnection(null, null);
314: }
315:
316: /**
317: * Creates a new queue connection
318: *
319: * @param username the username to authenticate with the server.
320: * @param password the password to authenticate with the server.
321: *
322: * @return the created connection
323: */
324: public QueueConnection createQueueConnection(String username,
325: String password) throws JMSException {
326: authenticate(username, password);
327:
328: QueueConnectionImpl conn = new QueueConnectionImpl(this );
329:
330: addConnection(conn);
331:
332: return conn;
333: }
334:
335: /**
336: * Creates a new queue connection
337: */
338: public TopicConnection createTopicConnection() throws JMSException {
339: return createTopicConnection(null, null);
340: }
341:
342: /**
343: * Creates a new queue connection
344: *
345: * @param username the username to authenticate with the server.
346: * @param password the password to authenticate with the server.
347: *
348: * @return the created connection
349: */
350: public TopicConnection createTopicConnection(String username,
351: String password) throws JMSException {
352: authenticate(username, password);
353:
354: TopicConnectionImpl conn = new TopicConnectionImpl(this , true);
355:
356: addConnection(conn);
357:
358: return conn;
359: }
360:
361: public XAConnection createXAConnection() throws JMSException {
362: return createXAConnection(null, null);
363: }
364:
365: public XAConnection createXAConnection(String username,
366: String password) throws JMSException {
367: authenticate(username, password);
368:
369: ConnectionImpl conn = new ConnectionImpl(this , true);
370:
371: if (_clientID != null) {
372: if (findByClientID(_clientID) != null)
373: throw new JMSException(
374: L
375: .l(
376: "ClientID[{0}] is only allowed for a single connection.",
377: _clientID));
378: conn.setClientID(_clientID);
379: }
380:
381: addConnection(conn);
382:
383: return conn;
384: }
385:
386: public XAQueueConnection createXAQueueConnection()
387: throws JMSException {
388: return createXAQueueConnection(null, null);
389: }
390:
391: public XAQueueConnection createXAQueueConnection(String username,
392: String password) throws JMSException {
393: authenticate(username, password);
394:
395: QueueConnectionImpl conn = new QueueConnectionImpl(this , true);
396:
397: addConnection(conn);
398:
399: return conn;
400: }
401:
402: public XATopicConnection createXATopicConnection()
403: throws JMSException {
404: return createXATopicConnection(null, null);
405: }
406:
407: public XATopicConnection createXATopicConnection(String username,
408: String password) throws JMSException {
409: authenticate(username, password);
410:
411: TopicConnectionImpl conn = new TopicConnectionImpl(this , true);
412:
413: addConnection(conn);
414:
415: return conn;
416: }
417:
418: /**
419: * Serialization code
420: */
421: private Object writeReplace() {
422: return _serializationHandle;
423: }
424:
425: public String toString() {
426: return getClass().getSimpleName() + "[]";
427: }
428: }
|