KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mq > SpyConnectionFactory


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 JavaDoc;
25 import java.util.Properties JavaDoc;
26
27 import javax.jms.Connection JavaDoc;
28 import javax.jms.ConnectionFactory JavaDoc;
29 import javax.jms.JMSException JavaDoc;
30 import javax.jms.QueueConnection JavaDoc;
31 import javax.jms.QueueConnectionFactory JavaDoc;
32 import javax.jms.TopicConnection JavaDoc;
33 import javax.jms.TopicConnectionFactory JavaDoc;
34 import javax.naming.NamingException JavaDoc;
35 import javax.naming.Reference JavaDoc;
36 import javax.naming.Referenceable JavaDoc;
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 JavaDoc, ConnectionFactory JavaDoc, QueueConnectionFactory JavaDoc, TopicConnectionFactory JavaDoc, Referenceable JavaDoc
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 JavaDoc config)
75    {
76       this.factory = new GenericConnectionFactory(null, config);
77    }
78
79    /**
80     * For testing
81     */

82    public Properties JavaDoc getProperties()
83    {
84       if (factory == null)
85          return null;
86       else
87          return factory.getProperties();
88    }
89
90    public Reference JavaDoc getReference() throws NamingException JavaDoc
91    {
92       return new Reference JavaDoc("org.jboss.mq.SpyConnectionFactory", new ObjectRefAddr("DCF", factory),
93             "org.jboss.mq.referenceable.SpyConnectionFactoryObjectFactory", null);
94    }
95
96    public Connection createConnection() throws JMSException JavaDoc
97    {
98       return internalCreateConnection(SpyConnection.UNIFIED);
99    }
100
101    public Connection createConnection(String JavaDoc userName, String JavaDoc password) throws JMSException JavaDoc
102    {
103       return internalCreateConnection(SpyConnection.UNIFIED, userName, password);
104    }
105
106    public QueueConnection JavaDoc createQueueConnection() throws JMSException JavaDoc
107    {
108       return (QueueConnection JavaDoc) internalCreateConnection(SpyConnection.QUEUE);
109    }
110
111    public QueueConnection JavaDoc createQueueConnection(String JavaDoc userName, String JavaDoc password) throws JMSException JavaDoc
112    {
113       return (QueueConnection JavaDoc) internalCreateConnection(SpyConnection.QUEUE, userName, password);
114    }
115
116    public TopicConnection JavaDoc createTopicConnection() throws JMSException JavaDoc
117    {
118       return (TopicConnection JavaDoc) internalCreateConnection(SpyConnection.TOPIC);
119    }
120
121    public TopicConnection JavaDoc createTopicConnection(String JavaDoc userName, String JavaDoc password) throws JMSException JavaDoc
122    {
123       return (TopicConnection JavaDoc) 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 JavaDoc
134    {
135       try
136       {
137          return new SpyConnection(type, factory);
138       }
139       catch (JMSException JavaDoc e)
140       {
141          throw e;
142       }
143       catch (Exception JavaDoc 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 JavaDoc userName, String JavaDoc password) throws JMSException JavaDoc
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 JavaDoc e)
170       {
171          throw e;
172       }
173       catch (Exception JavaDoc e)
174       {
175          throw new SpyJMSException("Failed to create Connection", e);
176       }
177    }
178 }
Popular Tags