View Javadoc

1   /*
2    * $Id: DefaultInboundEndpoint.java 20721 2010-12-14 19:39:43Z tcarlson $
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.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          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      }
72  
73      public MuleMessage request(long timeout) throws Exception
74      {
75          if (getConnector() != null)
76          {
77              return getConnector().request(this, timeout);
78          }
79          else
80          {
81              // TODO Either remove because this should never happen or i18n the
82              // message
83              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          this.listener = listener;
91      }
92  
93      public void start() throws MuleException
94      {
95          try
96          {
97              if (getMessageProcessorChain(flowConstruct) instanceof Startable)
98              {
99                  ((Startable) getMessageProcessorChain(flowConstruct)).start();
100             }
101             getConnector().registerListener(this, getMessageProcessorChain(flowConstruct), flowConstruct);
102             MessageProcessor polledMp = getPolledMessageProcessor();
103             if (polledMp instanceof Startable)
104             {
105                  ((Startable)polledMp).start();
106             }
107         }
108         catch (Exception e)
109         {
110             throw new LifecycleException(CoreMessages.failedToStartInboundEndpoint(this), e, this);
111         }
112     }
113 
114     public void stop() throws MuleException
115     {
116         try
117         {
118             getConnector().unregisterListener(this, flowConstruct);
119             if (getMessageProcessorChain(flowConstruct) instanceof Stoppable)
120             {
121                 ((Stoppable) getMessageProcessorChain(flowConstruct)).stop();
122             }
123             MessageProcessor polledMp = getPolledMessageProcessor();
124             if (polledMp instanceof Stoppable)
125             {
126                 ((Stoppable)polledMp).stop();
127             }
128         }
129         catch (Exception e)
130         {
131             throw new LifecycleException(CoreMessages.failedToStopInboundEndpoint(this), e, this);
132         }
133     }
134 
135     @Override
136     public MessageProcessor createMessageProcessorChain(FlowConstruct flowContruct) throws MuleException
137     {
138         EndpointMessageProcessorChainFactory factory = getMessageProcessorsFactory();
139         MessageProcessor processorChain = factory.createInboundMessageProcessorChain(this, flowConstruct,
140             listener);
141         if (processorChain instanceof MuleContextAware)
142         {
143             ((MuleContextAware) processorChain).setMuleContext(getMuleContext());
144         }
145         if (processorChain instanceof FlowConstructAware)
146         {
147             ((FlowConstructAware) processorChain).setFlowConstruct(flowContruct);
148         }
149         if (processorChain instanceof Initialisable)
150         {
151             ((Initialisable) processorChain).initialise();
152         }
153         MessageProcessor polledMp = getPolledMessageProcessor();
154         if (polledMp instanceof MuleContextAware)
155         {
156             ((MuleContextAware) polledMp).setMuleContext(getMuleContext());
157         }
158         if (polledMp instanceof FlowConstructAware)
159         {
160             ((FlowConstructAware) polledMp).setFlowConstruct(flowContruct);
161         }
162         if (polledMp instanceof Initialisable)
163         {
164             ((Initialisable) polledMp).initialise();
165         }
166         return processorChain;
167     }
168 
169     protected MessageProcessor getPolledMessageProcessor()
170     {
171         return (MessageProcessor) getProperty(MessageProcessorPollingMessageReceiver.SOURCE_MESSAGE_PROCESSOR_PROPERTY_NAME);
172     }
173 
174     public void setFlowConstruct(FlowConstruct flowConstruct)
175     {
176         this.flowConstruct = flowConstruct;
177     }
178 
179     public ExceptionListener getExceptionListener()
180     {
181         return exceptionListener;
182     }
183 
184     public void setExceptionListener(ExceptionListener exceptionListener)
185     {
186         this.exceptionListener = exceptionListener;
187     }
188 
189     @Override
190     public void dispose()
191     {
192         super.dispose();
193         this.flowConstruct = null;
194         this.listener = null;
195     }
196     
197     @Override
198     public int hashCode()
199     {
200         // We need unique hashcode for each inbound endpoint instance because flowConstuct and listener are not
201         // injected until after endpoint has been created and cached and the key used for caching is hashcode.
202         // If we don't do this then endpoints which are configured identically but used with different
203         // services get mixed up after deserialization of events
204         return System.identityHashCode(this);
205     }
206 }