View Javadoc

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