KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > jbi > messaging > MessageExchangeDecorator


1 /**
2  * PETALS: PETALS Services Platform
3  * Copyright (C) 2006 EBM WebSourcing
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): Adrien LOUIS
21  * --------------------------------------------------------------------------
22  * $Id: MessageExchangeDecorator.java,v 1.0 2006/04/11 10:24:27 alouis Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.petals.jbi.messaging;
27
28 import java.net.URI JavaDoc;
29 import java.util.Set JavaDoc;
30
31 import javax.jbi.messaging.ExchangeStatus;
32 import javax.jbi.messaging.Fault;
33 import javax.jbi.messaging.MessageExchange;
34 import javax.jbi.messaging.MessagingException;
35 import javax.jbi.messaging.NormalizedMessage;
36 import javax.jbi.servicedesc.ServiceEndpoint;
37 import javax.xml.namespace.QName JavaDoc;
38
39 /**
40  * <p>
41  * This class exposes the <code>MessageExchange</code> for a consumer
42  * component .
43  * </p>
44  * <p>
45  * Each actor (consumer or provider) owns a particular
46  * <code>MessageExchangeDecoratorForXXX</code> parametrized as a
47  * 'MessageExchangeDecorator', or MessageExchangeDecoratorForProvider'.
48  * </p>
49  * <p>
50  * The decorator checks that the methods which are called by the related
51  * component can be used for the current exchange state.
52  * </p>
53  *
54  * @author Adrien LOUIS - EBM WebSourcing
55  */

