2011/08/05 - Jakarta Cactus has been retired.

For more information, please explore the Attic.

View Javadoc

1   /* 
2    * ========================================================================
3    * 
4    * Licensed to the Apache Software Foundation (ASF) under one or more
5    * contributor license agreements.  See the NOTICE file distributed with
6    * this work for additional information regarding copyright ownership.
7    * The ASF licenses this file to You under the Apache License, Version 2.0
8    * (the "License"); you may not use this file except in compliance with
9    * the License.  You may obtain a copy of the License at
10   * 
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   * 
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * 
19   * ========================================================================
20   */
21  package org.apache.cactus.internal.client.jms;
22  
23  import java.util.Hashtable;
24  import javax.jms.QueueConnection;
25  import javax.jms.JMSException;
26  import javax.jms.QueueSession;
27  import javax.jms.Session;
28  import javax.jms.QueueConnectionFactory;
29  import javax.jms.Queue;
30  import javax.jms.QueueSender;
31  import javax.naming.InitialContext;
32  import javax.naming.Context;
33  import javax.naming.NamingException;
34  
35  import org.apache.cactus.util.JmsConfiguration;
36  import org.apache.cactus.util.ChainedRuntimeException;
37  
38  /**
39   * Helper class to send a JMS message.
40   *
41   * @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
42   *
43   * @since 1.4
44   *
45   * @version $Id$
46   */
47  public class JmsClientHelper
48  {
49      /**
50       * The JMS queue session used to send messages to the server side.
51       */
52      private static QueueSession queueSession;
53  
54      /**
55       * Create a JMS Queue Connection to be able to send messages later on.
56       *
57       * @return the created queue connection
58       * @exception JMSException if an error occurred
59       */
60      protected static QueueConnection createQueueConnection()
61          throws JMSException
62      {
63          QueueConnection queueConnection =
64              getQueueConnnectionFactory().createQueueConnection();
65          return queueConnection;
66      }
67  
68      /**
69       * Return the Queue session that we will use to send all JMS messages
70       * (the Session is created if it is the first time this method is
71       * called).
72       *
73       * @return the created Queue Session
74       */
75      public static synchronized QueueSession getQueueSession()
76      {
77          if (queueSession == null) {
78              try 
79              {
80                  queueSession =
81                      createQueueConnection().createQueueSession(false,
82                          Session.AUTO_ACKNOWLEDGE);
83              } 
84              catch (JMSException e) 
85              {
86                  throw new ChainedRuntimeException(
87                      "Failed to create JMS Queue Session", e);
88              }
89          }
90          return queueSession;
91      }
92  
93      /**
94       * @return the JNDI Initial Context as defined in the Cactus configuration
95       *         file (used to retrieve the JMS Queue Connection Factory)
96       */
97      public static InitialContext getInitialContext()
98      {
99          InitialContext context = null;
100         try 
101         {
102             Hashtable env = new Hashtable();
103             env.put(Context.INITIAL_CONTEXT_FACTORY,
104                 JmsConfiguration.getJndiInitialContextFactory());
105             env.put(Context.PROVIDER_URL,
106                 JmsConfiguration.getJndiProviderURL());
107             env.put(Context.URL_PKG_PREFIXES,
108                     JmsConfiguration.getJndiUrlPkgPrefixes());
109             env.put("j2ee.clientName", "JmsClientHelper");
110             
111             context = new InitialContext(env);
112             System.out.println(context);
113         } 
114         catch (NamingException e) 
115         {
116             throw new ChainedRuntimeException(
117                 "Failed to create JNDI initial context", e);
118         }
119 
120         return context;
121     }
122 
123     /**
124      * @return the JMS Queue Connection Factory from which to retrieve Queues
125      */
126     public static QueueConnectionFactory getQueueConnnectionFactory()
127     {
128         QueueConnectionFactory queueConnectionFactory = null;
129         try 
130         {
131             queueConnectionFactory =
132                 (QueueConnectionFactory) (getInitialContext().
133                     lookup(JmsConfiguration.getJmsConnectionFactoryJndiName()));
134         } 
135         catch (NamingException e) 
136         {
137             throw new ChainedRuntimeException(
138                 "Failed to lookup [" +
139                 JmsConfiguration.getJmsConnectionFactoryJndiName() +
140                 "] Connection Factory in JNDI", e);
141         }
142 
143         return queueConnectionFactory;
144     }
145 
146     /**
147      * Lookup a queue in JNDI.
148      *
149      * @param theQueueName the JNDI name of the queue to look up
150      * @return the queue object
151      */
152     public static Queue getQueue(String theQueueName)
153     {
154         Queue queue = null;
155         try 
156         {
157             queue = (Queue) (getInitialContext().lookup(theQueueName));
158         } 
159         catch (NamingException e) 
160         {
161             throw new ChainedRuntimeException(
162                 "Failed to lookup [" + theQueueName + "] Queue in JNDI", e);
163         }
164         return queue;
165     }
166 
167     /**
168      * Creates a Queue Sender to send JMS messages.
169      *
170      * @param theQueueName the JNDI name of the queue to use to send messages
171      * @return the queue sender object
172      */
173     public static QueueSender createQueueSender(String theQueueName)
174     {
175         Queue queue = getQueue(theQueueName);
176         QueueSession queueSession = getQueueSession();
177 
178         QueueSender queueSender = null;
179         try 
180         {
181             queueSender = queueSession.createSender(queue);
182         } 
183         catch (JMSException e) 
184         {
185             throw new ChainedRuntimeException("Failed to create queue sender" +
186                 "for queue [" + queue + "]", e);
187         }
188 
189         return queueSender;
190     }
191 }