KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > binding > jms > JMSConnection


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005 EBM Websourcing, http://www.ebmwebsourcing.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id: JmsConnexion.java 676 2006-06-27 15:44:03Z alouis $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.binding.jms;
23
24 import javax.jms.Connection JavaDoc;
25 import javax.jms.ConnectionFactory JavaDoc;
26 import javax.jms.Destination JavaDoc;
27 import javax.jms.JMSException JavaDoc;
28 import javax.jms.JMSSecurityException JavaDoc;
29 import javax.jms.MessageConsumer JavaDoc;
30 import javax.jms.MessageProducer JavaDoc;
31 import javax.jms.Session JavaDoc;
32 import javax.naming.Context JavaDoc;
33 import javax.naming.InitialContext JavaDoc;
34 import javax.naming.NamingException JavaDoc;
35
36 import org.objectweb.petals.component.common.PEtALSComponentSDKException;
37 import org.objectweb.petals.tools.jbicommon.util.StringHelper;
38
39 /**
40  * This class is used to create a JMS consumer or producer.
41  *
42  * @version $Revision: $ $Date: $
43  * @since Petals 1.1
44  * @author alouis
45  */

46 public class JMSConnection {
47
48     public enum Role {
49         JMS_PRODUCER, JMS_CONSUMER
50     };
51
52     private Context JavaDoc context;
53
54     private String JavaDoc connectionFactoryName;
55
56     private String JavaDoc destinationName;
57
58     private String JavaDoc user;
59
60     private String JavaDoc password;
61
62     private Connection JavaDoc connection;
63
64     private Session JavaDoc session;
65
66     private Destination JavaDoc destination;
67
68     private MessageConsumer JavaDoc messageConsumer;
69
70     private MessageProducer JavaDoc messageProducer;
71
72     private boolean transacted;
73
74     private boolean started = false;
75
76     private Role role;
77
78     /**
79      * Constructor. All required information to create the producer or consumer
80      * are set here.
81      *
82      * @param jndiContext
83      * the context where the connectionFactory and the destination
84      * can be retrieved.
85      * @param connectionFactoryName
86      * name of the jms connectionfactory
87      * @param destinationName
88      * name of the destination (queue or topic)
89      * @param transacted
90      * created a transacted session or not
91      * @param user
92      * user to use to connect to the connection factory if required
93      * @param password
94      * password to use to connect to the connection factory if
95      * required
96      * @param role
97      * JSM role : consumer or producer
98      */

99     public JMSConnection(Context JavaDoc jndiContext, String JavaDoc connectionFactoryName,
100         String JavaDoc destinationName, boolean transacted, String JavaDoc user,
101         String JavaDoc password, Role role) {
102         this.context = jndiContext;
103         this.connectionFactoryName = connectionFactoryName;
104         this.destinationName = destinationName;
105         this.user = user;
106         this.password = password;
107         this.transacted = transacted;
108         this.role = role;
109     }
110
111     /**
112      * Retrieve the connection factory and the destination from the JNDI
113      * context. Create a connection, using user/pwd if required, but do not
114      * start it.
115      *
116      * @throws PEtALSComponentSDKException
117      */

118     protected void init() throws PEtALSComponentSDKException {
119
120         checkJNDIAccess(context);
121
122         ConnectionFactory JavaDoc connFact = lookupConnectionFactory(context,
123             connectionFactoryName);
124
125         destination = lookupDestination(context, destinationName);
126
127         connection = createConnection(connFact, user, password);
128
129         try {
130             context.close();
131         } catch (NamingException JavaDoc e) {
132             throw new PEtALSComponentSDKException("JNDI context exception.", e);
133         }
134     }
135
136     /**
137      * Create a consumer/producer, using the specified transacted mode. Start
138      * the jms connection.
139      *
140      * @throws PEtALSComponentSDKException
141      * jmsException on starting the consumer/producer, or connection
142      * already started
143      */

144     public void start() throws PEtALSComponentSDKException {
145
146         if (!started) {
147
148             init();
149
150             try {
151                 session = connection.createSession(transacted,
152                     Session.AUTO_ACKNOWLEDGE);
153
154                 if (Role.JMS_CONSUMER.equals(role)) {
155                     try{
156                         messageConsumer = session.createConsumer(destination);
157                     } catch (JMSSecurityException JavaDoc e) {
158                         throw new PEtALSComponentSDKException("Can not start JMS consumer, READ right not granted.", e);
159                     }
160
161                 } else if (Role.JMS_PRODUCER.equals(role)) {
162                     messageProducer = session.createProducer(destination);
163
164                 } else {
165                     throw new PEtALSComponentSDKException("Unknown JMS Role :"
166                         + role);
167
168                 }
169                 connection.start();
170
171                 started = true;
172
173             } catch (JMSException JavaDoc e) {
174                 throw new PEtALSComponentSDKException(
175                     "Can not start the JMS consumer/producer.", e);
176             }
177
178         } else {
179             throw new PEtALSComponentSDKException(
180                 "JMS connection already started for the '" + destinationName
181                     + "' destination.");
182
183         }
184     }
185
186     /**
187      * Close messageConsumer/messageProducer, session and connection.
188      *
189      * @throws PEtALSComponentSDKException
190      * jmsException on close
191      */

192     public void stop() throws PEtALSComponentSDKException {
193         try {
194             if (messageConsumer != null) {
195                 messageConsumer.close();
196                 messageConsumer = null;
197             }
198             if (messageProducer != null) {
199                 messageProducer.close();
200                 messageProducer = null;
201             }
202             if (session != null) {
203                 session.close();
204                 session = null;
205             }
206             if (connection != null) {
207                 connection.close();
208                 connection = null;
209             }
210             started = false;
211         } catch (JMSException JavaDoc e) {
212             throw new PEtALSComponentSDKException(
213                 "Can no stop the JMS connection.", e);
214         }
215     }
216
217     public MessageConsumer JavaDoc getMessageConsumer() {
218         return messageConsumer;
219     }
220
221     public MessageProducer JavaDoc getMessageProducer() {
222         return messageProducer;
223     }
224
225     public Session JavaDoc getSession() {
226         return session;
227     }
228
229     public Role getRole() {
230         return role;
231     }
232
233     public String JavaDoc getDestinationName() {
234         return destinationName;
235     }
236
237     public boolean isTransacted() {
238         return transacted;
239     }
240
241     /**
242      * Test the JNDI access. A <code>listBindings("")</code> call is performed
243      * to check if no exception is thrown.
244      *
245      * @param ctx
246      * @throws PEtALSComponentSDKException
247      * the JNDI can not be accessed
248      */

249     private static void checkJNDIAccess(Context JavaDoc ctx)
250         throws PEtALSComponentSDKException {
251         try {
252             // test the JNDI access
253
ctx.listBindings("");
254         } catch (NamingException JavaDoc e) {
255             String JavaDoc providerUrl = "[JNDI provider URL not found]";
256             try {
257                 providerUrl = ctx.getEnvironment().get(
258                     InitialContext.PROVIDER_URL).toString();
259             } catch (NamingException JavaDoc e1) {
260                 throw new PEtALSComponentSDKException(
261                     "Exception during JNDI Context access.", e);
262             }
263             throw new PEtALSComponentSDKException(
264                 "The JNDI can not be accessed at " + providerUrl);
265         }
266     }
267
268     /**
269      * Retrieve the connectionFactory in the JNDI context
270      *
271      * @param ctx
272      * must be connected, must be non null
273      * @param connectionFactoryName
274      * must be non null
275      * @return the connectionFactory
276      * @throws PEtALSComponentSDKException
277      * the factory is not found
278      */

279     private static ConnectionFactory JavaDoc lookupConnectionFactory(Context JavaDoc ctx,
280         String JavaDoc connectionFactoryName) throws PEtALSComponentSDKException {
281         ConnectionFactory JavaDoc connFact = null;
282         try {
283             connFact = (ConnectionFactory JavaDoc) ctx.lookup(connectionFactoryName);
284         } catch (NamingException JavaDoc e) {
285             throw new PEtALSComponentSDKException("ConnectionFactory '"
286                 + connectionFactoryName + "' not found in the JNDI context.", e);
287         }
288         return connFact;
289     }
290
291     /**
292      * Retrieve the destination in the JNDI context
293      *
294      * @param ctx
295      * must be connected, must be non null
296      * @param destination
297      * must be non null
298      * @return the destination
299      * @throws PEtALSComponentSDKException
300      * the destination is not found
301      */

302     private static Destination JavaDoc lookupDestination(Context JavaDoc ctx,
303         String JavaDoc destinationName) throws PEtALSComponentSDKException {
304         Destination JavaDoc dest = null;
305         try {
306             dest = (Destination JavaDoc) ctx.lookup(destinationName);
307         } catch (NamingException JavaDoc e) {
308             throw new PEtALSComponentSDKException("Destination '"
309                 + destinationName + "' not found in the JNDI context.", e);
310         }
311         return dest;
312     }
313
314     /**
315      * Create a JMS Connection.<br>
316      * If user is null, call a <code>connectionFactory.createConnection()</code>.<br>
317      * If user is not null, call a
318      * <code>connectionFactory.createConnection(user,password)</code>.
319      *
320      * @param connectionFactory
321      * must be non null
322      * @param user
323      * can be null
324      * @param password
325      * can be null
326      * @return a JMS connection
327      * @throws PEtALSComponentSDKException
328      * JMS security exception or connectionfactory exception
329      */

330     private static Connection JavaDoc createConnection(
331         ConnectionFactory JavaDoc connectionFactory, String JavaDoc user, String JavaDoc password)
332         throws PEtALSComponentSDKException {
333         Connection JavaDoc conn = null;
334         try {
335             if (StringHelper.isNullOrEmpty(user)) {
336                 conn = connectionFactory.createConnection();
337             } else {
338                 conn = connectionFactory.createConnection(user, password);
339             }
340
341         } catch (JMSSecurityException JavaDoc e) {
342             throw new PEtALSComponentSDKException(
343                 "Invalid user/password to connect to the JMS connectionFactory.",
344                 e);
345         } catch (JMSException JavaDoc e) {
346             throw new PEtALSComponentSDKException(
347                 "JMS connectionFactory error.", e);
348         }
349         return conn;
350     }
351 }
Popular Tags