KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > component > common > listener > AbstractInternalMEProcessor


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2006 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.listener;
23
24 import java.util.logging.Level JavaDoc;
25 import java.util.logging.Logger JavaDoc;
26
27 import javax.jbi.messaging.DeliveryChannel;
28 import javax.jbi.messaging.ExchangeStatus;
29 import javax.jbi.messaging.Fault;
30 import javax.jbi.messaging.MessageExchange;
31 import javax.jbi.messaging.MessagingException;
32 import javax.jbi.messaging.NormalizedMessage;
33 import javax.xml.namespace.QName JavaDoc;
34 import javax.xml.transform.Source JavaDoc;
35
36 import org.objectweb.petals.component.common.MEPConstants;
37 import org.objectweb.petals.component.common.util.SourceHelper;
38
39 /**
40  * This abstract class helps the processing of MessageExchanges for a Service
41  * Engine that have to transfer this call to an internal service. Only two
42  * methods have to be implemented: - sendInMessage(in), for sending an In
43  * message to an internal service, - sendInOutMessage(in,out), same as above,
44  * and wait for the response.
45  *
46  * DeliveryChannel.send() is treated, Reception or send of DONE-status is
47  * treated, Fault message is generated if one of the method throws an exception
48  * (and if the MEP allow Fault message, otherwise, nothing is done).
49  *
50  * @version $Revision: $ $Date: $
51  * @since Petals 1.0
52  * @author alouis
53  */

