View Javadoc

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