View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.transport.tcp.integration;
8   
9   import org.mule.tck.functional.FunctionalStreamingTestComponent;
10  
11  import java.io.IOException;
12  import java.io.InputStream;
13  import java.net.SocketException;
14  import java.net.SocketTimeoutException;
15  
16  /**
17   * Extends the FunctionalStreamingTestComponent to wait for data in a non
18   * blocking fashion for the StreamingProtocol.
19   *
20   * @see org.mule.tck.functional.EventCallback
21   */
22  public class EOFStreamingTestComponent extends FunctionalStreamingTestComponent
23  {
24      protected int read(InputStream is, byte[] buffer) throws IOException
25      {
26          int len;
27          try
28          {
29              do
30              {
31                  len = is.read(buffer, 0, buffer.length);
32                  if (0 == len)
33                  {
34                      // wait for non-blocking input stream
35                      // use new lock since not expecting notification
36                      try
37                      {
38                          Thread.sleep(50);
39                      }
40                      catch (InterruptedException e)
41                      {
42                          // no-op
43                      }
44                  }
45              }
46              while (0 == len);
47              return len;
48          }
49          catch (SocketException e)
50          {
51              // do not pollute the log with a stacktrace, log only the message
52              logger.info("Socket exception occured: " + e.getMessage());
53              return -1;
54          }
55          catch (SocketTimeoutException e)
56          {
57              logger.debug("Socket timeout.");
58              return -1;
59          }
60      }
61  
62  }