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