View Javadoc

1   /*
2    * $Id: KeyedPoolMessageDispatcherFactoryAdapter.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.OutboundEndpoint;
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.MessageDispatcher;
19  import org.mule.api.transport.MessageDispatcherFactory;
20  import org.mule.config.i18n.CoreMessages;
21  
22  import org.apache.commons.pool.KeyedPoolableObjectFactory;
23  
24  /**
25   * <code>KeyedPoolMessageDispatcherFactoryAdapter</code> adapts a
26   * <code>MessageDispatcherFactory</code> with methods from commons-pool
27   * <code>KeyedPoolableObjectFactory</code>. It is only required for dispatcher
28   * factories that do not inherit from <code>AbstractMessageDispatcherFactory</code>.
29   * 
30   * @see AbstractMessageDispatcherFactory
31   */
32  public class KeyedPoolMessageDispatcherFactoryAdapter
33      implements MessageDispatcherFactory, KeyedPoolableObjectFactory
34  {
35      private final MessageDispatcherFactory factory;
36  
37      public KeyedPoolMessageDispatcherFactoryAdapter(MessageDispatcherFactory 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          OutboundEndpoint endpoint = (OutboundEndpoint)key;
52          //Ensure dispatcher has the same lifecycle as the connector
53          applyLifecycle((MessageDispatcher)obj);
54  
55          factory.activate((OutboundEndpoint) key, (MessageDispatcher) obj);
56      }
57  
58      public void destroyObject(Object key, Object obj) throws Exception
59      {
60          factory.destroy((OutboundEndpoint) key, (MessageDispatcher) obj);
61      }
62  
63      public Object makeObject(Object key) throws Exception
64      {
65          OutboundEndpoint endpoint = (OutboundEndpoint) key;
66          MessageDispatcher dispatcher = factory.create(endpoint);
67          applyLifecycle(dispatcher);
68          return dispatcher;
69      }
70  
71      public void passivateObject(Object key, Object obj) throws Exception
72      {
73          factory.passivate((OutboundEndpoint) key, (MessageDispatcher) obj);
74      }
75  
76      public boolean validateObject(Object key, Object obj)
77      {
78          return factory.validate((OutboundEndpoint) key, (MessageDispatcher) obj);
79      }
80  
81      public boolean isCreateDispatcherPerRequest()
82      {
83          return factory.isCreateDispatcherPerRequest();
84      }
85  
86      public MessageDispatcher create(OutboundEndpoint endpoint) throws MuleException
87      {
88          return factory.create(endpoint);
89      }
90  
91      public void activate(OutboundEndpoint endpoint, MessageDispatcher dispatcher) throws MuleException
92      {
93          //Ensure dispatcher has the same lifecycle as the connector
94          applyLifecycle(dispatcher);
95          factory.activate(endpoint, dispatcher);
96      }
97  
98      public void destroy(OutboundEndpoint endpoint, MessageDispatcher dispatcher)
99      {
100         factory.destroy(endpoint, dispatcher);
101     }
102 
103     public void passivate(OutboundEndpoint endpoint, MessageDispatcher dispatcher)
104     {
105         factory.passivate(endpoint, dispatcher);
106     }
107 
108     public boolean validate(OutboundEndpoint endpoint, MessageDispatcher dispatcher)
109     {
110         return factory.validate(endpoint, dispatcher);
111     }
112 
113     protected void applyLifecycle(MessageDispatcher dispatcher) throws MuleException
114     {
115         String phase = ((AbstractConnector)dispatcher.getConnector()).getLifecycleManager().getCurrentPhase();
116         if(phase.equals(Startable.PHASE_NAME) && !dispatcher.getLifecycleState().isStarted())
117         {
118             if(!dispatcher.getLifecycleState().isInitialised())
119             {
120                 dispatcher.initialise();
121             }
122             dispatcher.start();
123         }
124         else if(phase.equals(Stoppable.PHASE_NAME) && dispatcher.getLifecycleState().isStarted())
125         {
126             dispatcher.stop();
127         }
128         else if(Disposable.PHASE_NAME.equals(phase))
129         {
130             dispatcher.dispose();
131         }
132     }
133 
134 }