Coverage Report - org.mule.transport.udp.UdpMessageDispatcher
 
Classes in this File Line Coverage Branch Coverage Complexity
UdpMessageDispatcher
60%
25/42
14%
2/14
2.125
 
 1  
 /*
 2  
  * $Id: UdpMessageDispatcher.java 10961 2008-02-22 19:01:02Z dfeist $
 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.udp;
 12  
 
 13  
 import org.mule.DefaultMuleMessage;
 14  
 import org.mule.api.MuleEvent;
 15  
 import org.mule.api.MuleMessage;
 16  
 import org.mule.api.endpoint.ImmutableEndpoint;
 17  
 import org.mule.api.endpoint.OutboundEndpoint;
 18  
 import org.mule.transport.AbstractMessageDispatcher;
 19  
 
 20  
 import java.io.IOException;
 21  
 import java.net.DatagramPacket;
 22  
 import java.net.DatagramSocket;
 23  
 import java.net.InetAddress;
 24  
 
 25  
 /**
 26  
  * <code>UdpMessageDispatcher</code> is responsible for dispatching MuleEvents as
 27  
  * UDP packets on the network
 28  
  */
 29  
 
 30  
 public class UdpMessageDispatcher extends AbstractMessageDispatcher
 31  
 {
 32  
     protected final UdpConnector connector;
 33  
 
 34  
     public UdpMessageDispatcher(OutboundEndpoint endpoint)
 35  
     {
 36  2
         super(endpoint);
 37  2
         this.connector = (UdpConnector)endpoint.getConnector();
 38  2
     }
 39  
 
 40  
     protected void doConnect() throws Exception
 41  
     {
 42  
         // Test the connection
 43  2
         DatagramSocket socket = connector.getSocket(endpoint);
 44  2
         connector.releaseSocket(socket, endpoint);
 45  2
     }
 46  
 
 47  
     protected void doDisconnect() throws Exception
 48  
     {
 49  
         // nothing to do
 50  2
     }
 51  
 
 52  
 
 53  
     protected synchronized void doDispatch(MuleEvent event) throws Exception
 54  
     {
 55  3000
         ImmutableEndpoint ep = event.getEndpoint();
 56  
 
 57  3000
         DatagramSocket socket = connector.getSocket(ep);
 58  
         try
 59  
         {
 60  3000
             byte[] payload = event.transformMessageToBytes();
 61  
 
 62  3000
             int port = ep.getEndpointURI().getPort();
 63  3000
             InetAddress inetAddress = null;
 64  
             //TODO, check how expensive this operation is
 65  3000
             if("null".equalsIgnoreCase(ep.getEndpointURI().getHost()))
 66  
             {
 67  0
                 inetAddress = InetAddress.getLocalHost();
 68  
             }
 69  
             else
 70  
             {
 71  3000
                 inetAddress = InetAddress.getByName(ep.getEndpointURI().getHost());
 72  
             }
 73  
 
 74  3000
             write(socket, payload, port, inetAddress);
 75  
         }
 76  
         finally
 77  
         {
 78  3000
             connector.releaseSocket(socket, ep);
 79  3000
         }
 80  3000
     }
 81  
 
 82  
     protected void write(DatagramSocket socket, byte[] data, int port, InetAddress inetAddress) throws IOException
 83  
     {
 84  3000
         DatagramPacket packet = new DatagramPacket(data, data.length);
 85  3000
         if (port >= 0)
 86  
         {
 87  3000
             packet.setPort(port);
 88  
         }
 89  3000
         packet.setAddress(inetAddress);
 90  3000
         socket.send(packet);
 91  3000
     }
 92  
 
 93  
     protected synchronized MuleMessage doSend(MuleEvent event) throws Exception
 94  
     {
 95  0
         doDispatch(event);
 96  
         // If we're doing sync receive try and read return info from socket
 97  0
         if (event.getEndpoint().isRemoteSync())
 98  
         {
 99  0
             DatagramSocket socket = connector.getSocket(event.getEndpoint());
 100  0
             DatagramPacket result = receive(socket, event.getTimeout());
 101  0
             if (result == null)
 102  
             {
 103  0
                 return null;
 104  
             }
 105  0
             return new DefaultMuleMessage(connector.getMessageAdapter(result), event.getMessage());
 106  
         }
 107  
         else
 108  
         {
 109  0
             return event.getMessage();
 110  
         }
 111  
     }
 112  
 
 113  
     private DatagramPacket receive(DatagramSocket socket, int timeout) throws IOException
 114  
     {
 115  0
         int origTimeout = socket.getSoTimeout();
 116  
         try
 117  
         {
 118  0
             DatagramPacket packet = new DatagramPacket(new byte[connector.getReceiveBufferSize()],
 119  
                 connector.getReceiveBufferSize());
 120  
 
 121  0
             if(timeout > 0 && timeout != socket.getSoTimeout())
 122  
             {
 123  0
                 socket.setSoTimeout(timeout);
 124  
             }
 125  0
             socket.receive(packet);
 126  0
             return packet;
 127  
         }
 128  
         finally
 129  
         {
 130  0
             if(socket.getSoTimeout()!= origTimeout)
 131  
             {
 132  0
                 socket.setSoTimeout(origTimeout);
 133  
             }
 134  
         }
 135  
     }
 136  
 
 137  
     protected void doDispose()
 138  
     {
 139  
         // template method
 140  2
     }
 141  
 }