KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > jms > Jms102bSupport


1 /*
2  * $Id: Jms102bSupport.java 3937 2006-11-20 16:04:25Z lajos $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.providers.jms;
12
13 import javax.jms.Connection JavaDoc;
14 import javax.jms.ConnectionFactory JavaDoc;
15 import javax.jms.DeliveryMode JavaDoc;
16 import javax.jms.Destination JavaDoc;
17 import javax.jms.JMSException JavaDoc;
18 import javax.jms.Message JavaDoc;
19 import javax.jms.MessageConsumer JavaDoc;
20 import javax.jms.MessageProducer JavaDoc;
21 import javax.jms.Queue JavaDoc;
22 import javax.jms.QueueConnection JavaDoc;
23 import javax.jms.QueueConnectionFactory JavaDoc;
24 import javax.jms.QueueSender JavaDoc;
25 import javax.jms.QueueSession JavaDoc;
26 import javax.jms.Session JavaDoc;
27 import javax.jms.Topic JavaDoc;
28 import javax.jms.TopicConnection JavaDoc;
29 import javax.jms.TopicConnectionFactory JavaDoc;
30 import javax.jms.TopicPublisher JavaDoc;
31 import javax.jms.TopicSession JavaDoc;
32 import javax.naming.Context JavaDoc;
33
34 /**
35  * <code>Jms102bSupport</code> is a template class to provide an abstraction to the
36  * JMS 1.0.2b API specification.
37  */

