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: RmiJmsSessionStub.java,v 1.28 2003/08/07 13:32:55 tanderson Exp $
44   *
45   * Date         Author  Changes
46   * 04/18/2000    jima    Created
47   */
48  package org.exolab.jms.client.rmi;
49  
50  
51  import java.rmi.RemoteException;
52  import java.rmi.server.UnicastRemoteObject;
53  import java.util.Vector;
54  
55  import javax.jms.JMSException;
56  import javax.jms.Message;
57  import javax.transaction.xa.XAException;
58  import javax.transaction.xa.XAResource;
59  import javax.transaction.xa.Xid;
60  
61  import org.exolab.jms.client.JmsMessageListener;
62  import org.exolab.jms.client.JmsQueue;
63  import org.exolab.jms.client.JmsSessionStubIfc;
64  import org.exolab.jms.client.JmsTopic;
65  import org.exolab.jms.server.rmi.RemoteJmsServerSessionIfc;
66  
67  
68  /***
69   * This class is repsonsible for managing a reference to a remote session
70   * object in additon to registering itself as an object, with the RMI
71   * registry. This object is called back by the JMS Server when it has messages
72   * for it.
73   * <p>
74   * The act that it extends UnicastRemoteObject means that it is a server as
75   * well as a client to other RMI objects
76   *
77   * @version     $Revision: 1.28 $ $Date: 2003/08/07 13:32:55 $
78   * @author      <a href="mailto:jima@exoffice.com">Jim Alateras</a>
79   **/
80  public class RmiJmsSessionStub
81      extends UnicastRemoteObject
82      implements JmsSessionStubIfc, RemoteJmsMessageListenerIfc {
83  
84      /***
85       * This is the message listener for JMS messages. The MessageListener is
86       * then responsible for delivering it to the actual consumer. The listener
87       * is initialised during object construction time
88       */
89      private JmsMessageListener _listener = null;
90  
91      /***
92       * This is a remote reference to the session which is initialised at object
93       * construction time.
94       */
95      private RemoteJmsServerSessionIfc _delegate = null;
96  
97  
98      /***
99       * Instantiate an instance of this class using the specified session stub.
100      * It must then, immediately following, make a call to set the message
101      * listener for this session
102      *
103      * @param       RmiJmsServerSession
104      * @throws      JMSException
105      * @throws      RemoteException
106      */
107     protected RmiJmsSessionStub(RemoteJmsServerSessionIfc session)
108         throws JMSException, RemoteException {
109         if (session != null) {
110             _delegate = session;
111         } else {
112             throw new JMSException(
113                 "Cannot create session stub with a null session");
114         }
115     }
116 
117     // implementation of JmsSessionStubIfc.getClientId
118     public String getClientId() throws JMSException {
119         try {
120             return _delegate.getClientId();
121         } catch (RemoteException exception) {
122             // rethrow as a JMSException
123             throw new JMSException("Failed to getClientId  " + exception);
124         }
125     }
126 
127     // implementation of JmsSessionStubIfc.getSessionId
128     public String getSessionId() throws JMSException {
129         try {
130             return _delegate.getSessionId();
131         } catch (RemoteException exception) {
132             // rethrow as a JMSException
133             throw new JMSException("Failed to getSessionId  " + exception);
134         }
135     }
136 
137     // implementation of JmsSessionStubIfc.beforeClose
138     public void beforeClose() throws JMSException {
139     }
140 
141     // implementation of JmsSessionStubIfc.close
142     public void close() throws JMSException {
143         try {
144             _delegate.close();
145             UnicastRemoteObject.unexportObject(this, true);
146             _delegate = null;
147             _listener = null;
148             // todo. Jimbo to sort these out.
149             //            _delegate = null;
150             //            _listener = null;
151         } catch (RemoteException exception) {
152             // rethrow as a JMSException
153             throw new JMSException("Failed to close  " + exception);
154         }
155     }
156 
157     // implementation of JmsSessionStubIfc.acknowledgeMessage
158     public void acknowledgeMessage(long clientId, String messageId)
159         throws JMSException {
160         try {
161             // Send back the ack to the consumer endpoint that forwarded the
162             // message.
163             _delegate.acknowledgeMessage(clientId, messageId);
164         } catch (RemoteException exception) {
165             // rethrow as a JMSException
166             throw new JMSException("Failed to acknowledgeMessage  " +
167                 exception);
168         }
169     }
170 
171     // implementation of JmsSessionStubIfc.sendMessage
172     public void sendMessage(Message message) throws JMSException {
173         try {
174             _delegate.sendMessage(message);
175         } catch (RemoteException exception) {
176             // rethrow as a JMSException
177             throw new JMSException("Failed to sendMessage  " + exception);
178         }
179     }
180 
181     // implementation of JmsSessionStubIfc.sendMessages
182     public void sendMessages(Vector messages) throws JMSException {
183         try {
184             _delegate.sendMessages(messages);
185         } catch (RemoteException exception) {
186             // rethrow as a JMSException
187             exception.printStackTrace();
188             throw new JMSException("Failed to sendMessages  " + exception);
189         }
190     }
191 
192     // implementation of JmsSessionStubIfc.sendMessage
193     public Message receiveMessage(long clientId, long wait)
194         throws JMSException {
195         try {
196             return _delegate.receiveMessage(clientId, wait);
197         } catch (RemoteException exception) {
198             // rethrow the JMSException
199             throw new JMSException("Failed to receiveMessage " + exception);
200         }
201     }
202 
203     // implementation of JmsSessionStubIfc.receiveMessages
204     public Vector receiveMessages(long clientId, int count)
205         throws JMSException {
206         try {
207             return _delegate.receiveMessages(clientId, count);
208         } catch (RemoteException exception) {
209             // rethrow the JMSException
210             throw new JMSException("Failed to receiveMessages " + exception);
211         }
212     }
213 
214     // implementation of JmsSessionStubIfc.createQueue
215     public void createQueue(JmsQueue queue) throws JMSException {
216         try {
217             _delegate.createQueue(queue);
218         } catch (RemoteException exception) {
219             // rethrow as a JMSException
220             throw new JMSException("Failed to createQueue  " + exception);
221         }
222     }
223 
224     // implementation of JmsSessionStubIfc.createTopic
225     public void createTopic(JmsTopic topic) throws JMSException {
226         try {
227             _delegate.createTopic(topic);
228         } catch (RemoteException exception) {
229             // rethrow as a JMSException
230             throw new JMSException("Failed to createTopic  " + exception);
231         }
232     }
233 
234     // implementation of JmsSessionStubIfc.createReceiver
235     public void createReceiver(JmsQueue queue, long clientId, String selector)
236         throws JMSException {
237         try {
238             _delegate.createReceiver(queue, clientId, selector);
239         } catch (RemoteException exception) {
240             // rethrow as a JMSException
241             throw new JMSException("Failed to createReceiver  " + exception);
242         }
243     }
244 
245     // implementation of JmsSessionStubIfc.createSender
246     public void createSender(JmsQueue queue) throws JMSException {
247         try {
248             _delegate.createSender(queue);
249         } catch (RemoteException exception) {
250             // rethrow as a JMSException
251             throw new JMSException("Failed to createReceiver  " + exception);
252         }
253     }
254 
255     // implementation of JmsSessionStubIfc.createBrowser
256     public void createBrowser(JmsQueue queue, long clientId, String selector)
257         throws JMSException {
258         try {
259             _delegate.createBrowser(queue, clientId, selector);
260         } catch (RemoteException exception) {
261             // rethrow as a JMSException
262             throw new JMSException("Failed to createBrowser  " + exception);
263         }
264     }
265 
266     // implementation of JmsSessionStubIfc.deleteReceiver
267     public void deleteReceiver(long clientId) throws JMSException {
268         try {
269             _delegate.deleteReceiver(clientId);
270         } catch (RemoteException exception) {
271             // rethrow as a JMSException
272             throw new JMSException("Failed to deleteReceiver  " + exception);
273         }
274     }
275 
276     // implementation of JmsSessionStubIfc.deleteSender
277     public void deleteSender(long clientId) throws JMSException {
278         try {
279             _delegate.deleteSender(clientId);
280         } catch (RemoteException exception) {
281             // rethrow as a JMSException
282             throw new JMSException("Failed to deleteSender  " + exception);
283         }
284     }
285 
286     // implementation of JmsSessionStubIfc.deleteBrowser
287     public void deleteBrowser(long clientId) throws JMSException {
288         try {
289             _delegate.deleteBrowser(clientId);
290         } catch (RemoteException exception) {
291             // rethrow as a JMSException
292             throw new JMSException("Failed to deleteBrowser  " + exception);
293         }
294     }
295 
296     // implementation of JmsSessionStubIfc.createSubscriber
297     public void createSubscriber(JmsTopic topic, String name, long clientId,
298                                  String selector, boolean noLocal) throws JMSException {
299         try {
300             _delegate.createSubscriber(topic, name, clientId, selector,
301                 noLocal);
302         } catch (RemoteException exception) {
303             // rethrow as a JMSException
304             throw new JMSException("Failed to createSubscriber  " + exception);
305         }
306     }
307 
308     // implementation of JmsSessionStubIfc.createPublisher
309     public void createPublisher(JmsTopic topic) throws JMSException {
310         try {
311             _delegate.createPublisher(topic);
312         } catch (RemoteException exception) {
313             // rethrow as a JMSException
314             throw new JMSException("Failed to createPublisher  " + exception);
315         }
316     }
317 
318     // implementation of JmsSessionStubIfc.deleteSubscriber
319     public void deleteSubscriber(long clientId) throws JMSException {
320         try {
321             _delegate.deleteSubscriber(clientId);
322         } catch (RemoteException exception) {
323             // rethrow as a JMSException
324             throw new JMSException("Failed to deleteSubscriber  " + exception);
325         }
326     }
327 
328     // implementation of JmsSessionStubIfc.deletePublisher
329     public void deletePublisher(JmsTopic topic) throws JMSException {
330         try {
331             _delegate.deletePublisher(topic);
332         } catch (RemoteException exception) {
333             // rethrow as a JMSException
334             throw new JMSException("Failed to deletePublisher  " + exception);
335         }
336     }
337 
338     // implementation of JmsSessionStubIfc.unsubscribe
339     public void unsubscribe(String name) throws JMSException {
340         try {
341             _delegate.unsubscribe(name);
342         } catch (RemoteException exception) {
343             // rethrow as a JMSException
344             throw new JMSException("Failed to unsubscribe  " + exception);
345         }
346     }
347 
348     // implementation of JmsSessionStubIfc.stopMessageDelivery
349     public void stopMessageDelivery() throws JMSException {
350         try {
351             _delegate.stopMessageDelivery();
352         } catch (RemoteException exception) {
353             // rethrow as a JMSException
354             throw new JMSException("Failed to stopMessageDelivery  " +
355                 exception);
356         }
357     }
358 
359     // implementation of JmsSessionStubIfc.startMessageDelivery
360     public void startMessageDelivery() throws JMSException {
361         try {
362             _delegate.startMessageDelivery();
363         } catch (RemoteException exception) {
364             // rethrow as a JMSException
365             throw new JMSException("Failed to startMessageDelivery  " +
366                 exception);
367         }
368     }
369 
370     // implementation of JmsSessionStubIfc.setMessageListener
371     public void setMessageListener(JmsMessageListener listener) {
372         _listener = listener;
373     }
374 
375     // implementation of JmsSessionStubIfc.enableAsynchronousDelivery
376     public void enableAsynchronousDelivery(long clientId, String id,
377                                            boolean enable)
378         throws JMSException {
379         try {
380             _delegate.enableAsynchronousDelivery(clientId, id, enable);
381         } catch (RemoteException exception) {
382             // rethrow as a JMSException
383             throw new JMSException("Failed to enableAsynchronousDelivery  " +
384                 exception);
385         }
386     }
387 
388     // implementation of JmsSessionStubIfc.recover
389     public void recover() throws JMSException {
390         try {
391             _delegate.recover();
392         } catch (RemoteException exception) {
393             // rethrow as a JMSException
394             throw new JMSException("Failed to recover session " +
395                 exception);
396         }
397     }
398 
399     // implementation of JmsSessionStubIfc.commit
400     public void commit() throws JMSException {
401         try {
402             _delegate.commit();
403         } catch (RemoteException exception) {
404             // rethrow as a JMSException
405             throw new JMSException("Failed to commit session " +
406                 exception);
407         }
408     }
409 
410     // implementation of JmsSessionStubIfc.rollback
411     public void rollback() throws JMSException {
412         try {
413             _delegate.rollback();
414         } catch (RemoteException exception) {
415             // rethrow as a JMSException
416             throw new JMSException("Failed to rollback session " +
417                 exception);
418         }
419     }
420 
421     // implementation of JmsSessionStubIfc.commit
422     public void commit(Xid xid, boolean onePhase) throws XAException {
423         try {
424             _delegate.commit(xid, onePhase);
425         } catch (RemoteException exception) {
426             // rethrow as a XAException
427             throw new XAException("Failed to commit session " +
428                 exception);
429         }
430     }
431 
432     // implementation of JmsSessionStubIfc.end
433     public void end(Xid xid, int flags) throws XAException {
434         try {
435             _delegate.end(xid, flags);
436         } catch (RemoteException exception) {
437             // rethrow as a XAException
438             throw new XAException("Failed to end session " +
439                 exception);
440         }
441     }
442 
443     // implementation of JmsSessionStubIfc.forget
444     public void forget(Xid xid) throws XAException {
445         try {
446             _delegate.forget(xid);
447         } catch (RemoteException exception) {
448             // rethrow as a XAException
449             throw new XAException("Failed to forget session " +
450                 exception);
451         }
452     }
453 
454     // implementation of JmsSessionStubIfc.getResourceManagerId
455     public String getResourceManagerId() throws XAException {
456         try {
457             return _delegate.getResourceManagerId();
458         } catch (RemoteException exception) {
459             // rethrow as a XAException
460             throw new XAException("Failed to getResourceManagerId " +
461                 exception);
462         }
463     }
464 
465     // implementation of JmsSessionStubIfc.getTransactionTimeout
466     public int getTransactionTimeout() throws XAException {
467         try {
468             return _delegate.getTransactionTimeout();
469         } catch (RemoteException exception) {
470             // rethrow as a XAException
471             throw new XAException("Failed to getTransactionTimeout " +
472                 exception);
473         }
474     }
475 
476     // implementation of JmsSessionStubIfc.prepare
477     public int prepare(Xid xid) throws XAException {
478         try {
479             return _delegate.prepare(xid);
480         } catch (RemoteException exception) {
481             // rethrow as a XAException
482             throw new XAException("Failed to prepare session " +
483                 exception);
484         }
485     }
486 
487     // implementation of JmsSessionStubIfc.recover
488     public Xid[] recover(int flag) throws XAException {
489         try {
490             return _delegate.recover(flag);
491         } catch (RemoteException exception) {
492             // rethrow as a XAException
493             throw new XAException("Failed to recover session " +
494                 exception);
495         }
496     }
497 
498     // implementation of JmsSessionStubIfc.rollback
499     public void rollback(Xid xid) throws XAException {
500         try {
501             _delegate.rollback(xid);
502         } catch (RemoteException exception) {
503             // rethrow as a XAException
504             throw new XAException("Failed to rollback session " +
505                 exception);
506         }
507     }
508 
509     // implementation of JmsSessionStubIfc.setTransactionTimeout
510     public boolean setTransactionTimeout(int seconds) throws XAException {
511         try {
512             return _delegate.setTransactionTimeout(seconds);
513         } catch (RemoteException exception) {
514             // rethrow as a XAException
515             throw new XAException("Failed to setTransactionTimeout " +
516                 exception);
517         }
518     }
519 
520     // implementation of JmsSessionStubIfc.start
521     public void start(Xid xid, int flags) throws XAException {
522         try {
523             _delegate.start(xid, flags);
524         } catch (RemoteException exception) {
525             // rethrow as a XAException
526             throw new XAException("Failed to start session " +
527                 exception);
528         }
529     }
530 
531     // implementation of RmiJmsMessageListenerIfc.onMessages
532     public void onMessage(Message message) throws RemoteException {
533         // do not process if the message is null. This is also used by the
534         // server to determine whether the client is still active.
535         if (message == null) {
536             return;
537         }
538 
539         _listener.onMessage(message);
540     }
541 
542     // implementation of RmiJmsMessageListenerIfc.onMessages
543     public void onMessages(Vector messages) throws RemoteException {
544         _listener.onMessages(messages);
545     }
546 
547     // implementation of RmiJmsMessageListenerIfc.onMessageAvailable
548     public void onMessageAvailable(long clientId) throws RemoteException {
549         _listener.onMessageAvailable(clientId);
550     }
551 
552 } //-- RmiJmsSessionStub