54 public abstract class AbstractInternalMEProcessor implements
55     IMessageExchangeProcessor {
56
57     /**
58      * The component's delivery channel
59      */

60     protected DeliveryChannel channel;
61
62     protected Logger JavaDoc l;
63
64     /**
65      * Constructor. The componentcontext is used to retrieve the
66      * wsdl-information of the endpoint to contact. TODO use the SUMan instead
67      * of the CContext
68      *
69      * @param ctx,
70      * must be non null
71      * @param l
72      * logger, may be null
73      */

74     public AbstractInternalMEProcessor(DeliveryChannel channel, Logger JavaDoc l) {
75         this.channel = channel;
76         this.l = l;
77     }
78     
79     /**
80      * Default constructor
81      */

82     public AbstractInternalMEProcessor() {
83     }
84
85     /**
86      * Ignore DONE,ERROR status. Ignore "consumer" role.
87      */

88     public boolean process(MessageExchange exchange) throws Exception JavaDoc {
89         if (ExchangeStatus.ACTIVE.equals(exchange.getStatus())) {
90             if (MessageExchange.Role.PROVIDER.equals(exchange.getRole())) {
91                 if (MEPConstants.IN_ONLY_PATTERN.equals(exchange.getPattern())) {
92                     processInOnly(exchange);
93                 } else if (MEPConstants.ROBUST_IN_ONLY_PATTERN.equals(exchange
94                     .getPattern())) {
95                     processRobustInOnly(exchange);
96                 } else if (MEPConstants.IN_OUT_PATTERN.equals(exchange
97                     .getPattern())) {
98                     processInOut(exchange, false);
99                 } else if (MEPConstants.IN_OPTIONAL_OUT_PATTERN.equals(exchange
100                     .getPattern())) {
101                     processInOut(exchange, true);
102                 } else {
103                     Exception JavaDoc e = new Exception JavaDoc(
104                         "MessageExchangePattern not recognized :"
105                             + exchange.getPattern());
106                     exchange.setError(e);
107                     channel.send(exchange);
108                     throw e;
109                 }
110                 channel.send(exchange);
111             }
112         }
113         return true;
114     }
115
116     /**
117      * If the received exchange is a fault response from the consumer,
118      * acknowledge it : status DONE
119      *
120      * @param ex
121      * @return true if the ack. of the Fault is done; false if no fault is
122      * present in the exchange
123      * @throws Exception
124      */

125     protected boolean ackFaultReception(MessageExchange ex) throws Exception JavaDoc {
126         boolean result = false;
127
128         if (ex.getFault() != null) {
129             ex.setStatus(ExchangeStatus.DONE);
130
131             result = true;
132         }
133         return result;
134     }
135
136     /**
137      * Creates a Fault from an exception
138      *
139      * @param e
140      * @return
141      */

142     protected Fault createFault(Exception JavaDoc e, MessageExchange exchange)
143         throws MessagingException {
144         Fault f = exchange.createFault();
145         String JavaDoc faultString = SourceHelper.createSoapFault(e, null);
146         Source JavaDoc content = SourceHelper.createSource(faultString);
147         f.setContent(content);
148         return f;
149     }
150
151     /**
152      * Creates a NormalizedMessage for Out
153      *
154      * @return
155      */

156     protected NormalizedMessage createNormalizedMessage(MessageExchange exchange)
157         throws MessagingException {
158         return exchange.createMessage();
159     }
160
161     protected void logError(Throwable JavaDoc e) {
162         if (l != null)
163             l.log(Level.SEVERE, e.getMessage(), e);
164         else
165             e.printStackTrace();
166     }
167
168     protected void processInOnly(MessageExchange exchange) throws Exception JavaDoc {
169         try {
170             sendInMessage(exchange.getEndpoint().getServiceName(), exchange
171                 .getOperation(), exchange.getMessage("in"));
172         } catch (Exception JavaDoc e) {
173             // nothing, InOnly can not indicate a fault
174
logError(new Exception JavaDoc(
175                 "Error occured while processing, but InOnly message can not contains Fault.",
176                 e));
177         }
178         exchange.setStatus(ExchangeStatus.DONE);
179     }
180
181     protected void processInOut(MessageExchange exchange, boolean optional)
182         throws Exception JavaDoc {
183         try {
184             if (!ackFaultReception(exchange)) {
185                 NormalizedMessage out = createNormalizedMessage(exchange);
186
187                 boolean outTreated = sendInOutMessage(exchange.getEndpoint()
188                     .getServiceName(), exchange.getOperation(), exchange
189                     .getMessage("in"), out, optional);
190
191                 if (outTreated) {
192                     exchange.setMessage(out, "out");
193                 } else {
194                     exchange.setStatus(ExchangeStatus.DONE);
195                 }
196             }
197         } catch (Exception JavaDoc e) {
198             try {
199                 exchange.setFault(createFault(e, exchange));
200             } catch (Exception JavaDoc e1) {
201                 logError(new Exception JavaDoc(
202                     "Error occured while processing, send a Fault.", e1));
203             }
204         }
205     }
206
207     protected void processRobustInOnly(MessageExchange exchange)
208         throws Exception JavaDoc {
209         try {
210             sendInMessage(exchange.getEndpoint().getServiceName(), exchange
211                 .getOperation(), exchange.getMessage("in"));
212
213             exchange.setStatus(ExchangeStatus.DONE);
214         } catch (Exception JavaDoc e) {
215             try {
216                 exchange.setFault(createFault(e, exchange));
217             } catch (Exception JavaDoc e1) {
218                 logError(new Exception JavaDoc(
219                     "Error occured while processing, send a Fault.", e1));
220             }
221         }
222     }
223
224     /**
225      * Called to send an In message (InOnly or RobustInOnly).
226      *
227      * @param service
228      * @param operation
229      * @param in
230      * @throws Exception
231      * if RobustInOnly, a Fault will be generated with the exception
232      */

233     protected abstract void sendInMessage(QName JavaDoc service, QName JavaDoc operation,
234         NormalizedMessage in) throws Exception JavaDoc;
235
236     /**
237      * Called to send an InOut message (InOut or InOptionalOut).
238      *
239      * @param service
240      * @param operation
241      * @param in
242      * @param out
243      * the out message on which the response has to be set
244      * @param optionalOut
245      * the response is optional or not
246      * @return true if the out response has been set (correspond to the
247      * 'optionalOut' notion)
248      * @throws Exception
249      * a Fault will be generated with the exception, the "out" will
250      * not be treated
251      */

252     protected abstract boolean sendInOutMessage(QName JavaDoc service, QName JavaDoc operation,
253         NormalizedMessage in, NormalizedMessage out, boolean optionalOut)
254         throws Exception JavaDoc;
255
256     public DeliveryChannel getChannel() {
257         return channel;
258     }
259
260     public void setChannel(DeliveryChannel channel) {
261         this.channel = channel;
262     }
263
264     public Logger JavaDoc getLogger() {
265         return l;
266     }
267
268     public void setLogger(Logger JavaDoc l) {
269         this.l = l;
270     }
271 }
272
Popular Tags