View Javadoc

1   /***
2    * Redistribution and use of this software and associated documentation
3    * ("Software"), with or without modification, are permitted provided
4    * that the following conditions are met:
5    *
6    * 1. Redistributions of source code must retain copyright
7    *    statements and notices.  Redistributions must also contain a
8    *    copy of this document.
9    *
10   * 2. Redistributions in binary form must reproduce the
11   *    above copyright notice, this list of conditions and the
12   *    following disclaimer in the documentation and/or other
13   *    materials provided with the distribution.
14   *
15   * 3. The name "Exolab" must not be used to endorse or promote
16   *    products derived from this Software without prior written
17   *    permission of Exoffice Technologies.  For written permission,
18   *    please contact info@exolab.org.
19   *
20   * 4. Products derived from this Software may not be called "Exolab"
21   *    nor may "Exolab" appear in their names without prior written
22   *    permission of Exoffice Technologies. Exolab is a registered
23   *    trademark of Exoffice Technologies.
24   *
25   * 5. Due credit should be given to the Exolab Project
26   *    (http://www.exolab.org/).
27   *
28   * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29   * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
32   * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39   * OF THE POSSIBILITY OF SUCH DAMAGE.
40   *
41   * Copyright 2000-2003 (C) Exoffice Technologies Inc. All Rights Reserved.
42   *
43   * $Id: IntravmJmsSessionStub.java,v 1.25 2003/08/07 13:32:52 tanderson Exp $
44   *
45   * Date         Author  Changes
46   * 04/18/2000    jima    Created
47   */
48  package org.exolab.jms.client.intravm;
49  
50  import java.util.Iterator;
51  import java.util.Vector;
52  
53  import javax.jms.JMSException;
54  import javax.jms.Message;
55  import javax.transaction.xa.XAException;
56  import javax.transaction.xa.XAResource;
57  import javax.transaction.xa.Xid;
58  
59  import org.apache.commons.logging.Log;
60  import org.apache.commons.logging.LogFactory;
61  
62  import org.exolab.jms.client.JmsMessageListener;
63  import org.exolab.jms.client.JmsQueue;
64  import org.exolab.jms.client.JmsSessionStubIfc;
65  import org.exolab.jms.client.JmsTopic;
66  import org.exolab.jms.message.MessageImpl;
67  import org.exolab.jms.server.JmsServerSession;
68  
69  
70  /***
71   * The client stub implementation for the intra-JVM JMS server.
72   * <p>
73   * This delegates directly to a {@link org.exolab.jms.server.JmsServerSession
74   * JmsServerSession} instance.
75   *
76   * @version     $Revision: 1.25 $ $Date: 2003/08/07 13:32:52 $
77   * @author      <a href="mailto:jima@exoffice.com">Jim Alateras</a>
78   */
79  public class IntravmJmsSessionStub
80      implements JmsSessionStubIfc, JmsMessageListener {
81  
82      /***
83       * This is the message listener for JMS messages. The MessageListener is
84       * then responsible for delivering it to the actual consumer. The listener
85       * must be set by the client session after construction using
86       * {@link #setMessageListener}
87       */
88      private JmsMessageListener _listener = null;
89  
90      /***
91       * This is a remote reference to the session which is initialised at
92       * construction.
93       */
94      private JmsServerSession _delegate = null;
95  
96      /***
97       * The logger
98       */
99      private static final Log _log =
100         LogFactory.getLog(IntravmJmsSessionStub.class);
101 
102 
103     /***
104      * Create an instance of this class using the specified session stub.
105      * A {@link JmsMessageListener} must be registered after construction
106      * using {@link #setMessageListener} in order for client session to
107      * receive messages.
108      *
109      * @param       session             the server session to delegate to
110      * @throws      JMSException
111      */
112     IntravmJmsSessionStub(JmsServerSession session) throws JMSException {
113         if (session != null) {
114             _delegate = session;
115         } else {
116             throw new JMSException(
117                 "Cannot create session stub with a null session");
118         }
119     }
120 
121     // implementation of JmsSessionStubIfc.getClientId
122     public String getClientId() throws JMSException {
123         return _delegate.getClientId();
124     }
125 
126     // implementation of JmsSessionStubIfc.getSessionId
127     public String getSessionId() throws JMSException {
128         return _delegate.getSessionId();
129     }
130 
131     // implementation of JmsSessionStubIfc.beforeClose
132     public void beforeClose() throws JMSException {
133     }
134 
135     // implementation of JmsSessionStubIfc.close
136     public void close() throws JMSException {
137         _delegate.close();
138     }
139 
140     // implementation of JmsSessionStubIfc.acknowledgeMessage
141     public void acknowledgeMessage(long clientId, String messageId)
142         throws JMSException {
143         // Send back the ack to the consumer endpoint that forwarded the
144         // message.
145         _delegate.acknowledgeMessage(clientId, messageId);
146     }
147 
148     // implementation of JmsSessionStubIfc.sendMessage
149     public void sendMessage(Message message) throws JMSException {
150         // As the server resides in the same VM as the client, the message
151         // must be copied to allow the client to modify the original after
152         // it is sent. The limitation of this is that for transacted sessions,
153         // the message will ultimately be copied twice.
154         Message copy = null;
155         try {
156             copy = (Message) ((MessageImpl) message).clone();
157         } catch (CloneNotSupportedException exception) {
158             JMSException error = new JMSException(exception.getMessage());
159             error.setLinkedException(exception);
160             throw error;
161         }
162         _delegate.sendMessage(copy);
163     }
164 
165     // implementation of JmsSessionStubIfc.sendMessages
166     public void sendMessages(Vector messages) throws JMSException {
167         // messages are already cloned
168         _delegate.sendMessages(messages);
169     }
170 
171     // implementation of JmsSessionStubIfc.receiveMessage
172     public Message receiveMessage(long clientId, long wait)
173         throws JMSException {
174         // As the server resides in the same VM as the client, any returned
175         // message must be copied so that if the client modifies it, the
176         // server instance is not affected.
177         Message message = _delegate.receiveMessage(clientId, wait);
178         Message copy = null;
179         if (message != null) {
180             try {
181                 copy = (Message) ((MessageImpl) message).clone();
182             } catch (CloneNotSupportedException exception) {
183                 JMSException error = new JMSException(exception.getMessage());
184                 error.setLinkedException(exception);
185                 throw error;
186             }
187         }
188         return copy;
189     }
190 
191     // implementation of JmsSessionStubIfc.receiveMessages
192     public Vector receiveMessages(long clientId, int count)
193         throws JMSException {
194         // As the server resides in the same VM as the client, any returned
195         // messages must be copied so that if the client modifies it, the
196         // server instance is not affected.
197         Vector messages = _delegate.receiveMessages(clientId, count);
198         Vector copied = new Vector();
199         if (messages.size() > 0) {
200             for (int index = 0; index < messages.size(); index++) {
201                 Message copy = null;
202                 Message message = (MessageImpl) messages.elementAt(index);
203                 try {
204                     copy = (MessageImpl) ((MessageImpl) message).clone();
205                     copied.addElement(copy);
206                 } catch (CloneNotSupportedException exception) {
207                     JMSException error = new JMSException(exception.getMessage());
208                     error.setLinkedException(exception);
209                     throw error;
210                 }
211             }
212         }
213 
214         return copied;
215     }
216 
217     // implementation of JmsSessionStubIfc.createQueue
218     public void createQueue(JmsQueue queue) throws JMSException {
219         _delegate.createQueue(queue);
220     }
221 
222     // implementation of JmsSessionStubIfc.createTopic
223     public void createTopic(JmsTopic topic) throws JMSException {
224         _delegate.createTopic(topic);
225     }
226 
227     // implementation of JmsSessionStubIfc.createReceiver
228     public void createReceiver(JmsQueue queue, long clientId, String selector)
229         throws JMSException {
230         _delegate.createReceiver(queue, clientId, selector);
231     }
232 
233     // implementation of JmsSessionStubIfc.createSender
234     public void createSender(JmsQueue queue) throws JMSException {
235         _delegate.createSender(queue);
236     }
237 
238     // implementation of JmsSessionStubIfc.createBrowser
239     public void createBrowser(JmsQueue queue, long clientId, String selector)
240         throws JMSException {
241         _delegate.createBrowser(queue, clientId, selector);
242     }
243 
244     // implementation of JmsSessionStubIfc.deleteReceiver
245     public void deleteReceiver(long clientId) throws JMSException {
246         _delegate.deleteReceiver(clientId);
247     }
248 
249     // implementation of JmsSessionStubIfc.deleteBrowser
250     public void deleteBrowser(long clientId) throws JMSException {
251         _delegate.deleteBrowser(clientId);
252     }
253 
254     // implementation of JmsSessionStubIfc.createSubscriber
255     public void createSubscriber(JmsTopic topic, String name, long clientId,
256                                  String selector, boolean noLocal)
257         throws JMSException {
258         _delegate.createSubscriber(topic, name, clientId, selector, noLocal);
259     }
260 
261     // implementation of JmsSessionStubIfc.createPublisher
262     public void createPublisher(JmsTopic topic) throws JMSException {
263         _delegate.createPublisher(topic);
264     }
265 
266     // implementation of JmsSessionStubIfc.deleteSubscriber
267     public void deleteSubscriber(long clientId) throws JMSException {
268         _delegate.deleteSubscriber(clientId);
269     }
270 
271     // implementation of JmsSessionStubIfc.unsubscribe
272     public void unsubscribe(String name) throws JMSException {
273         _delegate.unsubscribe(name);
274     }
275 
276     // implementation of JmsSessionStubIfc.stopMessageDelivery
277     public void stopMessageDelivery() throws JMSException {
278         _delegate.stopMessageDelivery();
279     }
280 
281     // implementation of JmsSessionStubIfc.startMessageDelivery
282     public void startMessageDelivery() throws JMSException {
283         _delegate.startMessageDelivery();
284     }
285 
286     // implementation of JmsSessionStubIfc.setMessageListener
287     public void setMessageListener(JmsMessageListener listener) {
288         _listener = listener;
289     }
290 
291     // implementation of JmsSessionStubIfc.enableAsynchronousDelivery
292     public void enableAsynchronousDelivery(long clientId, String id,
293                                            boolean enable)
294         throws JMSException {
295         _delegate.enableAsynchronousDelivery(clientId, id, enable);
296     }
297 
298     // implementation of JmsSessionStubIfc.recover
299     public void recover() throws JMSException {
300         _delegate.recover();
301     }
302 
303     // implementation of JmsSessionStubIfc.commit
304     public void commit() throws JMSException {
305         _delegate.commit();
306     }
307 
308     // implementation of JmsSessionStubIfc.rollback
309     public void rollback() throws JMSException {
310         _delegate.rollback();
311     }
312 
313     // implementation of JmsSessionStubIfc.commit
314     public void commit(Xid xid, boolean onePhase)
315         throws XAException {
316         _delegate.commit(xid, onePhase);
317     }
318 
319     // implementation of JmsSessionStubIfc.end
320     public void end(Xid xid, int flags)
321         throws XAException {
322         _delegate.end(xid, flags);
323     }
324 
325     // implementation of JmsSessionStubIfc.forget
326     public void forget(Xid xid)
327         throws XAException {
328         _delegate.forget(xid);
329     }
330 
331     // implementation of JmsSessionStubIfc.getResourceManagerId
332     public String getResourceManagerId() throws XAException {
333         return _delegate.getResourceManagerId();
334     }
335 
336     // implementation of JmsSessionStubIfc.getTransactionTimeout
337     public int getTransactionTimeout()
338         throws XAException {
339         return _delegate.getTransactionTimeout();
340     }
341 
342     // implementation of JmsSessionStubIfc.prepare
343     public int prepare(Xid xid)
344         throws XAException {
345         return _delegate.prepare(xid);
346     }
347 
348     // implementation of JmsSessionStubIfc.recover
349     public Xid[] recover(int flag)
350         throws XAException {
351         return _delegate.recover(flag);
352     }
353 
354     // implementation of JmsSessionStubIfc.rollback
355     public void rollback(Xid xid)
356         throws XAException {
357         _delegate.rollback(xid);
358     }
359 
360     // implementation of JmsSessionStubIfc.setTransactionTimeout
361     public boolean setTransactionTimeout(int seconds)
362         throws XAException {
363         return _delegate.setTransactionTimeout(seconds);
364     }
365 
366     // implementation of JmsSessionStubIfc.start
367     public void start(Xid xid, int flags)
368         throws XAException {
369         _delegate.start(xid, flags);
370     }
371 
372     // implementation of JmsMessageListener.onMessage
373     public void onMessage(Message message) {
374         // As the server resides in the same VM as the client, the message
375         // must be copied to allow the client to modify it after it is
376         // received.
377         if (message != null) {
378             // ignore null messages generated by
379             // JmsServerSession.isClientEndpointActive()
380             Message copy = null;
381             try {
382                 copy = (Message) ((MessageImpl) message).clone();
383             } catch (CloneNotSupportedException exception) {
384                 _log.error(exception);
385             }
386             _listener.onMessage(copy);
387         }
388     }
389 
390     // implementation of JmsMessageListener.onMessages
391     public void onMessages(Vector messages) {
392         // As the server resides in the same VM as the client, the messages
393         // must be copied to allow the client to modify them after they are
394         // received.
395         Vector copy = new Vector();
396         try {
397             Iterator iter = messages.iterator();
398             while (iter.hasNext()) {
399                 MessageImpl message = (MessageImpl) iter.next();
400                 copy.add(message.clone());
401             }
402         } catch (CloneNotSupportedException exception) {
403             _log.error(exception);
404         }
405         _listener.onMessages(copy);
406     }
407 
408     // implementation of JmsMessageListener.onMessageAvailable
409     public void onMessageAvailable(long clientId) {
410         _listener.onMessageAvailable(clientId);
411     }
412 
413 } //-- IntravmJmsSessionStub