Coverage Report - org.mule.transport.tcp.ExceptionReturnTcpMessageReceiver
 
Classes in this File Line Coverage Branch Coverage Complexity
ExceptionReturnTcpMessageReceiver
0%
0/4
N/A
1.8
ExceptionReturnTcpMessageReceiver$TcpWorker
0%
0/19
N/A
1.8
 
 1  
 /*
 2  
  * $Id:$
 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  
 package org.mule.transport.tcp;
 11  
 
 12  
 import org.mule.DefaultMuleMessage;
 13  
 import org.mule.api.ExceptionPayload;
 14  
 import org.mule.api.MuleMessage;
 15  
 import org.mule.api.endpoint.InboundEndpoint;
 16  
 import org.mule.api.lifecycle.CreateException;
 17  
 import org.mule.api.service.Service;
 18  
 import org.mule.api.transport.Connector;
 19  
 import org.mule.message.DefaultExceptionPayload;
 20  
 import org.mule.transport.AbstractMessageReceiver;
 21  
 import org.mule.transport.NullPayload;
 22  
 
 23  
 import java.io.IOException;
 24  
 import java.net.Socket;
 25  
 import java.util.ArrayList;
 26  
 import java.util.List;
 27  
 
 28  
 import javax.resource.spi.work.Work;
 29  
 
 30  
 /**
 31  
  * Extends {@link TcpMessageReceiver} providing managing of protocol error conditions.
 32  
  * {@link TcpMessageReceiver.TcpWorker#getNextMessage(Object)} is extended so, in case
 33  
  * of an protocol error it will try to send the exception back to the client instead
 34  
  * of ignoring it. If an exception is thrown managing the error it will ignored.
 35  
  */
 36  0
 public class ExceptionReturnTcpMessageReceiver extends TcpMessageReceiver
 37  
 {
 38  
 
 39  
     public ExceptionReturnTcpMessageReceiver(Connector connector, Service service, InboundEndpoint endpoint)
 40  
             throws CreateException
 41  
     {
 42  0
         super(connector, service, endpoint);
 43  0
     }
 44  
 
 45  
     protected Work createWork(Socket socket) throws IOException
 46  
     {
 47  0
         return new TcpWorker(socket, this);
 48  
     }
 49  
 
 50  
     protected class TcpWorker extends TcpMessageReceiver.TcpWorker
 51  
     {
 52  
 
 53  
         public TcpWorker(Socket socket, AbstractMessageReceiver receiver) throws IOException
 54  0
         {
 55  0
             super(socket, receiver);
 56  0
         }
 57  
 
 58  
         protected Object getNextMessage(Object resource) throws Exception
 59  
         {
 60  
             try
 61  
             {
 62  0
                 return super.getNextMessage(resource);
 63  
             }
 64  0
             catch (Exception e)
 65  
             {
 66  0
                 manageException(e);
 67  0
                 return null;
 68  
             }
 69  
         }
 70  
 
 71  
         private void manageException(Exception readingException) throws Exception
 72  
         {
 73  
             try
 74  
             {
 75  0
                 logger.warn("Failed to read message: " + readingException);
 76  
 
 77  0
                 MuleMessage msg = new DefaultMuleMessage(NullPayload.getInstance(), connector.getMuleContext());
 78  0
                 ExceptionPayload exceptionPayload = new DefaultExceptionPayload(readingException);
 79  0
                 msg.setExceptionPayload(exceptionPayload);
 80  0
                 List msgList = new ArrayList(1);
 81  0
                 msgList.add(msg);
 82  
 
 83  0
                 handleResults(msgList);
 84  
             }
 85  0
             catch (Exception writingException)
 86  
             {
 87  0
                 logger.warn("Failed to write exception back to client: " + writingException);
 88  0
                 throw writingException;
 89  0
             }
 90  0
         }
 91  
     }
 92  
 }