View Javadoc

1   /*
2    * $Id: DefaultInboundEndpoint.java 22826 2011-09-02 07:30:19Z mike.schilling $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
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.processor.AbstractRedeliveryPolicy;
34  import org.mule.transport.ConnectException;
35  import org.mule.transport.polling.MessageProcessorPollingMessageReceiver;
36  
37  import java.beans.ExceptionListener;
38  import java.util.List;
39  import java.util.Map;
40  
41  public class DefaultInboundEndpoint extends AbstractEndpoint implements InboundEndpoint
42  {
43      private static final long serialVersionUID = -4752772777414636142L;
44      private MessageProcessor listener;
45      private FlowConstruct flowConstruct;
46      private ExceptionListener exceptionListener;
47  
48      public DefaultInboundEndpoint(Connector connector,
49                                    EndpointURI endpointUri,
50                                    String name,
51                                    Map properties,
52                                    TransactionConfig transactionConfig,
53                                    boolean deleteUnacceptedMessage,
54                                    MessageExchangePattern messageExchangePattern,
55                                    int responseTimeout,
56                                    String initialState,
57                                    String endpointEncoding,
58                                    String endpointBuilderName,
59                                    MuleContext muleContext,
60                                    RetryPolicyTemplate retryPolicyTemplate,
61                                    AbstractRedeliveryPolicy redeliveryPolicy,
62                                    EndpointMessageProcessorChainFactory messageProcessorsFactory,
63                                    List <MessageProcessor> messageProcessors,
64                                    List <MessageProcessor> responseMessageProcessors,
65                                    boolean disableTransportTransformer,
66                                    String mimeType)
67      {
68          super(connector, endpointUri, name, properties, 
69              transactionConfig, deleteUnacceptedMessage,
70              messageExchangePattern, responseTimeout, initialState, endpointEncoding, 
71              endpointBuilderName, muleContext, retryPolicyTemplate, redeliveryPolicy, messageProcessorsFactory,
72              messageProcessors, responseMessageProcessors, disableTransportTransformer,
73              mimeType);
74      }
75  
76      public MuleMessage request(long timeout) throws Exception
77      {
78          if (getConnector() != null)
79          {
80              return getConnector().request(this, timeout);
81          }
82          else
83          {
84              // TODO Either remove because this should never happen or i18n the
85              // message
86              throw new IllegalStateException("The connector on the endpoint: " + toString()
87                                              + " is null. Please contact " + MuleManifest.getDevListEmail());
88          }
89      }
90  
91      public void setListener(MessageProcessor listener)
92      {
93          this.listener = listener;
94      }
95  
96      public void start() throws MuleException
97      {
98          try
99          {
100             if (getMessageProcessorChain(flowConstruct) instanceof Startable)
101             {
102                 ((Startable) getMessageProcessorChain(flowConstruct)).start();
103             }
104             getConnector().registerListener(this, getMessageProcessorChain(flowConstruct), flowConstruct);
105             MessageProcessor polledMp = getPolledMessageProcessor();
106             if (polledMp instanceof Startable)
107             {
108                  ((Startable)polledMp).start();
109             }
110         }
111         // Let connection exceptions bubble up to trigger the reconnection strategy.
112         catch (ConnectException ce)
113         {
114             throw ce;
115         }
116         catch (Exception e)
117         {
118             throw new LifecycleException(CoreMessages.failedToStartInboundEndpoint(this), e, this);
119         }
120     }
121 
122     public void stop() throws MuleException
123     {
124         try
125         {
126             getConnector().unregisterListener(this, flowConstruct);
127             if (getMessageProcessorChain(flowConstruct) instanceof Stoppable)
128             {
129                 ((Stoppable) getMessageProcessorChain(flowConstruct)).stop();
130             }
131             MessageProcessor polledMp = getPolledMessageProcessor();
132             if (polledMp instanceof Stoppable)
133             {
134                 ((Stoppable)polledMp).stop();
135             }
136         }
137         catch (Exception e)
138         {
139             throw new LifecycleException(CoreMessages.failedToStopInboundEndpoint(this), e, this);
140         }
141     }
142 
143     @Override
144     public MessageProcessor createMessageProcessorChain(FlowConstruct flowContruct) throws MuleException
145     {
146         EndpointMessageProcessorChainFactory factory = getMessageProcessorsFactory();
147         MessageProcessor processorChain = factory.createInboundMessageProcessorChain(this, flowConstruct,
148             listener);
149         if (processorChain instanceof MuleContextAware)
150         {
151             ((MuleContextAware) processorChain).setMuleContext(getMuleContext());
152         }
153         if (processorChain instanceof FlowConstructAware)
154         {
155             ((FlowConstructAware) processorChain).setFlowConstruct(flowContruct);
156         }
157         if (processorChain instanceof Initialisable)
158         {
159             ((Initialisable) processorChain).initialise();
160         }
161         MessageProcessor polledMp = getPolledMessageProcessor();
162         if (polledMp instanceof MuleContextAware)
163         {
164             ((MuleContextAware) polledMp).setMuleContext(getMuleContext());
165         }
166         if (polledMp instanceof FlowConstructAware)
167         {
168             ((FlowConstructAware) polledMp).setFlowConstruct(flowContruct);
169         }
170         if (polledMp instanceof Initialisable)
171         {
172             ((Initialisable) polledMp).initialise();
173         }
174         return processorChain;
175     }
176 
177     protected MessageProcessor getPolledMessageProcessor()
178     {
179         return (MessageProcessor) getProperty(MessageProcessorPollingMessageReceiver.SOURCE_MESSAGE_PROCESSOR_PROPERTY_NAME);
180     }
181 
182     public void setFlowConstruct(FlowConstruct flowConstruct)
183     {
184         this.flowConstruct = flowConstruct;
185     }
186 
187     public ExceptionListener getExceptionListener()
188     {
189         return exceptionListener;
190     }
191 
192     public void setExceptionListener(ExceptionListener exceptionListener)
193     {
194         this.exceptionListener = exceptionListener;
195     }
196 
197     @Override
198     public void dispose()
199     {
200         super.dispose();
201         this.flowConstruct = null;
202         this.listener = null;
203     }
204     
205     @Override
206     public int hashCode()
207     {
208         // We need unique hashcode for each inbound endpoint instance because flowConstuct and listener are not
209         // injected until after endpoint has been created and cached and the key used for caching is hashcode.
210         // If we don't do this then endpoints which are configured identically but used with different
211         // services get mixed up after deserialization of events
212         return System.identityHashCode(this);
213     }
214     
215 }