View Javadoc

1   /*
2    * $Id: EOFStreamingTestComponent.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.tcp.integration;
12  
13  import org.mule.tck.functional.FunctionalStreamingTestComponent;
14  
15  import java.io.IOException;
16  import java.io.InputStream;
17  import java.net.SocketException;
18  import java.net.SocketTimeoutException;
19  
20  /**
21   * Extends the FunctionalStreamingTestComponent to wait for data in a non
22   * blocking fashion for the StreamingProtocol.
23   *
24   * @see org.mule.tck.functional.EventCallback
25   */
26  public class EOFStreamingTestComponent extends FunctionalStreamingTestComponent
27  {
28      protected int read(InputStream is, byte[] buffer) throws IOException
29      {
30          int len;
31          try
32          {
33              do
34              {
35                  len = is.read(buffer, 0, buffer.length);
36                  if (0 == len)
37                  {
38                      // wait for non-blocking input stream
39                      // use new lock since not expecting notification
40                      try
41                      {
42                          Thread.sleep(50);
43                      }
44                      catch (InterruptedException e)
45                      {
46                          // no-op
47                      }
48                  }
49              }
50              while (0 == len);
51              return len;
52          }
53          catch (SocketException e)
54          {
55              // do not pollute the log with a stacktrace, log only the message
56              logger.info("Socket exception occured: " + e.getMessage());
57              return -1;
58          }
59          catch (SocketTimeoutException e)
60          {
61              logger.debug("Socket timeout.");
62              return -1;
63          }
64      }
65  
66  }