1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.transport.bpm; |
12 | |
|
13 | |
import org.mule.DefaultMuleEvent; |
14 | |
import org.mule.MessageExchangePattern; |
15 | |
import org.mule.RequestContext; |
16 | |
import org.mule.api.MuleEvent; |
17 | |
import org.mule.api.MuleException; |
18 | |
import org.mule.api.MuleMessage; |
19 | |
import org.mule.api.construct.FlowConstruct; |
20 | |
import org.mule.api.context.WorkManager; |
21 | |
import org.mule.api.endpoint.EndpointBuilder; |
22 | |
import org.mule.api.endpoint.InboundEndpoint; |
23 | |
import org.mule.api.endpoint.OutboundEndpoint; |
24 | |
import org.mule.api.lifecycle.CreateException; |
25 | |
import org.mule.api.transport.Connector; |
26 | |
import org.mule.api.transport.ConnectorException; |
27 | |
import org.mule.api.transport.PropertyScope; |
28 | |
import org.mule.config.i18n.MessageFactory; |
29 | |
import org.mule.module.bpm.Process; |
30 | |
import org.mule.session.DefaultMuleSession; |
31 | |
import org.mule.transport.AbstractMessageReceiver; |
32 | |
|
33 | |
import java.util.Map; |
34 | |
|
35 | |
import javax.resource.spi.work.Work; |
36 | |
import javax.resource.spi.work.WorkException; |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
public class ProcessMessageReceiver extends AbstractMessageReceiver |
44 | |
{ |
45 | |
|
46 | 0 | private ProcessConnector connector = null; |
47 | |
|
48 | |
public ProcessMessageReceiver(Connector connector, FlowConstruct flowConstruct, InboundEndpoint endpoint) |
49 | |
throws CreateException |
50 | |
{ |
51 | 0 | super(connector, flowConstruct, endpoint); |
52 | 0 | this.connector = (ProcessConnector) connector; |
53 | 0 | } |
54 | |
|
55 | |
public MuleMessage generateSynchronousEvent(String endpoint, Object payload, Map messageProperties) throws MuleException |
56 | |
{ |
57 | 0 | logger.debug("Executing process is sending an event (synchronously) to Mule endpoint = " + endpoint); |
58 | 0 | MuleMessage response = generateEvent(endpoint, payload, messageProperties, |
59 | |
MessageExchangePattern.REQUEST_RESPONSE); |
60 | 0 | if (logger.isDebugEnabled()) |
61 | |
{ |
62 | 0 | logger.debug("Synchronous response is " + (response != null ? response.getPayload() : null)); |
63 | |
} |
64 | 0 | return response; |
65 | |
} |
66 | |
|
67 | |
public void generateAsynchronousEvent(String endpoint, Object payload, Map messageProperties) throws MuleException, WorkException |
68 | |
{ |
69 | 0 | logger.debug("Executing process is dispatching an event (asynchronously) to Mule endpoint = " + endpoint); |
70 | 0 | WorkManager workManager = getWorkManager(); |
71 | 0 | if (workManager != null) |
72 | |
{ |
73 | 0 | workManager.scheduleWork(new Worker(endpoint, payload, messageProperties)); |
74 | |
} |
75 | |
else |
76 | |
{ |
77 | 0 | throw new ConnectorException(MessageFactory.createStaticMessage("WorkManager not available"), getConnector()); |
78 | |
} |
79 | 0 | } |
80 | |
|
81 | |
protected MuleMessage generateEvent(String endpoint, Object payload, Map messageProperties, MessageExchangePattern exchangePattern) throws MuleException |
82 | |
{ |
83 | |
MuleMessage message; |
84 | 0 | if (payload instanceof MuleMessage) |
85 | |
{ |
86 | 0 | message = (MuleMessage) payload; |
87 | |
} |
88 | |
else |
89 | |
{ |
90 | 0 | message = createMuleMessage(payload, this.endpoint.getEncoding()); |
91 | |
} |
92 | 0 | message.addProperties(messageProperties, PropertyScope.INBOUND); |
93 | 0 | message.addProperties(messageProperties, PropertyScope.INVOCATION); |
94 | |
|
95 | |
|
96 | 0 | EndpointBuilder endpointBuilder = connector.getMuleContext().getRegistry().lookupEndpointFactory().getEndpointBuilder(endpoint); |
97 | 0 | endpointBuilder.setExchangePattern(exchangePattern); |
98 | 0 | OutboundEndpoint ep = endpointBuilder.buildOutboundEndpoint(); |
99 | |
|
100 | 0 | DefaultMuleEvent event = new DefaultMuleEvent(message, ep, new DefaultMuleSession(flowConstruct, connector.getMuleContext())); |
101 | |
|
102 | |
|
103 | 0 | RequestContext.setEvent(event); |
104 | 0 | if (messageProperties.get(Process.PROPERTY_PROCESS_TYPE) != null) |
105 | |
{ |
106 | 0 | event.getMessage().setSessionProperty(Process.PROPERTY_PROCESS_TYPE, messageProperties.get(Process.PROPERTY_PROCESS_TYPE)); |
107 | |
} |
108 | 0 | if (messageProperties.get(Process.PROPERTY_PROCESS_ID) != null) |
109 | |
{ |
110 | 0 | event.getMessage().setSessionProperty(Process.PROPERTY_PROCESS_ID, messageProperties.get(Process.PROPERTY_PROCESS_ID)); |
111 | |
} |
112 | |
|
113 | 0 | MuleEvent resultEvent = ep.process(event); |
114 | |
|
115 | 0 | MuleMessage response = null; |
116 | 0 | if (resultEvent != null) |
117 | |
{ |
118 | 0 | response = resultEvent.getMessage(); |
119 | 0 | if (response.getExceptionPayload() != null) |
120 | |
{ |
121 | 0 | throw new ConnectorException(MessageFactory.createStaticMessage("Unable to send or route message"), getConnector(), response.getExceptionPayload().getRootException()); |
122 | |
} |
123 | |
} |
124 | |
|
125 | 0 | return response; |
126 | |
} |
127 | |
|
128 | |
private class Worker implements Work |
129 | |
{ |
130 | |
private String endpoint; |
131 | |
private Object payload; |
132 | |
private Map messageProperties; |
133 | |
|
134 | |
public Worker(String endpoint, Object payload, Map messageProperties) |
135 | 0 | { |
136 | 0 | this.endpoint = endpoint; |
137 | 0 | this.payload = payload; |
138 | 0 | this.messageProperties = messageProperties; |
139 | 0 | } |
140 | |
|
141 | |
public void run() |
142 | |
{ |
143 | |
try |
144 | |
{ |
145 | 0 | generateEvent(endpoint, payload, messageProperties, MessageExchangePattern.ONE_WAY); |
146 | |
} |
147 | 0 | catch (Exception e) |
148 | |
{ |
149 | 0 | getConnector().getMuleContext().getExceptionListener().handleException(e); |
150 | 0 | } |
151 | 0 | } |
152 | |
|
153 | |
public void release() |
154 | 0 | { } |
155 | |
} |
156 | |
} |