KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > joram > client > connector > OutboundConsumer


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2004 - Bull SA
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 any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA.
19  *
20  * Initial developer(s): Frederic Maistre (Bull SA)
21  * Contributor(s): Nicolas Tachker (Bull SA)
22  */

23 package org.objectweb.joram.client.connector;
24
25 import javax.jms.IllegalStateException JavaDoc;
26 import javax.jms.JMSException JavaDoc;
27 import javax.jms.MessageConsumer JavaDoc;
28
29 import org.objectweb.util.monolog.api.BasicLevel;
30
31 /**
32  * An <code>OutboundConsumer</code> instance wraps a JMS consumer
33  * for a component involved in outbound messaging.
34  */

35 public class OutboundConsumer implements javax.jms.MessageConsumer JavaDoc
36 {
37   /** The <code>OutboundSession</code> this consumer belongs to. */
38   protected OutboundSession session;
39   /** Wrapped JMS consumer. */
40   protected MessageConsumer JavaDoc consumer;
41
42   /** <code>false</code> if consumer is no more valid. */
43   boolean valid = true;
44   
45
46   /**
47    * Constructs an <code>OutboundConsumer</code> instance.
48    *
49    * @param consumer JMS consumer to wrap.
50    * @param session The OutboundSession this consumer belongs to.
51    */

52   OutboundConsumer(MessageConsumer JavaDoc consumer,
53                    OutboundSession session) {
54
55     if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG))
56       AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG,
57                                     "OutboundConsumer(" + consumer +
58                                     ", " + session + ")");
59     
60     this.consumer = consumer;
61     this.session = session;
62   }
63
64
65   /**
66    * Forbidden call on a component's outbound consumer, throws a
67    * <code>IllegalStateException</code> instance.
68    */

69   public void setMessageListener(javax.jms.MessageListener JavaDoc messageListener)
70               throws JMSException JavaDoc
71   {
72     checkValidity();
73     throw new IllegalStateException JavaDoc("Invalid call on a component's producer.");
74   }
75
76   /**
77    * Forbidden call on a component's outbound consumer, throws a
78    * <code>IllegalStateException</code> instance.
79    */

80   public javax.jms.MessageListener JavaDoc getMessageListener() throws JMSException JavaDoc
81   {
82     checkValidity();
83     throw new IllegalStateException JavaDoc("Invalid call on a component's producer.");
84   }
85
86   /**
87    * Delegates the call to the wrapped JMS consumer.
88    */

89   public String JavaDoc getMessageSelector() throws JMSException JavaDoc {
90     if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG))
91       AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG,
92                                     this + " getMessageSelector()");
93     
94     checkValidity();
95     return consumer.getMessageSelector();
96   }
97
98   /**
99    * Delegates the call to the wrapped JMS consumer.
100    */

101   public javax.jms.Message JavaDoc receive(long timeOut) throws JMSException JavaDoc {
102     if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG))
103       AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG,
104                                     this + " receive(" + timeOut + ")");
105
106     checkValidity();
107     return consumer.receive(timeOut);
108   }
109
110   /**
111    * Delegates the call to the wrapped JMS consumer.
112    */

113   public javax.jms.Message JavaDoc receive() throws JMSException JavaDoc {
114     if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG))
115       AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG,
116                                     this + " receive()");
117
118     checkValidity();
119     return consumer.receive();
120   }
121
122   /**
123    * Delegates the call to the wrapped JMS consumer.
124    */

125   public javax.jms.Message JavaDoc receiveNoWait() throws JMSException JavaDoc {
126     if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG))
127       AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG,
128                                     this + " receiveNoWait()");
129
130     checkValidity();
131     if (!session.isStarted())
132       return null;
133     return consumer.receiveNoWait();
134   }
135
136   /**
137    * Delegates the call to the wrapped JMS consumer.
138    */

139   public void close() throws JMSException JavaDoc {
140     if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG))
141       AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG,
142                                     this + " close()");
143
144     valid = false;
145     consumer.close();
146   }
147
148   /** Checks the validity of the subscriber instance. */
149   protected void checkValidity() throws IllegalStateException JavaDoc
150   {
151     session.checkValidity();
152
153     if (! valid)
154      throw new IllegalStateException JavaDoc("Invalid call on a closed producer.");
155   }
156 }
157
Popular Tags