View Javadoc

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