38
39 public class Jms102bSupport extends Jms11Support
40 {
41
42     public Jms102bSupport(JmsConnector connector,
43                           Context JavaDoc context,
44                           boolean jndiDestinations,
45                           boolean forceJndiDestinations)
46     {
47         super(connector, context, jndiDestinations, forceJndiDestinations);
48     }
49
50     public Connection JavaDoc createConnection(ConnectionFactory JavaDoc connectionFactory, String JavaDoc username, String JavaDoc password)
51         throws JMSException JavaDoc
52     {
53         if (connectionFactory == null)
54         {
55             throw new IllegalArgumentException JavaDoc("connectionFactory cannot be null");
56         }
57         if (connectionFactory instanceof QueueConnectionFactory JavaDoc)
58         {
59             return ((QueueConnectionFactory JavaDoc) connectionFactory).createQueueConnection(username, password);
60         }
61         else if (connectionFactory instanceof TopicConnectionFactory JavaDoc)
62         {
63             return ((TopicConnectionFactory JavaDoc) connectionFactory).createTopicConnection(username, password);
64         }
65         else
66         {
67             throw new IllegalArgumentException JavaDoc("Unsupported ConnectionFactory type: "
68                                                + connectionFactory.getClass().getName());
69         }
70     }
71
72     public Connection JavaDoc createConnection(ConnectionFactory JavaDoc connectionFactory) throws JMSException JavaDoc
73     {
74         if (connectionFactory == null)
75         {
76             throw new IllegalArgumentException JavaDoc("connectionFactory cannot be null");
77         }
78         if (connectionFactory instanceof QueueConnectionFactory JavaDoc)
79         {
80             return ((QueueConnectionFactory JavaDoc) connectionFactory).createQueueConnection();
81         }
82         else if (connectionFactory instanceof TopicConnectionFactory JavaDoc)
83         {
84             return ((TopicConnectionFactory JavaDoc) connectionFactory).createTopicConnection();
85         }
86         else
87         {
88             throw new IllegalArgumentException JavaDoc("Unsupported ConnectionFactory type: "
89                                                + connectionFactory.getClass().getName());
90         }
91     }
92
93     public Session JavaDoc createSession(Connection JavaDoc connection,
94                                  boolean topic,
95                                  boolean transacted,
96                                  int ackMode,
97                                  boolean noLocal) throws JMSException JavaDoc
98     {
99         if (topic && connection instanceof TopicConnection JavaDoc)
100         {
101             return ((TopicConnection JavaDoc) connection).createTopicSession(noLocal, ackMode);
102         }
103         else if (connection instanceof QueueConnection JavaDoc)
104         {
105             return ((QueueConnection JavaDoc) connection).createQueueSession(transacted, ackMode);
106         }
107         else
108         {
109             throw new IllegalArgumentException JavaDoc("Connection and domain type do not match");
110         }
111     }
112
113     public MessageConsumer JavaDoc createConsumer(Session JavaDoc session,
114                                           Destination JavaDoc destination,
115                                           String JavaDoc messageSelector,
116                                           boolean noLocal,
117                                           String JavaDoc durableName,
118                                           boolean topic) throws JMSException JavaDoc
119     {
120         if (topic && session instanceof TopicSession JavaDoc)
121         {
122             if (durableName == null)
123             {
124                 return ((TopicSession JavaDoc) session).createSubscriber((Topic JavaDoc) destination, messageSelector, noLocal);
125             }
126             else
127             {
128                 return session.createDurableSubscriber((Topic JavaDoc) destination, durableName, messageSelector, noLocal);
129             }
130         }
131         else if (session instanceof QueueSession JavaDoc)
132         {
133             if (messageSelector != null)
134             {
135                 return ((QueueSession JavaDoc) session).createReceiver((Queue JavaDoc) destination, messageSelector);
136             }
137             else
138             {
139                 return ((QueueSession JavaDoc) session).createReceiver((Queue JavaDoc) destination);
140             }
141         }
142         else
143         {
144             throw new IllegalArgumentException JavaDoc("Session and domain type do not match");
145         }
146     }
147
148     public MessageProducer JavaDoc createProducer(Session JavaDoc session, Destination JavaDoc dest, boolean topic)
149         throws JMSException JavaDoc
150     {
151         if (topic && session instanceof TopicSession JavaDoc)
152         {
153             return ((TopicSession JavaDoc) session).createPublisher((Topic JavaDoc) dest);
154         }
155         else if (session instanceof QueueSession JavaDoc)
156         {
157             return ((QueueSession JavaDoc) session).createSender((Queue JavaDoc) dest);
158         }
159         else
160         {
161             throw new IllegalArgumentException JavaDoc("Session and domain type do not match");
162         }
163     }
164
165     public Destination JavaDoc createDestination(Session JavaDoc session, String JavaDoc name, boolean topic) throws JMSException JavaDoc
166     {
167         if (session == null)
168         {
169             throw new IllegalArgumentException JavaDoc("Session cannot be null when creating a destination");
170         }
171         if (name == null)
172         {
173             throw new IllegalArgumentException JavaDoc("Destination name cannot be null when creating a destination");
174         }
175
176         if (jndiDestinations)
177         {
178             if (context == null)
179             {
180                 throw new IllegalArgumentException JavaDoc(
181                     "Jndi Context name cannot be null when looking up a destination");
182             }
183             Destination JavaDoc dest = getJndiDestination(name);
184             if (dest != null)
185             {
186                 return dest;
187             }
188             else if (forceJndiDestinations)
189             {
190                 throw new JMSException JavaDoc("JNDI destination not found with name: " + name);
191             }
192         }
193
194         if (topic)
195         {
196             return session.createTopic(name);
197         }
198         else
199         {
200             return session.createQueue(name);
201         }
202     }
203
204     public void send(MessageProducer JavaDoc producer,
205                      Message JavaDoc message,
206                      boolean persistent,
207                      int priority,
208                      long ttl,
209                      boolean topic) throws JMSException JavaDoc
210     {
211         if (topic && producer instanceof TopicPublisher JavaDoc)
212         {
213             ((TopicPublisher JavaDoc) producer).publish(message, (persistent
214                             ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT), priority, ttl);
215         }
216         else if (producer instanceof QueueSender JavaDoc)
217         {
218             producer.send(message, (persistent
219                             ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT), priority, ttl);
220         }
221         else
222         {
223             throw new IllegalArgumentException JavaDoc("Producer and domain type do not match");
224         }
225     }
226
227     public void send(MessageProducer JavaDoc producer,
228                      Message JavaDoc message,
229                      Destination JavaDoc dest,
230                      boolean persistent,
231                      int priority,
232                      long ttl,
233                      boolean topic) throws JMSException JavaDoc
234     {
235         if (topic && producer instanceof TopicPublisher JavaDoc)
236         {
237             ((TopicPublisher JavaDoc) producer).publish((Topic JavaDoc) dest, message, (persistent
238                             ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT), priority, ttl);
239         }
240         else if (producer instanceof QueueSender JavaDoc)
241         {
242             ((QueueSender JavaDoc) producer).send((Queue JavaDoc) dest, message, (persistent
243                             ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT), priority, ttl);
244         }
245         else
246         {
247             throw new IllegalArgumentException JavaDoc("Producer and domain type do not match");
248         }
249     }
250
251 }
252
Popular Tags