Coverage Report - org.mule.transport.udp.UdpMessageRequester
 
Classes in this File Line Coverage Branch Coverage Complexity
UdpMessageRequester
0%
0/31
0%
0/12
0
 
 1  
 /*
 2  
  * $Id: UdpMessageRequester.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.udp;
 12  
 
 13  
 import org.mule.api.MuleMessage;
 14  
 import org.mule.api.endpoint.InboundEndpoint;
 15  
 import org.mule.api.retry.RetryContext;
 16  
 import org.mule.transport.AbstractMessageRequester;
 17  
 
 18  
 import java.io.IOException;
 19  
 import java.net.DatagramPacket;
 20  
 import java.net.DatagramSocket;
 21  
 
 22  
 /**
 23  
  * Responsible for requesting MuleEvents as UDP packets on the network
 24  
  */
 25  
 
 26  
 public class UdpMessageRequester extends AbstractMessageRequester
 27  
 {
 28  
     
 29  
     protected final UdpConnector connector;
 30  
 
 31  
     public UdpMessageRequester(InboundEndpoint endpoint)
 32  
     {
 33  0
         super(endpoint);
 34  0
         this.connector = (UdpConnector)endpoint.getConnector();
 35  0
     }
 36  
 
 37  
     @Override
 38  
     protected void doConnect() throws Exception
 39  
     {
 40  
         // nothing, there is an optional validation in validateConnection()
 41  0
     }
 42  
 
 43  
     @Override
 44  
     public RetryContext validateConnection(RetryContext retryContext)
 45  
     {
 46  0
         DatagramSocket socket = null;
 47  
         try
 48  
         {
 49  0
             socket = connector.getSocket(endpoint);
 50  
 
 51  0
             retryContext.setOk();
 52  
         }
 53  0
         catch (Exception ex)
 54  
         {
 55  0
             retryContext.setFailed(ex);
 56  
         }
 57  
         finally
 58  
         {
 59  0
             if (socket != null)
 60  
             {
 61  
                 try
 62  
                 {
 63  0
                     connector.releaseSocket(socket, endpoint);
 64  
                 }
 65  0
                 catch (Exception e)
 66  
                 {
 67  0
                     if (logger.isDebugEnabled())
 68  
                     {
 69  0
                         logger.debug("Failed to release a socket " + socket, e);
 70  
                     }
 71  0
                 }
 72  
             }
 73  
         }
 74  
 
 75  0
         return retryContext;
 76  
 
 77  
     }
 78  
 
 79  
     @Override
 80  
     protected void doDisconnect() throws Exception
 81  
     {
 82  
         // nothing to do
 83  0
     }
 84  
 
 85  
     private DatagramPacket request(DatagramSocket socket, int timeout) throws IOException
 86  
     {
 87  0
         int origTimeout = socket.getSoTimeout();
 88  
         try
 89  
         {
 90  0
             DatagramPacket packet = new DatagramPacket(new byte[connector.getReceiveBufferSize()],
 91  
                 connector.getReceiveBufferSize());
 92  
 
 93  0
             if(timeout > 0 && timeout != socket.getSoTimeout())
 94  
             {
 95  0
                 socket.setSoTimeout(timeout);
 96  
             }
 97  0
             socket.receive(packet);
 98  0
             return packet;
 99  
         }
 100  
         finally
 101  
         {
 102  0
             if(socket.getSoTimeout()!= origTimeout)
 103  
             {
 104  0
                 socket.setSoTimeout(origTimeout);
 105  
             }
 106  
         }
 107  
     }
 108  
 
 109  
     /**
 110  
      * Make a specific request to the underlying transport
 111  
      *
 112  
      * @param timeout the maximum time the operation should block before returning.
 113  
      *            The call should return immediately if there is data available. If
 114  
      *            no data becomes available before the timeout elapses, null will be
 115  
      *            returned
 116  
      * @return the result of the request wrapped in a MuleMessage object. Null will be
 117  
      *         returned if no data was avaialable
 118  
      * @throws Exception if the call to the underlying protocal cuases an exception
 119  
      */
 120  
     @Override
 121  
     protected MuleMessage doRequest(long timeout) throws Exception
 122  
     {
 123  0
         DatagramSocket socket = connector.getSocket(endpoint);
 124  0
         DatagramPacket result = request(socket, (int)timeout);
 125  0
         if (result == null)
 126  
         {
 127  0
             return null;
 128  
         }
 129  0
         return createMuleMessage(result, endpoint.getEncoding());
 130  
     }
 131  
 
 132  
     @Override
 133  
     protected void doDispose()
 134  
     {
 135  
         // template method
 136  0
     }
 137  
 
 138  
 }