KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exolab > jms > client > net > JmsSessionStubImpl


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-2005 (C) Exoffice Technologies Inc. All Rights Reserved.
42  *
43  * $Id: JmsSessionStubImpl.java,v 1.3 2005/05/24 13:36:38 tanderson Exp $
44  */

45 package org.exolab.jms.client.net;
46
47 import java.rmi.RemoteException JavaDoc;
48 import java.util.List JavaDoc;
49 import javax.jms.JMSException JavaDoc;
50 import javax.jms.Message JavaDoc;
51 import javax.transaction.xa.XAException JavaDoc;
52 import javax.transaction.xa.Xid JavaDoc;
53
54 import org.exolab.jms.client.JmsDestination;
55 import org.exolab.jms.client.JmsMessageListener;
56 import org.exolab.jms.client.JmsQueue;
57 import org.exolab.jms.client.JmsTopic;
58 import org.exolab.jms.message.MessageImpl;
59 import org.exolab.jms.net.orb.ORB;
60 import org.exolab.jms.net.proxy.Proxy;
61 import org.exolab.jms.server.ServerSession;
62
63
64 /**
65  * Wraps an {@link ServerSession}.
66  *
67  * @author <a HREF="mailto:jima@exoffice.com">Jim Alateras</a>
68  * @version $Revision: 1.3 $ $Date: 2005/05/24 13:36:38 $
69  */

