View Javadoc

1   /*
2    * $Id: DefaultInboundEndpoint.java 19547 2010-09-10 12:22:31Z dfeist $
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  
34  import java.beans.ExceptionListener;
35  import java.util.List;
36  import java.util.Map;
37  
38  public class DefaultInboundEndpoint extends AbstractEndpoint implements InboundEndpoint
39  {
40      private static final long serialVersionUID = -4752772777414636142L;
41      private MessageProcessor listener;
42      private FlowConstruct flowConstruct;
43      private ExceptionListener exceptionListener;
44  
45      public DefaultInboundEndpoint(Connector connector,
46                                    EndpointURI endpointUri,
47                                    String name,
48                                    Map properties,
49                                    TransactionConfig transactionConfig,
50                                    boolean deleteUnacceptedMessage,
51                                    MessageExchangePattern messageExchangePattern,
52                                    int responseTimeout,
53                                    String initialState,
54                                    String endpointEncoding,
55                                    String endpointBuilderName,
56                                    MuleContext muleContext,
57                                    RetryPolicyTemplate retryPolicyTemplate,
58                                    EndpointMessageProcessorChainFactory messageProcessorsFactory,
59                                    List <MessageProcessor> messageProcessors,
60                                    List <MessageProcessor> responseMessageProcessors,
61                                    boolean disableTransportTransformer,
62                                    String mimeType)
63      {
64          super(connector, endpointUri, name, properties, 
65              transactionConfig, deleteUnacceptedMessage,
66              messageExchangePattern, responseTimeout, initialState, endpointEncoding, 
67              endpointBuilderName, muleContext, retryPolicyTemplate,  messageProcessorsFactory, 
68              messageProcessors, responseMessageProcessors, disableTransportTransformer,
69              mimeType);
70      }
71  
72      public MuleMessage request(long timeout) throws Exception
73      {
74          if (getConnector() != null)
75          {
76              return getConnector().request(this, timeout);
77          }
78          else
79          {
80              // TODO Either remove because this should never happen or i18n the
81              // message
82              throw new IllegalStateException("The connector on the endpoint: " + toString()
83                                              + " is null. Please contact " + MuleManifest.getDevListEmail());
84          }
85      }
86  
87      public void setListener(MessageProcessor listener)
88      {
89          this.listener = listener;
90      }
91  
92      public void start() throws MuleException
93      {
94          try
95          {
96              if (getMessageProcessorChain(flowConstruct) instanceof Startable)
97              {
98                  ((Startable) getMessageProcessorChain(flowConstruct)).start();
99              }
100             getConnector().registerListener(this, getMessageProcessorChain(flowConstruct), flowConstruct);
101         }
102         catch (Exception e)
103         {
104             throw new LifecycleException(CoreMessages.failedToStartInboundEndpoint(this), e, this);
105         }
106     }
107 
108     public void stop() throws MuleException
109     {
110         try
111         {
112             getConnector().unregisterListener(this, flowConstruct);
113             if (getMessageProcessorChain(flowConstruct) instanceof Stoppable)
114             {
115                 ((Stoppable) getMessageProcessorChain(flowConstruct)).stop();
116             }
117         }
118         catch (Exception e)
119         {
120             throw new LifecycleException(CoreMessages.failedToStartInboundEndpoint(this), e, this);
121         }
122     }
123 
124     @Override
125     public MessageProcessor createMessageProcessorChain(FlowConstruct flowContruct) throws MuleException
126     {
127         EndpointMessageProcessorChainFactory factory = getMessageProcessorsFactory();
128         MessageProcessor processorChain = factory.createInboundMessageProcessorChain(this, flowConstruct,
129             listener);
130         if (processorChain instanceof MuleContextAware)
131         {
132             ((MuleContextAware) processorChain).setMuleContext(getMuleContext());
133         }
134         if (processorChain instanceof FlowConstructAware)
135         {
136             ((FlowConstructAware) processorChain).setFlowConstruct(flowContruct);
137         }
138         if (processorChain instanceof Initialisable)
139         {
140             ((Initialisable) processorChain).initialise();
141         }
142         return processorChain;
143     }
144 
145     public void setFlowConstruct(FlowConstruct flowConstruct)
146     {
147         this.flowConstruct = flowConstruct;
148     }
149 
150     public ExceptionListener getExceptionListener()
151     {
152         return exceptionListener;
153     }
154 
155     public void setExceptionListener(ExceptionListener exceptionListener)
156     {
157         this.exceptionListener = exceptionListener;
158     }
159 
160     @Override
161     public void dispose()
162     {
163         super.dispose();
164         this.flowConstruct = null;
165         this.listener = null;
166     }
167     
168     @Override
169     public int hashCode()
170     {
171         // We need unique hashcode for each inbound endpoint instance because flowConstuct and listener are not
172         // injected until after endpoint has been created and cached and the key used for caching is hashcode.
173         // If we don't do this then endpoints which are configured identically but used with different
174         // services get mixed up after deserialization of events
175         return System.identityHashCode(this);
176     }
177 }