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