1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.endpoint; |
12 | |
|
13 | |
import org.mule.MessageExchangePattern; |
14 | |
import org.mule.api.MuleContext; |
15 | |
import org.mule.api.MuleException; |
16 | |
import org.mule.api.MuleMessage; |
17 | |
import org.mule.api.construct.FlowConstruct; |
18 | |
import org.mule.api.construct.FlowConstructAware; |
19 | |
import org.mule.api.context.MuleContextAware; |
20 | |
import org.mule.api.endpoint.EndpointMessageProcessorChainFactory; |
21 | |
import org.mule.api.endpoint.EndpointURI; |
22 | |
import org.mule.api.endpoint.InboundEndpoint; |
23 | |
import org.mule.api.lifecycle.Initialisable; |
24 | |
import org.mule.api.lifecycle.LifecycleException; |
25 | |
import org.mule.api.lifecycle.Startable; |
26 | |
import org.mule.api.lifecycle.Stoppable; |
27 | |
import org.mule.api.processor.MessageProcessor; |
28 | |
import org.mule.api.retry.RetryPolicyTemplate; |
29 | |
import org.mule.api.transaction.TransactionConfig; |
30 | |
import org.mule.api.transport.Connector; |
31 | |
import org.mule.config.MuleManifest; |
32 | |
import org.mule.config.i18n.CoreMessages; |
33 | |
import org.mule.transport.polling.MessageProcessorPollingMessageReceiver; |
34 | |
|
35 | |
import java.beans.ExceptionListener; |
36 | |
import java.util.List; |
37 | |
import java.util.Map; |
38 | |
|
39 | |
public class DefaultInboundEndpoint extends AbstractEndpoint implements InboundEndpoint |
40 | |
{ |
41 | |
private static final long serialVersionUID = -4752772777414636142L; |
42 | |
private MessageProcessor listener; |
43 | |
private FlowConstruct flowConstruct; |
44 | |
private ExceptionListener exceptionListener; |
45 | |
|
46 | |
public DefaultInboundEndpoint(Connector connector, |
47 | |
EndpointURI endpointUri, |
48 | |
String name, |
49 | |
Map properties, |
50 | |
TransactionConfig transactionConfig, |
51 | |
boolean deleteUnacceptedMessage, |
52 | |
MessageExchangePattern messageExchangePattern, |
53 | |
int responseTimeout, |
54 | |
String initialState, |
55 | |
String endpointEncoding, |
56 | |
String endpointBuilderName, |
57 | |
MuleContext muleContext, |
58 | |
RetryPolicyTemplate retryPolicyTemplate, |
59 | |
EndpointMessageProcessorChainFactory messageProcessorsFactory, |
60 | |
List <MessageProcessor> messageProcessors, |
61 | |
List <MessageProcessor> responseMessageProcessors, |
62 | |
boolean disableTransportTransformer, |
63 | |
String mimeType) |
64 | |
{ |
65 | 0 | super(connector, endpointUri, name, properties, |
66 | |
transactionConfig, deleteUnacceptedMessage, |
67 | |
messageExchangePattern, responseTimeout, initialState, endpointEncoding, |
68 | |
endpointBuilderName, muleContext, retryPolicyTemplate, messageProcessorsFactory, |
69 | |
messageProcessors, responseMessageProcessors, disableTransportTransformer, |
70 | |
mimeType); |
71 | 0 | } |
72 | |
|
73 | |
public MuleMessage request(long timeout) throws Exception |
74 | |
{ |
75 | 0 | if (getConnector() != null) |
76 | |
{ |
77 | 0 | return getConnector().request(this, timeout); |
78 | |
} |
79 | |
else |
80 | |
{ |
81 | |
|
82 | |
|
83 | 0 | throw new IllegalStateException("The connector on the endpoint: " + toString() |
84 | |
+ " is null. Please contact " + MuleManifest.getDevListEmail()); |
85 | |
} |
86 | |
} |
87 | |
|
88 | |
public void setListener(MessageProcessor listener) |
89 | |
{ |
90 | 0 | this.listener = listener; |
91 | 0 | } |
92 | |
|
93 | |
public void start() throws MuleException |
94 | |
{ |
95 | |
try |
96 | |
{ |
97 | 0 | if (getMessageProcessorChain(flowConstruct) instanceof Startable) |
98 | |
{ |
99 | 0 | ((Startable) getMessageProcessorChain(flowConstruct)).start(); |
100 | |
} |
101 | 0 | getConnector().registerListener(this, getMessageProcessorChain(flowConstruct), flowConstruct); |
102 | 0 | MessageProcessor polledMp = getPolledMessageProcessor(); |
103 | 0 | if (polledMp instanceof Startable) |
104 | |
{ |
105 | 0 | ((Startable)polledMp).start(); |
106 | |
} |
107 | |
} |
108 | 0 | catch (Exception e) |
109 | |
{ |
110 | 0 | throw new LifecycleException(CoreMessages.failedToStartInboundEndpoint(this), e, this); |
111 | 0 | } |
112 | 0 | } |
113 | |
|
114 | |
public void stop() throws MuleException |
115 | |
{ |
116 | |
try |
117 | |
{ |
118 | 0 | getConnector().unregisterListener(this, flowConstruct); |
119 | 0 | if (getMessageProcessorChain(flowConstruct) instanceof Stoppable) |
120 | |
{ |
121 | 0 | ((Stoppable) getMessageProcessorChain(flowConstruct)).stop(); |
122 | |
} |
123 | 0 | MessageProcessor polledMp = getPolledMessageProcessor(); |
124 | 0 | if (polledMp instanceof Stoppable) |
125 | |
{ |
126 | 0 | ((Stoppable)polledMp).stop(); |
127 | |
} |
128 | |
} |
129 | 0 | catch (Exception e) |
130 | |
{ |
131 | 0 | throw new LifecycleException(CoreMessages.failedToStopInboundEndpoint(this), e, this); |
132 | 0 | } |
133 | 0 | } |
134 | |
|
135 | |
@Override |
136 | |
public MessageProcessor createMessageProcessorChain(FlowConstruct flowContruct) throws MuleException |
137 | |
{ |
138 | 0 | EndpointMessageProcessorChainFactory factory = getMessageProcessorsFactory(); |
139 | 0 | MessageProcessor processorChain = factory.createInboundMessageProcessorChain(this, flowConstruct, |
140 | |
listener); |
141 | 0 | if (processorChain instanceof MuleContextAware) |
142 | |
{ |
143 | 0 | ((MuleContextAware) processorChain).setMuleContext(getMuleContext()); |
144 | |
} |
145 | 0 | if (processorChain instanceof FlowConstructAware) |
146 | |
{ |
147 | 0 | ((FlowConstructAware) processorChain).setFlowConstruct(flowContruct); |
148 | |
} |
149 | 0 | if (processorChain instanceof Initialisable) |
150 | |
{ |
151 | 0 | ((Initialisable) processorChain).initialise(); |
152 | |
} |
153 | 0 | MessageProcessor polledMp = getPolledMessageProcessor(); |
154 | 0 | if (polledMp instanceof MuleContextAware) |
155 | |
{ |
156 | 0 | ((MuleContextAware) polledMp).setMuleContext(getMuleContext()); |
157 | |
} |
158 | 0 | if (polledMp instanceof FlowConstructAware) |
159 | |
{ |
160 | 0 | ((FlowConstructAware) polledMp).setFlowConstruct(flowContruct); |
161 | |
} |
162 | 0 | if (polledMp instanceof Initialisable) |
163 | |
{ |
164 | 0 | ((Initialisable) polledMp).initialise(); |
165 | |
} |
166 | 0 | return processorChain; |
167 | |
} |
168 | |
|
169 | |
protected MessageProcessor getPolledMessageProcessor() |
170 | |
{ |
171 | 0 | return (MessageProcessor) getProperty(MessageProcessorPollingMessageReceiver.SOURCE_MESSAGE_PROCESSOR_PROPERTY_NAME); |
172 | |
} |
173 | |
|
174 | |
public void setFlowConstruct(FlowConstruct flowConstruct) |
175 | |
{ |
176 | 0 | this.flowConstruct = flowConstruct; |
177 | 0 | } |
178 | |
|
179 | |
public ExceptionListener getExceptionListener() |
180 | |
{ |
181 | 0 | return exceptionListener; |
182 | |
} |
183 | |
|
184 | |
public void setExceptionListener(ExceptionListener exceptionListener) |
185 | |
{ |
186 | 0 | this.exceptionListener = exceptionListener; |
187 | 0 | } |
188 | |
|
189 | |
@Override |
190 | |
public void dispose() |
191 | |
{ |
192 | 0 | super.dispose(); |
193 | 0 | this.flowConstruct = null; |
194 | 0 | this.listener = null; |
195 | 0 | } |
196 | |
|
197 | |
@Override |
198 | |
public int hashCode() |
199 | |
{ |
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | 0 | return System.identityHashCode(this); |
205 | |
} |
206 | |
} |