70 public class JmsSessionStubImpl
71         implements ServerSession, JmsMessageListener {
72
73     /**
74      * This is the message listener for JMS messages. The MessageListener is
75      * then responsible for delivering it to the actual consumer.
76      */

77     private JmsMessageListener _listener = null;
78
79     /**
80      * The ORB to export this with.
81      */

82     private ORB _orb;
83
84     /**
85      * The reference to the server session.
86      */

87     private ServerSession _session = null;
88
89
90     /**
91      * Construct a new <code>JmsSessionStubImpl</code>.
92      *
93      * @param session the reference to the server session
94      * @param orb the ORB to export this with
95      * @param uri the URI to export this on
96      * @param principal the security principal. May be <code>null</code>
97      * @param credentials the security credentials. May be <code>null</code>
98      */

99     protected JmsSessionStubImpl(ServerSession session, ORB orb,
100                                  String JavaDoc uri, String JavaDoc principal,
101                                  String JavaDoc credentials)
102             throws RemoteException JavaDoc {
103         if (session == null) {
104             throw new IllegalArgumentException JavaDoc("Argument 'session' is null");
105         }
106         if (orb == null) {
107             throw new IllegalArgumentException JavaDoc("Argument 'orb' is null");
108         }
109         _session = session;
110         _orb = orb;
111         Proxy proxy = orb.exportObjectTo(this, uri, principal, credentials);
112         _session.setMessageListener((JmsMessageListener) proxy);
113     }
114
115     /**
116      * Close and release any resource allocated to this session.
117      *
118      * @throws JMSException for any JMS error
119      */

120     public void close() throws JMSException JavaDoc {
121         try {
122             _session.close();
123             _orb.unexportObject(this);
124             _session = null;
125             _listener = null;
126         } catch (RemoteException JavaDoc exception) {
127             rethrow(exception);
128         }
129     }
130
131     /**
132      * Acknowledge that a message has been processed.
133      *
134      * @param consumerId the identity of the consumer performing the ack
135      * @param messageId the message identifier
136      * @throws JMSException for any error
137      */

138     public void acknowledgeMessage(long consumerId, String JavaDoc messageId)
139             throws JMSException JavaDoc {
140         _session.acknowledgeMessage(consumerId, messageId);
141     }
142
143     /**
144      * Send a message.
145      *
146      * @param message the message to send
147      * @throws JMSException for any error
148      */

149     public void send(MessageImpl message) throws JMSException JavaDoc {
150         _session.send(message);
151     }
152
153     /**
154      * Send a set of messages.
155      *
156      * @param messages a list of <code>MessageImpl</code> instances
157      * @throws JMSException for any JMS error
158      */

159     public void send(List JavaDoc messages) throws JMSException JavaDoc {
160         _session.send(messages);
161     }
162
163     /**
164      * Return the next available message to the specified consumer.
165      * <p/>
166      * The <code>wait</code> parameter indicates how many milliseconds to wait
167      * for a message before returning. If <code>wait</code> is <code>0</code>
168      * then do not wait. If <code>wait</code> is <code>-1</code> then wait
169      * indefinitely for the next message.
170      *
171      * @param consumerId the consumer identifier
172      * @param wait number of milliseconds to wait
173      * @return the next message or <code>null</code>
174      * @throws JMSException for any JMS error
175      */

176     public MessageImpl receive(long consumerId, long wait)
177             throws JMSException JavaDoc {
178         return _session.receive(consumerId, wait);
179     }
180
181     /**
182      * Browse up to count messages.
183      *
184      * @param consumerId the consumer identifier
185      * @param count the maximum number of messages to receive
186      * @return a list of {@link MessageImpl} instances
187      * @throws JMSException for any JMS error
188      */

189     public List JavaDoc browse(long consumerId, int count) throws JMSException JavaDoc {
190         return _session.browse(consumerId, count);
191     }
192
193     /**
194      * Create a new message consumer.
195      *
196      * @param destination the destination to consume messages from
197      * @param selector the message selector. May be <code>null</code>
198      * @param noLocal if true, and the destination is a topic, inhibits the
199      * delivery of messages published by its own connection.
200      * The behavior for <code>noLocal</code> is not specified
201      * if the destination is a queue.
202      * @return the identifty of the message consumer
203      * @throws JMSException for any JMS error
204      */

205     public long createConsumer(JmsDestination destination, String JavaDoc selector,
206                                boolean noLocal) throws JMSException JavaDoc {
207         return _session.createConsumer(destination, selector, noLocal);
208     }
209
210     /**
211      * Create a new durable consumer. Durable consumers may only consume from
212      * non-temporary <code>Topic</code> destinations.
213      *
214      * @param topic the non-temporary <code>Topic</code> to subscribe to
215      * @param name the name used to identify this subscription
216      * @param selector only messages with properties matching the message
217      * selector expression are delivered. A value of null or an
218      * empty string indicates that there is no message selector
219      * for the message consumer.
220      * @param noLocal if set, inhibits the delivery of messages published by
221      * its own connection
222      * @return the identity of the durable consumer
223      * @throws JMSException for any JMS error
224      */

225     public long createDurableConsumer(JmsTopic topic, String JavaDoc name,
226                                       String JavaDoc selector, boolean noLocal)
227             throws JMSException JavaDoc {
228         return _session.createDurableConsumer(topic, name, selector, noLocal);
229     }
230
231     /**
232      * Create a queue browser for this session. This allows clients to browse a
233      * queue without removing any messages.
234      *
235      * @param queue the queue to browse
236      * @param selector the message selector. May be <code>null</code>
237      * @return the identity of the queue browser
238      * @throws JMSException for any JMS error
239      */

240     public long createBrowser(JmsQueue queue, String JavaDoc selector)
241             throws JMSException JavaDoc {
242         return _session.createBrowser(queue, selector);
243     }
244
245     /**
246      * Remove a message consumer.
247      *
248      * @param consumerId the identity of the consumer to remove
249      * @throws JMSException for any JMS error
250      */

251     public void removeConsumer(long consumerId) throws JMSException JavaDoc {
252         _session.removeConsumer(consumerId);
253     }
254
255     /**
256      * Unsubscribe a durable subscription.
257      *
258      * @param name the name used to identify the subscription
259      * @throws JMSException for any JMS error
260      */

261     public void unsubscribe(String JavaDoc name) throws JMSException JavaDoc {
262         _session.unsubscribe(name);
263     }
264
265     /**
266      * Start message delivery to this session.
267      *
268      * @throws JMSException for any JMS error
269      */

270     public void start() throws JMSException JavaDoc {
271         _session.start();
272     }
273
274     /**
275      * Stop message delivery to this session.
276      *
277      * @throws JMSException for any JMS error
278      */

279     public void stop() throws JMSException JavaDoc {
280         _session.stop();
281     }
282
283     /**
284      * Enable or disable asynchronous message delivery for a particular
285      * consumer.
286      *
287      * @param consumerId the consumer identifier
288      * @param enable true to enable; false to disable
289      * @throws JMSException for any JMS error
290      */

291     public void enableAsynchronousDelivery(long consumerId, boolean enable)
292             throws JMSException JavaDoc {
293         _session.enableAsynchronousDelivery(consumerId, enable);
294     }
295
296     /**
297      * Recover the session. This means all unacknowledged messages are resent
298      * with the redelivery flag set
299      *
300      * @throws JMSException if the session cannot be recovered
301      */

302     public void recover() throws JMSException JavaDoc {
303         _session.recover();
304     }
305
306     /**
307      * Commit the session.
308      *
309      * @throws JMSException if the session cannot be committed
310      */

311     public void commit() throws JMSException JavaDoc {
312         _session.commit();
313     }
314
315     /**
316      * Rollback the session.
317      *
318      * @throws JMSException if the session cannot be rolled back
319      */

320     public void rollback() throws JMSException JavaDoc {
321         _session.rollback();
322     }
323
324     /**
325      * Start work on behalf of a transaction branch specified in xid. If TMJOIN
326      * is specified, the start is for joining a transaction previously seen by
327      * the resource manager.
328      *
329      * @param xid the xa transaction identity
330      * @param flags One of TMNOFLAGS, TMJOIN, or TMRESUME
331      * @throws XAException if there is a problem completing the call
332      */

333     public void start(Xid JavaDoc xid, int flags) throws XAException JavaDoc {
334         _session.start(xid, flags);
335     }
336
337     /**
338      * Ask the resource manager to prepare for a transaction commit of the
339      * transaction specified in xid.
340      *
341      * @param xid the xa transaction identity
342      * @return XA_RDONLY or XA_OK
343      * @throws XAException if there is a problem completing the call
344      */

345     public int prepare(Xid JavaDoc xid) throws XAException JavaDoc {
346         return _session.prepare(xid);
347     }
348
349     /**
350      * Commits an XA transaction that is in progress.
351      *
352      * @param xid the xa transaction identity
353      * @param onePhase true if it is a one phase commit
354      * @throws XAException if there is a problem completing the call
355      */

356     public void commit(Xid JavaDoc xid, boolean onePhase) throws XAException JavaDoc {
357         _session.commit(xid, onePhase);
358     }
359
360     /**
361      * Ends the work performed on behalf of a transaction branch. The resource
362      * manager disassociates the XA resource from the transaction branch
363      * specified and let the transaction be completedCommits an XA transaction
364      * that is in progress.
365      *
366      * @param xid the xa transaction identity
367      * @param flags one of TMSUCCESS, TMFAIL, or TMSUSPEND
368      * @throws XAException if there is a problem completing the call
369      */

370     public void end(Xid JavaDoc xid, int flags) throws XAException JavaDoc {
371         _session.end(xid, flags);
372     }
373
374     /**
375      * Tell the resource manager to forget about a heuristically completed
376      * transaction branch.
377      *
378      * @param xid the xa transaction identity
379      * @throws XAException if there is a problem completing the call
380      */

381     public void forget(Xid JavaDoc xid) throws XAException JavaDoc {
382         _session.forget(xid);
383     }
384
385     /**
386      * Inform the resource manager to roll back work done on behalf of a
387      * transaction branch.
388      *
389      * @param xid the xa transaction identity
390      * @throws XAException if there is a problem completing the call
391      */

392     public void rollback(Xid JavaDoc xid) throws XAException JavaDoc {
393         _session.rollback(xid);
394     }
395
396     /**
397      * Obtain a list of prepared transaction branches from a resource manager.
398      * The transaction manager calls this method during recovery to obtain the
399      * list of transaction branches that are currently in prepared or
400      * heuristically completed states.
401      *
402      * @param flag One of TMSTARTRSCAN, TMENDRSCAN, TMNOFLAGS. TMNOFLAGS
403      * @return the set of Xids to recover
404      * @throws XAException - if there is a problem completing the call
405      */

406     public Xid JavaDoc[] recover(int flag) throws XAException JavaDoc {
407         return _session.recover(flag);
408     }
409
410     /**
411      * Return the transaction timeout for this instance of the resource
412      * manager.
413      *
414      * @return the timeout in seconds
415      * @throws XAException if there is a problem completing the call
416      */

417     public int getTransactionTimeout() throws XAException JavaDoc {
418         return _session.getTransactionTimeout();
419     }
420
421     /**
422      * Set the current transaction timeout value for this XAResource instance.
423      *
424      * @param seconds timeout in seconds
425      * @return if the new transaction timeout was accepted
426      * @throws XAException if there is a problem completing the call
427      */

428     public boolean setTransactionTimeout(int seconds) throws XAException JavaDoc {
429         return _session.setTransactionTimeout(seconds);
430     }
431
432     /**
433      * Return the identity of the associated resource manager.
434      *
435      * @return the identity of the resource manager
436      * @throws XAException if there is a problem completing the call
437      */

438     public String JavaDoc getResourceManagerId() throws XAException JavaDoc {
439         return _session.getResourceManagerId();
440     }
441
442     /**
443      * Set the message listener for this session.
444      *
445      * @param listener the message listener
446      */

447     public void setMessageListener(JmsMessageListener listener) {
448         _listener = listener;
449     }
450
451     /**
452      * Deliver a message.
453      *
454      * @param message the message to deliver
455      * @throws RemoteException if the message can't be delivered
456      */

457     public void onMessage(Message JavaDoc message) throws RemoteException JavaDoc {
458         _listener.onMessage(message);
459     }
460
461     /**
462      * Inform the session that there is a message available for a particular
463      * consumer.
464      *
465      * @param consumerId the consumer identity
466      * @throws RemoteException if the session can't be notified
467      */

468     public void onMessageAvailable(long consumerId) throws RemoteException JavaDoc {
469         _listener.onMessageAvailable(consumerId);
470     }
471
472     /**
473      * Rethrows a <code>RemoteException</code> as a <code>JMSException</code>.
474      *
475      * @param exception the exception to rethrow
476      * @throws JMSException the rethrown exception
477      */

478     private void rethrow(RemoteException JavaDoc exception) throws JMSException JavaDoc {
479         JMSException JavaDoc error = new JMSException JavaDoc(exception.getMessage());
480         error.setLinkedException(exception);
481         throw error;
482     }
483
484 }
485
Popular Tags