Coverage Report - org.mule.transport.KeyedPoolMessageDispatcherFactoryAdapter
 
Classes in this File Line Coverage Branch Coverage Complexity
KeyedPoolMessageDispatcherFactoryAdapter
0%
0/38
0%
0/14
1.462
 
 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  0
         super();
 40  
 
 41  0
         if (factory == null)
 42  
         {
 43  0
             throw new IllegalArgumentException(CoreMessages.objectIsNull("factory").toString());
 44  
         }
 45  
 
 46  0
         this.factory = factory;
 47  0
     }
 48  
 
 49  
     public void activateObject(Object key, Object obj) throws Exception
 50  
     {
 51  0
         OutboundEndpoint endpoint = (OutboundEndpoint)key;
 52  
         //Ensure dispatcher has the same lifecycle as the connector
 53  0
         applyLifecycle((MessageDispatcher)obj);
 54  
 
 55  0
         factory.activate((OutboundEndpoint) key, (MessageDispatcher) obj);
 56  0
     }
 57  
 
 58  
     public void destroyObject(Object key, Object obj) throws Exception
 59  
     {
 60  0
         factory.destroy((OutboundEndpoint) key, (MessageDispatcher) obj);
 61  0
     }
 62  
 
 63  
     public Object makeObject(Object key) throws Exception
 64  
     {
 65  0
         OutboundEndpoint endpoint = (OutboundEndpoint) key;
 66  0
         MessageDispatcher dispatcher = factory.create(endpoint);
 67  0
         applyLifecycle(dispatcher);
 68  0
         return dispatcher;
 69  
     }
 70  
 
 71  
     public void passivateObject(Object key, Object obj) throws Exception
 72  
     {
 73  0
         factory.passivate((OutboundEndpoint) key, (MessageDispatcher) obj);
 74  0
     }
 75  
 
 76  
     public boolean validateObject(Object key, Object obj)
 77  
     {
 78  0
         return factory.validate((OutboundEndpoint) key, (MessageDispatcher) obj);
 79  
     }
 80  
 
 81  
     public boolean isCreateDispatcherPerRequest()
 82  
     {
 83  0
         return factory.isCreateDispatcherPerRequest();
 84  
     }
 85  
 
 86  
     public MessageDispatcher create(OutboundEndpoint endpoint) throws MuleException
 87  
     {
 88  0
         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  0
         applyLifecycle(dispatcher);
 95  0
         factory.activate(endpoint, dispatcher);
 96  0
     }
 97  
 
 98  
     public void destroy(OutboundEndpoint endpoint, MessageDispatcher dispatcher)
 99  
     {
 100  0
         factory.destroy(endpoint, dispatcher);
 101  0
     }
 102  
 
 103  
     public void passivate(OutboundEndpoint endpoint, MessageDispatcher dispatcher)
 104  
     {
 105  0
         factory.passivate(endpoint, dispatcher);
 106  0
     }
 107  
 
 108  
     public boolean validate(OutboundEndpoint endpoint, MessageDispatcher dispatcher)
 109  
     {
 110  0
         return factory.validate(endpoint, dispatcher);
 111  
     }
 112  
 
 113  
     protected void applyLifecycle(MessageDispatcher dispatcher) throws MuleException
 114  
     {
 115  0
         String phase = ((AbstractConnector)dispatcher.getConnector()).getLifecycleManager().getCurrentPhase();
 116  0
         if(phase.equals(Startable.PHASE_NAME) && !dispatcher.getLifecycleState().isStarted())
 117  
         {
 118  0
             if(!dispatcher.getLifecycleState().isInitialised())
 119  
             {
 120  0
                 dispatcher.initialise();
 121  
             }
 122  0
             dispatcher.start();
 123  
         }
 124  0
         else if(phase.equals(Stoppable.PHASE_NAME) && dispatcher.getLifecycleState().isStarted())
 125  
         {
 126  0
             dispatcher.stop();
 127  
         }
 128  0
         else if(Disposable.PHASE_NAME.equals(phase))
 129  
         {
 130  0
             dispatcher.dispose();
 131  
         }
 132  0
     }
 133  
 
 134  
 }