View Javadoc

1   /*
2    * $Id: MuleEndpoint.java 9554 2007-11-02 09:23:03Z marie.rizzo $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.impl.endpoint;
12  
13  import org.mule.config.i18n.CoreMessages;
14  import org.mule.impl.ImmutableMuleEndpoint;
15  import org.mule.providers.service.TransportFactory;
16  import org.mule.umo.UMOException;
17  import org.mule.umo.UMOFilter;
18  import org.mule.umo.UMOTransactionConfig;
19  import org.mule.umo.endpoint.EndpointException;
20  import org.mule.umo.endpoint.UMOEndpoint;
21  import org.mule.umo.endpoint.UMOEndpointURI;
22  import org.mule.umo.endpoint.UMOImmutableEndpoint;
23  import org.mule.umo.lifecycle.InitialisationException;
24  import org.mule.umo.provider.UMOConnector;
25  import org.mule.umo.security.UMOEndpointSecurityFilter;
26  import org.mule.umo.transformer.UMOTransformer;
27  
28  import java.util.Map;
29  
30  /**
31   * <code>MuleEndpoint</code> describes a Provider in the Mule Server. A endpoint is
32   * a grouping of an endpoint, an endpointUri and a transformer.
33   */
34  public class MuleEndpoint extends ImmutableMuleEndpoint implements UMOEndpoint
35  {
36      public static final String ALWAYS_CREATE_STRING = "ALWAYS_CREATE";
37      public static final String NEVER_CREATE_STRING = "NEVER_CREATE";
38  
39      /**
40       * Default constructor This is required right now for the Mule digester to set
41       * the properties through the classes mutators
42       */
43      public MuleEndpoint()
44      {
45          super(null, null, null, null, ENDPOINT_TYPE_SENDER_AND_RECEIVER, 0, null, null);
46      }
47  
48      public MuleEndpoint(String name,
49                          UMOEndpointURI endpointUri,
50                          UMOConnector connector,
51                          UMOTransformer transformer,
52                          String type,
53                          int createConnector,
54                          String endpointEncoding,
55                          Map props)
56      {
57          super(name, endpointUri, connector, transformer, type, createConnector, endpointEncoding,
58              props);
59      }
60  
61      public MuleEndpoint(UMOImmutableEndpoint endpoint)
62      {
63          super(endpoint);
64      }
65  
66      public MuleEndpoint(String uri, boolean receiver) throws UMOException
67      {
68          super(uri, receiver);
69      }
70  
71      public Object clone()
72      {
73          UMOEndpoint clone = new MuleEndpoint(name, endpointUri, connector, transformer, type,
74              createConnector, endpointEncoding, properties);
75  
76          clone.setTransactionConfig(transactionConfig);
77          clone.setFilter(filter);
78          clone.setSecurityFilter(securityFilter);
79          try
80          {
81              if (responseTransformer != null)
82              {
83                  clone.setResponseTransformer((UMOTransformer)responseTransformer.clone());
84              }
85          }
86          catch (CloneNotSupportedException e1)
87          {
88              // TODO throw exception instead of suppressing it
89              logger.error(e1.getMessage(), e1);
90          }
91  
92          if (remoteSync != null)
93          {
94              clone.setRemoteSync(isRemoteSync());
95          }
96          if (remoteSyncTimeout != null)
97          {
98              clone.setRemoteSyncTimeout(getRemoteSyncTimeout());
99          }
100 
101         if (synchronous != null)
102         {
103             clone.setSynchronous(synchronous.booleanValue());
104         }
105 
106         clone.setDeleteUnacceptedMessages(deleteUnacceptedMessages);
107 
108         clone.setInitialState(initialState);
109         if (initialised.get())
110         {
111             try
112             {
113                 clone.initialise();
114             }
115             catch (InitialisationException e)
116             {
117                 // this really should never happen as the endpoint is already
118                 // initialised
119                 // TODO Mule-863: Then die or be quiet!
120                 logger.error(e.getMessage(), e);
121             }
122         }
123         
124         if (streaming)
125         {
126             clone.setStreaming(streaming);
127         }
128 
129         return clone;
130     }
131 
132     public void setEndpointURI(UMOEndpointURI endpointUri) throws EndpointException
133     {
134         if (connector != null && endpointUri != null
135             && !connector.supportsProtocol(endpointUri.getFullScheme()))
136         {
137             throw new IllegalArgumentException(
138                 CoreMessages.connectorSchemeIncompatibleWithEndpointScheme(connector.getProtocol(),
139                     endpointUri).getMessage());
140         }
141         this.endpointUri = endpointUri;
142         if (endpointUri != null)
143         {
144             properties.putAll(endpointUri.getParams());
145         }
146     }
147 
148     public void setEncoding(String endpointEncoding)
149     {
150         this.endpointEncoding = endpointEncoding;
151     }
152 
153     public void setType(String type)
154     {
155         this.type = type;
156     }
157 
158     public void setConnector(UMOConnector connector)
159     {
160         if (connector != null && endpointUri != null
161             && !connector.supportsProtocol(endpointUri.getFullScheme()))
162         {
163             throw new IllegalArgumentException(
164                 CoreMessages.connectorSchemeIncompatibleWithEndpointScheme(connector.getProtocol(),
165                     endpointUri).getMessage());
166         }
167         this.connector = connector;
168     }
169 
170     public void setName(String name)
171     {
172         this.name = name;
173     }
174 
175     public void setTransformer(UMOTransformer trans)
176     {
177         transformer = trans;
178         if (transformer != null)
179         {
180             transformer.setEndpoint(this);
181         }
182     }
183 
184     public void setProperties(Map props)
185     {
186         properties.clear();
187         properties.putAll(props);
188     }
189 
190     public boolean isReadOnly()
191     {
192         return false;
193     }
194 
195     public void setTransactionConfig(UMOTransactionConfig config)
196     {
197         transactionConfig = config;
198     }
199 
200     public void setFilter(UMOFilter filter)
201     {
202         this.filter = filter;
203     }
204 
205     /**
206      * If a filter is configured on this endpoint, this property will determine if
207      * message that are not excepted by the filter are deleted
208      * 
209      * @param delete if message should be deleted, false otherwise
210      */
211     public void setDeleteUnacceptedMessages(boolean delete)
212     {
213         deleteUnacceptedMessages = delete;
214     }
215 
216     /**
217      * Sets an UMOEndpointSecurityFilter for this endpoint. If a filter is set all
218      * traffice via this endpoint with be subject to authentication.
219      * 
220      * @param filter the UMOSecurityFilter responsible for authenticating message
221      *            flow via this endpoint.
222      * @see org.mule.umo.security.UMOEndpointSecurityFilter
223      */
224     public void setSecurityFilter(UMOEndpointSecurityFilter filter)
225     {
226         securityFilter = filter;
227         if (securityFilter != null)
228         {
229             securityFilter.setEndpoint(this);
230         }
231     }
232 
233     /**
234      * Determines if requests originating from this endpoint should be synchronous
235      * i.e. execute in a single thread and possibly return an result. This property
236      * is only used when the endpoint is of type 'receiver'.
237      * 
238      * @param synchronous whether requests on this endpoint should execute in a
239      *            single thread. This property is only used when the endpoint is of
240      *            type 'receiver'
241      */
242     public void setSynchronous(boolean synchronous)
243     {
244         this.synchronous = Boolean.valueOf(synchronous);
245     }
246 
247     public void setCreateConnector(int action)
248     {
249         createConnector = action;
250     }
251 
252     public void setCreateConnectorAsString(String action)
253     {
254         if (ALWAYS_CREATE_STRING.equals(action))
255         {
256             createConnector = TransportFactory.ALWAYS_CREATE_CONNECTOR;
257         }
258         else if (NEVER_CREATE_STRING.equals(action))
259         {
260             createConnector = TransportFactory.NEVER_CREATE_CONNECTOR;
261         }
262         else
263         {
264             createConnector = TransportFactory.GET_OR_CREATE_CONNECTOR;
265         }
266     }
267 
268     /**
269      * For certain providers that support the notion of a backchannel such as sockets
270      * (outputStream) or Jms (ReplyTo) Mule can automatically wait for a response
271      * from a backchannel when dispatching over these protocols. This is different
272      * for synchronous as synchronous behavior only applies to in
273      * 
274      * @param value whether the endpoint should perfrom sync receives
275      */
276     public void setRemoteSync(boolean value)
277     {
278         this.remoteSync = Boolean.valueOf(value);
279         if (value)
280         {
281             this.synchronous = Boolean.TRUE;
282         }
283     }
284 
285     /**
286      * The timeout value for remoteSync invocations
287      * 
288      * @param timeout the timeout in milliseconds
289      */
290     public void setRemoteSyncTimeout(int timeout)
291     {
292         this.remoteSyncTimeout = new Integer(timeout);
293     }
294 
295     /**
296      * Sets the state the endpoint will be loaded in. The States are 'stopped' and
297      * 'started' (default)
298      * 
299      * @param state
300      */
301     public void setInitialState(String state)
302     {
303         this.initialState = state;
304     }
305 
306     public void setResponseTransformer(UMOTransformer trans)
307     {
308         responseTransformer = trans;
309     }
310 
311     /**
312      * Determines whether the endpoint should deal with requests as streams
313      * 
314      * @param stream true if the request should be streamed
315      */
316     public void setStreaming(boolean stream)
317     {
318         this.streaming = stream;
319     }
320 
321     /**
322      * Sets a property on the endpoint
323      * 
324      * @param key the property key
325      * @param value the value of the property
326      */
327     public void setProperty(String key, Object value)
328     {
329         properties.put(key, value);
330     }
331 }