1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.transport.tcp; |
8 | |
|
9 | |
import org.mule.DefaultMuleMessage; |
10 | |
import org.mule.api.ExceptionPayload; |
11 | |
import org.mule.api.MuleMessage; |
12 | |
import org.mule.api.construct.FlowConstruct; |
13 | |
import org.mule.api.endpoint.InboundEndpoint; |
14 | |
import org.mule.api.lifecycle.CreateException; |
15 | |
import org.mule.api.transport.Connector; |
16 | |
import org.mule.message.DefaultExceptionPayload; |
17 | |
import org.mule.transport.AbstractMessageReceiver; |
18 | |
import org.mule.transport.NullPayload; |
19 | |
|
20 | |
import java.io.IOException; |
21 | |
import java.net.Socket; |
22 | |
import java.util.ArrayList; |
23 | |
import java.util.List; |
24 | |
|
25 | |
import javax.resource.spi.work.Work; |
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | 0 | public class ExceptionReturnTcpMessageReceiver extends TcpMessageReceiver |
34 | |
{ |
35 | |
|
36 | |
public ExceptionReturnTcpMessageReceiver(Connector connector, FlowConstruct flowConstruct, |
37 | |
InboundEndpoint endpoint) |
38 | |
throws CreateException |
39 | |
{ |
40 | 0 | super(connector, flowConstruct, endpoint); |
41 | 0 | } |
42 | |
|
43 | |
@Override |
44 | |
protected Work createWork(Socket socket) throws IOException |
45 | |
{ |
46 | 0 | return new TcpWorker(socket, this); |
47 | |
} |
48 | |
|
49 | |
protected class TcpWorker extends TcpMessageReceiver.TcpWorker |
50 | |
{ |
51 | |
|
52 | |
public TcpWorker(Socket socket, AbstractMessageReceiver receiver) throws IOException |
53 | 0 | { |
54 | 0 | super(socket, receiver); |
55 | 0 | } |
56 | |
|
57 | |
@Override |
58 | |
protected Object getNextMessage(Object resource) throws Exception |
59 | |
{ |
60 | |
try |
61 | |
{ |
62 | 0 | return super.getNextMessage(resource); |
63 | |
} |
64 | 0 | catch (Exception e) |
65 | |
{ |
66 | 0 | manageException(e); |
67 | 0 | return null; |
68 | |
} |
69 | |
} |
70 | |
|
71 | |
private void manageException(Exception readingException) throws Exception |
72 | |
{ |
73 | |
try |
74 | |
{ |
75 | 0 | logger.warn("Failed to read message: " + readingException); |
76 | |
|
77 | 0 | MuleMessage msg = new DefaultMuleMessage(NullPayload.getInstance(), connector.getMuleContext()); |
78 | 0 | ExceptionPayload exceptionPayload = new DefaultExceptionPayload(readingException); |
79 | 0 | msg.setExceptionPayload(exceptionPayload); |
80 | 0 | List msgList = new ArrayList(1); |
81 | 0 | msgList.add(msg); |
82 | |
|
83 | 0 | handleResults(msgList); |
84 | |
} |
85 | 0 | catch (Exception writingException) |
86 | |
{ |
87 | 0 | logger.warn("Failed to write exception back to client: " + writingException); |
88 | 0 | throw writingException; |
89 | 0 | } |
90 | 0 | } |
91 | |
} |
92 | |
} |