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.transport;
8   
9   import org.mule.api.MuleException;
10  import org.mule.api.endpoint.OutboundEndpoint;
11  import org.mule.api.transport.MessageDispatcher;
12  import org.mule.api.transport.MessageDispatcherFactory;
13  import org.mule.util.ClassUtils;
14  
15  /**
16   * <code>AbstractMessageDispatcherFactory</code> is a base implementation of the
17   * <code>MessageDispatcherFactory</code> interface for managing the lifecycle of
18   * message dispatchers.
19   * 
20   * @see MessageDispatcherFactory
21   */
22  public abstract class AbstractMessageDispatcherFactory implements MessageDispatcherFactory
23  {
24  
25      public AbstractMessageDispatcherFactory()
26      {
27          super();
28      }
29  
30      /**
31       * This default implementation of
32       * {@link MessageDispatcherFactory#isCreateDispatcherPerRequest()} returns
33       * <code>false</code>, which means that dispatchers are pooled according to
34       * their lifecycle as described in {@link MessageDispatcher}.
35       * 
36       * @return <code>false</code> by default, unless overwritten by a subclass.
37       */
38      public boolean isCreateDispatcherPerRequest()
39      {
40          return false;
41      }
42  
43      public abstract MessageDispatcher create(OutboundEndpoint endpoint) throws MuleException;
44  
45      public void activate(OutboundEndpoint endpoint, MessageDispatcher dispatcher) throws MuleException
46      {
47          dispatcher.activate();
48      }
49  
50      public void destroy(OutboundEndpoint endpoint, MessageDispatcher dispatcher)
51      {
52          dispatcher.dispose();
53      }
54  
55      public void passivate(OutboundEndpoint endpoint, MessageDispatcher dispatcher)
56      {
57          dispatcher.passivate();
58      }
59  
60      public boolean validate(OutboundEndpoint endpoint, MessageDispatcher dispatcher)
61      {
62          // Unless dispatchers are to be disposed of after every request, we check if
63          // the dispatcher is still valid or has e.g. disposed itself after an
64          // exception.
65          return (this.isCreateDispatcherPerRequest() ? false : dispatcher.validate());
66      }
67  
68      @Override
69      public String toString()
70      {
71          final StringBuffer sb = new StringBuffer(60);
72          sb.append(ClassUtils.getSimpleName(this.getClass()));
73          sb.append("{this=").append(Integer.toHexString(System.identityHashCode(this)));
74          sb.append(", createDispatcherPerRequest=").append(this.isCreateDispatcherPerRequest());
75          sb.append('}');
76          return sb.toString();
77      }
78  
79  }