View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.endpoint;
8   
9   import org.mule.MessageExchangePattern;
10  import org.mule.api.MuleContext;
11  import org.mule.api.MuleException;
12  import org.mule.api.MuleMessage;
13  import org.mule.api.construct.FlowConstruct;
14  import org.mule.api.endpoint.EndpointMessageProcessorChainFactory;
15  import org.mule.api.endpoint.EndpointURI;
16  import org.mule.api.endpoint.InboundEndpoint;
17  import org.mule.api.lifecycle.LifecycleException;
18  import org.mule.api.processor.MessageProcessor;
19  import org.mule.api.retry.RetryPolicyTemplate;
20  import org.mule.api.routing.filter.Filter;
21  import org.mule.api.security.EndpointSecurityFilter;
22  import org.mule.api.transaction.TransactionConfig;
23  import org.mule.api.transformer.Transformer;
24  import org.mule.api.transport.Connector;
25  import org.mule.config.i18n.CoreMessages;
26  import org.mule.transport.ConnectException;
27  
28  import java.util.List;
29  import java.util.Map;
30  
31  /**
32   * Allow's EndpointURI to be set and changed dynamically by wrapping up an immutable endpoint instance.
33   */
34  public class DynamicURIInboundEndpoint implements InboundEndpoint
35  {
36  
37      private static final long serialVersionUID = -2814979100270307813L;
38  
39      protected InboundEndpoint endpoint;
40      private EndpointURI dynamicEndpointURI;
41      private MessageProcessor listener;
42      private FlowConstruct flowConstruct;
43  
44      public DynamicURIInboundEndpoint(InboundEndpoint endpoint)
45      {
46          this(endpoint, null);
47      }
48  
49      public DynamicURIInboundEndpoint(InboundEndpoint endpoint, EndpointURI dynamicEndpointURI)
50      {
51  //        if (endpoint instanceof DynamicURIInboundEndpoint) 
52  //        {
53  //            throw new IllegalArgumentException("Dynamic endpoints can only wrap immuntable InboundEndpoint instances!");
54  //        }
55  //        
56          this.endpoint = endpoint;
57          setEndpointURI(dynamicEndpointURI);
58      }
59  
60      public EndpointURI getEndpointURI()
61      {
62          if (dynamicEndpointURI != null)
63          {
64              return dynamicEndpointURI;
65          }
66          else
67          {
68              return endpoint.getEndpointURI();
69          }
70      }
71  
72      public String getAddress()
73      {
74          EndpointURI uri = getEndpointURI();
75          if (uri != null)
76          {
77              return uri.getUri().toString();
78          }
79          else
80          {
81              return null;
82          }
83      }
84  
85      public void setEndpointURI(EndpointURI dynamicEndpointURI)
86      {
87          this.dynamicEndpointURI = dynamicEndpointURI;
88      }
89  
90      public RetryPolicyTemplate getRetryPolicyTemplate()
91      {
92          return endpoint.getRetryPolicyTemplate();
93      }
94  
95      public Connector getConnector()
96      {
97          return endpoint.getConnector();
98      }
99  
100     public String getEncoding()
101     {
102         return endpoint.getEncoding();
103     }
104 
105     public String getMimeType()
106     {
107         return endpoint.getMimeType();
108     }
109 
110     public Filter getFilter()
111     {
112         return endpoint.getFilter();
113     }
114 
115     public String getInitialState()
116     {
117         return endpoint.getInitialState();
118     }
119 
120     public MuleContext getMuleContext()
121     {
122         return endpoint.getMuleContext();
123     }
124 
125     public String getName()
126     {
127         return endpoint.getName();
128     }
129 
130     public Map getProperties()
131     {
132         return endpoint.getProperties();
133     }
134 
135     public Object getProperty(Object key)
136     {
137         return endpoint.getProperty(key);
138     }
139 
140     public String getProtocol()
141     {
142         return endpoint.getProtocol();
143     }
144 
145     public int getResponseTimeout()
146     {
147         return endpoint.getResponseTimeout();
148     }
149 
150     public List<Transformer> getResponseTransformers()
151     {
152         return endpoint.getResponseTransformers();
153     }
154 
155     public EndpointMessageProcessorChainFactory getMessageProcessorsFactory()
156     {
157         return endpoint.getMessageProcessorsFactory();
158     }
159     
160     public List <MessageProcessor> getMessageProcessors()
161     {
162         return endpoint.getMessageProcessors();
163     }
164 
165     public List<MessageProcessor> getResponseMessageProcessors()
166     {
167         return endpoint.getResponseMessageProcessors();
168     }
169 
170     public EndpointSecurityFilter getSecurityFilter()
171     {
172         return endpoint.getSecurityFilter();
173     }
174 
175     public TransactionConfig getTransactionConfig()
176     {
177         return endpoint.getTransactionConfig();
178     }
179 
180     public List<Transformer> getTransformers()
181     {
182         return endpoint.getTransformers();
183     }
184 
185     public boolean isDeleteUnacceptedMessages()
186     {
187         return endpoint.isDeleteUnacceptedMessages();
188     }
189 
190     public boolean isReadOnly()
191     {
192         return endpoint.isReadOnly();
193     }
194     
195     public MessageExchangePattern getExchangePattern()
196     {
197         return endpoint.getExchangePattern();
198     }
199 
200     public MuleMessage request(long timeout) throws Exception
201     {
202         return getConnector().request(this, timeout);
203     }
204 
205     public String getEndpointBuilderName()
206     {
207         return endpoint.getEndpointBuilderName();
208     }
209 
210     public boolean isProtocolSupported(String protocol)
211     {
212         return getConnector().supportsProtocol(protocol);
213     }
214 
215     public boolean isDisableTransportTransformer()
216     {
217         return endpoint.isDisableTransportTransformer();
218     }
219 
220     @Override
221     public int hashCode()
222     {
223         final int prime = 31;
224         int result = 1;
225         result = prime * result + ((dynamicEndpointURI == null) ? 0 : dynamicEndpointURI.hashCode());
226         result = prime * result + ((endpoint == null) ? 0 : endpoint.hashCode());
227         return result;
228     }
229 
230     @Override
231     public boolean equals(Object obj)
232     {
233         if (this == obj)
234         {
235             return true;
236         }
237         if (obj == null)
238         {
239             return false;
240         }
241         if (getClass() != obj.getClass())
242         {
243             return false;
244         }
245         final DynamicURIInboundEndpoint other = (DynamicURIInboundEndpoint) obj;
246         if (dynamicEndpointURI == null)
247         {
248             if (other.dynamicEndpointURI != null)
249             {
250                 return false;
251             }
252         }
253         else if (!dynamicEndpointURI.equals(other.dynamicEndpointURI))
254         {
255             return false;
256         }
257         if (endpoint == null)
258         {
259             if (other.endpoint != null)
260             {
261                 return false;
262             }
263         }
264         else if (!endpoint.equals(other.endpoint))
265         {
266             return false;
267         }
268         return true;
269     }
270 
271     public void start() throws MuleException
272     {
273         try
274         {
275             getConnector().registerListener(this, listener, flowConstruct);
276         }
277         // Let connection exceptions bubble up to trigger the reconnection strategy.
278         catch (ConnectException ce)
279         {
280             throw ce;
281         }
282         catch (Exception e)
283         {
284             throw new LifecycleException(CoreMessages.failedToStartInboundEndpoint(this), e, this);
285         }
286     }
287 
288     public void stop() throws MuleException
289     {
290         try
291         {
292             getConnector().unregisterListener(this, flowConstruct);
293         }
294         catch (Exception e)
295         {
296             throw new LifecycleException(CoreMessages.failedToStartInboundEndpoint(this), e, this);
297         }
298     }
299 
300     public void setFlowConstruct(FlowConstruct flowConstruct)
301     {
302         this.flowConstruct = flowConstruct;
303     }
304 
305     public void setListener(MessageProcessor listener)
306     {
307         this.listener = listener;
308     }
309 }