1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */
22 package org.jboss.mq;
23
24 import java.io.Serializable;
25 import java.util.Properties;
26
27 import javax.jms.Connection;
28 import javax.jms.ConnectionFactory;
29 import javax.jms.JMSException;
30 import javax.jms.QueueConnection;
31 import javax.jms.QueueConnectionFactory;
32 import javax.jms.TopicConnection;
33 import javax.jms.TopicConnectionFactory;
34 import javax.naming.NamingException;
35 import javax.naming.Reference;
36 import javax.naming.Referenceable;
37
38 import org.jboss.mq.referenceable.ObjectRefAddr;
39
40 /**
41 * This class implements <code>javax.jms.TopicConnectionFactory</code> and
42 * <code>javax.jms.QueueConnectionFactory</code>.
43 *
44 * @author Norbert Lataille (Norbert.Lataille@m4x.org)
45 * @author Hiram Chirino (Cojonudo14@hotmail.com)
46 * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
47 * @author <a href="mailto:adrian@jboss.org">Adrian Brock</a>
48 * @version <tt>$Revision: 38288 $</tt>
49 */
50 public class SpyConnectionFactory
51 implements Serializable, ConnectionFactory, QueueConnectionFactory, TopicConnectionFactory, Referenceable
52 {
53 /** The serialVersionUID */
54 static final long serialVersionUID = 3392566934963731105L;
55
56 /** The delegate factory */
57 protected GenericConnectionFactory factory;
58
59 /**
60 * Create a new SpyConnectionFactory
61 *
62 * @param factory the delegate factory
63 */
64 public SpyConnectionFactory(GenericConnectionFactory factory)
65 {
66 this.factory = factory;
67 }
68
69 /**
70 * Create a new SpyConnectionFactory
71 *
72 * @param config the configuration
73 */
74 public SpyConnectionFactory(Properties config)
75 {
76 this.factory = new GenericConnectionFactory(null, config);
77 }
78
79 /**
80 * For testing
81 */
82 public Properties getProperties()
83 {
84 if (factory == null)
85 return null;
86 else
87 return factory.getProperties();
88 }
89
90 public Reference getReference() throws NamingException
91 {
92 return new Reference("org.jboss.mq.SpyConnectionFactory", new ObjectRefAddr("DCF", factory),
93 "org.jboss.mq.referenceable.SpyConnectionFactoryObjectFactory", null);
94 }
95
96 public Connection createConnection() throws JMSException
97 {
98 return internalCreateConnection(SpyConnection.UNIFIED);
99 }
100
101 public Connection createConnection(String userName, String password) throws JMSException
102 {
103 return internalCreateConnection(SpyConnection.UNIFIED, userName, password);
104 }
105
106 public QueueConnection createQueueConnection() throws JMSException
107 {
108 return (QueueConnection) internalCreateConnection(SpyConnection.QUEUE);
109 }
110
111 public QueueConnection createQueueConnection(String userName, String password) throws JMSException
112 {
113 return (QueueConnection) internalCreateConnection(SpyConnection.QUEUE, userName, password);
114 }
115
116 public TopicConnection createTopicConnection() throws JMSException
117 {
118 return (TopicConnection) internalCreateConnection(SpyConnection.TOPIC);
119 }
120
121 public TopicConnection createTopicConnection(String userName, String password) throws JMSException
122 {
123 return (TopicConnection) internalCreateConnection(SpyConnection.TOPIC, userName, password);
124 }
125
126 /**
127 * Create a connection
128 *
129 * @param type the type
130 * @return the connection
131 * @throws JMSException for any error
132 */
133 protected Connection internalCreateConnection(int type) throws JMSException
134 {
135 try
136 {
137 return new SpyConnection(type, factory);
138 }
139 catch (JMSException e)
140 {
141 throw e;
142 }
143 catch (Exception e)
144 {
145 throw new SpyJMSException("Failed to create Connection", e);
146 }
147 }
148
149 /**
150 * Create a connection
151 *
152 * @param type the type
153 * @param userName the user name
154 * @param password the password
155 * @return the connection
156 * @throws JMSException for any error
157 */
158 protected Connection internalCreateConnection(int type, String userName, String password) throws JMSException
159 {
160 try
161 {
162 if (userName == null)
163 throw new SpyJMSException("Username is null");
164 if (password == null)
165 throw new SpyJMSException("Password is null");
166
167 return new SpyConnection(type, userName, password, factory);
168 }
169 catch (JMSException e)
170 {
171 throw e;
172 }
173 catch (Exception e)
174 {
175 throw new SpyJMSException("Failed to create Connection", e);
176 }
177 }
178 }