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.lifecycle.Disposable;
12  import org.mule.api.lifecycle.Startable;
13  import org.mule.api.lifecycle.Stoppable;
14  import org.mule.api.transport.MessageRequester;
15  import org.mule.api.transport.MessageRequesterFactory;
16  import org.mule.config.i18n.CoreMessages;
17  
18  import org.apache.commons.pool.KeyedPoolableObjectFactory;
19  
20  /**
21   * <code>KeyedPoolMessageRequesterFactoryAdapter</code> adapts a
22   * <code>MessageRequesterFactory</code> with methods from commons-pool
23   * <code>KeyedPoolableObjectFactory</code>. It is only required for requester
24   * factories that do not inherit from <code>AbstractMessageRequesterFactory</code>.
25   *
26   * @see org.mule.transport.AbstractMessageRequesterFactory
27   */
28  public class KeyedPoolMessageRequesterFactoryAdapter
29      implements MessageRequesterFactory, KeyedPoolableObjectFactory
30  {
31      private final MessageRequesterFactory factory;
32  
33      public KeyedPoolMessageRequesterFactoryAdapter(MessageRequesterFactory factory)
34      {
35          super();
36  
37          if (factory == null)
38          {
39              throw new IllegalArgumentException(CoreMessages.objectIsNull("factory").toString());
40          }
41  
42          this.factory = factory;
43      }
44  
45      public void activateObject(Object key, Object obj) throws Exception
46      {
47          //Ensure requester has the same lifecycle as the connector
48          applyLifecycle((MessageRequester)obj, false);
49  
50          factory.activate((InboundEndpoint) key, (MessageRequester) obj);
51      }
52  
53      public void destroyObject(Object key, Object obj) throws Exception
54      {
55          factory.destroy((InboundEndpoint) key, (MessageRequester) obj);
56      }
57  
58      public Object makeObject(Object key) throws Exception
59      {
60          Object obj = factory.create((InboundEndpoint) key);
61          applyLifecycle((MessageRequester)obj, true);
62          return obj;
63      }
64  
65      public void passivateObject(Object key, Object obj) throws Exception
66      {
67          factory.passivate((InboundEndpoint) key, (MessageRequester) obj);
68      }
69  
70      public boolean validateObject(Object key, Object obj)
71      {
72          return factory.validate((InboundEndpoint) key, (MessageRequester) obj);
73      }
74  
75      public boolean isCreateRequesterPerRequest()
76      {
77          return factory.isCreateRequesterPerRequest();
78      }
79  
80      public MessageRequester create(InboundEndpoint endpoint) throws MuleException
81      {
82          return factory.create(endpoint);
83      }
84  
85      public void activate(InboundEndpoint endpoint, MessageRequester requester) throws MuleException
86      {
87          factory.activate(endpoint, requester);
88      }
89  
90      public void destroy(InboundEndpoint endpoint, MessageRequester requester)
91      {
92          factory.destroy(endpoint, requester);
93      }
94  
95      public void passivate(InboundEndpoint endpoint, MessageRequester requester)
96      {
97          factory.passivate(endpoint, requester);
98      }
99  
100     public boolean validate(InboundEndpoint endpoint, MessageRequester requester)
101     {
102         return factory.validate(endpoint, requester);
103     }
104 
105     protected void applyLifecycle(MessageRequester requester, boolean created) throws MuleException
106     {
107         String phase = ((AbstractConnector)requester.getConnector()).getLifecycleManager().getCurrentPhase();
108         if(phase.equals(Startable.PHASE_NAME) && !requester.getLifecycleState().isStarted())
109         {
110             if(!requester.getLifecycleState().isInitialised())
111             {
112                 requester.initialise();
113             }
114             requester.start();
115         }
116         else if(phase.equals(Stoppable.PHASE_NAME) && requester.getLifecycleState().isStarted())
117         {
118             requester.stop();
119         }
120         else if(Disposable.PHASE_NAME.equals(phase))
121         {
122             requester.dispose();
123         }
124     }
125 }