KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > component > common > util > MessageExchangeWrapperImpl


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005 EBM Websourcing, http://www.ebmwebsourcing.com/
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 (at your option) any later version.
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.component.common.util;
23
24 import java.net.URI JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.Set JavaDoc;
28
29 import javax.activation.DataHandler JavaDoc;
30 import javax.jbi.messaging.ExchangeStatus;
31 import javax.jbi.messaging.Fault;
32 import javax.jbi.messaging.MessageExchange;
33 import javax.jbi.messaging.MessagingException;
34 import javax.jbi.messaging.NormalizedMessage;
35 import javax.jbi.servicedesc.ServiceEndpoint;
36 import javax.xml.namespace.QName JavaDoc;
37 import javax.xml.transform.Source JavaDoc;
38
39 import org.objectweb.petals.component.common.HandlingException;
40 import org.objectweb.petals.component.common.MEPConstants;
41 import org.objectweb.petals.component.common.PEtALSComponentSDKException;
42
43 /**
44  * This is a Message Exchange Wrapper
45  *
46  * @author ofabre
47  *
48  */

49 public class MessageExchangeWrapperImpl implements MessageExchangeWrapper {
50
51     private MessageExchange exchange;
52
53     private static final String JavaDoc IN_MESSAGE_NAME = "in";
54
55     private static final String JavaDoc OUT_MESSAGE_NAME = "out";
56
57     public MessageExchangeWrapperImpl(MessageExchange exchange) {
58         super();
59         this.exchange = exchange;
60     }
61     
62     public MessageExchange getMessageExchange() {
63         return exchange;
64     }
65
66     public Fault createFault() throws MessagingException {
67         return exchange.createFault();
68     }
69
70     public NormalizedMessage createMessage() throws MessagingException {
71         return exchange.createMessage();
72     }
73
74     public ServiceEndpoint getEndpoint() {
75         return exchange.getEndpoint();
76     }
77
78     public Exception JavaDoc getError() {
79         return exchange.getError();
80     }
81
82     public String JavaDoc getExchangeId() {
83         return exchange.getExchangeId();
84     }
85
86     public Fault getFault() {
87         return exchange.getFault();
88     }
89
90     public QName JavaDoc getInterfaceName() {
91         return exchange.getInterfaceName();
92     }
93
94     public NormalizedMessage getMessage(String JavaDoc name) {
95         return exchange.getMessage(name);
96     }
97
98     public QName JavaDoc getOperation() {
99         return exchange.getOperation();
100     }
101
102     public URI JavaDoc getPattern() {
103         return exchange.getPattern();
104     }
105
106     public Object JavaDoc getProperty(String JavaDoc name) {
107         return exchange.getProperty(name);
108     }
109
110     public Set JavaDoc getPropertyNames() {
111         return exchange.getPropertyNames();
112     }
113
114     public Role getRole() {
115         return exchange.getRole();
116     }
117
118     public QName JavaDoc getService() {
119         return exchange.getService();
120     }
121
122     public ExchangeStatus getStatus() {
123         return exchange.getStatus();
124     }
125
126     public boolean isTransacted() {
127         return exchange.isTransacted();
128     }
129
130     public void setEndpoint(ServiceEndpoint endpoint) {
131         exchange.setEndpoint(endpoint);
132     }
133
134     public void setError(Exception JavaDoc error) {
135         exchange.setError(error);
136     }
137
138     public void setFault(Fault fault) throws MessagingException {
139         exchange.setFault(fault);
140     }
141
142     public void setInterfaceName(QName JavaDoc interfaceName) {
143         exchange.setInterfaceName(interfaceName);
144     }
145
146     public void setMessage(NormalizedMessage msg, String JavaDoc name)
147             throws MessagingException {
148         exchange.setMessage(msg, name);
149     }
150
151     public void setOperation(QName JavaDoc name) {
152         exchange.setOperation(name);
153     }
154
155     public void setProperty(String JavaDoc name, Object JavaDoc obj) {
156         exchange.setProperty(name, obj);
157     }
158
159     public void setService(QName JavaDoc service) {
160         exchange.setService(service);
161     }
162
163     public void setStatus(ExchangeStatus status) throws MessagingException {
164         exchange.setStatus(status);
165     }
166
167     public void setOutMessageContent(String JavaDoc outContent)
168             throws HandlingException {
169         try {
170             NormalizedMessage outMess = getOutMessage();
171             if (outMess == null) {
172                 outMess = exchange.createMessage();
173             }
174             outMess.setContent(SourceHelper.createSource(outContent));
175             setOutMessage(outMess);
176         } catch (MessagingException e) {
177             throw new HandlingException("Error creating response message");
178         } catch (PEtALSComponentSDKException e) {
179             throw new HandlingException("Error creating response message");
180         }
181     }
182
183     public void setOutMessageAttachements(Map JavaDoc<String JavaDoc, DataHandler JavaDoc> attachements)
184             throws HandlingException {
185         try {
186             NormalizedMessage outMess = getOutMessage();
187             if (outMess == null) {
188                 outMess = exchange.createMessage();
189             }
190             for (String JavaDoc attachementId : attachements.keySet()) {
191                 outMess.addAttachment(attachementId, attachements
192                         .get(attachementId));
193             }
194             setOutMessage(outMess);
195         } catch (MessagingException e) {
196             throw new HandlingException("Error creating response message");
197         }
198     }
199
200     public String JavaDoc getInMessageContent() throws HandlingException {
201         NormalizedMessage inMessage = getInMessage();
202         if (inMessage == null) {
203             throw new HandlingException("In message must not be null");
204         }
205         Source JavaDoc content = inMessage.getContent();
206         String JavaDoc stringContent = null;
207         try {
208             stringContent = SourceHelper.createString(content);
209         } catch (PEtALSComponentSDKException e) {
210             throw new HandlingException("Error creating string from source", e);
211         }
212         return stringContent;
213     }
214
215     public Set JavaDoc<DataHandler JavaDoc> getInMessageAttachments() throws HandlingException {
216         Set JavaDoc<DataHandler JavaDoc> attachments = new HashSet JavaDoc<DataHandler JavaDoc>();
217
218         NormalizedMessage inMessage = getInMessage();
219         if (inMessage == null) {
220             throw new HandlingException("In message must not be null");
221         }
222
223         Set JavaDoc names = inMessage.getAttachmentNames();
224         for (Object JavaDoc attachId : names) {
225             DataHandler JavaDoc dh = inMessage.getAttachment((String JavaDoc) attachId);
226             if (dh != null)
227                 attachments.add(dh);
228         }
229         return attachments;
230     }
231
232     public URI JavaDoc getExchangePattern() throws HandlingException {
233         URI JavaDoc pattern = exchange.getPattern();
234         if (pattern == null) {
235             throw new HandlingException(
236                     "You must specify a message exchange pattern in the message exchange");
237         }
238         return pattern;
239     }
240
241     public String JavaDoc getOperationName() throws HandlingException {
242         QName JavaDoc operation = exchange.getOperation();
243         if (operation == null) {
244             throw new HandlingException(
245                     "You must specify an operation the message exchange");
246         }
247         return operation.getLocalPart();
248     }
249
250     public String JavaDoc getEndpointName() throws HandlingException {
251         ServiceEndpoint ep = exchange.getEndpoint();
252         if (ep == null) {
253             throw new HandlingException(
254                     "You must specify an endpoint in the message exchange");
255         }
256         return ep.getEndpointName();
257     }
258
259     public NormalizedMessage getInMessage() {
260         return exchange.getMessage(IN_MESSAGE_NAME);
261     }
262
263     public void setInMessage(final NormalizedMessage msg)
264             throws MessagingException {
265         exchange.setMessage(msg, IN_MESSAGE_NAME);
266     }
267
268     public NormalizedMessage getOutMessage() {
269         return exchange.getMessage(OUT_MESSAGE_NAME);
270     }
271
272     public void setOutMessage(final NormalizedMessage msg)
273             throws MessagingException {
274         exchange.setMessage(msg, OUT_MESSAGE_NAME);
275     }
276     
277     /*### STATUS ###*/
278     
279     public void setDoneStatus() throws MessagingException {
280         setStatus(ExchangeStatus.DONE);
281     }
282     
283     public boolean isDoneStatus() {
284         return (ExchangeStatus.DONE.equals(getStatus()));
285     }
286
287     public void setErrorStatus() throws MessagingException {
288         setStatus(ExchangeStatus.ERROR);
289     }
290     
291     public boolean isErrorStatus() {
292         return (ExchangeStatus.ERROR.equals(getStatus()));
293     }
294     
295     public void setActiveStatus() throws MessagingException {
296         setStatus(ExchangeStatus.ACTIVE);
297     }
298     
299     public boolean isActiveStatus() {
300         return (ExchangeStatus.ACTIVE.equals(getStatus()));
301     }
302     
303     /*### ROLE ###*/
304     
305     public boolean isProviderRole() {
306         return (MessageExchange.Role.PROVIDER.equals(getRole()));
307     }
308     
309     public boolean isConsumerRole() {
310         return (MessageExchange.Role.CONSUMER.equals(getRole()));
311     }
312
313     /*### PATTERN ###*/
314     
315     public boolean isInOnlyPattern() {
316         return (MEPConstants.IN_ONLY_PATTERN.value().equals(
317             exchange.getPattern()));
318     }
319     
320     public boolean isRobustInOnlyPattern() {
321         return (MEPConstants.ROBUST_IN_ONLY_PATTERN.value().equals(
322             exchange.getPattern()));
323     }
324     
325     public boolean isInOutPattern() {
326         return (MEPConstants.IN_OUT_PATTERN.value().equals(
327             exchange.getPattern()));
328     }
329     
330     public boolean isInOptionalOutPattern() {
331         return (MEPConstants.IN_OPTIONAL_OUT_PATTERN.value().equals(
332             exchange.getPattern()));
333     }
334 }
335
Popular Tags