56 public class MessageExchangeDecorator implements MessageExchange {
57
58     protected MessageExchangeImpl messageExchange;
59
60     /**
61      * Role of the observer (consumer or provider). Used for checking that the
62      * ME is in the good role before calling a method.
63      */

64     protected Role observerRole;
65
66     /**
67      * Used for SendSync. It is set to TRUE when a sendSync is performed with
68      * this ME. When the response is sent, it is set to False. If timeout
69      * occured before the response has been sent, this boolean is always set to
70      * TRUE, and the requester knows that a timeout occured before the response
71      * has been sent.
72      */

73     protected boolean waitingOnSynchronousSend;
74
75     /**
76      * Create a MessageExchangeDecorator. Set this object as the
77      * consumerMEDecorator or providerMEDecorator of the MessageExchangeImpl,
78      * depending on the given Role.
79      *
80      * @param consumerDeliveryChannel
81      */

82     public MessageExchangeDecorator(MessageExchangeImpl me, Role observerRole) {
83         this.messageExchange = me;
84
85         this.observerRole = observerRole;
86     }
87
88     /**
89      * @see javax.jbi.messaging.MessageExchange#createFault()
90      */

91     public Fault createFault() throws MessagingException {
92         checkComponentOwnsExchange();
93
94         return messageExchange.createFault();
95     }
96
97     /**
98      * @see javax.jbi.messaging.MessageExchange#createMessage()
99      */

100     public NormalizedMessage createMessage() throws MessagingException {
101         checkComponentOwnsExchange();
102
103         return messageExchange.createMessage();
104     }
105
106     /**
107      * @see javax.jbi.messaging.MessageExchange#getEndpoint()
108      */

109     public ServiceEndpoint getEndpoint() {
110         checkComponentOwnsExchange();
111
112         return messageExchange.getEndpoint();
113     }
114
115     /**
116      * @see javax.jbi.messaging.MessageExchange#getError()
117      */

118     public Exception JavaDoc getError() {
119         checkComponentOwnsExchange();
120
121         return messageExchange.getError();
122     }
123
124     /**
125      * @see javax.jbi.messaging.MessageExchange#getExchangeId()
126      */

127     public String JavaDoc getExchangeId() {
128         checkComponentOwnsExchange();
129
130         return messageExchange.getExchangeId();
131     }
132
133     /**
134      * @see javax.jbi.messaging.MessageExchange#getFault()
135      */

136     public Fault getFault() {
137         checkComponentOwnsExchange();
138
139         return messageExchange.getFault();
140     }
141
142     /**
143      * @see javax.jbi.messaging.MessageExchange#getInterfaceName()
144      */

145     public QName JavaDoc getInterfaceName() {
146         checkComponentOwnsExchange();
147
148         return messageExchange.getInterfaceName();
149     }
150
151     /**
152      * @see javax.jbi.messaging.MessageExchange#getMessage(java.lang.String)
153      */

154     public NormalizedMessage getMessage(String JavaDoc name) {
155         checkComponentOwnsExchange();
156
157         return messageExchange.getMessage(name);
158     }
159
160     /**
161      * Return the real implementation of this MessageExchange.
162      *
163      * @return MessageExchangeImpl
164      */

165     public MessageExchangeImpl getMessageExchange() {
166         return messageExchange;
167     }
168
169     /**
170      * Return the Role that the owner of this object has in the exchange, which
171      * is invariant ( unlike the Role in which the exchange is).
172      *
173      * @return
174      */

175     public Role getObserverRole() {
176         return observerRole;
177     }
178
179     /**
180      * @see javax.jbi.messaging.MessageExchange#getOperation()
181      */

182     public QName JavaDoc getOperation() {
183         checkComponentOwnsExchange();
184
185         return messageExchange.getOperation();
186     }
187
188     /**
189      * @see javax.jbi.messaging.MessageExchange#getPattern()
190      */

191     public URI JavaDoc getPattern() {
192         checkComponentOwnsExchange();
193
194         return messageExchange.getPattern();
195     }
196
197     /**
198      * @see javax.jbi.messaging.MessageExchange#getProperty(java.lang.String)
199      */

200     public Object JavaDoc getProperty(String JavaDoc name) {
201         checkComponentOwnsExchange();
202
203         return messageExchange.getProperty(name);
204     }
205
206     /**
207      * @see javax.jbi.messaging.MessageExchange#getPropertyNames()
208      */

209     public Set JavaDoc getPropertyNames() {
210         checkComponentOwnsExchange();
211
212         return messageExchange.getPropertyNames();
213     }
214
215     /**
216      * @see javax.jbi.messaging.MessageExchange#getRole()
217      */

218     public Role getRole() {
219         checkComponentOwnsExchange();
220
221         return messageExchange.getRole();
222     }
223
224     /**
225      * @see javax.jbi.messaging.MessageExchange#getService()
226      */

227     public QName JavaDoc getService() {
228         checkComponentOwnsExchange();
229
230         return messageExchange.getService();
231     }
232
233     /**
234      * @see javax.jbi.messaging.MessageExchange#getStatus()
235      */

236     public javax.jbi.messaging.ExchangeStatus getStatus() {
237         return messageExchange.getStatus();
238     }
239
240     /**
241      * Return if the component owns the message exchange. (Check that the role
242      * of the observer is the same than the role of the message exchange)
243      *
244      * @throws IllegalStateException
245      */

246     public boolean hasOwnership() {
247         return messageExchange.getRole().equals(observerRole);
248     }
249
250     /**
251      * @see javax.jbi.messaging.MessageExchange#isTransacted()
252      */

253     public boolean isTransacted() {
254         checkComponentOwnsExchange();
255
256         return messageExchange.isTransacted();
257     }
258
259     public boolean isWaitingOnSynchronousSend() {
260         return waitingOnSynchronousSend;
261     }
262
263     /**
264      * @see javax.jbi.messaging.MessageExchange#setEndpoint(java.lang.Exception)
265      */

266     public void setEndpoint(ServiceEndpoint endpoint) {
267         checkComponentOwnsExchange();
268
269         messageExchange.setEndpoint(endpoint);
270     }
271
272     /**
273      * @see javax.jbi.messaging.MessageExchange#setError(java.lang.Exception)
274      */

275     public void setError(Exception JavaDoc error) {
276         checkComponentOwnsExchange();
277
278         messageExchange.setError(error);
279     }
280
281     /**
282      * @see javax.jbi.messaging.MessageExchange#setFault(javax.jbi.messaging.Fault)
283      */

284     public void setFault(Fault fault) throws MessagingException {
285         checkComponentOwnsExchange();
286
287         messageExchange.setFault(fault);
288     }
289
290     public void setInterfaceName(QName JavaDoc interfaceName) {
291         checkComponentOwnsExchange();
292
293         messageExchange.setInterfaceName(interfaceName);
294     }
295
296     /**
297      * @see javax.jbi.messaging.MessageExchange#setMessage(javax.jbi.messaging.NormalizedMessage,
298      * java.lang.String)
299      */

300     public void setMessage(NormalizedMessage msg, String JavaDoc name)
301         throws MessagingException {
302         checkComponentOwnsExchange();
303
304         messageExchange.setMessage(msg, name);
305     }
306
307     // //////////////////////////////////////////
308
// Specific methods
309
// //////////////////////////////////////////
310

311     /**
312      * @see javax.jbi.messaging.MessageExchange#setOperation(javax.xml.namespace.QName)
313      */

314     public void setOperation(QName JavaDoc name) {
315         checkComponentOwnsExchange();
316
317         messageExchange.setOperation(name);
318     }
319
320     /**
321      * @see javax.jbi.messaging.MessageExchange#setProperty(java.lang.String,
322      * java.lang.Object)
323      */

324     public void setProperty(String JavaDoc name, Object JavaDoc obj) {
325         checkComponentOwnsExchange();
326
327         messageExchange.setProperty(name, obj);
328     }
329
330     /**
331      * @see javax.jbi.messaging.MessageExchange#setService(javax.xml.namespace.QName)
332      */

333     public void setService(QName JavaDoc service) {
334         checkComponentOwnsExchange();
335
336         messageExchange.setService(service);
337     }
338
339     /**
340      * @see javax.jbi.messaging.MessageExchange#setStatus(javax.jbi.messaging.ExchangeStatus)
341      */

342     public void setStatus(ExchangeStatus status) throws MessagingException {
343         checkComponentOwnsExchange();
344
345         messageExchange.setStatus(status);
346     }
347
348     public void setWaitingOnSynchronousSend(boolean waitingOnSynchronousSend) {
349         this.waitingOnSynchronousSend = waitingOnSynchronousSend;
350     }
351
352     
353     public void setMessageExchange(MessageExchangeImpl messageExchange) {
354         this.messageExchange = messageExchange;
355     }
356
357     /**
358      * Check that the component owns the message exchange. (Check that the role
359      * of the observer is the same than the role of the message exchange)
360      *
361      * @throws IllegalStateException
362      */

363     protected void checkComponentOwnsExchange() throws IllegalStateException JavaDoc {
364         if (!hasOwnership()) {
365             throw new IllegalStateException JavaDoc(
366                     "The component does not owns the MessageExchange");
367         }
368     }
369
370 }
371
Popular Tags