KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: Jms11Support.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.Session JavaDoc;
22 import javax.jms.Topic JavaDoc;
23 import javax.naming.Context JavaDoc;
24 import javax.naming.NamingException JavaDoc;
25
26 /**
27  * <code>Jms11Support</code> is a template class to provide an abstraction to to
28  * the JMS 1.1 API specification.
29  */

30
31 public class Jms11Support implements JmsSupport
32 {
33     protected Context JavaDoc context;
34     protected boolean jndiDestinations = false;
35     protected boolean forceJndiDestinations = false;
36     protected JmsConnector connector;
37
38     public Jms11Support(JmsConnector connector,
39                         Context JavaDoc context,
40                         boolean jndiDestinations,
41                         boolean forceJndiDestinations)
42     {
43         this.connector = connector;
44         this.context = context;
45         this.jndiDestinations = jndiDestinations;
46         this.forceJndiDestinations = forceJndiDestinations;
47     }
48
49     public Connection JavaDoc createConnection(ConnectionFactory JavaDoc connectionFactory, String JavaDoc username, String JavaDoc password)
50         throws JMSException JavaDoc
51     {
52         if (connectionFactory == null)
53         {
54             throw new IllegalArgumentException JavaDoc("connectionFactory cannot be null");
55         }
56         return connectionFactory.createConnection(username, password);
57     }
58
59     public Connection JavaDoc createConnection(ConnectionFactory JavaDoc connectionFactory) throws JMSException JavaDoc
60     {
61         if (connectionFactory == null)
62         {
63             throw new IllegalArgumentException JavaDoc("connectionFactory cannot be null");
64         }
65         return connectionFactory.createConnection();
66     }
67
68     public Session JavaDoc createSession(Connection JavaDoc connection,
69                                  boolean topic,
70                                  boolean transacted,
71                                  int ackMode,
72                                  boolean noLocal) throws JMSException JavaDoc
73     {
74         return connection.createSession(transacted, (transacted ? Session.SESSION_TRANSACTED : ackMode));
75     }
76
77     public MessageProducer JavaDoc createProducer(Session JavaDoc session, Destination JavaDoc destination, boolean topic)
78         throws JMSException JavaDoc
79     {
80         return session.createProducer(destination);
81     }
82
83     public MessageConsumer JavaDoc createConsumer(Session JavaDoc session, Destination JavaDoc destination, boolean topic)
84         throws JMSException JavaDoc
85     {
86         return createConsumer(session, destination, null, false, null, topic);
87     }
88
89     public MessageConsumer JavaDoc createConsumer(Session JavaDoc session,
90                                           Destination JavaDoc destination,
91                                           String JavaDoc messageSelector,
92                                           boolean noLocal,
93                                           String JavaDoc durableName,
94                                           boolean topic) throws JMSException JavaDoc
95     {
96         if (durableName == null)
97         {
98             if (topic)
99             {
100                 return session.createConsumer(destination, messageSelector, noLocal);
101             }
102             else
103             {
104                 return session.createConsumer(destination, messageSelector);
105             }
106         }
107         else
108         {
109             if (topic)
110             {
111                 return session.createDurableSubscriber((Topic JavaDoc) destination, durableName, messageSelector,
112                     noLocal);
113             }
114             else
115             {
116                 throw new JMSException JavaDoc(
117                     "A durable subscriber name was set but the destination was not a Topic");
118             }
119         }
120     }
121
122     public Destination JavaDoc createDestination(Session JavaDoc session, String JavaDoc name, boolean topic) throws JMSException JavaDoc
123     {
124         if (session == null)
125         {
126             throw new IllegalArgumentException JavaDoc("Session cannot be null when creating a destination");
127         }
128         if (name == null)
129         {
130             throw new IllegalArgumentException JavaDoc("Destination name cannot be null when creating a destination");
131         }
132
133         if (jndiDestinations)
134         {
135             if (context == null)
136             {
137                 throw new IllegalArgumentException JavaDoc(
138                     "Jndi Context name cannot be null when looking up a destination");
139             }
140             Destination JavaDoc dest = getJndiDestination(name);
141             if (dest != null)
142             {
143                 return dest;
144             }
145             else if (forceJndiDestinations)
146             {
147                 throw new JMSException JavaDoc("JNDI destination not found with name: " + name);
148             }
149         }
150
151         if (topic)
152         {
153             return session.createTopic(name);
154         }
155         else
156         {
157             return session.createQueue(name);
158         }
159     }
160
161     protected Destination JavaDoc getJndiDestination(String JavaDoc name) throws JMSException JavaDoc
162     {
163         Object JavaDoc temp;
164         try
165         {
166             temp = context.lookup(name);
167         }
168         catch (NamingException JavaDoc e)
169         {
170             throw new JMSException JavaDoc("Failed to look up destination: " + e.getMessage());
171         }
172         if (temp != null)
173         {
174             if (temp instanceof Destination JavaDoc)
175             {
176                 return (Destination JavaDoc)temp;
177             }
178             else if (forceJndiDestinations)
179             {
180                 throw new JMSException JavaDoc("JNDI destination not found with name: " + name);
181             }
182         }
183         return null;
184     }
185
186     public Destination JavaDoc createTemporaryDestination(Session JavaDoc session, boolean topic) throws JMSException JavaDoc
187     {
188         if (session == null)
189         {
190             throw new IllegalArgumentException JavaDoc("Session cannot be null when creating a destination");
191         }
192
193         if (topic)
194         {
195             return session.createTemporaryTopic();
196         }
197         else
198         {
199             return session.createTemporaryQueue();
200         }
201     }
202
203     public void send(MessageProducer JavaDoc producer, Message JavaDoc message, boolean topic) throws JMSException JavaDoc
204     {
205         send(producer, message, connector.isPersistentDelivery(), Message.DEFAULT_PRIORITY,
206             Message.DEFAULT_TIME_TO_LIVE, topic);
207     }
208
209     public void send(MessageProducer JavaDoc producer, Message JavaDoc message, Destination JavaDoc dest, boolean topic)
210         throws JMSException JavaDoc
211     {
212         send(producer, message, dest, connector.isPersistentDelivery(), Message.DEFAULT_PRIORITY,
213             Message.DEFAULT_TIME_TO_LIVE, topic);
214     }
215
216     public void send(MessageProducer JavaDoc producer,
217                      Message JavaDoc message,
218                      boolean persistent,
219                      int priority,
220                      long ttl,
221                      boolean topic) throws JMSException JavaDoc
222     {
223         producer.send(message, (persistent ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT),
224             priority, ttl);
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         producer.send(dest, message, (persistent ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT),
236             priority, ttl);
237     }
238
239 }
240
Popular Tags