Coverage Report - org.mule.transport.vm.VMMessageDispatcher
 
Classes in this File Line Coverage Branch Coverage Complexity
VMMessageDispatcher
85%
40/47
67%
12/18
2.625
VMMessageDispatcher$1
100%
3/3
N/A
2.625
VMMessageDispatcher$2
100%
2/2
N/A
2.625
 
 1  
 /*
 2  
  * $Id: VMMessageDispatcher.java 11756 2008-05-14 16:43:50Z aguenther $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.vm;
 12  
 
 13  
 import org.mule.api.MuleEvent;
 14  
 import org.mule.api.MuleMessage;
 15  
 import org.mule.api.endpoint.EndpointURI;
 16  
 import org.mule.api.endpoint.OutboundEndpoint;
 17  
 import org.mule.api.transaction.TransactionCallback;
 18  
 import org.mule.api.transport.DispatchException;
 19  
 import org.mule.api.transport.NoReceiverForEndpointException;
 20  
 import org.mule.config.i18n.CoreMessages;
 21  
 import org.mule.transaction.TransactionTemplate;
 22  
 import org.mule.transport.AbstractMessageDispatcher;
 23  
 import org.mule.transport.vm.i18n.VMMessages;
 24  
 import org.mule.util.queue.Queue;
 25  
 import org.mule.util.queue.QueueSession;
 26  
 
 27  
 /**
 28  
  * <code>VMMessageDispatcher</code> is used for providing in memory interaction
 29  
  * between components.
 30  
  */
 31  
 public class VMMessageDispatcher extends AbstractMessageDispatcher
 32  
 {
 33  
     private final VMConnector connector;
 34  
 
 35  
     public VMMessageDispatcher(OutboundEndpoint endpoint)
 36  
     {
 37  243
         super(endpoint);
 38  243
         this.connector = (VMConnector) endpoint.getConnector();
 39  243
     }
 40  
 
 41  
     protected void doDispatch(final MuleEvent event) throws Exception
 42  
     {
 43  6194
         EndpointURI endpointUri = event.getEndpoint().getEndpointURI();
 44  
         //Apply any outbound transformers on this event before we dispatch
 45  6194
         event.transformMessage();
 46  
 
 47  6194
         if (endpointUri == null)
 48  
         {
 49  0
             throw new DispatchException(
 50  
                     CoreMessages.objectIsNull("Endpoint"), event.getMessage(), event.getEndpoint());
 51  
         }
 52  6194
         if (connector.isQueueEvents())
 53  
         {
 54  2150
             QueueSession session = connector.getQueueSession();
 55  2150
             Queue queue = session.getQueue(endpointUri.getAddress());
 56  2150
             queue.put(event.getMessage());
 57  2150
         }
 58  
         else
 59  
         {
 60  4044
             final VMMessageReceiver receiver = connector.getReceiver(event.getEndpoint().getEndpointURI());
 61  4044
             if (receiver == null)
 62  
             {
 63  0
                 logger.warn("No receiver for endpointUri: " + event.getEndpoint().getEndpointURI());
 64  0
                 return;
 65  
             }
 66  4044
             MuleMessage message = event.getMessage(); 
 67  4044
             connector.getSessionHandler().storeSessionInfoToMessage(event.getSession(), message);
 68  4044
             TransactionTemplate tt = new TransactionTemplate(receiver.getEndpoint().getTransactionConfig(), 
 69  
                 connector.getExceptionListener(), event.getMuleContext());
 70  
 
 71  4044
             TransactionCallback cb = new TransactionCallback()
 72  
             {
 73  4044
                 public Object doInTransaction() throws Exception
 74  
                 {
 75  4044
                     receiver.onMessage(event.getMessage());
 76  4044
                     return null;
 77  
                 }
 78  
             };
 79  4044
             tt.execute(cb);
 80  
         }
 81  6194
         if (logger.isDebugEnabled())
 82  
         {
 83  0
             logger.debug("dispatched MuleEvent on endpointUri: " + endpointUri);
 84  
         }
 85  6194
     }
 86  
 
 87  
     protected MuleMessage doSend(final MuleEvent event) throws Exception
 88  
     {
 89  
         MuleMessage retMessage;
 90  4088
         EndpointURI endpointUri = event.getEndpoint().getEndpointURI();
 91  4088
         final VMMessageReceiver receiver = connector.getReceiver(endpointUri);
 92  
         //Apply any outbound transformers on this event before we dispatch
 93  4088
         event.transformMessage();
 94  4088
         if (receiver == null)
 95  
         {
 96  26
             if (connector.isQueueEvents())
 97  
             {
 98  26
                 if (logger.isDebugEnabled())
 99  
                 {
 100  0
                     logger.debug("Writing to queue as there is no receiver on connector: "
 101  
                             + connector.getName() + ", for endpointUri: "
 102  
                             + event.getEndpoint().getEndpointURI());
 103  
                 }
 104  26
                 doDispatch(event);
 105  26
                 return null;
 106  
             }
 107  
             else
 108  
             {
 109  0
                 throw new NoReceiverForEndpointException(
 110  
                         VMMessages.noReceiverForEndpoint(connector.getName(),
 111  
                                 event.getEndpoint().getEndpointURI()));
 112  
             }
 113  
         }
 114  
 
 115  4062
         MuleMessage message = event.getMessage(); 
 116  4062
         connector.getSessionHandler().storeSessionInfoToMessage(event.getSession(), message);
 117  
 
 118  4062
         TransactionTemplate tt = new TransactionTemplate(receiver.getEndpoint().getTransactionConfig(), 
 119  
             connector.getExceptionListener(), event.getMuleContext());
 120  
         
 121  4062
         TransactionCallback cb = new TransactionCallback()
 122  
         {
 123  4062
             public Object doInTransaction() throws Exception
 124  
             {
 125  4062
                 return receiver.onCall(event.getMessage(), true);
 126  
             }
 127  
         };
 128  4062
         retMessage = (MuleMessage) tt.execute(cb);
 129  
         
 130  4062
         if (logger.isDebugEnabled())
 131  
         {
 132  0
             logger.debug("sent event on endpointUri: " + event.getEndpoint().getEndpointURI());
 133  
         }
 134  4062
         return retMessage;
 135  
     }
 136  
 
 137  
     protected void doDispose()
 138  
     {
 139  
         // template method
 140  243
     }
 141  
 
 142  
     protected void doConnect() throws Exception
 143  
     {
 144  243
         if (connector.isQueueEvents())
 145  
         {
 146  
             // use the default queue profile to configure this queue.
 147  151
             connector.getQueueProfile().configureQueue(
 148  
                     endpoint.getEndpointURI().getAddress(), connector.getQueueManager());
 149  
         }
 150  243
     }
 151  
 
 152  
     protected void doDisconnect() throws Exception
 153  
     {
 154  
         // template method
 155  243
     }
 156  
 
 157  
 }